66 lines
2.1 KiB
Java
66 lines
2.1 KiB
Java
package org.dynmap.web;
|
|
|
|
import java.io.IOException;
|
|
import java.net.InetAddress;
|
|
import java.net.ServerSocket;
|
|
import java.net.Socket;
|
|
import java.util.Collections;
|
|
import java.util.SortedMap;
|
|
import java.util.TreeMap;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
public class HttpServer extends Thread {
|
|
protected static final Logger log = Logger.getLogger("Minecraft");
|
|
protected static final String LOG_PREFIX = "[dynmap] ";
|
|
|
|
private ServerSocket sock = null;
|
|
private Thread listeningThread;
|
|
|
|
private InetAddress bindAddress;
|
|
private int port;
|
|
|
|
public SortedMap<String, HttpHandler> handlers = new TreeMap<String, HttpHandler>(Collections.reverseOrder());
|
|
|
|
public HttpServer(InetAddress bindAddress, int port) {
|
|
this.bindAddress = bindAddress;
|
|
this.port = port;
|
|
}
|
|
|
|
public void startServer() throws IOException {
|
|
sock = new ServerSocket(port, 5, bindAddress);
|
|
listeningThread = this;
|
|
start();
|
|
log.info(LOG_PREFIX + "Dynmap WebServer started on " + bindAddress + ":" + port);
|
|
}
|
|
|
|
public void run() {
|
|
try {
|
|
while (listeningThread == Thread.currentThread()) {
|
|
try {
|
|
Socket socket = sock.accept();
|
|
HttpServerConnection requestThread = new HttpServerConnection(socket, this);
|
|
requestThread.start();
|
|
} catch (IOException e) {
|
|
log.info(LOG_PREFIX + "map WebServer.run() stops with IOException");
|
|
break;
|
|
}
|
|
}
|
|
log.info(LOG_PREFIX + "Webserver shut down.");
|
|
} catch (Exception ex) {
|
|
log.log(Level.SEVERE, LOG_PREFIX + "Exception on WebServer-thread", ex);
|
|
}
|
|
}
|
|
|
|
public void shutdown() {
|
|
log.info(LOG_PREFIX + "Shutting down webserver...");
|
|
try {
|
|
if (sock != null) {
|
|
sock.close();
|
|
}
|
|
} catch (IOException e) {
|
|
log.log(Level.INFO, LOG_PREFIX + "Exception while closing socket for webserver shutdown", e);
|
|
}
|
|
listeningThread = null;
|
|
}
|
|
}
|