diff options
Diffstat (limited to 'map-server/altos-map')
-rw-r--r-- | map-server/altos-map/.gitignore | 6 | ||||
-rw-r--r-- | map-server/altos-map/AltosMap.java | 156 | ||||
-rw-r--r-- | map-server/altos-map/Makefile.am | 56 | ||||
-rw-r--r-- | map-server/altos-map/Manifest.txt | 2 | ||||
-rwxr-xr-x | map-server/altos-map/altos-map-fake | 6 |
5 files changed, 226 insertions, 0 deletions
diff --git a/map-server/altos-map/.gitignore b/map-server/altos-map/.gitignore new file mode 100644 index 00000000..ea012eef --- /dev/null +++ b/map-server/altos-map/.gitignore @@ -0,0 +1,6 @@ +altos-map +altos-map-jdb +altos-map-test +*.jar +*.stamp +classes diff --git a/map-server/altos-map/AltosMap.java b/map-server/altos-map/AltosMap.java new file mode 100644 index 00000000..83bc7cea --- /dev/null +++ b/map-server/altos-map/AltosMap.java @@ -0,0 +1,156 @@ +/* + * 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 altosmap; + +import java.net.*; +import java.io.*; +import java.util.*; +import java.text.*; + +import org.altusmetrum.altoslib_13.*; + +public class AltosMap { + + public final static int port = 16717; + + String query_string; + String remote_addr; + + public String reason_string(int code) { + switch (code) { + case 200: + return "OK"; + case 400: + return "Bad Request"; + case 403: + return "Forbidden"; + case 404: + return "Not Found"; + case 408: + return "Request Timeout"; + default: + return "Failure"; + } + } + + public void write_status(int status) { + System.out.printf("Status: %d %s\n", status, reason_string(status)); + } + + public void write_type(String type) { + System.out.printf("Content-Type: %s\n", type); + } + + public void fail(int status, String reason) { + write_status(status); + write_type("text/html"); + System.out.printf("<body>%s</body>\n", reason); + System.exit(1); + } + + public void process() { + query_string = System.getenv("QUERY_STRING"); + + if (query_string == null) + fail(400, "Missing query string"); + + remote_addr = System.getenv("REMOTE_ADDR"); + + if (remote_addr == null) + fail(400, "Missing remote address"); + + String[] queries = query_string.split("&"); + + double lon = AltosLib.MISSING; + double lat = AltosLib.MISSING; + int zoom = AltosLib.MISSING; + + try { + for (String query : queries) { + String[] q = query.split("="); + if (q.length >= 2) { + String name = q[0]; + String value = q[1]; + if (name.equals("lon")) + lon = AltosParse.parse_double_net(value); + else if (name.equals("lat")) + lat = AltosParse.parse_double_net(value); + else if (name.equals("zoom")) + zoom = AltosParse.parse_int(value); + else + fail(400, String.format("Extra query param \"%s\"", query)); + } + } + } catch (ParseException pe) { + fail(400, String.format("Invalid query: %s", pe.toString())); + } + + if (lon == AltosLib.MISSING) + fail(400, "Missing longitude"); + if (lat == AltosLib.MISSING) + fail(400, "Missing latitude"); + if (zoom == AltosLib.MISSING) + fail(400, "Missing zoom"); + + try { + Socket socket = new Socket(InetAddress.getLoopbackAddress(), port); + + AltosJson request = new AltosJson(); + + request.put("lat", lat); + request.put("lon", lon); + request.put("zoom", zoom); + request.put("remote_addr", remote_addr); + + Writer writer = new PrintWriter(socket.getOutputStream()); + request.write(writer); + writer.flush(); + + AltosJson reply = AltosJson.fromInputStream(socket.getInputStream()); + + int status = reply.get_int("status", 400); + + if (status != 200) + fail(status, "Bad cache status"); + + String filename = reply.get_string("filename", null); + try { + File file = new File(filename); + long length = file.length(); + FileInputStream in = new FileInputStream(file); + String content_type = reply.get_string("content_type", null); + System.out.printf("Content-Type: %s\n", content_type); + System.out.printf("Content-Length: %d\n", file.length()); + byte[] buf = new byte[4096]; + int bytes_read; + while ((bytes_read = in.read(buf)) > 0) + System.out.write(buf); + } catch (IOException ie) { + fail(404, String.format("IO Exception: %s", ie.toString())); + } + } catch (Exception e) { + fail(404, String.format("Exception %s", e.toString())); + } + } + + public AltosMap() { + } + + public static void main(final String[] args) { + + new AltosMap().process(); + + } +} diff --git a/map-server/altos-map/Makefile.am b/map-server/altos-map/Makefile.am new file mode 100644 index 00000000..efaae457 --- /dev/null +++ b/map-server/altos-map/Makefile.am @@ -0,0 +1,56 @@ +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-map + +altosmap_JAVA = \ + AltosMap.java + +ALTOSLIB_CLASS=\ + altoslib_$(ALTOSLIB_VERSION).jar + +JAR=altosmap.jar + +FATJAR=altosmap-fat.jar + +all-local: classes/altosmap $(JAR) altos-map altos-map-test altos-map-jdb + +classes/altosmap: + mkdir -p classes/altosmap + +$(JAR): classaltosmap.stamp Manifest.txt $(ALTOSLIB_CLASS) + jar cfm $@ Manifest.txt \ + -C classes altosmap + +altosmapdir=$(datadir)/java + +$(FATJAR): classaltosmap.stamp Manifest-fat.txt $(ALTOSLIB_CLASS) + jar cfm $@ Manifest-fat.txt \ + -C classes altosmap + +altos-map: Makefile + echo "#!/bin/sh" > $@ + echo 'exec java -Djava.library.path="$(altoslibdir)" -jar "$(altosmapdir)/altosmap.jar" "$$@"' >> $@ + chmod +x $@ + +altos-map-test: Makefile + echo '#!/bin/sh' > $@ + echo 'dir="$$(dirname $$0)"' >> $@ + echo 'cd "$$dir"' >> $@ + echo 'altosmap="$$(pwd -P)"' >> $@ + echo 'exec java -jar "$$altosmap/altosmap.jar" "$$@"' >> $@ + chmod +x $@ + +altos-map-jdb: Makefile + echo "#!/bin/sh" > $@ + echo 'exec jdb altosmap/AltosMap "$$@"' >> $@ + chmod +x $@ + +$(ALTOSLIB_CLASS): + -rm -f "$@" + $(LN_S) ../../altoslib/"$@" . + diff --git a/map-server/altos-map/Manifest.txt b/map-server/altos-map/Manifest.txt new file mode 100644 index 00000000..1a285b40 --- /dev/null +++ b/map-server/altos-map/Manifest.txt @@ -0,0 +1,2 @@ +Main-Class: altosmap.AltosMap +Class-Path: altoslib_13.jar diff --git a/map-server/altos-map/altos-map-fake b/map-server/altos-map/altos-map-fake new file mode 100755 index 00000000..a78bbd64 --- /dev/null +++ b/map-server/altos-map/altos-map-fake @@ -0,0 +1,6 @@ +#!/bin/sh +# map-N43.799102,W120.586281-hybrid-20.jpg +export QUERY_STRING="lat=43.799102&lon=-120.586281&zoom=20" +export REMOTE_ADDR="127.0.0.1" +./altos-map-test + |