1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.concurrent.ThreadPoolExecutor;
|
---|
7 | import java.util.concurrent.TimeUnit;
|
---|
8 |
|
---|
9 | import org.apache.commons.jcs.access.behavior.ICacheAccess;
|
---|
10 | import org.openstreetmap.gui.jmapviewer.Tile;
|
---|
11 | import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader;
|
---|
12 | import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
|
---|
13 | import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
|
---|
14 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
15 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
|
---|
16 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
17 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
18 | import org.openstreetmap.josm.data.cache.HostLimitQueue;
|
---|
19 | import org.openstreetmap.josm.data.cache.JCSCacheManager;
|
---|
20 | import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;
|
---|
21 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @author Wiktor Niesiobędzki
|
---|
25 | *
|
---|
26 | * Wrapper class that bridges between JCS cache and Tile Loaders
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public class TMSCachedTileLoader implements TileLoader, CachedTileLoader, TileCache {
|
---|
30 |
|
---|
31 | private ICacheAccess<String, BufferedImageCacheEntry> cache;
|
---|
32 | private int connectTimeout;
|
---|
33 | private int readTimeout;
|
---|
34 | private Map<String, String> headers;
|
---|
35 | private TileLoaderListener listener;
|
---|
36 | private static final String PREFERENCE_PREFIX = "imagery.tms.cache.";
|
---|
37 | /**
|
---|
38 | * how many object on disk should be stored for TMS region. Average tile size is about 20kb
|
---|
39 | */
|
---|
40 | public static final IntegerProperty MAX_OBJECTS_ON_DISK = new IntegerProperty(PREFERENCE_PREFIX + "max_objects_disk", 25000); // 25000 is around 500MB under this assumptions
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * overrides the THREAD_LIMIT in superclass, as we want to have separate limit and pool for TMS
|
---|
44 | */
|
---|
45 | public static final IntegerProperty THREAD_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobs", 25);
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Limit definition for per host concurrent connections
|
---|
49 | */
|
---|
50 | public static final IntegerProperty HOST_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobsperhost", 6);
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * separate from JCS thread pool for TMS loader, so we can have different thread pools for default JCS
|
---|
55 | * and for TMS imagery
|
---|
56 | */
|
---|
57 | private static ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER = getThreadPoolExecutor();
|
---|
58 |
|
---|
59 | private static ThreadPoolExecutor getThreadPoolExecutor() {
|
---|
60 | return new ThreadPoolExecutor(
|
---|
61 | THREAD_LIMIT.get().intValue(), // keep the thread number constant
|
---|
62 | THREAD_LIMIT.get().intValue(), // do not this number of threads
|
---|
63 | 30, // keepalive for thread
|
---|
64 | TimeUnit.SECONDS,
|
---|
65 | new HostLimitQueue(HOST_LIMIT.get().intValue()),
|
---|
66 | JCSCachedTileLoaderJob.getNamedThreadFactory("TMS downloader")
|
---|
67 | );
|
---|
68 | }
|
---|
69 |
|
---|
70 | private ThreadPoolExecutor downloadExecutor = DEFAULT_DOWNLOAD_JOB_DISPATCHER;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Constructor
|
---|
74 | * @param listener called when tile loading has finished
|
---|
75 | * @param name of the cache
|
---|
76 | * @param connectTimeout to remote resource
|
---|
77 | * @param readTimeout to remote resource
|
---|
78 | * @param headers to be sent along with request
|
---|
79 | * @param cacheDir where cache file shall reside
|
---|
80 | * @throws IOException when cache initialization fails
|
---|
81 | */
|
---|
82 | public TMSCachedTileLoader(TileLoaderListener listener, String name, int connectTimeout, int readTimeout, Map<String, String> headers, String cacheDir) throws IOException {
|
---|
83 | this.cache = JCSCacheManager.getCache(name,
|
---|
84 | 200, // use fairly small memory cache, as cached objects are quite big, as they contain BufferedImages
|
---|
85 | MAX_OBJECTS_ON_DISK.get(),
|
---|
86 | cacheDir);
|
---|
87 | this.connectTimeout = connectTimeout;
|
---|
88 | this.readTimeout = readTimeout;
|
---|
89 | this.headers = headers;
|
---|
90 | this.listener = listener;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public TileJob createTileLoaderJob(Tile tile) {
|
---|
95 | return new TMSCachedTileLoaderJob(listener, tile, cache,
|
---|
96 | connectTimeout, readTimeout, headers, downloadExecutor);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public void clearCache(TileSource source) {
|
---|
101 | this.cache.clear();
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public Tile getTile(TileSource source, int x, int y, int z) {
|
---|
106 | return createTileLoaderJob(new Tile(source,x, y, z)).getTile();
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public void addTile(Tile tile) {
|
---|
111 | createTileLoaderJob(tile).getTile();
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public int getTileCount() {
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public void clear() {
|
---|
121 | cache.clear();
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * @return cache statistics as string
|
---|
126 | */
|
---|
127 | public String getStats() {
|
---|
128 | return cache.getStats();
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * cancels all outstanding tasks in the queue. This rollbacks the state of the tiles in the queue
|
---|
133 | * to loading = false / loaded = false
|
---|
134 | */
|
---|
135 | public void cancelOutstandingTasks() {
|
---|
136 | for(Runnable r: downloadExecutor.getQueue()) {
|
---|
137 | if (downloadExecutor.remove(r) && r instanceof TMSCachedTileLoaderJob) {
|
---|
138 | ((TMSCachedTileLoaderJob)r).handleJobCancellation();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|