diff --git a/src/main/java/org/dynmap/MapManager.java b/src/main/java/org/dynmap/MapManager.java index 244a5e5c..b7a13254 100644 --- a/src/main/java/org/dynmap/MapManager.java +++ b/src/main/java/org/dynmap/MapManager.java @@ -79,61 +79,63 @@ public class MapManager extends Thread { } void renderFullWorld(Location l) { - debugger.debug("Full render starting..."); - for (MapType map : maps) { - HashSet found = new HashSet(); - HashSet rendered = new HashSet(); - LinkedList renderQueue = new LinkedList(); - LinkedList loadedChunks = new LinkedList(); - - for (MapTile tile : map.getTiles(l)) { - if (!found.contains(tile)) { - found.add(tile); - renderQueue.add(tile); + synchronized (lock) { + debugger.debug("Full render starting..."); + for (MapType map : maps) { + HashSet found = new HashSet(); + HashSet rendered = new HashSet(); + LinkedList renderQueue = new LinkedList(); + LinkedList loadedChunks = new LinkedList(); + + for (MapTile tile : map.getTiles(l)) { + if (!found.contains(tile)) { + found.add(tile); + renderQueue.add(tile); + } } - } - while (!renderQueue.isEmpty()) { - MapTile tile = renderQueue.pollFirst(); - - DynmapChunk[] requiredChunks = tile.getMap().getRequiredChunks(tile); - - // Unload old chunks. - while (loadedChunks.size() >= Math.max(0, 200 - requiredChunks.length)) { + while (!renderQueue.isEmpty()) { + MapTile tile = renderQueue.pollFirst(); + + DynmapChunk[] requiredChunks = tile.getMap().getRequiredChunks(tile); + + // Unload old chunks. + while (loadedChunks.size() >= Math.max(0, 200 - requiredChunks.length)) { + DynmapChunk c = loadedChunks.pollFirst(); + world.unloadChunk(c.x, c.y, false, true); + } + + // Load the required chunks. + for (DynmapChunk chunk : requiredChunks) { + boolean wasLoaded = world.isChunkLoaded(chunk.x, chunk.y); + world.loadChunk(chunk.x, chunk.y, false); + if (!wasLoaded) + loadedChunks.add(chunk); + } + + debugger.debug("renderQueue: " + renderQueue.size() + "/" + found.size()); + if (map.render(tile)) { + found.remove(tile); + rendered.add(tile); + updateQueue.pushUpdate(new Client.Tile(tile.getName())); + for (MapTile adjTile : map.getAdjecentTiles(tile)) { + if (!(found.contains(adjTile) || rendered.contains(adjTile))) { + found.add(adjTile); + renderQueue.add(adjTile); + } + } + } + found.remove(tile); + System.gc(); + } + + // Unload remaining chunks to clean-up. + while (!loadedChunks.isEmpty()) { DynmapChunk c = loadedChunks.pollFirst(); world.unloadChunk(c.x, c.y, false, true); } - - // Load the required chunks. - for (DynmapChunk chunk : requiredChunks) { - boolean wasLoaded = world.isChunkLoaded(chunk.x, chunk.y); - world.loadChunk(chunk.x, chunk.y, false); - if (!wasLoaded) - loadedChunks.add(chunk); - } - - debugger.debug("renderQueue: " + renderQueue.size() + "/" + found.size()); - if (map.render(tile)) { - found.remove(tile); - rendered.add(tile); - updateQueue.pushUpdate(new Client.Tile(tile.getName())); - for (MapTile adjTile : map.getAdjecentTiles(tile)) { - if (!(found.contains(adjTile) || rendered.contains(adjTile))) { - found.add(adjTile); - renderQueue.add(adjTile); - } - } - } - found.remove(tile); - System.gc(); - } - - // Unload remaining chunks to clean-up. - while (!loadedChunks.isEmpty()) { - DynmapChunk c = loadedChunks.pollFirst(); - world.unloadChunk(c.x, c.y, false, true); } + debugger.debug("Full render finished."); } - debugger.debug("Full render finished."); } private MapType[] loadMapTypes(ConfigurationNode configuration) { diff --git a/src/main/java/org/dynmap/web/HttpHandler.java b/src/main/java/org/dynmap/web/HttpHandler.java index 52af077e..274217dd 100644 --- a/src/main/java/org/dynmap/web/HttpHandler.java +++ b/src/main/java/org/dynmap/web/HttpHandler.java @@ -1,7 +1,6 @@ package org.dynmap.web; -import java.io.IOException; public interface HttpHandler { - void handle(String path, HttpRequest request, HttpResponse response) throws IOException; + void handle(String path, HttpRequest request, HttpResponse response) throws Exception; } diff --git a/src/main/java/org/dynmap/web/HttpServerConnection.java b/src/main/java/org/dynmap/web/HttpServerConnection.java index a758d66d..7e7647cb 100644 --- a/src/main/java/org/dynmap/web/HttpServerConnection.java +++ b/src/main/java/org/dynmap/web/HttpServerConnection.java @@ -75,7 +75,7 @@ public class HttpServerConnection extends Thread { public void run() { try { - socket.setSoTimeout(30000); + socket.setSoTimeout(5000); HttpRequest request = new HttpRequest(); if (!readRequestHeader(socket.getInputStream(), request)) { @@ -106,10 +106,13 @@ public class HttpServerConnection extends Thread { try { handler.handle(relativePath, request, response); + } catch (IOException e) { + throw e; } catch (Exception e) { log.log(Level.SEVERE, "HttpHandler '" + handler + "' has thown an exception", e); - e.printStackTrace(); - socket.close(); + if (socket != null) { + socket.close(); + } return; } diff --git a/src/main/java/org/dynmap/web/handlers/ClientConfigurationHandler.java b/src/main/java/org/dynmap/web/handlers/ClientConfigurationHandler.java index aefc71ad..02cfdfa3 100644 --- a/src/main/java/org/dynmap/web/handlers/ClientConfigurationHandler.java +++ b/src/main/java/org/dynmap/web/handlers/ClientConfigurationHandler.java @@ -1,7 +1,6 @@ package org.dynmap.web.handlers; import java.io.BufferedOutputStream; -import java.io.IOException; import java.util.Date; import java.util.Map; @@ -16,7 +15,7 @@ public class ClientConfigurationHandler implements HttpHandler { this.configuration = configuration; } @Override - public void handle(String path, HttpRequest request, HttpResponse response) throws IOException { + public void handle(String path, HttpRequest request, HttpResponse response) throws Exception { String s = Json.stringifyJson(configuration); byte[] bytes = s.getBytes(); diff --git a/src/main/java/org/dynmap/web/handlers/ClientUpdateHandler.java b/src/main/java/org/dynmap/web/handlers/ClientUpdateHandler.java index a17a612f..c814db9c 100644 --- a/src/main/java/org/dynmap/web/handlers/ClientUpdateHandler.java +++ b/src/main/java/org/dynmap/web/handlers/ClientUpdateHandler.java @@ -1,7 +1,6 @@ package org.dynmap.web.handlers; import java.io.BufferedOutputStream; -import java.io.IOException; import java.util.Date; import org.bukkit.World; @@ -26,7 +25,7 @@ public class ClientUpdateHandler implements HttpHandler { } @Override - public void handle(String path, HttpRequest request, HttpResponse response) throws IOException { + public void handle(String path, HttpRequest request, HttpResponse response) throws Exception { long current = System.currentTimeMillis(); long cutoff = 0; diff --git a/src/main/java/org/dynmap/web/handlers/FileHandler.java b/src/main/java/org/dynmap/web/handlers/FileHandler.java index a3ee089d..cd4ce2ba 100644 --- a/src/main/java/org/dynmap/web/handlers/FileHandler.java +++ b/src/main/java/org/dynmap/web/handlers/FileHandler.java @@ -56,30 +56,38 @@ public abstract class FileHandler implements HttpHandler { } @Override - public void handle(String path, HttpRequest request, HttpResponse response) throws IOException { - path = formatPath(path); - InputStream fileInput = getFileInput(path); - if (fileInput == null) { - response.statusCode = 404; - response.statusMessage = "Not found"; - return; - } - - String extension = getExtension(path); - String mimeType = getMimeTypeFromExtension(extension); - - response.fields.put("Content-Type", mimeType); - response.fields.put("Connection", "close"); - OutputStream out = response.getBody(); + public void handle(String path, HttpRequest request, HttpResponse response) throws Exception { + InputStream fileInput = null; try { - int readBytes; - while ((readBytes = fileInput.read(readBuffer)) > 0) { - out.write(readBuffer, 0, readBytes); + path = formatPath(path); + fileInput = getFileInput(path); + if (fileInput == null) { + response.statusCode = 404; + response.statusMessage = "Not found"; + return; + } + + String extension = getExtension(path); + String mimeType = getMimeTypeFromExtension(extension); + + response.fields.put("Content-Type", mimeType); + response.fields.put("Connection", "close"); + OutputStream out = response.getBody(); + try { + int readBytes; + while ((readBytes = fileInput.read(readBuffer)) > 0) { + out.write(readBuffer, 0, readBytes); + } + } catch (IOException e) { + fileInput.close(); + throw e; } - } catch (IOException e) { fileInput.close(); + } catch (Exception e) { + if (fileInput != null) { + try { fileInput.close(); } catch (IOException ex) { } + } throw e; } - fileInput.close(); } }