summaryrefslogtreecommitdiff
path: root/map-server/altos-mapd/AltosMapdClient.java
blob: 70ceae82275800e817bc1ec4e820c293458f2f55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright © 2018 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package altosmapd;

import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import java.io.*;

import org.altusmetrum.altoslib_13.*;

public class AltosMapdClient extends Thread implements AltosMapStoreListener {
	private Socket		socket;
	private AltosJson	request;
	private AltosJson	reply;

	private void set_status(int status) {
		reply.put("status", status);
	}

	private void set_filename(String filename) {
		reply.put("filename", filename);

	}

	private void set_content_type(String content_type) {
		reply.put("content_type", content_type);
	}

	private String content_type(File file) {
		String content_type = "application/octet-stream";
		String basename = file.getName();
		if (basename.endsWith(".jpg"))
			content_type = "image/jpeg";
		if (basename.endsWith(".png"))
			content_type = "image/png";
		return content_type;
	}

	private void set_file(File file) {
		set_filename(file.getAbsolutePath());
		set_content_type(content_type(file));
	}

	private Semaphore store_ready;

	public void notify_store(AltosMapStore map_store, int status) {
		if (status != AltosMapTile.fetching)
			store_ready.release();
	}

	public void run() {
		reply = new AltosJson();
		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)
			{
				set_status(400);
			} else if (!AltosMapd.check_lat_lon(lat, lon)) {
				set_status(403);	/* Forbidden */
			} else {

				store_ready = new Semaphore(0);

				System.out.printf("Fetching tile for %g %g %d\n", lat, lon, zoom);

				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;
				} else {
					map_store.add_listener(this);

					System.out.printf("Waiting for tile\n");

					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);
				}
			}
		} catch (Exception e) {
			System.out.printf("client exception %s\n", e.toString());
			e.printStackTrace(System.out);
			set_status(400);

		} finally {
			try {
				Writer writer = new PrintWriter(socket.getOutputStream());
				reply.write(writer);
				writer.write('\n');
				writer.flush();
			} catch (IOException ie) {
			}
			try {
				socket.close();
			} catch (IOException ie) {
			}
			System.out.printf("client done\n");
		}
	}

	public AltosMapdClient(Socket socket) {
		this.socket = socket;
		start();
	}
}