altos-map: Install .jar file for altos-map
[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 void set_status(int status) {
30                 reply.put("status", status);
31         }
32
33         private void set_filename(String filename) {
34                 reply.put("filename", filename);
35
36         }
37
38         private void set_content_type(String content_type) {
39                 reply.put("content_type", content_type);
40         }
41
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";
49                 return content_type;
50         }
51
52         private void set_file(File file) {
53                 set_filename(file.getAbsolutePath());
54                 set_content_type(content_type(file));
55         }
56
57         private Semaphore store_ready;
58
59         public void notify_store(AltosMapStore map_store, int status) {
60                 if (status != AltosMapTile.fetching)
61                         store_ready.release();
62         }
63
64         public void run() {
65                 reply = new AltosJson();
66                 try {
67                         request = AltosJson.fromInputStream(socket.getInputStream());
68
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);
73
74                         if (lat == AltosLib.MISSING ||
75                             lon == AltosLib.MISSING ||
76                             zoom == AltosLib.MISSING ||
77                             addr == null)
78                         {
79                                 set_status(400);
80                         } else {
81                                 store_ready = new Semaphore(0);
82
83                                 System.out.printf("Fetching tile for %g %g %d\n", lat, lon, zoom);
84
85                                 AltosMapStore   map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
86                                                                               zoom,
87                                                                               AltosMapd.maptype,
88                                                                               AltosMapd.px_size,
89                                                                               AltosMapd.scale);
90                                 int status;
91
92                                 if (map_store == null) {
93                                         System.out.printf("no store?\n");
94                                         status = AltosMapTile.failed;
95                                 } else {
96                                         map_store.add_listener(this);
97
98                                         System.out.printf("Waiting for tile\n");
99
100                                         try {
101                                                 store_ready.acquire();
102                                         } catch (Exception ie) {
103                                         }
104
105                                         status = map_store.status();
106                                 }
107
108                                 if (status == AltosMapTile.fetched || status == AltosMapTile.loaded) {
109                                         set_status(200);
110                                         set_file(map_store.file);
111                                 } else if (status == AltosMapTile.failed) {
112                                         set_status(404);
113                                 } else if (status == AltosMapTile.fetching) {
114                                         set_status(408);
115                                 } else if (status == AltosMapTile.bad_request) {
116                                         set_status(400);
117                                 } else if (status == AltosMapTile.forbidden) {
118                                         set_status(403);
119                                 } else {
120                                         set_status(400);
121                                 }
122                         }
123                 } catch (Exception e) {
124                         System.out.printf("client exception %s\n", e.toString());
125                         e.printStackTrace(System.out);
126                         set_status(400);
127
128                 } finally {
129                         try {
130                                 Writer writer = new PrintWriter(socket.getOutputStream());
131                                 reply.write(writer);
132                                 writer.write('\n');
133                                 writer.flush();
134                         } catch (IOException ie) {
135                         }
136                         try {
137                                 socket.close();
138                         } catch (IOException ie) {
139                         }
140                         System.out.printf("client done\n");
141                 }
142         }
143
144         public AltosMapdClient(Socket socket) {
145                 this.socket = socket;
146                 start();
147         }
148 }