reinvent downloader mechanism
git-svn-id: https://osmand.googlecode.com/svn/trunk@26 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
parent
76b36291bb
commit
57475433ec
3 changed files with 61 additions and 41 deletions
|
@ -213,7 +213,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
|
||||||
cache.remove(list.get(i));
|
cache.remove(list.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(en.exists()){
|
if(en.exists() && !downloader.isFileCurrentlyDownloaded(en)){
|
||||||
long time = System.currentTimeMillis();
|
long time = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
cache.put(file, ImageIO.read(en));
|
cache.put(file, ImageIO.read(en));
|
||||||
|
@ -230,8 +230,22 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tileDownloaded(String dowloadedUrl, DownloadRequest fileSaved) {
|
public void tileDownloaded(String dowloadedUrl, DownloadRequest request) {
|
||||||
prepareImage(false);
|
int tileSize = getTileSize();
|
||||||
|
double xTileLeft = getXTile() - getSize().width / (2d * tileSize);
|
||||||
|
double yTileUp = getYTile() - getSize().height / (2d * tileSize);
|
||||||
|
int i = request.xTile - (int)xTileLeft;
|
||||||
|
int j = request.yTile - (int)yTileUp;
|
||||||
|
if(request.zoom == this.zoom &&
|
||||||
|
(i >=0 && i<images.length) && (j>=0 && j< images[i].length)){
|
||||||
|
try {
|
||||||
|
images[i][j] = getImageFor(request.xTile, request.yTile, zoom);
|
||||||
|
repaint();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Eror reading png " + request.xTile +" " + request.yTile + " zoom : " + zoom, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void prepareImage(){
|
public void prepareImage(){
|
||||||
|
@ -272,7 +286,8 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
|
||||||
String urlToLoad = map.getUrlToLoad(x, y, zoom);
|
String urlToLoad = map.getUrlToLoad(x, y, zoom);
|
||||||
if(urlToLoad != null){
|
if(urlToLoad != null){
|
||||||
downloader.requestToDownload(urlToLoad,
|
downloader.requestToDownload(urlToLoad,
|
||||||
new DownloadRequest(new File(tilesLocation, getFileForImage(x, y, zoom))));
|
new DownloadRequest(new File(tilesLocation, getFileForImage(x, y, zoom)),
|
||||||
|
x, y, zoom));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -296,7 +311,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
|
||||||
|
|
||||||
repaint();
|
repaint();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error("Eror reading png preparing images");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,9 @@ import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Set;
|
||||||
import java.util.concurrent.PriorityBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@ -24,7 +24,8 @@ public class MapTileDownloader {
|
||||||
|
|
||||||
private ThreadPoolExecutor threadPoolExecutor;
|
private ThreadPoolExecutor threadPoolExecutor;
|
||||||
private IMapDownloaderCallback callback;
|
private IMapDownloaderCallback callback;
|
||||||
private Map<String, DownloadRequest> requestsToLoad;
|
|
||||||
|
private Set<File> currentlyDownloaded;
|
||||||
|
|
||||||
|
|
||||||
public static MapTileDownloader getInstance(){
|
public static MapTileDownloader getInstance(){
|
||||||
|
@ -64,16 +65,15 @@ public class MapTileDownloader {
|
||||||
yTile = -1;
|
yTile = -1;
|
||||||
zoom = -1;
|
zoom = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public MapTileDownloader(int numberOfThreads){
|
public MapTileDownloader(int numberOfThreads){
|
||||||
threadPoolExecutor = new ThreadPoolExecutor(1, numberOfThreads, DefaultLauncherConstants.TILE_DOWNLOAD_SECONTS_TO_WORK,
|
threadPoolExecutor = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, DefaultLauncherConstants.TILE_DOWNLOAD_SECONTS_TO_WORK,
|
||||||
TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>());
|
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
|
||||||
requestsToLoad = Collections.synchronizedMap(new LinkedHashMap<String, DownloadRequest>());
|
// 1.6 method but very useful to kill non-running threads
|
||||||
|
// threadPoolExecutor.allowCoreThreadTimeOut(true);
|
||||||
|
currentlyDownloaded = Collections.synchronizedSet(new HashSet<File>());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,15 +85,22 @@ public class MapTileDownloader {
|
||||||
return callback;
|
return callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isFileCurrentlyDownloaded(File f){
|
||||||
|
return currentlyDownloaded.contains(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void refuseAllPreviousRequests(){
|
public void refuseAllPreviousRequests(){
|
||||||
requestsToLoad.clear();
|
while(!threadPoolExecutor.getQueue().isEmpty()){
|
||||||
|
threadPoolExecutor.getQueue().remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void requestToDownload(String url, DownloadRequest request){
|
public void requestToDownload(String url, DownloadRequest request){
|
||||||
requestsToLoad.put(url, request);
|
if (!isFileCurrentlyDownloaded(request.fileToSave)) {
|
||||||
threadPoolExecutor.execute(new DownloadMapWorker(url, request));
|
threadPoolExecutor.execute(new DownloadMapWorker(url, request));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private class DownloadMapWorker implements Runnable, Comparable<DownloadMapWorker> {
|
private class DownloadMapWorker implements Runnable, Comparable<DownloadMapWorker> {
|
||||||
|
@ -108,28 +115,25 @@ public class MapTileDownloader {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
synchronized (requestsToLoad) {
|
|
||||||
if(!requestsToLoad.containsKey(downloadUrl)){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
request = requestsToLoad.remove(downloadUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if(log.isDebugEnabled()){
|
||||||
|
log.debug("Start downloading tile : " + downloadUrl);
|
||||||
|
}
|
||||||
URL url = new URL(downloadUrl);
|
URL url = new URL(downloadUrl);
|
||||||
// if(log.isDebugEnabled()){
|
|
||||||
log.debug("Downloading tile : " + downloadUrl);
|
|
||||||
// }
|
|
||||||
URLConnection connection = url.openConnection();
|
URLConnection connection = url.openConnection();
|
||||||
connection.setRequestProperty("User-Agent", DefaultLauncherConstants.APP_NAME+"/"+DefaultLauncherConstants.APP_VERSION);
|
connection.setRequestProperty("User-Agent", DefaultLauncherConstants.APP_NAME+"/"+DefaultLauncherConstants.APP_VERSION);
|
||||||
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
|
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
|
||||||
try {
|
try {
|
||||||
if(request != null && request.fileToSave != null){
|
if (request != null && request.fileToSave != null) {
|
||||||
request.fileToSave.getParentFile().mkdirs();
|
request.fileToSave.getParentFile().mkdirs();
|
||||||
|
|
||||||
FileOutputStream stream = new FileOutputStream(request.fileToSave);
|
FileOutputStream stream = new FileOutputStream(request.fileToSave);
|
||||||
|
currentlyDownloaded.add(request.fileToSave);
|
||||||
try {
|
try {
|
||||||
Algoritms.streamCopy(inputStream, stream);
|
Algoritms.streamCopy(inputStream, stream);
|
||||||
|
stream.flush();
|
||||||
} finally {
|
} finally {
|
||||||
|
currentlyDownloaded.remove(request.fileToSave);
|
||||||
Algoritms.closeStream(stream);
|
Algoritms.closeStream(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,7 +141,7 @@ public class MapTileDownloader {
|
||||||
Algoritms.closeStream(inputStream);
|
Algoritms.closeStream(inputStream);
|
||||||
}
|
}
|
||||||
if(log.isDebugEnabled()){
|
if(log.isDebugEnabled()){
|
||||||
log.debug("Downloading tile : " + downloadUrl + " successfull");
|
log.debug("Downloading tile : " + downloadUrl + " successfull " + (System.currentTimeMillis() - time) + " ms");
|
||||||
}
|
}
|
||||||
if(callback != null){
|
if(callback != null){
|
||||||
callback.tileDownloaded(downloadUrl, request);
|
callback.tileDownloaded(downloadUrl, request);
|
||||||
|
@ -151,7 +155,7 @@ public class MapTileDownloader {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(DownloadMapWorker o) {
|
public int compareTo(DownloadMapWorker o) {
|
||||||
return (int) (time - o.time);
|
return 0; //(int) (time - o.time);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,8 +72,9 @@ public class TileSourceManager {
|
||||||
list.add(getCloudMadeSource());
|
list.add(getCloudMadeSource());
|
||||||
list.add(getOpenPisteMapSource());
|
list.add(getOpenPisteMapSource());
|
||||||
list.add(getGoogleMapsSource());
|
list.add(getGoogleMapsSource());
|
||||||
list.add(getGoogleMapsSatelliteSource());
|
// TODO ?
|
||||||
list.add(getGoogleMapsTerrainSource());
|
// list.add(getGoogleMapsSatelliteSource());
|
||||||
|
// list.add(getGoogleMapsTerrainSource());
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
|
|
||||||
|
@ -115,7 +116,7 @@ public class TileSourceManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TileSourceTemplate getGoogleMapsTerrainSource(){
|
public static TileSourceTemplate getGoogleMapsTerrainSource(){
|
||||||
return new TileSourceTemplate("GoogleMaps Terrain", "http://mt3.google.com/mt/v=w2p.87&x={1}&y={2}&z={0}", ".png", 15, 0, 256, 18000);
|
return new TileSourceTemplate("GoogleMaps Terrain", "http://mt3.google.com/mt/v=w2.87&x={1}&y={2}&z={0}", ".png", 15, 0, 256, 18000);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue