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.
19 import java.util.concurrent.*;
22 import org.altusmetrum.altoslib_13.*;
24 public class AltosMapdClient extends Thread implements AltosMapStoreListener {
25 private Socket socket;
26 private AltosJson request;
27 private AltosJson reply;
29 private void set_status(int status) {
30 reply.put("status", status);
33 private void set_filename(String filename) {
34 reply.put("filename", filename);
38 private void set_content_type(String content_type) {
39 reply.put("content_type", content_type);
42 private String content_type(File file) {
43 String content_type = "application/octet-stream";
44 String basename = file.getName();
45 if (basename.endsWith(".jpg"))
46 content_type = "image/jpeg";
47 if (basename.endsWith(".png"))
48 content_type = "image/png";
52 private void set_file(File file) {
53 set_filename(file.getAbsolutePath());
54 set_content_type(content_type(file));
57 private Semaphore store_ready;
59 public void notify_store(AltosMapStore map_store, int status) {
60 if (status != AltosMapTile.fetching)
61 store_ready.release();
65 reply = new AltosJson();
67 request = AltosJson.fromInputStream(socket.getInputStream());
69 double lat = request.get_double("lat", AltosLib.MISSING);
70 double lon = request.get_double("lon", AltosLib.MISSING);
71 int zoom = request.get_int("zoom", AltosLib.MISSING);
72 String addr = request.get_string("remote_addr", null);
74 if (lat == AltosLib.MISSING ||
75 lon == AltosLib.MISSING ||
76 zoom == AltosLib.MISSING ||
80 } else if (!AltosMapd.check_lat_lon(lat, lon)) {
81 set_status(403); /* Forbidden */
84 store_ready = new Semaphore(0);
86 System.out.printf("Fetching tile for %g %g %d\n", lat, lon, zoom);
88 AltosMapStore map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
95 if (map_store == null) {
96 System.out.printf("no store?\n");
97 status = AltosMapTile.failed;
99 map_store.add_listener(this);
101 System.out.printf("Waiting for tile\n");
104 store_ready.acquire();
105 } catch (Exception ie) {
108 status = map_store.status();
111 if (status == AltosMapTile.fetched || status == AltosMapTile.loaded) {
113 set_file(map_store.file);
114 } else if (status == AltosMapTile.failed) {
116 } else if (status == AltosMapTile.fetching) {
118 } else if (status == AltosMapTile.bad_request) {
120 } else if (status == AltosMapTile.forbidden) {
126 } catch (Exception e) {
127 System.out.printf("client exception %s\n", e.toString());
128 e.printStackTrace(System.out);
133 Writer writer = new PrintWriter(socket.getOutputStream());
137 } catch (IOException ie) {
141 } catch (IOException ie) {
143 System.out.printf("client done\n");
147 public AltosMapdClient(Socket socket) {
148 this.socket = socket;