1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.imagery;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.Main;
|
---|
5 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
6 | import org.openstreetmap.josm.data.imagery.GeorefImage.State;
|
---|
7 | import org.openstreetmap.josm.gui.MapView;
|
---|
8 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
---|
9 |
|
---|
10 | public abstract class Grabber implements Runnable {
|
---|
11 | protected final MapView mv;
|
---|
12 | protected final WMSLayer layer;
|
---|
13 | private final boolean localOnly;
|
---|
14 |
|
---|
15 | protected ProjectionBounds b;
|
---|
16 | protected volatile boolean canceled;
|
---|
17 |
|
---|
18 | Grabber(MapView mv, WMSLayer layer, boolean localOnly) {
|
---|
19 | this.mv = mv;
|
---|
20 | this.layer = layer;
|
---|
21 | this.localOnly = localOnly;
|
---|
22 | }
|
---|
23 |
|
---|
24 | abstract void fetch(WMSRequest request, int attempt) throws Exception; // the image fetch code
|
---|
25 |
|
---|
26 | int width(){
|
---|
27 | return layer.getBaseImageWidth();
|
---|
28 | }
|
---|
29 |
|
---|
30 | int height(){
|
---|
31 | return layer.getBaseImageHeight();
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public void run() {
|
---|
36 | while (true) {
|
---|
37 | if (canceled)
|
---|
38 | return;
|
---|
39 | WMSRequest request = layer.getRequest(localOnly);
|
---|
40 | if (request == null)
|
---|
41 | return;
|
---|
42 | this.b = layer.getBounds(request);
|
---|
43 | if (request.isPrecacheOnly()) {
|
---|
44 | if (!layer.cache.hasExactMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth)) {
|
---|
45 | attempt(request);
|
---|
46 | }
|
---|
47 | } else {
|
---|
48 | if(!loadFromCache(request)){
|
---|
49 | attempt(request);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | layer.finishRequest(request);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected void attempt(WMSRequest request){ // try to fetch the image
|
---|
57 | int maxTries = 5; // n tries for every image
|
---|
58 | for (int i = 1; i <= maxTries; i++) {
|
---|
59 | if (canceled)
|
---|
60 | return;
|
---|
61 | try {
|
---|
62 | if (!request.isPrecacheOnly() && !layer.requestIsVisible(request))
|
---|
63 | return;
|
---|
64 | fetch(request, i);
|
---|
65 | break; // break out of the retry loop
|
---|
66 | } catch (Exception e) {
|
---|
67 | try { // sleep some time and then ask the server again
|
---|
68 | Thread.sleep(random(1000, 2000));
|
---|
69 | } catch (InterruptedException e1) {
|
---|
70 | Main.debug("InterruptedException in "+getClass().getSimpleName()+" during WMS request");
|
---|
71 | }
|
---|
72 | if(i == maxTries) {
|
---|
73 | Main.error(e);
|
---|
74 | request.finish(State.FAILED, null);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public static int random(int min, int max) {
|
---|
81 | return (int)(Math.random() * ((max+1)-min) ) + min;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public abstract boolean loadFromCache(WMSRequest request);
|
---|
85 |
|
---|
86 | public void cancel() {
|
---|
87 | canceled = true;
|
---|
88 | }
|
---|
89 | }
|
---|