Changeset 28505 in osm for applications/viewer


Ignore:
Timestamp:
2012-07-23T19:28:26+02:00 (12 years ago)
Author:
stoecker
Message:

improve task handling for jmapviewer in JOSM context

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
1 added
11 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java

    r28427 r28505  
    661661    /**
    662662     * Decreases the current zoom level by one
     663     *
     664     * @param mapPoint point to choose as center for new zoom level
    663665     */
    664666    public void zoomOut(Point mapPoint) {
     
    666668    }
    667669
     670    /**
     671     * Set the zoom level and center point for display
     672     *
     673     * @param zoom new zoom level
     674     * @param mapPoint point to choose as center for new zoom level
     675     */
    668676    public void setZoom(int zoom, Point mapPoint) {
    669677        if (zoom > tileController.getTileSource().getMaxZoom() || zoom < tileController.getTileSource().getMinZoom()
     
    678686    }
    679687
     688    /**
     689     * Set the zoom level
     690     *
     691     * @param zoom new zoom level
     692     */
    680693    public void setZoom(int zoom) {
    681694        setZoom(zoom, new Point(getWidth() / 2, getHeight() / 2));
     
    859872    }
    860873
    861     /*
    862      * (non-Javadoc)
    863      *
    864      * @see
    865      * org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener#getTileCache
    866      * ()
     874    /**
     875     * Return tile information caching class
     876     * @see TileLoaderListener#getTileCache()
    867877     */
    868878    public TileCache getTileCache() {
     
    877887
    878888    /**
    879      * @param listener to set
     889     * @param listener listener to set
    880890     */
    881891    public void addJMVListener(JMapViewerEventListener listener) {
     
    884894
    885895    /**
    886      * @param listener to remove
     896     * @param listener listener to remove
    887897     */
    888898    public void removeJMVListener(JMapViewerEventListener listener) {
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JobDispatcher.java

    r19231 r28505  
    33//License: GPL. Copyright 2008 by Jan Peter Stotz
    44
    5 import java.util.concurrent.BlockingQueue;
    6 import java.util.concurrent.LinkedBlockingQueue;
     5import java.util.concurrent.BlockingDeque;
     6import java.util.concurrent.LinkedBlockingDeque;
    77import java.util.concurrent.TimeUnit;
     8
     9import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
    810
    911/**
     
    3133    }
    3234
    33     protected BlockingQueue<Runnable> jobQueue = new LinkedBlockingQueue<Runnable>();
     35    protected BlockingDeque<TileJob> jobQueue = new LinkedBlockingDeque<TileJob>();
    3436
    3537    public static int WORKER_THREAD_MAX_COUNT = 8;
     
    4244     */
    4345    public static int WORKER_THREAD_TIMEOUT = 30;
     46
     47    /**
     48     * Type of queue, FIFO if <code>false</code>, LIFO if <code>true</code>
     49     */
     50    protected boolean modeLIFO = false;
    4451
    4552    /**
     
    6572    }
    6673
    67     public void addJob(Runnable job) {
     74    /**
     75     * Function to set the maximum number of workers for tile loading.
     76     */
     77    static public void setMaxWorkers(int workers) {
     78        WORKER_THREAD_MAX_COUNT = workers;
     79    }
     80
     81    /**
     82     * Function to set the LIFO/FIFO mode for tile loading job.
     83     *
     84     * @param lifo <code>true</code> for LIFO mode, <code>false</code> for FIFO mode
     85     */
     86    public void setLIFO(boolean lifo) {
     87        modeLIFO = lifo;
     88    }
     89
     90    /**
     91     * Adds a job to the queue.
     92     * Jobs for tiles already contained in the are ignored (using a <code>null</code> tile
     93     * prevents skipping).
     94     *
     95     * @param job the the job to be added
     96     */
     97    public void addJob(TileJob job) {
    6898        try {
     99            if(job.getTile() != null) {
     100                for(TileJob oldJob : jobQueue) {
     101                    if(oldJob.getTile() == job.getTile()) {
     102                        return;
     103                    }
     104                }
     105            }
    69106            jobQueue.put(job);
    70107            if (workerThreadIdleCount == 0 && workerThreadCount < WORKER_THREAD_MAX_COUNT)
     
    108145                        workerThreadIdleCount++;
    109146                    }
    110                     if (firstThread)
    111                         job = jobQueue.take();
    112                     else
    113                         job = jobQueue.poll(WORKER_THREAD_TIMEOUT, TimeUnit.SECONDS);
     147                    if(modeLIFO) {
     148                        if (firstThread)
     149                            job = jobQueue.takeLast();
     150                        else
     151                            job = jobQueue.pollLast(WORKER_THREAD_TIMEOUT, TimeUnit.SECONDS);
     152                    } else {
     153                        if (firstThread)
     154                            job = jobQueue.take();
     155                        else
     156                            job = jobQueue.poll(WORKER_THREAD_TIMEOUT, TimeUnit.SECONDS);
     157                    }
    114158                } catch (InterruptedException e1) {
    115159                    return;
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/MemoryTileCache.java

    r18772 r28505  
    163163         * Add the element to the head of the list.
    164164         *
    165          * @param new element to be added
     165         * @param element new element to be added
    166166         */
    167167        public synchronized void addFirst(CacheEntry element) {
     
    181181
    182182        /**
    183          * Removes the specified elemntent form the list.
     183         * Removes the specified element from the list.
    184184         *
    185          * @param element
    186          *            to be removed
     185         * @param element element to be removed
    187186         */
    188187        public synchronized void removeEntry(CacheEntry element) {
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java

    r28254 r28505  
    2727import java.util.logging.Logger;
    2828
    29 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
     29import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
    3030import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
    3131import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
     
    9090     * Create a OSMFileCacheTileLoader with given cache directory.
    9191     * If cacheDir is not set or invalid, IOException will be thrown.
    92      * @param map
    93      * @param cacheDir
     92     * @param map the listener checking for tile load events (usually the map for display)
     93     * @param cacheDir directory to store cached tiles
    9494     */
    9595    public OsmFileCacheTileLoader(TileLoaderListener map, File cacheDir) throws IOException  {
     
    106106     * Create a OSMFileCacheTileLoader with system property temp dir.
    107107     * If not set an IOException will be thrown.
    108      * @param map
     108     * @param map the listener checking for tile load events (usually the map for display)
    109109     */
    110110    public OsmFileCacheTileLoader(TileLoaderListener map) throws SecurityException, IOException {
     
    113113
    114114    @Override
    115     public Runnable createTileLoaderJob(final TileSource source, final int tilex, final int tiley, final int zoom) {
    116         return new FileLoadJob(source, tilex, tiley, zoom);
     115    public TileJob createTileLoaderJob(final Tile tile) {
     116        return new FileLoadJob(tile);
    117117    }
    118118
     
    128128    }
    129129   
    130     protected class FileLoadJob implements Runnable {
     130    protected class FileLoadJob implements TileJob {
    131131        InputStream input = null;
    132132
    133         int tilex, tiley, zoom;
    134133        Tile tile;
    135         TileSource source;
    136134        File tileCacheDir;
    137135        File tileFile = null;
     
    139137        boolean fileTilePainted = false;
    140138
    141         public FileLoadJob(TileSource source, int tilex, int tiley, int zoom) {
    142             this.source = source;
    143             this.tilex = tilex;
    144             this.tiley = tiley;
    145             this.zoom = zoom;
     139        public FileLoadJob(Tile tile) {
     140            this.tile = tile;
     141        }
     142
     143        public Tile getTile() {
     144            return tile;
    146145        }
    147146
    148147        public void run() {
    149             TileCache cache = listener.getTileCache();
    150             synchronized (cache) {
    151                 tile = cache.getTile(source, tilex, tiley, zoom);
    152                 if (tile == null || (tile.isLoaded() && !tile.hasError()) || tile.loading)
     148            synchronized (tile) {
     149                if ((tile.isLoaded() && !tile.hasError()) || tile.isLoading())
    153150                    return;
    154151                tile.loaded = false;
     
    156153                tile.loading = true;
    157154            }
    158             tileCacheDir = getSourceCacheDir(source);
    159             if (loadTileFromFile())
     155            tileCacheDir = getSourceCacheDir(tile.getSource());
     156            if (loadTileFromFile()) {
    160157                return;
     158            }
    161159            if (fileTilePainted) {
    162                 Runnable job = new Runnable() {
     160                TileJob job = new TileJob() {
    163161
    164162                    public void run() {
    165163                        loadOrUpdateTile();
     164                    }
     165                    public Tile getTile() {
     166                        return tile;
    166167                    }
    167168                };
     
    174175        protected void loadOrUpdateTile() {
    175176            try {
    176                 // log.finest("Loading tile from OSM: " + tile);
    177177                URLConnection urlConn = loadTileFromOsm(tile);
    178178                if (tileFile != null) {
    179                     switch (source.getTileUpdate()) {
     179                    switch (tile.getSource().getTileUpdate()) {
    180180                    case IfModifiedSince:
    181181                        urlConn.setIfModifiedSince(fileAge);
     
    191191                    }
    192192                }
    193                 if (source.getTileUpdate() == TileUpdate.ETag || source.getTileUpdate() == TileUpdate.IfNoneMatch) {
     193                if (tile.getSource().getTileUpdate() == TileUpdate.ETag || tile.getSource().getTileUpdate() == TileUpdate.IfNoneMatch) {
    194194                    String fileETag = tile.getValue("etag");
    195195                    if (fileETag != null) {
    196                         switch (source.getTileUpdate()) {
     196                        switch (tile.getSource().getTileUpdate()) {
    197197                        case IfNoneMatch:
    198198                            urlConn.addRequestProperty("If-None-Match", fileETag);
     
    245245                listener.tileLoadingFinished(tile, false);
    246246                if (input == null) {
    247                     System.err.println("failed loading " + zoom + "/" + tilex + "/" + tiley + " " + e.getMessage());
     247                    try {
     248                        System.err.println("Failed loading " + tile.getUrl() +": " + e.getMessage());
     249                    } catch(IOException i) {
     250                    }
    248251                }
    249252            } finally {
     
    325328         * <code>LastModified</code> header:
    326329         * <ul>
    327          * <li>{@link OsmTileLoader#MAP_OSMA} - supported</li>
    328          * <li>{@link OsmTileLoader#MAP_MAPNIK} - not supported</li>
     330         * <li>{@link tilesources.OsmTileSource.CycleMap} - supported</li>
     331         * <li>{@link tilesources.OsmTileSource.Mapnik} - not supported</li>
    329332         * </ul>
    330333         *
    331          * @param fileAge
     334         * @param fileAge time of the
    332335         * @return <code>true</code> if the tile on the server is newer than the
    333336         *         file
     
    368371        protected File getTileFile() {
    369372            return new File(tileCacheDir + "/" + tile.getZoom() + "_" + tile.getXtile() + "_" + tile.getYtile() + "."
    370                     + source.getTileType());
     373                    + tile.getSource().getTileType());
    371374        }
    372375
     
    379382            try {
    380383                FileOutputStream f = new FileOutputStream(tileCacheDir + "/" + tile.getZoom() + "_" + tile.getXtile()
    381                         + "_" + tile.getYtile() + "." + source.getTileType());
     384                        + "_" + tile.getYtile() + "." + tile.getSource().getTileType());
    382385                f.write(rawData);
    383386                f.close();
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmMercator.java

    r26701 r28505  
    2929     * TILE_WIDTH where TILE_WIDTH is the width of a tile in pixels
    3030     *
    31      * @param aZoomlevel
    32      * @return
     31     * @param aZoomlevel zoom level to request pixel data
     32     * @return number of pixels
    3333     */
    3434    public static int getMaxPixels(int aZoomlevel) {
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmTileLoader.java

    r26938 r28505  
    1212import java.util.HashMap;
    1313
    14 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
     14import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
    1515import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
    1616import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
     
    3939    }
    4040
    41     public Runnable createTileLoaderJob(final TileSource source, final int tilex, final int tiley, final int zoom) {
    42         return new Runnable() {
     41    public TileJob createTileLoaderJob(final Tile tile) {
     42        return new TileJob() {
    4343
    4444            InputStream input = null;
    4545
    4646            public void run() {
    47                 TileCache cache = listener.getTileCache();
    48                 Tile tile;
    49                 synchronized (cache) {
    50                     tile = cache.getTile(source, tilex, tiley, zoom);
    51                     if (tile == null || (tile.isLoaded() && !tile.hasError()) || tile.loading)
     47                synchronized (tile) {
     48                    if ((tile.isLoaded() && !tile.hasError()) || tile.isLoading())
    5249                        return;
    5350                    tile.loaded = false;
     
    5653                }
    5754                try {
    58                     // Thread.sleep(500);
    5955                    URLConnection conn = loadTileFromOsm(tile);
    6056                    loadTileMetadata(tile, conn);
     
    7369                    listener.tileLoadingFinished(tile, false);
    7470                    if (input == null) {
    75                         System.err.println("failed loading " + zoom + "/" + tilex + "/" + tiley + " " + e.getMessage());
     71                        try {
     72                            System.err.println("Failed loading " + tile.getUrl() +": " + e.getMessage());
     73                        } catch(IOException i) {
     74                        }
    7675                    }
    7776                } finally {
     
    8180            }
    8281
     82            public Tile getTile() {
     83                return tile;
     84            }
    8385        };
    8486    }
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/TileController.java

    r25369 r28505  
    2626     * a load job is added to the working queue of {@link JobThread}.
    2727     *
    28      * @param tilex
    29      * @param tiley
    30      * @param zoom
     28     * @param tilex the X position of the tile
     29     * @param tiley the Y position of the tile
     30     * @param zoom the zoom level of the tile
    3131     * @return specified tile from the cache or <code>null</code> if the tile
    3232     *         was not found in the cache.
     
    4343        }
    4444        if (!tile.isLoaded()) {
    45             jobDispatcher.addJob(tileLoader.createTileLoaderJob(tileSource, tilex, tiley, zoom));
     45            jobDispatcher.addJob(tileLoader.createTileLoaderJob(tile));
    4646        }
    4747        return tile;
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/MapRectangle.java

    r18890 r28505  
    1515 * @see JMapViewer#addMapRectangle(MapRectangle)
    1616 * @see JMapViewer#getMapRectangleList()
    17  * @date 21.06.2009S
    1817 */
    1918public interface MapRectangle {
     
    3332     * <code>bottomRight</code> are specifying the coordinates within <code>g</code>
    3433     *
    35      * @param g
    36      * @param position
     34     * @param g graphics structure for painting
     35     * @param topLeft lop left edge of painting region
     36     * @param bottomRight bottom right edge of painting region
    3737     */
    3838    public void paint(Graphics g, Point topLeft, Point bottomRight);
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/TileLoader.java

    r18772 r28505  
    22
    33//License: GPL. Copyright 2008 by Jan Peter Stotz
     4
     5import org.openstreetmap.gui.jmapviewer.Tile;
    46
    57/**
     
    1214
    1315    /**
    14      * A typical {@link #createTileLoaderJob(int, int, int)} implementation
    15      * should create and return a new {@link Job} instance that performs the
    16      * load action.
     16     * A typical implementation of this function should create and return a
     17     * new {@link TileJob} instance that performs the load action.
    1718     *
    18      * @param tileLayerSource
    19      * @param tilex
    20      * @param tiley
    21      * @param zoom
    22      * @returns {@link Runnable} implementation that performs the desired load
     19     * @param tile the tile to be loaded
     20     * @return {@link TileJob} implementation that performs the desired load
    2321     *          action.
    2422     */
    25     public Runnable createTileLoaderJob(TileSource tileLayerSource, int tilex, int tiley, int zoom);
     23    public TileJob createTileLoaderJob(Tile tile);
    2624}
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/TileLoaderListener.java

    r18772 r28505  
    1515    public void tileLoadingFinished(Tile tile, boolean success);
    1616
     17    /**
     18     * Return the {@link TileCache} class containing {@link Tile}
     19     * data for requested and loaded tiles
     20     *
     21     * @return tile information caching class
     22     */
    1723    public TileCache getTileCache();
    1824}
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/package.html

    r16960 r28505  
    11<html>
     2<head><title>org.openstreetmap.gui.jmapviewer</title></head>
    23<body>
    3 <h1>org.openstreetmap.gui.jmapviewer</h1>
    44<p>This package and all sub-packages are belonging to the Java
    55component <a href="http://wiki.openstreetmap.org/wiki/JMapViewer">JMapViewer</a>
     
    99depends on other libraries or applications</b>. Only functions and methods
    1010provided by the runtime library of Java 5 should be used.</p>
    11 <h2>In particular, this concerns the developer of JOSM!</h2>
    12 <p>2009-08-10 Jan Peter Stotz jpstotz@gmx.de (maintainer of JMapViewer)</p>
    1311</body>
    1412</html>
Note: See TracChangeset for help on using the changeset viewer.