09833363a0c4f6255ba4e0369059fe664fbbd063
[fw/altos] / map-server / altos-mapd / AltosMapdClient.java
1 /*
2  * Copyright © 2018 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 package altosmapd;
16
17 import java.net.*;
18 import java.util.*;
19 import java.util.concurrent.*;
20 import java.io.*;
21
22 import org.altusmetrum.altoslib_13.*;
23
24 public class AltosMapdClient extends Thread implements AltosMapStoreListener {
25         private Socket          socket;
26         private AltosJson       request;
27         private AltosJson       reply;
28
29         private int             http_status;
30
31         private void set_status(int status) {
32                 http_status = status;
33                 reply.put("status", status);
34         }
35
36         private void set_filename(String filename) {
37                 reply.put("filename", filename);
38
39         }
40
41         private void set_content_type(String content_type) {
42                 reply.put("content_type", content_type);
43         }
44
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";
52                 return content_type;
53         }
54
55         private void set_file(File file) {
56                 set_filename(file.getAbsolutePath());
57                 set_content_type(content_type(file));
58         }
59
60         private Semaphore store_ready;
61
62         public void notify_store(AltosMapStore map_store, int status) {
63                 if (status != AltosMapTile.fetching)
64                         store_ready.release();
65         }
66
67         public void run() {
68                 reply = new AltosJson();
69                 try {
70                         request = AltosJson.fromInputStream(socket.getInputStream());
71
72                         double  lat = request.get_double("lat", AltosLib.MISSING);
73                         double  lon = request.get_double("lon", AltosLib.MISSING);
74                         int     zoom = request.get_int("zoom", AltosLib.MISSING);
75                         String  addr = request.get_string("remote_addr", null);
76
77                         if (lat == AltosLib.MISSING ||
78                             lon == AltosLib.MISSING ||
79                             zoom == AltosLib.MISSING ||
80                             addr == null)
81                         {
82                                 set_status(400);
83                         } else if (!AltosMapd.check_lat_lon(lat, lon)) {
84                                 set_status(403);        /* Forbidden */
85                         } else {
86
87                                 store_ready = new Semaphore(0);
88
89                                 AltosMapStore   map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
90                                                                               zoom,
91                                                                               AltosMapd.maptype,
92                                                                               AltosMapd.px_size,
93                                                                               AltosMapd.scale);
94                                 int status;
95
96                                 if (map_store == null) {
97                                         status = AltosMapTile.failed;
98                                 } else {
99                                         map_store.add_listener(this);
100
101                                         try {
102                                                 store_ready.acquire();
103                                         } catch (Exception ie) {
104                                         }
105
106                                         status = map_store.status();
107                                 }
108
109                                 if (status == AltosMapTile.fetched || status == AltosMapTile.loaded) {
110                                         set_status(200);
111                                         set_file(map_store.file);
112                                 } else if (status == AltosMapTile.failed) {
113                                         set_status(404);
114                                 } else if (status == AltosMapTile.fetching) {
115                                         set_status(408);
116                                 } else if (status == AltosMapTile.bad_request) {
117                                         set_status(400);
118                                 } else if (status == AltosMapTile.forbidden) {
119                                         set_status(403);
120                                 } else {
121                                         set_status(400);
122                                 }
123                         }
124                         System.out.printf("%s: %.6f %.6f %d status %d\n",
125                                           addr, lat, lon, zoom, http_status);
126
127                 } catch (Exception e) {
128                         System.out.printf("client exception %s\n", e.toString());
129                         e.printStackTrace(System.out);
130                         set_status(400);
131
132                 } finally {
133                         try {
134                                 Writer writer = new PrintWriter(socket.getOutputStream());
135                                 reply.write(writer);
136                                 writer.write('\n');
137                                 writer.flush();
138                         } catch (IOException ie) {
139                         }
140                         try {
141                                 socket.close();
142                         } catch (IOException ie) {
143                         }
144                 }
145         }
146
147         public AltosMapdClient(Socket socket) {
148                 this.socket = socket;
149                 start();
150         }
151 }