import java.util.concurrent.*;
import java.io.*;
-import org.altusmetrum.altoslib_13.*;
+import org.altusmetrum.altoslib_14.*;
public class AltosMapdClient extends Thread implements AltosMapStoreListener {
private Socket socket;
private AltosJson request;
private AltosJson reply;
+ private int http_status;
+
private void set_status(int status) {
+ http_status = status;
reply.put("status", status);
}
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)
- {
+ if (request == null) {
set_status(400);
+ System.out.printf("client failed %d\n", http_status);
} else {
- store_ready = new Semaphore(0);
- System.out.printf("Fetching tile for %g %g %d\n", lat, lon, zoom);
+ 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);
- 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;
+ if (lat == AltosLib.MISSING ||
+ lon == AltosLib.MISSING ||
+ zoom == AltosLib.MISSING ||
+ addr == null)
+ {
+ set_status(400);
+ } else if (!AltosMapd.check_lat_lon(lat, lon, zoom)) {
+ set_status(403); /* Forbidden */
} else {
- map_store.add_listener(this);
- System.out.printf("Waiting for tile\n");
+ store_ready = new Semaphore(0);
- try {
- store_ready.acquire();
- } catch (Exception ie) {
- }
+ AltosMapStore map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
+ zoom,
+ AltosMapd.maptype,
+ AltosMapd.px_size,
+ AltosMapd.scale);
+ int status;
- status = map_store.status();
- }
+ if (map_store == null) {
+ status = AltosMapTile.failed;
+ } else {
+ map_store.add_listener(this);
- 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);
+ 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);
+ }
}
+ System.out.printf("%s: %.6f %.6f %d status %d\n",
+ addr, lat, lon, zoom, http_status);
}
} catch (Exception e) {
System.out.printf("client exception %s\n", e.toString());
socket.close();
} catch (IOException ie) {
}
- System.out.printf("client done\n");
}
}