From 57475433ec869870d2dd0112265b82736b17cbf4 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Fri, 30 Apr 2010 19:35:25 +0000 Subject: [PATCH] reinvent downloader mechanism git-svn-id: https://osmand.googlecode.com/svn/trunk@26 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../src/com/osmand/MapPanel.java | 25 +++++-- .../src/com/osmand/MapTileDownloader.java | 70 ++++++++++--------- .../src/com/osmand/map/TileSourceManager.java | 7 +- 3 files changed, 61 insertions(+), 41 deletions(-) diff --git a/DataExtractionOSM/src/com/osmand/MapPanel.java b/DataExtractionOSM/src/com/osmand/MapPanel.java index 150bde4ffa..f523bf0ecd 100644 --- a/DataExtractionOSM/src/com/osmand/MapPanel.java +++ b/DataExtractionOSM/src/com/osmand/MapPanel.java @@ -213,7 +213,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { cache.remove(list.get(i)); } } - if(en.exists()){ + if(en.exists() && !downloader.isFileCurrentlyDownloaded(en)){ long time = System.currentTimeMillis(); try { cache.put(file, ImageIO.read(en)); @@ -230,8 +230,22 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { } @Override - public void tileDownloaded(String dowloadedUrl, DownloadRequest fileSaved) { - prepareImage(false); + public void tileDownloaded(String dowloadedUrl, DownloadRequest request) { + 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=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(){ @@ -272,7 +286,8 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { String urlToLoad = map.getUrlToLoad(x, y, zoom); if(urlToLoad != null){ 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(); } catch (IOException e) { - e.printStackTrace(); + log.error("Eror reading png preparing images"); } } diff --git a/DataExtractionOSM/src/com/osmand/MapTileDownloader.java b/DataExtractionOSM/src/com/osmand/MapTileDownloader.java index 8ef941b35e..a63792324f 100644 --- a/DataExtractionOSM/src/com/osmand/MapTileDownloader.java +++ b/DataExtractionOSM/src/com/osmand/MapTileDownloader.java @@ -8,9 +8,9 @@ import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.concurrent.PriorityBlockingQueue; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -24,7 +24,8 @@ public class MapTileDownloader { private ThreadPoolExecutor threadPoolExecutor; private IMapDownloaderCallback callback; - private Map requestsToLoad; + + private Set currentlyDownloaded; public static MapTileDownloader getInstance(){ @@ -64,16 +65,15 @@ public class MapTileDownloader { yTile = -1; zoom = -1; } - - - } public MapTileDownloader(int numberOfThreads){ - threadPoolExecutor = new ThreadPoolExecutor(1, numberOfThreads, DefaultLauncherConstants.TILE_DOWNLOAD_SECONTS_TO_WORK, - TimeUnit.SECONDS, new PriorityBlockingQueue()); - requestsToLoad = Collections.synchronizedMap(new LinkedHashMap()); + threadPoolExecutor = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, DefaultLauncherConstants.TILE_DOWNLOAD_SECONTS_TO_WORK, + TimeUnit.SECONDS, new LinkedBlockingQueue()); + // 1.6 method but very useful to kill non-running threads +// threadPoolExecutor.allowCoreThreadTimeOut(true); + currentlyDownloaded = Collections.synchronizedSet(new HashSet()); } @@ -85,14 +85,21 @@ public class MapTileDownloader { return callback; } + public boolean isFileCurrentlyDownloaded(File f){ + return currentlyDownloaded.contains(f); + } + public void refuseAllPreviousRequests(){ - requestsToLoad.clear(); + while(!threadPoolExecutor.getQueue().isEmpty()){ + threadPoolExecutor.getQueue().remove(); + } } public void requestToDownload(String url, DownloadRequest request){ - requestsToLoad.put(url, request); - threadPoolExecutor.execute(new DownloadMapWorker(url, request)); + if (!isFileCurrentlyDownloaded(request.fileToSave)) { + threadPoolExecutor.execute(new DownloadMapWorker(url, request)); + } } @@ -108,36 +115,33 @@ public class MapTileDownloader { @Override public void run() { - synchronized (requestsToLoad) { - if(!requestsToLoad.containsKey(downloadUrl)){ - return; - } - request = requestsToLoad.remove(downloadUrl); - } - try { + if(log.isDebugEnabled()){ + log.debug("Start downloading tile : " + downloadUrl); + } URL url = new URL(downloadUrl); -// if(log.isDebugEnabled()){ - log.debug("Downloading tile : " + downloadUrl); -// } URLConnection connection = url.openConnection(); connection.setRequestProperty("User-Agent", DefaultLauncherConstants.APP_NAME+"/"+DefaultLauncherConstants.APP_VERSION); BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024); try { - if(request != null && request.fileToSave != null){ - request.fileToSave.getParentFile().mkdirs(); - FileOutputStream stream = new FileOutputStream(request.fileToSave); - try { - Algoritms.streamCopy(inputStream, stream); - } finally { - Algoritms.closeStream(stream); + if (request != null && request.fileToSave != null) { + request.fileToSave.getParentFile().mkdirs(); + + FileOutputStream stream = new FileOutputStream(request.fileToSave); + currentlyDownloaded.add(request.fileToSave); + try { + Algoritms.streamCopy(inputStream, stream); + stream.flush(); + } finally { + currentlyDownloaded.remove(request.fileToSave); + Algoritms.closeStream(stream); + } } - } } finally { Algoritms.closeStream(inputStream); } if(log.isDebugEnabled()){ - log.debug("Downloading tile : " + downloadUrl + " successfull"); + log.debug("Downloading tile : " + downloadUrl + " successfull " + (System.currentTimeMillis() - time) + " ms"); } if(callback != null){ callback.tileDownloaded(downloadUrl, request); @@ -151,7 +155,7 @@ public class MapTileDownloader { @Override public int compareTo(DownloadMapWorker o) { - return (int) (time - o.time); + return 0; //(int) (time - o.time); } } diff --git a/DataExtractionOSM/src/com/osmand/map/TileSourceManager.java b/DataExtractionOSM/src/com/osmand/map/TileSourceManager.java index 7a18e9de3f..575cc06825 100644 --- a/DataExtractionOSM/src/com/osmand/map/TileSourceManager.java +++ b/DataExtractionOSM/src/com/osmand/map/TileSourceManager.java @@ -72,8 +72,9 @@ public class TileSourceManager { list.add(getCloudMadeSource()); list.add(getOpenPisteMapSource()); list.add(getGoogleMapsSource()); - list.add(getGoogleMapsSatelliteSource()); - list.add(getGoogleMapsTerrainSource()); + // TODO ? +// list.add(getGoogleMapsSatelliteSource()); +// list.add(getGoogleMapsTerrainSource()); } return list; @@ -115,7 +116,7 @@ public class TileSourceManager { } 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); } }