summaryrefslogtreecommitdiff
path: root/map-server/altos-mapd
diff options
context:
space:
mode:
Diffstat (limited to 'map-server/altos-mapd')
-rw-r--r--map-server/altos-mapd/.gitignore6
-rw-r--r--map-server/altos-mapd/AltosMapd.java50
-rw-r--r--map-server/altos-mapd/AltosMapdClient.java148
-rw-r--r--map-server/altos-mapd/AltosMapdPreferences.java85
-rw-r--r--map-server/altos-mapd/AltosMapdServer.java37
-rw-r--r--map-server/altos-mapd/Makefile.am59
-rw-r--r--map-server/altos-mapd/Manifest.txt2
7 files changed, 387 insertions, 0 deletions
diff --git a/map-server/altos-mapd/.gitignore b/map-server/altos-mapd/.gitignore
new file mode 100644
index 00000000..5f5ce0ae
--- /dev/null
+++ b/map-server/altos-mapd/.gitignore
@@ -0,0 +1,6 @@
+*.stamp
+*.jar
+altos-mapd
+altos-mapd-jdb
+altos-mapd-test
+classes
diff --git a/map-server/altos-mapd/AltosMapd.java b/map-server/altos-mapd/AltosMapd.java
new file mode 100644
index 00000000..cfa1ef35
--- /dev/null
+++ b/map-server/altos-mapd/AltosMapd.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright © 2018 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+package altosmapd;
+
+import java.net.*;
+import java.io.*;
+
+import org.altusmetrum.altoslib_13.*;
+
+public class AltosMapd {
+
+ public final static int port = 16717;
+
+ public final static int maptype = AltosMap.maptype_hybrid;
+
+ public final static int px_size = 512;
+
+ public final static int scale = 1;
+
+ public static void main(final String[] args) {
+
+ AltosMapdServer server = new AltosMapdServer(port);
+
+ AltosPreferences.init(new AltosMapdPreferences());
+
+ AltosPreferences.mapdir = new File("/home/keithp/misc/rockets/flights/maps");
+
+ for (;;) {
+ Socket client = server.accept();
+ if (client == null) {
+ System.out.printf("accept failed\n");
+ continue;
+ }
+ System.out.printf("got client\n");
+ new AltosMapdClient(client);
+ }
+ }
+}
diff --git a/map-server/altos-mapd/AltosMapdClient.java b/map-server/altos-mapd/AltosMapdClient.java
new file mode 100644
index 00000000..fb0c08e6
--- /dev/null
+++ b/map-server/altos-mapd/AltosMapdClient.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright © 2018 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+package altosmapd;
+
+import java.net.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.io.*;
+
+import org.altusmetrum.altoslib_13.*;
+
+public class AltosMapdClient extends Thread implements AltosMapStoreListener {
+ private Socket socket;
+ private AltosJson request;
+ private AltosJson reply;
+
+ private void set_status(int status) {
+ reply.put("status", status);
+ }
+
+ private void set_filename(String filename) {
+ reply.put("filename", filename);
+
+ }
+
+ private void set_content_type(String content_type) {
+ reply.put("content_type", content_type);
+ }
+
+ private String content_type(File file) {
+ String content_type = "application/octet-stream";
+ String basename = file.getName();
+ if (basename.endsWith(".jpg"))
+ content_type = "image/jpeg";
+ if (basename.endsWith(".png"))
+ content_type = "image/png";
+ return content_type;
+ }
+
+ private void set_file(File file) {
+ set_filename(file.getAbsolutePath());
+ set_content_type(content_type(file));
+ }
+
+ private Semaphore store_ready;
+
+ public void notify_store(AltosMapStore map_store, int status) {
+ if (status != AltosMapTile.fetching)
+ store_ready.release();
+ }
+
+ public void run() {
+ reply = new AltosJson();
+ try {
+ request = AltosJson.fromInputStream(socket.getInputStream());
+
+ double lat = request.get_double("lat", AltosLib.MISSING);
+ double lon = request.get_double("lon", AltosLib.MISSING);
+ int zoom = request.get_int("zoom", AltosLib.MISSING);
+ String addr = request.get_string("remote_addr", null);
+
+ if (lat == AltosLib.MISSING ||
+ lon == AltosLib.MISSING ||
+ zoom == AltosLib.MISSING ||
+ addr == null)
+ {
+ set_status(400);
+ } else {
+ store_ready = new Semaphore(0);
+
+ System.out.printf("Fetching tile for %g %g %d\n", lat, lon, zoom);
+
+ AltosMapStore map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
+ zoom,
+ AltosMapd.maptype,
+ AltosMapd.px_size,
+ AltosMapd.scale);
+ int status;
+
+ if (map_store == null) {
+ System.out.printf("no store?\n");
+ status = AltosMapTile.failed;
+ } else {
+ map_store.add_listener(this);
+
+ System.out.printf("Waiting for tile\n");
+
+ try {
+ store_ready.acquire();
+ } catch (Exception ie) {
+ }
+
+ status = map_store.status();
+ }
+
+ if (status == AltosMapTile.fetched || status == AltosMapTile.loaded) {
+ set_status(200);
+ set_file(map_store.file);
+ } else if (status == AltosMapTile.failed) {
+ set_status(404);
+ } else if (status == AltosMapTile.fetching) {
+ set_status(408);
+ } else if (status == AltosMapTile.bad_request) {
+ set_status(400);
+ } else if (status == AltosMapTile.forbidden) {
+ set_status(403);
+ } else {
+ set_status(400);
+ }
+ }
+ } catch (Exception e) {
+ System.out.printf("client exception %s\n", e.toString());
+ e.printStackTrace(System.out);
+ set_status(400);
+
+ } finally {
+ try {
+ Writer writer = new PrintWriter(socket.getOutputStream());
+ reply.write(writer);
+ writer.write('\n');
+ writer.flush();
+ } catch (IOException ie) {
+ }
+ try {
+ socket.close();
+ } catch (IOException ie) {
+ }
+ System.out.printf("client done\n");
+ }
+ }
+
+ public AltosMapdClient(Socket socket) {
+ this.socket = socket;
+ start();
+ }
+}
diff --git a/map-server/altos-mapd/AltosMapdPreferences.java b/map-server/altos-mapd/AltosMapdPreferences.java
new file mode 100644
index 00000000..fcfe3261
--- /dev/null
+++ b/map-server/altos-mapd/AltosMapdPreferences.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright © 2018 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+package altosmapd;
+
+import java.io.*;
+
+import org.altusmetrum.altoslib_13.*;
+
+public class AltosMapdPreferences extends AltosPreferencesBackend {
+
+ public String getString(String key, String def) {
+ return def;
+ }
+ public void putString(String key, String value) {
+ }
+
+ public int getInt(String key, int def) {
+ return def;
+ }
+
+ public void putInt(String key, int value) {
+ }
+
+ public double getDouble(String key, double def) {
+ return def;
+ }
+
+ public void putDouble(String key, double value) {
+ }
+
+ public boolean getBoolean(String key, boolean def) {
+ return def;
+ }
+
+ public void putBoolean(String key, boolean value) {
+ }
+
+ public byte[] getBytes(String key, byte[] def) {
+ return def;
+ }
+
+ public void putBytes(String key, byte[] value) {
+ }
+
+ public boolean nodeExists(String key) {
+ return false;
+ }
+
+ public AltosPreferencesBackend node(String key) {
+ return this;
+ }
+
+ public String[] keys() {
+ return null;
+ }
+
+ public void remove(String key) {
+ }
+
+ public void flush() {
+ }
+
+ public File homeDirectory() {
+ return new File (".");
+ }
+
+ public void debug(String format, Object ... arguments) {
+ System.out.printf(format, arguments);
+ }
+
+ public AltosMapdPreferences() {
+ }
+}
diff --git a/map-server/altos-mapd/AltosMapdServer.java b/map-server/altos-mapd/AltosMapdServer.java
new file mode 100644
index 00000000..68b427f0
--- /dev/null
+++ b/map-server/altos-mapd/AltosMapdServer.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright © 2018 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+package altosmapd;
+
+import java.net.*;
+
+public class AltosMapdServer {
+ ServerSocket socket;
+
+ public Socket accept() {
+ try {
+ return socket.accept();
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ public AltosMapdServer(int port) {
+ try {
+ socket = new ServerSocket(port, 5, InetAddress.getLoopbackAddress());
+ } catch (Exception e) {
+ socket = null;
+ }
+ }
+}
diff --git a/map-server/altos-mapd/Makefile.am b/map-server/altos-mapd/Makefile.am
new file mode 100644
index 00000000..c099d1c7
--- /dev/null
+++ b/map-server/altos-mapd/Makefile.am
@@ -0,0 +1,59 @@
+JAVAROOT=classes
+AM_JAVACFLAGS=-target 1.6 -encoding UTF-8 -Xlint:deprecation -Xlint:unchecked -source 6
+
+altoslibdir=$(libdir)/altos
+
+CLASSPATH_ENV=mkdir -p $(JAVAROOT); CLASSPATH="$(JAVAROOT):../../altoslib/*"
+
+bin_SCRIPTS=altos-mapd
+
+altosmapd_JAVA = \
+ AltosMapd.java \
+ AltosMapdServer.java \
+ AltosMapdClient.java \
+ AltosMapdPreferences.java
+
+ALTOSLIB_CLASS=\
+ altoslib_$(ALTOSLIB_VERSION).jar
+
+JAR=altosmapd.jar
+
+FATJAR=altosmapd-fat.jar
+
+all-local: classes/altosmapd $(JAR) altos-mapd altos-mapd-test altos-mapd-jdb
+
+classes/altosmapd:
+ mkdir -p classes/altosmapd
+
+$(JAR): classaltosmapd.stamp Manifest.txt $(ALTOSLIB_CLASS)
+ jar cfm $@ Manifest.txt \
+ -C classes altosmapd
+
+altosmapddir=$(datadir)/java
+
+$(FATJAR): classaltosmapd.stamp Manifest-fat.txt $(ALTOSLIB_CLASS)
+ jar cfm $@ Manifest-fat.txt \
+ -C classes altosmapd
+
+altos-mapd: Makefile
+ echo "#!/bin/sh" > $@
+ echo 'exec java -Djava.library.path="$(altoslibdir)" -jar "$(altosmapddir)/altosmapd.jar" "$$@"' >> $@
+ chmod +x $@
+
+altos-mapd-test: Makefile
+ echo '#!/bin/sh' > $@
+ echo 'dir="$$(dirname $$0)"' >> $@
+ echo 'cd "$$dir"' >> $@
+ echo 'altosmapd="$$(pwd -P)"' >> $@
+ echo 'exec java -jar "$$altosmapd/altosmapd.jar" "$$@"' >> $@
+ chmod +x $@
+
+altos-mapd-jdb: Makefile
+ echo "#!/bin/sh" > $@
+ echo 'exec jdb altosmapd/AltosMapd "$$@"' >> $@
+ chmod +x $@
+
+$(ALTOSLIB_CLASS):
+ -rm -f "$@"
+ $(LN_S) ../../altoslib/"$@" .
+
diff --git a/map-server/altos-mapd/Manifest.txt b/map-server/altos-mapd/Manifest.txt
new file mode 100644
index 00000000..42c0313b
--- /dev/null
+++ b/map-server/altos-mapd/Manifest.txt
@@ -0,0 +1,2 @@
+Main-Class: altosmapd.AltosMapd
+Class-Path: altoslib_13.jar