TileLoader/TileDataSource: add cancel() method

- used to force closing sockets when changing theme or tilesource
This commit is contained in:
Hannes Janetzek
2014-10-03 02:34:13 +02:00
parent 41085f915e
commit 5f9a9cc909
18 changed files with 107 additions and 76 deletions

View File

@@ -1,34 +0,0 @@
package org.oscim.layers;
import org.oscim.layers.tile.MapTile;
import org.oscim.layers.tile.TileLoader;
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
import org.oscim.map.Map;
import org.oscim.tiling.source.bitmap.BitmapTileSource;
public class JeoTileLayer extends BitmapTileLayer {
public JeoTileLayer(Map map, BitmapTileSource tileSource) {
super(map, tileSource);
}
@Override
protected TileLoader createLoader() {
return new TileLoader(this.getManager()) {
@Override
public void cleanup() {
// TODO Auto-generated method stub
}
@Override
protected boolean loadTile(MapTile tile) {
// TODO Auto-generated method stub
return false;
}
};
}
}

View File

@@ -61,9 +61,15 @@ public class JeoTileSource extends TileSource {
} }
@Override @Override
public void destroy() { public void dispose() {
} }
@Override
public void cancel() {
}
}; };
} }

View File

@@ -30,7 +30,7 @@ public class BitmapTileSourceTest {
LwHttp lwHttp = Mockito.mock(LwHttp.class); LwHttp lwHttp = Mockito.mock(LwHttp.class);
tileSource.setHttpEngine(new TestHttpFactory(lwHttp)); tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
ITileDataSource dataSource = tileSource.getDataSource(); ITileDataSource dataSource = tileSource.getDataSource();
dataSource.destroy(); dataSource.dispose();
Mockito.verify(lwHttp).close(); Mockito.verify(lwHttp).close();
} }
@@ -39,7 +39,7 @@ public class BitmapTileSourceTest {
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class); OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
tileSource.setHttpEngine(new TestHttpFactory(okHttp)); tileSource.setHttpEngine(new TestHttpFactory(okHttp));
UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource(); UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource();
dataSource.destroy(); dataSource.dispose();
Mockito.verify(okHttp).close(); Mockito.verify(okHttp).close();
} }

View File

@@ -29,7 +29,7 @@ public class OSciMap4TileSourceTest {
LwHttp lwHttp = Mockito.mock(LwHttp.class); LwHttp lwHttp = Mockito.mock(LwHttp.class);
tileSource.setHttpEngine(new TestHttpFactory(lwHttp)); tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
ITileDataSource dataSource = tileSource.getDataSource(); ITileDataSource dataSource = tileSource.getDataSource();
dataSource.destroy(); dataSource.dispose();
Mockito.verify(lwHttp).close(); Mockito.verify(lwHttp).close();
} }
@@ -38,7 +38,7 @@ public class OSciMap4TileSourceTest {
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class); OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
tileSource.setHttpEngine(new TestHttpFactory(okHttp)); tileSource.setHttpEngine(new TestHttpFactory(okHttp));
ITileDataSource dataSource = tileSource.getDataSource(); ITileDataSource dataSource = tileSource.getDataSource();
dataSource.destroy(); dataSource.dispose();
Mockito.verify(okHttp).close(); Mockito.verify(okHttp).close();
} }

View File

@@ -40,17 +40,22 @@ public abstract class TileLoader implements ITileDataSink {
mTileManager = tileManager; mTileManager = tileManager;
} }
public abstract void cleanup(); public abstract void dispose();
protected abstract boolean loadTile(MapTile tile); protected abstract boolean loadTile(MapTile tile);
boolean isInterrupted; boolean isInterrupted;
public void interrupt() { public void finish() {
isInterrupted = true; isInterrupted = true;
// cancel loading // cancel loading
} }
public void cancel() {
isInterrupted = true;
// cancel loading... ?
}
boolean mPausing; boolean mPausing;
public boolean isCanceled() { public boolean isCanceled() {

View File

@@ -84,7 +84,13 @@ public class UrlTileDataSource implements ITileDataSource {
} }
@Override @Override
public void destroy() { public void dispose() {
mConn.close(); mConn.close();
} }
@Override
public void cancel() {
mConn.close();
}
} }

View File

@@ -126,7 +126,12 @@ public class BitmapTileSource extends UrlTileSource {
} }
@Override @Override
public void destroy() { public void dispose() {
} }
@Override
public void cancel() {
}
} }
} }

View File

