From fef6f936219e06086af082a3ed88d63fb026d0a1 Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 4 Sep 2020 18:22:24 +0300 Subject: [PATCH] refactoring --- .../plus/server/endpoints/TileEndpoint.java | 85 +++++++++---------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java index d3ff591ab7..3672a70550 100644 --- a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java +++ b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java @@ -1,7 +1,6 @@ package net.osmand.plus.server.endpoints; import android.graphics.Bitmap; -import android.util.Pair; import fi.iki.elonen.NanoHTTPD; import net.osmand.PlatformUtil; import net.osmand.data.RotatedTileBox; @@ -18,13 +17,12 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Scanner; -import java.util.concurrent.*; import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse; public class TileEndpoint implements ApiEndpoint { - private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class); - ExecutorService executor = Executors.newFixedThreadPool(3); + private static final int RENDER_WAIT_THRESHOLD = 5000; + private static final Object lock = new Object(); Map hashMap = new HashMap<>(); Map map = Collections.synchronizedMap(hashMap); OsmandApplication application; @@ -35,27 +33,29 @@ public class TileEndpoint implements ApiEndpoint { @Override public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) { - int zoom; - double lat; - double lon; - String fullUri = session.getUri().replace("/tile/", ""); - Scanner s = new Scanner(fullUri).useDelimiter("/"); - zoom = s.nextInt(); - lat = s.nextDouble(); - lon = s.nextDouble(); - Bitmap bitmap = requestTile(lat, lon, zoom); - if (bitmap == null) { - return OsmAndHttpServer.ErrorResponses.response500; + synchronized (lock) { + int zoom; + double lat; + double lon; + String fullUri = session.getUri().replace("/tile/", ""); + Scanner s = new Scanner(fullUri).useDelimiter("/"); + zoom = s.nextInt(); + lat = s.nextDouble(); + lon = s.nextDouble(); + Bitmap bitmap = requestTile(lat, lon, zoom); + if (bitmap == null) { + return OsmAndHttpServer.ErrorResponses.response500; + } + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); + byte[] byteArray = stream.toByteArray(); + ByteArrayInputStream str = new ByteArrayInputStream(byteArray); + return newFixedLengthResponse( + NanoHTTPD.Response.Status.OK, + "image/png", + str, + str.available()); } - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); - byte[] byteArray = stream.toByteArray(); - ByteArrayInputStream str = new ByteArrayInputStream(byteArray); - return newFixedLengthResponse( - NanoHTTPD.Response.Status.OK, - "image/png", - str, - str.available()); } @Override @@ -63,40 +63,31 @@ public class TileEndpoint implements ApiEndpoint { this.application = application; } - private synchronized Bitmap requestTile(double lat, double lon, int zoom) { - Future> future; + private Bitmap requestTile(double lat, double lon, int zoom) { final RotatedTileBox rotatedTileBox = new RotatedTileBox.RotatedTileBoxBuilder() .setLocation(lat, lon) .setZoom(zoom) .setPixelDimensions(512, 512, 0.5f, 0.5f).build(); final MapRenderRepositories renderer = application.getResourceManager().getRenderer(); - future = executor.submit(new Callable>() { - @Override - public Pair call() throws Exception { - Bitmap bmp; - int sleepTime = 500; - while ((bmp = map.get(rotatedTileBox)) == null) { - Thread.sleep(sleepTime); - sleepTime += 500; - } - return Pair.create(rotatedTileBox, bmp); - } - }); application.getResourceManager().updateRendererMap(rotatedTileBox, new AsyncLoadingThread.OnMapLoadedListener() { @Override public void onMapLoaded(boolean interrupted) { map.put(rotatedTileBox, renderer.getBitmap()); } }); - try { - Pair pair = future.get(); - Bitmap bitmap = pair.second; - return bitmap; - } catch (ExecutionException e) { - LOG.error("Execution exception", e); - } catch (InterruptedException e) { - LOG.error("Interrupted exception", e); + Bitmap bmp; + int sleepTime = 500; + while ((bmp = map.get(rotatedTileBox)) == null) { + try { + Thread.sleep(sleepTime); + } catch (InterruptedException e) { + e.printStackTrace(); + } + sleepTime += 500; + if (sleepTime > RENDER_WAIT_THRESHOLD) { + break; + } } - return null; + return bmp; } }