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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -83,7 +83,12 @@ public class TestTileLayer extends TileLayer {
}
@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
public void cleanup() {
public void dispose() {
if (mTileDataSource != null)
mTileDataSource.destroy();
mTileDataSource.dispose();
}
@Override
public void cancel() {
if (mTileDataSource != null)
mTileDataSource.cancel();
}
@Override
@ -149,7 +155,7 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
}
public void setDataSource(ITileDataSource dataSource) {
cleanup();
dispose();
mTileDataSource = dataSource;
}

View File

@ -1,6 +1,5 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2012 Hannes Janetzek
* Copyright 2014 Hannes Janetzek
*
* 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;
/**
*
*
*/
public interface ITileDataSource {
/**
* Starts a database query with the given parameters.
*
* @param tile
* the tile to read.
* the tile to load.
* @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 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
public void destroy() {
public void dispose() {
mConn.close();
}
@Override
public void cancel() {
mConn.close();
}
}

View File

@ -213,13 +213,13 @@ public class MapDatabase implements ITileDataSource {
} catch (IOException e) {
log.error(e.getMessage());
// make sure that the file is closed
destroy();
dispose();
throw new IOException();
}
}
@Override
public void destroy() {
public void dispose() {
mReadBuffer = 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.
*/

View File

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