reinvent downloader mechanism

git-svn-id: https://osmand.googlecode.com/svn/trunk@26 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-04-30 19:35:25 +00:00
parent 76b36291bb
commit 57475433ec
3 changed files with 61 additions and 41 deletions

View file

@ -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<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(){
@ -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");
}
}

View file

@ -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<String, DownloadRequest> requestsToLoad;
private Set<File> 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<Runnable>());
requestsToLoad = Collections.synchronizedMap(new LinkedHashMap<String, DownloadRequest>());
threadPoolExecutor = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, DefaultLauncherConstants.TILE_DOWNLOAD_SECONTS_TO_WORK,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
// 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;
}
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);
if (!isFileCurrentlyDownloaded(request.fileToSave)) {
threadPoolExecutor.execute(new DownloadMapWorker(url, request));
}
}
private class DownloadMapWorker implements Runnable, Comparable<DownloadMapWorker> {
@ -108,28 +115,25 @@ 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){
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);
}
}
@ -137,7 +141,7 @@ public class MapTileDownloader {
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);
}
}

View file

@ -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);
}
}