c4316dabf4f5628cc26c3bddcbfcdab0b960e279
[fw/altos] / altosuilib / AltosSiteMapCache.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_2;
19
20 import javax.swing.*;
21 import javax.imageio.ImageIO;
22 import java.awt.image.*;
23 import java.awt.*;
24 import java.io.*;
25 import java.net.*;
26
27 public class AltosSiteMapCache {
28         static final long google_maps_ratelimit_ms = 1200;
29         // Google limits static map queries to 50 per minute per IP, so
30         // each query should take at least 1.2 seconds.
31
32         static final int        success = 0;
33         static final int        loading = 1;
34         static final int        failed = 2;
35         static final int        bad_request = 3;
36         static final int        forbidden = 4;
37
38         public static boolean has_map(File file, String url) {
39                 return file.exists();
40         }
41
42         static long     forbidden_time;
43         static boolean  forbidden_set = false;
44         static final long       forbidden_interval = 60l * 1000l * 1000l * 1000l;
45
46         static private Object fetch_lock = new Object();
47
48         private static int fetch_one(File file, String url) {
49                 URL u;
50
51                 System.out.printf("Loading URL %s\n", url);
52                 try {
53                         u = new URL(url);
54                 } catch (java.net.MalformedURLException e) {
55                         return bad_request;
56                 }
57
58                 byte[] data;
59                 URLConnection uc = null;
60                 try {
61                         uc = u.openConnection();
62                         String type = uc.getContentType();
63                         int contentLength = uc.getContentLength();
64                         if (uc instanceof HttpURLConnection) {
65                                 int response = ((HttpURLConnection) uc).getResponseCode();
66                                 switch (response) {
67                                 case HttpURLConnection.HTTP_FORBIDDEN:
68                                 case HttpURLConnection.HTTP_PAYMENT_REQUIRED:
69                                 case HttpURLConnection.HTTP_UNAUTHORIZED:
70                                         forbidden_time = System.nanoTime();
71                                         forbidden_set = true;
72                                         return forbidden;
73                                 }
74                         }
75                         InputStream in = new BufferedInputStream(uc.getInputStream());
76                         int bytesRead = 0;
77                         int offset = 0;
78                         data = new byte[contentLength];
79                         while (offset < contentLength) {
80                                 bytesRead = in.read(data, offset, data.length - offset);
81                                 if (bytesRead == -1)
82                                         break;
83                                 offset += bytesRead;
84                         }
85                         in.close();
86
87                         if (offset != contentLength)
88                                 return failed;
89
90                 } catch (IOException e) {
91                         return failed;
92                 }
93
94                 try {
95                         FileOutputStream out = new FileOutputStream(file);
96                         out.write(data);
97                         out.flush();
98                         out.close();
99                 } catch (FileNotFoundException e) {
100                         return bad_request;
101                 } catch (IOException e) {
102                         if (file.exists())
103                                 file.delete();
104                         return bad_request;
105                 }
106                 return success;
107         }
108
109         public static int fetch_map(File file, String url) {
110                 if (file.exists())
111                         return success;
112
113                 if (forbidden_set && (System.nanoTime() - forbidden_time) < forbidden_interval)
114                         return forbidden;
115
116                 int     status = bad_request;
117
118                 if (!AltosUIVersion.has_google_maps_api_key()) {
119                         synchronized (fetch_lock) {
120                                 long startTime = System.nanoTime();
121                                 status = fetch_one(file, url);
122                                 if (status == success) {
123                                         long duration_ms = (System.nanoTime() - startTime) / 1000000;
124                                         if (duration_ms < google_maps_ratelimit_ms) {
125                                                 try {
126                                                         Thread.sleep(google_maps_ratelimit_ms - duration_ms);
127                                                 } catch (InterruptedException e) {
128                                                         Thread.currentThread().interrupt();
129                                                 }
130                                         }
131                                 }
132                         }
133                 } else {
134                         status = fetch_one(file, url);
135                 }
136                 return status;
137         }
138
139         static final int                min_cache_size = 9;
140         static final int                max_cache_size = 24;
141
142         static int                      cache_size = min_cache_size;
143
144         static AltosSiteMapImage[]      images = new AltosSiteMapImage[cache_size];
145
146         static Object cache_lock = new Object();
147
148         public  static void set_cache_size(int new_size) {
149                 if (new_size < min_cache_size)
150                         new_size = min_cache_size;
151                 if (new_size > max_cache_size)
152                         new_size = max_cache_size;
153                 if (new_size == cache_size)
154                         return;
155
156                 synchronized(cache_lock) {
157                         AltosSiteMapImage[]     new_images = new AltosSiteMapImage[new_size];
158
159                         for (int i = 0; i < cache_size; i++) {
160                                 if (i < new_size)
161                                         new_images[i] = images[i];
162                                 else
163                                         images[i].flush();
164                         }
165                         images = new_images;
166                         cache_size = new_size;
167                 }
168         }
169
170         static long                     used;
171
172         private static Point tile_loc(AltosSiteMapTile tile) {
173                 Rectangle       r = tile.getBounds();
174                 int             x = r.x / 512;
175                 int             y = r.y / 512;
176
177                 return new Point (x, y);
178         }
179
180         private static void dump_cache() {
181                 int     min_x = 1000, max_x = -1000, min_y = 1000, max_y = -1000;
182
183                 for (int i = 0; i < cache_size; i++) {
184                         AltosSiteMapImage       image = images[i];
185                         if (image != null) {
186                                 Point p = tile_loc(image.tile);
187                                 min_x = min_x < p.x ? min_x : p.x;
188                                 max_x = max_x > p.x ? max_x : p.x;
189                                 min_y = min_y < p.y ? min_y : p.y;
190                                 max_y = max_y > p.y ? max_y : p.y;
191                                 System.out.printf ("entry %d %d,%d used %d\n", i, p.x, p.y, image.used);
192                         } else {
193                                 System.out.printf ("entry %d empty\n", i);
194                         }
195                 }
196
197                 int[][] map = new int[max_x - min_x + 1][max_y - min_y + 1];
198                 for (int i = 0; i < cache_size; i++) {
199                         AltosSiteMapImage       image = images[i];
200                         if (image != null) {
201                                 Point p = tile_loc(image.tile);
202                                 map[p.x - min_x][p.y - min_y]++;
203                         }
204                 }
205
206                 for (int y = min_y; y <= max_y; y++) {
207                         for (int x = min_x; x <= max_x; x++)
208                                 System.out.printf (" %2d", map[x - min_x][y - min_y]);
209                         System.out.printf("\n");
210                 }
211         }
212
213         public static AltosSiteMapImage get_image(AltosSiteMapTile tile, File file, int width, int height) {
214                 int             oldest = -1;
215                 long            age = used;
216
217                 synchronized(cache_lock) {
218                         AltosSiteMapImage       image = null;
219                         for (int i = 0; i < cache_size; i++) {
220                                 image = images[i];
221
222                                 if (image == null) {
223                                         oldest = i;
224                                         break;
225                                 }
226                                 if (image.tile == tile && file.equals(image.file)) {
227                                         image.used = used++;
228                                         return image;
229                                 }
230                                 if (image.used < age) {
231                                         oldest = i;
232                                         age = image.used;
233                                 }
234                         }
235
236                         try {
237                                 image = new AltosSiteMapImage(tile, file, width, height);
238                                 image.used = used++;
239                                 if (images[oldest] != null) {
240 //                                      dump_cache();
241                                         AltosSiteMap.debug_component(images[oldest].tile, "replacing cache");
242                                         AltosSiteMap.debug_component(tile, "replaced cache");
243                                         images[oldest].flush();
244                                 }
245                                 images[oldest] = image;
246                                 return image;
247                         } catch (IOException e) {
248                                 return null;
249                         }
250                 }
251         }
252 }