create HttpEngine instances for each loader thread
This commit is contained in:
parent
f3035d827b
commit
f75702a575
@ -1,19 +1,24 @@
|
|||||||
package org.oscim.tiling.source;
|
package org.oscim.tiling.source;
|
||||||
|
|
||||||
import org.oscim.core.Tile;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
import org.oscim.core.Tile;
|
||||||
|
|
||||||
public interface HttpEngine {
|
public interface HttpEngine {
|
||||||
InputStream read() throws IOException;
|
|
||||||
|
|
||||||
boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException;
|
InputStream read() throws IOException;
|
||||||
|
|
||||||
void close();
|
boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException;
|
||||||
|
|
||||||
void setCache(OutputStream os);
|
void close();
|
||||||
|
|
||||||
boolean requestCompleted(boolean success);
|
void setCache(OutputStream os);
|
||||||
|
|
||||||
|
boolean requestCompleted(boolean success);
|
||||||
|
|
||||||
|
public interface Factory {
|
||||||
|
public HttpEngine create();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,35 @@
|
|||||||
package org.oscim.tiling.source;
|
package org.oscim.tiling.source;
|
||||||
|
|
||||||
import com.squareup.okhttp.OkHttpClient;
|
|
||||||
|
|
||||||
import org.oscim.core.Tile;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.oscim.core.Tile;
|
||||||
|
|
||||||
|
import com.squareup.okhttp.OkHttpClient;
|
||||||
|
|
||||||
public class OkHttpEngine implements HttpEngine {
|
public class OkHttpEngine implements HttpEngine {
|
||||||
private final URL baseUrl;
|
|
||||||
private final OkHttpClient client;
|
private final OkHttpClient client;
|
||||||
|
|
||||||
|
public static class OkHttpFactory implements HttpEngine.Factory {
|
||||||
|
private final OkHttpClient client;
|
||||||
|
|
||||||
|
public OkHttpFactory() {
|
||||||
|
this.client = new OkHttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpEngine create() {
|
||||||
|
return new OkHttpEngine(client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private InputStream inputStream;
|
private InputStream inputStream;
|
||||||
|
|
||||||
public OkHttpEngine(URL baseUrl) {
|
public OkHttpEngine(OkHttpClient client) {
|
||||||
this.baseUrl = baseUrl;
|
this.client = client;
|
||||||
this.client = new OkHttpClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -31,14 +43,14 @@ public class OkHttpEngine implements HttpEngine {
|
|||||||
throw new IllegalArgumentException("Tile cannot be null.");
|
throw new IllegalArgumentException("Tile cannot be null.");
|
||||||
}
|
}
|
||||||
|
|
||||||
final URL requestUrl = new URL(baseUrl.toString()
|
final URL requestUrl = new URL(tileSource.getUrl()
|
||||||
+ "/"
|
+ "/"
|
||||||
+ Byte.toString(tile.zoomLevel)
|
+ Byte.toString(tile.zoomLevel)
|
||||||
+ "/"
|
+ "/"
|
||||||
+ tile.tileX
|
+ tile.tileX
|
||||||
+ "/"
|
+ "/"
|
||||||
+ tile.tileY
|
+ tile.tileY
|
||||||
+ ".vtm");
|
+ ".vtm");
|
||||||
|
|
||||||
final HttpURLConnection connection = client.open(requestUrl);
|
final HttpURLConnection connection = client.open(requestUrl);
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ public abstract class UrlTileSource extends TileSource {
|
|||||||
|
|
||||||
private final URL mUrl;
|
private final URL mUrl;
|
||||||
private byte[] mExt;
|
private byte[] mExt;
|
||||||
private HttpEngine httpEngine;
|
private HttpEngine.Factory mHttpFactory;
|
||||||
|
|
||||||
public UrlTileSource(String urlString) {
|
public UrlTileSource(String urlString) {
|
||||||
URL url = null;
|
URL url = null;
|
||||||
@ -109,15 +109,15 @@ public abstract class UrlTileSource extends TileSource {
|
|||||||
return mUrl;
|
return mUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHttpEngine(HttpEngine httpEngine) {
|
public void setHttpEngine(HttpEngine.Factory httpFactory) {
|
||||||
this.httpEngine = httpEngine;
|
mHttpFactory = httpFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpEngine getHttpEngine() {
|
public HttpEngine getHttpEngine() {
|
||||||
if (httpEngine == null) {
|
if (mHttpFactory == null) {
|
||||||
httpEngine = new LwHttp(getUrl());
|
return new LwHttp(getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
return httpEngine;
|
return mHttpFactory.create();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user