3ebac60142889b4eebd81154efaf6c1862417244
[fw/altos] / altoslib / AltosLaunchSites.java
1 /*
2  * Copyright © 2015 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_10;
19
20 import java.io.*;
21 import java.lang.*;
22 import java.util.*;
23 import java.util.concurrent.*;
24 import java.net.*;
25 import java.text.*;
26
27 public class AltosLaunchSites extends Thread {
28         URL                             url;
29         LinkedList<AltosLaunchSite>     sites;
30         AltosLaunchSiteListener         listener;
31
32         void notify_complete() {
33                 listener.notify_launch_sites(sites);
34         }
35
36         void add(AltosLaunchSite site) {
37                 sites.add(site);
38         }
39
40         void add(String line) {
41                 try {
42                         add(new AltosLaunchSite(line));
43                 } catch (ParseException pe) {
44                         System.out.printf("parse exception %s\n", pe.toString());
45                 }
46         }
47
48         public void run() {
49                 try {
50                         String  path;
51
52                         path = System.getenv(AltosLib.launch_sites_env);
53                         if (path == null)
54                                 path = AltosLib.launch_sites_url;
55                         url = new URL(path);
56                         URLConnection uc = url.openConnection();
57
58                         InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set);
59                         BufferedReader in = new BufferedReader(in_stream);
60
61                         for (;;) {
62                                 String line = in.readLine();
63                                 if (line == null)
64                                         break;
65                                 add(line);
66                         }
67                 } catch (Exception e) {
68                         System.out.printf("file exception %s\n", e.toString());
69                 } finally {
70                         notify_complete();
71                 }
72         }
73
74         public AltosLaunchSites(AltosLaunchSiteListener listener) {
75                 sites = new LinkedList<AltosLaunchSite>();
76                 this.listener = listener;
77                 start();
78         }
79 }