2 * Copyright © 2018 Keith Packard <keithp@keithp.com>
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, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
21 import java.util.concurrent.*;
23 import org.altusmetrum.altoslib_13.*;
25 public class AltosMapd implements AltosLaunchSiteListener {
27 public static int port = 16717;
29 public final static int maptype = AltosMap.maptype_hybrid;
31 public final static int px_size = 512;
33 public final static int scale = 1;
35 public static double valid_radius = 17000; /* 17km */
37 public String map_dir = null;
38 public String launch_sites_file = null;
39 public String key_file = null;
42 System.out.printf("usage: altos-mapd [--mapdir <map-directory] [--launch-sites <launch-sites-file>]\n" +
43 " [--radius <valid-radius-m> [--port <port>] [--key <key-file>]\n");
47 private static Semaphore launch_sites_ready;
49 private static List<AltosLaunchSite> launch_sites;
51 public void notify_launch_sites(List<AltosLaunchSite> sites) {
52 synchronized (launch_sites_ready) {
55 launch_sites_ready.release();
60 public static boolean check_lat_lon(double lat, double lon) {
61 synchronized (launch_sites_ready) {
62 if (launch_sites == null) {
64 launch_sites_ready.acquire();
65 } catch (InterruptedException ie) {
70 if (launch_sites == null) {
71 System.out.printf("No launch site data available, refusing all requests\n");
75 for (AltosLaunchSite site : launch_sites) {
76 AltosGreatCircle gc = new AltosGreatCircle(site.latitude, site.longitude,
78 if (gc.distance <= valid_radius)
85 AltosMapdServer server;
87 public void process(String[] args) {
89 AltosPreferences.init(new AltosMapdPreferences());
92 for (int i = 0; i < args.length; i += skip) {
94 if (args[i].equals("--mapdir") && i < args.length-1) {
97 } else if (args[i].equals("--launch-sites") && i < args.length-1) {
98 launch_sites_file = args[i+1];
100 } else if (args[i].equals("--radius") && i < args.length-1) {
102 valid_radius = AltosParse.parse_double_locale(args[i+1]);
103 } catch (ParseException pe) {
107 } else if (args[i].equals("--port") && i < args.length-1) {
109 port = AltosParse.parse_int(args[i+1]);
110 } catch (ParseException pe) {
114 } else if (args[i].equals("--key") && i < args.length-1) {
115 key_file = args[i+1];
125 if (key_file != null) {
127 BufferedReader key_reader = new BufferedReader(new FileReader(key_file));
129 String line = key_reader.readLine();
130 if (line == null || line.length() != 39) {
131 System.out.printf("%s: invalid contents %d \"%s\"\n",
132 key_file, line.length(), line);
136 AltosMapStore.google_maps_api_key = line;
137 } catch (Exception e) {
138 System.out.printf("%s: %s\n", key_file, e.toString());
143 AltosPreferences.mapdir = new File(map_dir);
145 if (launch_sites_file != null)
146 AltosLaunchSites.launch_sites_url = "file://" + launch_sites_file;
148 launch_sites_ready = new Semaphore(0);
150 new AltosLaunchSites(this);
153 server = new AltosMapdServer(port);
154 } catch (IOException ie) {
155 System.out.printf("Cannot bind to port %d: %s\n", port, ie.toString());
161 Socket client = server.accept();
162 if (client == null) {
163 System.out.printf("accept failed\n");
166 new AltosMapdClient(client);
167 } catch (Exception e) {
168 System.out.printf("Exception %s\n", e.toString());
173 public void AltosMapd() {
176 public static void main(final String[] args) {
177 new AltosMapd().process(args);