Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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_7;
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                         url = new URL(AltosLib.launch_sites_url);
51                         URLConnection uc = url.openConnection();
52
53                         InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set);
54                         BufferedReader in = new BufferedReader(in_stream);
55
56                         for (;;) {
57                                 String line = in.readLine();
58                                 if (line == null)
59                                         break;
60                                 add(line);
61                         }
62                 } catch (Exception e) {
63                 } finally {
64                         notify_complete();
65                 }
66         }
67
68         public AltosLaunchSites(AltosLaunchSiteListener listener) {
69                 sites = new LinkedList<AltosLaunchSite>();
70                 this.listener = listener;
71                 start();
72         }
73 }