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_14.*;
24 public class AltosMapdClient extends Thread implements AltosMapStoreListener {
25 private Socket socket;
26 private AltosJson request;
27 private AltosJson reply;
29 private int http_status;
31 private void set_status(int status) {
33 reply.put("status", status);
36 private void set_filename(String filename) {
37 reply.put("filename", filename);
41 private void set_content_type(String content_type) {
42 reply.put("content_type", content_type);
45 private String content_type(File file) {
46 String content_type = "application/octet-stream";
47 String basename = file.getName();
48 if (basename.endsWith(".jpg"))
49 content_type = "image/jpeg";
50 if (basename.endsWith(".png"))
51 content_type = "image/png";
55 private void set_file(File file) {
56 set_filename(file.getAbsolutePath());
57 set_content_type(content_type(file));
60 private Semaphore store_ready;
62 public void notify_store(AltosMapStore map_store, int status) {
63 if (status != AltosMapTile.fetching)
64 store_ready.release();
68 reply = new AltosJson();
70 request = AltosJson.fromInputStream(socket.getInputStream());
72 if (request == null) {
74 System.out.printf("client failed %d\n", http_status);
77 double lat = request.get_double("lat", AltosLib.MISSING);
78 double lon = request.get_double("lon", AltosLib.MISSING);
79 int zoom = request.get_int("zoom", AltosLib.MISSING);
80 String addr = request.get_string("remote_addr", null);
82 if (lat == AltosLib.MISSING ||
83 lon == AltosLib.MISSING ||
84 zoom == AltosLib.MISSING ||
88 } else if (!AltosMapd.check_lat_lon(lat, lon, zoom)) {
89 set_status(403); /* Forbidden */
92 store_ready = new Semaphore(0);
94 AltosMapStore map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
101 if (map_store == null) {
102 status = AltosMapTile.failed;
104 map_store.add_listener(this);
107 store_ready.acquire();
108 } catch (Exception ie) {
111 status = map_store.status();
114 if (status == AltosMapTile.fetched || status == AltosMapTile.loaded) {
116 set_file(map_store.file);
117 } else if (status == AltosMapTile.failed) {
119 } else if (status == AltosMapTile.fetching) {
121 } else if (status == AltosMapTile.bad_request) {
123 } else if (status == AltosMapTile.forbidden) {
129 System.out.printf("%s: %.6f %.6f %d status %d\n",
130 addr, lat, lon, zoom, http_status);
132 } catch (Exception e) {
133 System.out.printf("client exception %s\n", e.toString());
134 e.printStackTrace(System.out);
139 Writer writer = new PrintWriter(socket.getOutputStream());
143 } catch (IOException ie) {
147 } catch (IOException ie) {
152 public AltosMapdClient(Socket socket) {
153 this.socket = socket;