Improve handling of /reload, clean up dead code

This commit is contained in:
Mike Primm 2011-07-24 23:23:24 -05:00
parent 3ddce85f89
commit 737bcb98d9
23 changed files with 279 additions and 338 deletions

View file

@ -11,6 +11,7 @@ import java.net.Socket;
import java.net.SocketAddress;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Logger;
@ -28,6 +29,9 @@ public class HttpServer extends Thread {
private boolean check_banned_ips;
public SortedMap<String, HttpHandler> handlers = new TreeMap<String, HttpHandler>(Collections.reverseOrder());
private Object lock = new Object();
private HashSet<HttpServerConnection> active_connections = new HashSet<HttpServerConnection>();
public HttpServer(InetAddress bindAddress, int port, boolean check_banned_ips) {
this.bindAddress = bindAddress;
@ -62,6 +66,9 @@ public class HttpServer extends Thread {
}
HttpServerConnection requestThread = new HttpServerConnection(socket, this);
synchronized(lock) {
active_connections.add(requestThread);
}
requestThread.start();
} catch (IOException e) {
if(listeningThread != null) /* Only report this if we didn't initiate the shutdown */
@ -83,11 +90,25 @@ public class HttpServer extends Thread {
sock.close();
sock = null;
}
/* And kill off the active connections */
HashSet<HttpServerConnection> sc;
synchronized(lock) {
sc = new HashSet<HttpServerConnection>(active_connections);
}
for(HttpServerConnection c : sc) {
c.shutdownConnection();
}
} catch (IOException e) {
Log.warning("Exception while closing socket for webserver shutdown", e);
}
}
public void connectionEnded(HttpServerConnection c) {
synchronized(lock) {
active_connections.remove(c);
}
}
private HashSet<String> banned_ips = new HashSet<String>();
private HashSet<String> banned_ips_notified = new HashSet<String>();
private long last_loaded = 0;

View file

@ -19,13 +19,13 @@ import java.net.InetSocketAddress;
public class HttpServerConnection extends Thread {
protected static final Logger log = Logger.getLogger("Minecraft");
protected static final String LOG_PREFIX = "[dynmap] ";
private static Pattern requestHeaderLine = Pattern.compile("^(\\S+)\\s+(\\S+)\\s+HTTP/(.+)$");
private static Pattern requestHeaderField = Pattern.compile("^([^:]+):\\s*(.+)$");
private Socket socket;
private HttpServer server;
private boolean do_shutdown;
private PrintStream printOut;
private StringWriter sw = new StringWriter();
@ -35,6 +35,7 @@ public class HttpServerConnection extends Thread {
public HttpServerConnection(Socket socket, HttpServer server) {
this.socket = socket;
this.server = server;
do_shutdown = false;
}
private final static void readLine(InputStream in, StringWriter sw) throws IOException {
@ -232,8 +233,10 @@ public class HttpServerConnection extends Thread {
} catch (IOException e) {
} catch (Exception e) {
Log.severe("Exception while handling request: ", e);
e.printStackTrace();
if(!do_shutdown) {
Log.severe("Exception while handling request: ", e);
e.printStackTrace();
}
} finally {
if (socket != null) {
try {
@ -241,6 +244,18 @@ public class HttpServerConnection extends Thread {
} catch (IOException ex) {
}
}
server.connectionEnded(this);
}
}
public void shutdownConnection() {
try {
do_shutdown = true;
if(socket != null) {
socket.close();
}
join(); /* Wait for thread to die */
} catch (IOException iox) {
} catch (InterruptedException ix) {
}
}
}

View file

@ -38,7 +38,10 @@ public class ClientUpdateHandler implements HttpHandler {
String worldName = match.group(1);
String timeKey = match.group(2);
DynmapWorld dynmapWorld = plugin.mapManager.getWorld(worldName);
DynmapWorld dynmapWorld = null;
if(plugin.mapManager != null) {
dynmapWorld = plugin.mapManager.getWorld(worldName);
}
if (dynmapWorld == null || dynmapWorld.world == null) {
response.status = WorldNotFound;
return;

View file

@ -16,10 +16,7 @@ import org.dynmap.web.HttpStatus;
public abstract class FileHandler implements HttpHandler {
protected static final Logger log = Logger.getLogger("Minecraft");
protected static final String LOG_PREFIX = "[dynmap] ";
//BUG-this breaks re-entrancy of this handler, which is called from multiple threads (one per request)
//private byte[] readBuffer = new byte[40960];
//Replace with pool of buffers
private LinkedList<byte[]> bufferpool = new LinkedList<byte[]>();
private Object lock = new Object();
private static final int MAX_FREE_IN_POOL = 2;

View file

@ -18,7 +18,6 @@ import org.json.simple.parser.JSONParser;
public class SendMessageHandler implements HttpHandler {
protected static final Logger log = Logger.getLogger("Minecraft");
protected static final String LOG_PREFIX = "[dynmap] ";
private static final JSONParser parser = new JSONParser();
public Event<Message> onMessageReceived = new Event<SendMessageHandler.Message>();