@@ -72,7 +72,12 @@ public class JsonTileDataSource implements ITileDataSource {
boolean mFinished; boolean mFinished;
@Override @Override
public void destroy() { public void dispose() {
mFinished = true;
}
@Override
public void cancel() {
mFinished = true; mFinished = true;
} }

View File

@@ -105,8 +105,8 @@ public abstract class TileLayer extends Layer implements UpdateListener {
public void onDetach() { public void onDetach() {
for (TileLoader loader : mTileLoader) { for (TileLoader loader : mTileLoader) {
loader.pause(); loader.pause();
loader.interrupt(); loader.finish();
loader.cleanup(); loader.dispose();
} }
} }
@@ -117,9 +117,12 @@ public abstract class TileLayer extends Layer implements UpdateListener {
protected void pauseLoaders(boolean wait) { protected void pauseLoaders(boolean wait) {
for (TileLoader loader : mTileLoader) { for (TileLoader loader : mTileLoader) {
loader.cancel();
if (!loader.isPausing()) if (!loader.isPausing())
loader.pause(); loader.pause();
} }
if (!wait) if (!wait)
return; return;

View File

@@ -23,8 +23,13 @@ import org.oscim.backend.canvas.Bitmap;
import org.oscim.core.MapElement; import org.oscim.core.MapElement;
import org.oscim.tiling.ITileDataSink; import org.oscim.tiling.ITileDataSink;
import org.oscim.utils.PausableThread; import org.oscim.utils.PausableThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class TileLoader extends PausableThread implements ITileDataSink { public abstract class TileLoader extends PausableThread implements ITileDataSink {
static final Logger log = LoggerFactory.getLogger(TileLoader.class);
private static int id; private static int id;
private final String THREAD_NAME; private final String THREAD_NAME;
@@ -77,7 +82,9 @@ public abstract class TileLoader extends PausableThread implements ITileDataSink
return mTileManager.hasTileJobs(); return mTileManager.hasTileJobs();
} }
public abstract void cleanup(); public abstract void dispose();
public abstract void cancel();
/** /**
* Callback to be called by TileDataSource when finished * Callback to be called by TileDataSource when finished
@@ -85,15 +92,12 @@ public abstract class TileLoader extends PausableThread implements ITileDataSink
*/ */
@Override @Override
public void completed(QueryResult result) { public void completed(QueryResult result) {
boolean success = result == SUCCESS; boolean ok = (result == SUCCESS);
if (isCanceled()) if (ok && (isCanceled() || isInterrupted()))
success = false; ok = false;
if (isInterrupted()) mTileManager.jobCompleted(mTile, ok);
success = false;
mTileManager.jobCompleted(mTile, success);
mTile = null; mTile = null;
} }

View File

@@ -67,6 +67,11 @@ public class BitmapTileLoader extends TileLoader {
} }
@Override @Override
public void cleanup() { public void dispose() {
} }
@Override
public void cancel() {
}
} }

View File

@@ -50,8 +50,13 @@ class S3DBTileLoader extends TileLoader {
} }
@Override @Override
public void cleanup() { public void dispose() {
mTileDataSource.destroy(); mTileDataSource.dispose();
}
@Override
public void cancel() {
mTileDataSource.cancel();
} }
@Override @Override

View File

@@ -83,7 +83,12 @@ public class TestTileLayer extends TileLayer {
} }
@Override @Override
public void cleanup() { public void dispose() {
} }
@Override
public void cancel() {
}
} }
} }

View File

@@ -81,9 +81,15 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
} }
@Override @Override
public void cleanup() { public void dispose() {
if (mTileDataSource != null) if (mTileDataSource != null)
mTileDataSource.destroy(); mTileDataSource.dispose();
}
@Override
public void cancel() {
if (mTileDataSource != null)
mTileDataSource.cancel();
} }
@Override @Override
@@ -149,7 +155,7 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
} }
public void setDataSource(ITileDataSource dataSource) { public void setDataSource(ITileDataSource dataSource) {
cleanup(); dispose();
mTileDataSource = dataSource; mTileDataSource = dataSource;
} }

View File

@@ -1,6 +1,5 @@
/* /*
* Copyright 2010, 2011, 2012 mapsforge.org * Copyright 2014 Hannes Janetzek
* Copyright 2012 Hannes Janetzek
* *
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
* *
@@ -19,22 +18,20 @@ package org.oscim.tiling;
import org.oscim.layers.tile.MapTile; import org.oscim.layers.tile.MapTile;
/**
*
*
*/
public interface ITileDataSource { public interface ITileDataSource {
/** /**
* Starts a database query with the given parameters.
*
* @param tile * @param tile
* the tile to read. * the tile to load.
* @param mapDataSink * @param mapDataSink
* the callback which handles the extracted map elements. * the callback to handle the extracted map elements.
*/ */
abstract void query(MapTile tile, ITileDataSink mapDataSink); abstract void query(MapTile tile, ITileDataSink mapDataSink);
abstract void destroy(); /** Implementations should cancel and release all resources */
abstract void dispose();
/** Implementations should cancel their IO work and return */
abstract void cancel();
} }

View File

@@ -98,7 +98,12 @@ public class UrlTileDataSource implements ITileDataSource {
} }
@Override @Override
public void destroy() { public void dispose() {
mConn.close();
}
@Override
public void cancel() {
mConn.close(); mConn.close();
} }
} }

View File

@@ -213,13 +213,13 @@ public class MapDatabase implements ITileDataSource {
} catch (IOException e) { } catch (IOException e) {
log.error(e.getMessage()); log.error(e.getMessage());
// make sure that the file is closed // make sure that the file is closed
destroy(); dispose();
throw new IOException(); throw new IOException();
} }
} }
@Override @Override
public void destroy() { public void dispose() {
mReadBuffer = null; mReadBuffer = null;
if (mInputFile != null) { if (mInputFile != null) {
@@ -232,6 +232,10 @@ public class MapDatabase implements ITileDataSource {
} }
} }
@Override
public void cancel() {
}
/** /**
* Logs the debug signatures of the current way and block. * Logs the debug signatures of the current way and block.
*/ */

View File

@@ -184,7 +184,11 @@ public class TestTileSource extends TileSource {
} }
@Override @Override
public void destroy() { public void dispose() {
}
@Override
public void cancel() {
} }
} }