From 297eb795b24ec31f6599f48bc8c3769557a7ec6f Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 5 Aug 2018 12:32:42 +0800 Subject: Bump java lib versions to 13 Signed-off-by: Keith Packard --- altoslib/AltosMapStore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'altoslib/AltosMapStore.java') diff --git a/altoslib/AltosMapStore.java b/altoslib/AltosMapStore.java index 4eba3a04..7af439c4 100644 --- a/altoslib/AltosMapStore.java +++ b/altoslib/AltosMapStore.java @@ -16,7 +16,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altoslib_12; +package org.altusmetrum.altoslib_13; import java.io.*; import java.net.*; -- cgit v1.2.3 From 3b817a2b854065af23c9ec8e849150e6930f51e9 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 6 Oct 2018 16:04:39 -0700 Subject: map-server: Add maps proxy server This creates a map proxy server to handle the new Google Maps API requirements Signed-off-by: Keith Packard --- Makefile.am | 2 +- altoslib/AltosMapStore.java | 2 +- configure.ac | 3 + map-server/Makefile.am | 1 + map-server/altos-map/.gitignore | 6 + map-server/altos-map/AltosMap.java | 156 ++++++++++++++++++++++++ map-server/altos-map/Makefile.am | 56 +++++++++ map-server/altos-map/Manifest.txt | 2 + map-server/altos-map/altos-map-fake | 6 + map-server/altos-mapd/.gitignore | 6 + map-server/altos-mapd/AltosMapd.java | 50 ++++++++ map-server/altos-mapd/AltosMapdClient.java | 148 ++++++++++++++++++++++ map-server/altos-mapd/AltosMapdPreferences.java | 85 +++++++++++++ map-server/altos-mapd/AltosMapdServer.java | 37 ++++++ map-server/altos-mapd/Makefile.am | 59 +++++++++ map-server/altos-mapd/Manifest.txt | 2 + 16 files changed, 619 insertions(+), 2 deletions(-) create mode 100644 map-server/Makefile.am create mode 100644 map-server/altos-map/.gitignore create mode 100644 map-server/altos-map/AltosMap.java create mode 100644 map-server/altos-map/Makefile.am create mode 100644 map-server/altos-map/Manifest.txt create mode 100755 map-server/altos-map/altos-map-fake create mode 100644 map-server/altos-mapd/.gitignore create mode 100644 map-server/altos-mapd/AltosMapd.java create mode 100644 map-server/altos-mapd/AltosMapdClient.java create mode 100644 map-server/altos-mapd/AltosMapdPreferences.java create mode 100644 map-server/altos-mapd/AltosMapdServer.java create mode 100644 map-server/altos-mapd/Makefile.am create mode 100644 map-server/altos-mapd/Manifest.txt (limited to 'altoslib/AltosMapStore.java') diff --git a/Makefile.am b/Makefile.am index 4f47417e..89fdd6c9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS=ao-tools src doc icon altoslib libaltos altosuilib altosui micropeak ao-utils altosdroid telegps +SUBDIRS=ao-tools src doc icon altoslib libaltos altosuilib altosui micropeak ao-utils altosdroid telegps map-server EXTRA_DIST = ChangeLog diff --git a/altoslib/AltosMapStore.java b/altoslib/AltosMapStore.java index 7af439c4..b1cfcbd7 100644 --- a/altoslib/AltosMapStore.java +++ b/altoslib/AltosMapStore.java @@ -68,7 +68,7 @@ public class AltosMapStore { center.lat, center.lon, z, px_size/scale, px_size/scale, AltosMap.maptype_names[maptype], format_string); } - public int status() { + public synchronized int status() { return status; } diff --git a/configure.ac b/configure.ac index a14762b0..0a5a76ec 100644 --- a/configure.ac +++ b/configure.ac @@ -565,6 +565,9 @@ ao-tools/ao-usbtrng/Makefile ao-tools/ao-chaosread/Makefile ao-tools/ao-makebin/Makefile ao-utils/Makefile +map-server/Makefile +map-server/altos-mapd/Makefile +map-server/altos-map/Makefile src/Version ]) diff --git a/map-server/Makefile.am b/map-server/Makefile.am new file mode 100644 index 00000000..f9b8a727 --- /dev/null +++ b/map-server/Makefile.am @@ -0,0 +1 @@ +SUBDIRS=altos-mapd altos-map 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 + * + * 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("%s\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 + 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 + * + * 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 + * + * 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 + * + * 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 + * + * 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 -- cgit v1.2.3 From c7b1f3c6722ae59d09a888084075664446ff2768 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 6 Oct 2018 19:29:01 -0700 Subject: altoslib: Switch to maps.altusmetrum.org when no google map key is available The map key is only usable from maps.altusmetrum.org at this point, and that service will be proxying for everyone in the near future. Signed-off-by: Keith Packard --- altosdroid/AndroidManifest.xml.in | 2 - .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 1 - altoslib/AltosMapStore.java | 46 ++++++++++------------ altoslib/AltosVersion.java.in | 6 --- altosuilib/AltosUIConfigure.java | 5 +-- configure.ac | 14 ------- 6 files changed, 23 insertions(+), 51 deletions(-) (limited to 'altoslib/AltosMapStore.java') diff --git a/altosdroid/AndroidManifest.xml.in b/altosdroid/AndroidManifest.xml.in index 3f17188e..43a0787e 100644 --- a/altosdroid/AndroidManifest.xml.in +++ b/altosdroid/AndroidManifest.xml.in @@ -107,8 +107,6 @@ - diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 453e898e..1bcb67ef 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -678,7 +678,6 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener, // Display the Version mVersion = (TextView) findViewById(R.id.version); mVersion.setText("Version: " + BuildInfo.version + - (AltosVersion.has_google_maps_api_key() ? " maps" : "") + " Built: " + BuildInfo.builddate + " " + BuildInfo.buildtime + " " + BuildInfo.buildtz + " (" + BuildInfo.branch + "-" + BuildInfo.commitnum + "-" + BuildInfo.commithash + ")"); diff --git a/altoslib/AltosMapStore.java b/altoslib/AltosMapStore.java index b1cfcbd7..abd8c240 100644 --- a/altoslib/AltosMapStore.java +++ b/altoslib/AltosMapStore.java @@ -48,9 +48,21 @@ public class AltosMapStore { chlat, lat, chlon, lon, maptype_string, zoom, scale == 1 ? "" : String.format("-%d", scale), format_string)); } + public static String google_maps_api_key = null; + + private static String google_map_url(AltosLatLon center, int zoom, int maptype, int px_size, int scale, String format_string) { + return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&scale=%d&sensor=false&maptype=%s&format=%s&key=%s", + center.lat, center.lon, zoom, px_size, px_size, scale, + AltosMap.maptype_names[maptype], format_string, google_maps_api_key); + } + + private static String altos_map_url(AltosLatLon center, int zoom, int maptype, int px_size, int scale, String format_string) { + return String.format("https://maps.altusmetrum.org/altos-map?center=%.6f,%.6f&zoom=%d", + center.lat, center.lon, zoom); + } + private static String map_url(AltosLatLon center, int zoom, int maptype, int px_size, int scale) { String format_string; - int z = zoom; if (maptype == AltosMap.maptype_hybrid || maptype == AltosMap.maptype_satellite || maptype == AltosMap.maptype_terrain) format_string = "jpg"; @@ -58,14 +70,14 @@ public class AltosMapStore { format_string = "png32"; for (int s = 1; s < scale; s <<= 1) - z--; + zoom--; - if (AltosVersion.has_google_maps_api_key()) - return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&scale=%d&sensor=false&maptype=%s&format=%s&key=%s", - center.lat, center.lon, z, px_size/scale, px_size/scale, scale, AltosMap.maptype_names[maptype], format_string, AltosVersion.google_maps_api_key); + px_size /= scale; + + if (google_maps_api_key != null) + return google_map_url(center, zoom, maptype, px_size, scale, format_string); else - return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&scale=%d&sensor=false&maptype=%s&format=%s", - center.lat, center.lon, z, px_size/scale, px_size/scale, AltosMap.maptype_names[maptype], format_string); + return altos_map_url(center, zoom, maptype, px_size, scale, format_string); } public synchronized int status() { @@ -209,24 +221,8 @@ public class AltosMapStore { int new_status; - if (!AltosVersion.has_google_maps_api_key()) { - synchronized (fetch_lock) { - long startTime = System.nanoTime(); - new_status = fetch_url(); - if (new_status == AltosMapTile.fetched) { - long duration_ms = (System.nanoTime() - startTime) / 1000000; - if (duration_ms < google_maps_ratelimit_ms) { - try { - Thread.sleep(google_maps_ratelimit_ms - duration_ms); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - } - } - } else { - new_status = fetch_url(); - } + new_status = fetch_url(); + notify_listeners(new_status); } finally { finish_fetcher(); diff --git a/altoslib/AltosVersion.java.in b/altoslib/AltosVersion.java.in index fd9256aa..6b660bb5 100644 --- a/altoslib/AltosVersion.java.in +++ b/altoslib/AltosVersion.java.in @@ -20,10 +20,4 @@ package org.altusmetrum.altoslib_13; public class AltosVersion { public final static String version = "@VERSION@"; - - public final static String google_maps_api_key = "@GOOGLEKEY@"; - - public static boolean has_google_maps_api_key() { - return google_maps_api_key != null && google_maps_api_key.length() > 1; - } } diff --git a/altosuilib/AltosUIConfigure.java b/altosuilib/AltosUIConfigure.java index e466f8b2..e61b5d52 100644 --- a/altosuilib/AltosUIConfigure.java +++ b/altosuilib/AltosUIConfigure.java @@ -270,9 +270,8 @@ public class AltosUIConfigure constraints(0, 3)); row++; - pane.add(new JLabel (String.format("AltOS version %s (%smaps key)", - AltosVersion.version, - AltosVersion.has_google_maps_api_key() ? "" : "no ")), + pane.add(new JLabel (String.format("AltOS version %s", + AltosVersion.version)), constraints(0, 3)); row++; diff --git a/configure.ac b/configure.ac index 1705745d..741fa013 100644 --- a/configure.ac +++ b/configure.ac @@ -172,26 +172,12 @@ AM_CONDITIONAL(FATINSTALL, [test "x$FATDIR" != "xnone"]) AC_SUBST(FATDIR) -AC_ARG_WITH(google-key, AS_HELP_STRING([--with-google-key=PATH], - [Set the file to read the google maps API key from (defaults to ~/altusmetrumllc/google-maps-api-key)]), - [GOOGLEKEYFILE=$withval], [GOOGLEKEYFILE=$HOME/altusmetrumllc/google-maps-api-key]) - -if test -r "$GOOGLEKEYFILE" -a -s "$GOOGLEKEYFILE"; then - GOOGLEKEY=`cat "$GOOGLEKEYFILE"` - HAVE_GOOGLE_KEY="yes" -else - GOOGLEKEY='null' - HAVE_GOOGLE_KEY="no" -fi - AC_ARG_ENABLE(faketime, AS_HELP_STRING([--enable-faketime], [Use faketime program to ensure pdf files are reproducible (default=no)]), [FAKETIME=$enableval], [FAKETIME=no]) AM_CONDITIONAL(FAKETIME, [test x$FAKETIME = xyes]) -AC_SUBST(GOOGLEKEY) - AC_PROG_CC AC_PROG_INSTALL AC_PROG_LN_S -- cgit v1.2.3 From ff5faf1cc710b7e9299dcaec719bc2421f4ca5b4 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 6 Oct 2018 20:21:07 -0700 Subject: altoslib: Change map loading to lat=&lon= from center= This is easier to parse. Signed-off-by: Keith Packard --- altoslib/AltosMapStore.java | 2 +- doc/map-loading.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'altoslib/AltosMapStore.java') diff --git a/altoslib/AltosMapStore.java b/altoslib/AltosMapStore.java index abd8c240..7e0f3fb8 100644 --- a/altoslib/AltosMapStore.java +++ b/altoslib/AltosMapStore.java @@ -57,7 +57,7 @@ public class AltosMapStore { } private static String altos_map_url(AltosLatLon center, int zoom, int maptype, int px_size, int scale, String format_string) { - return String.format("https://maps.altusmetrum.org/altos-map?center=%.6f,%.6f&zoom=%d", + return String.format("https://maps.altusmetrum.org/cgi-bin/altos-map?lat=%.6f&lon=%.6f&zoom=%d", center.lat, center.lon, zoom); } diff --git a/doc/map-loading.txt b/doc/map-loading.txt index 3dffcd04..1b39dd6b 100644 --- a/doc/map-loading.txt +++ b/doc/map-loading.txt @@ -89,7 +89,7 @@ server. The elements of the elements are: Encoding this in a URI is straightforward: -\ altos-map?center=,&zoom= +\ altos-map?lat=&lon=&zoom= Latitude and longitude are both encoded using decimal degrees with 6 digits following the decimal point. -- cgit v1.2.3 From a8f4af38d7abbb1da922b5e4d84af218475f1752 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 6 Oct 2018 21:30:58 -0700 Subject: altoslib: Retry map connections 10 times on network failure This covers a host of problems between the application and google maps. Signed-off-by: Keith Packard --- altoslib/AltosMapStore.java | 103 +++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 49 deletions(-) (limited to 'altoslib/AltosMapStore.java') diff --git a/altoslib/AltosMapStore.java b/altoslib/AltosMapStore.java index 7e0f3fb8..2f85fb8d 100644 --- a/altoslib/AltosMapStore.java +++ b/altoslib/AltosMapStore.java @@ -100,11 +100,6 @@ public class AltosMapStore { listener.notify_store(this, status); } - static Object forbidden_lock = new Object(); - static long forbidden_time; - static boolean forbidden_set; - public static int forbidden_response; - private int fetch_url() { URL u; @@ -114,48 +109,64 @@ public class AltosMapStore { return AltosMapTile.bad_request; } - byte[] data; + byte[] data = null; URLConnection uc = null; - try { - uc = u.openConnection(); - String type = uc.getContentType(); - int contentLength = uc.getContentLength(); - if (uc instanceof HttpURLConnection) { - int response = ((HttpURLConnection) uc).getResponseCode(); - switch (response) { - case HttpURLConnection.HTTP_FORBIDDEN: - case HttpURLConnection.HTTP_PAYMENT_REQUIRED: - case HttpURLConnection.HTTP_UNAUTHORIZED: - synchronized (forbidden_lock) { - forbidden_time = System.nanoTime(); - forbidden_set = true; - forbidden_response = response; + + int status = AltosMapTile.failed; + int tries = 0; + + while (tries < 10 && status != AltosMapTile.fetched) { + try { + uc = u.openConnection(); + String type = uc.getContentType(); + int contentLength = uc.getContentLength(); + if (uc instanceof HttpURLConnection) { + int response = ((HttpURLConnection) uc).getResponseCode(); + switch (response) { + case HttpURLConnection.HTTP_FORBIDDEN: + case HttpURLConnection.HTTP_PAYMENT_REQUIRED: + case HttpURLConnection.HTTP_UNAUTHORIZED: return AltosMapTile.forbidden; } } - } - InputStream in = new BufferedInputStream(uc.getInputStream()); - int bytesRead = 0; - int offset = 0; - data = new byte[contentLength]; - while (offset < contentLength) { - bytesRead = in.read(data, offset, data.length - offset); - if (bytesRead == -1) - break; - offset += bytesRead; - } - in.close(); + InputStream in = new BufferedInputStream(uc.getInputStream()); + int bytesRead = 0; + int offset = 0; + data = new byte[contentLength]; + while (offset < contentLength) { + bytesRead = in.read(data, offset, data.length - offset); + if (bytesRead == -1) + break; + offset += bytesRead; + } + in.close(); - if (offset != contentLength) - return AltosMapTile.failed; + if (offset == contentLength) + status = AltosMapTile.fetched; + else + status = AltosMapTile.failed; - } catch (IOException e) { - return AltosMapTile.failed; + } catch (IOException e) { + status = AltosMapTile.failed; + } + + if (status != AltosMapTile.fetched) { + try { + Thread.sleep(100); + } catch (InterruptedException ie) { + } + tries++; + System.out.printf("Fetch failed, retrying %d\n", tries); + } } + if (status != AltosMapTile.fetched) + return status; + try { FileOutputStream out = new FileOutputStream(file); - out.write(data); + if (data != null) + out.write(data); out.flush(); out.close(); } catch (FileNotFoundException e) { @@ -170,18 +181,19 @@ public class AltosMapStore { static Object fetch_lock = new Object(); - static final long forbidden_interval = 60l * 1000l * 1000l * 1000l; - static final long google_maps_ratelimit_ms = 1200; - static Object fetcher_lock = new Object(); static LinkedList waiting = new LinkedList(); static LinkedList running = new LinkedList(); - static final int concurrent_fetchers = 128; + static int concurrent_fetchers() { + if (google_maps_api_key == null) + return 16; + return 128; + } static void start_fetchers() { - while (!waiting.isEmpty() && running.size() < concurrent_fetchers) { + while (!waiting.isEmpty() && running.size() < concurrent_fetchers()) { AltosMapStore s = waiting.remove(); running.add(s); Thread lt = s.make_fetcher_thread(); @@ -212,13 +224,6 @@ public class AltosMapStore { return; } - synchronized(forbidden_lock) { - if (forbidden_set && (System.nanoTime() - forbidden_time) < forbidden_interval) { - notify_listeners(AltosMapTile.forbidden); - return; - } - } - int new_status; new_status = fetch_url(); -- cgit v1.2.3