Commit 02d5eb22 authored by akwizgran's avatar akwizgran

Use a proper logger for SnoozeServer.

parent a02c0099
......@@ -5,9 +5,12 @@ import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;
public class SnoozeServer implements Runnable {
private static final Logger LOG = Logger.getLogger(SnoozeServer.class.getName());
private static final int SOCKET_TIMEOUT_MS = 2 * 60 * 1000;
private final int port;
......@@ -36,10 +39,10 @@ public class SnoozeServer implements Runnable {
public void run() {
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("Listening: " + ss.getLocalSocketAddress());
LOG.info("Listening: " + ss.getLocalSocketAddress());
while (true) new WriterThread(ss.accept()).start();
} catch (IOException e) {
System.err.println(e.toString());
LOG.warning(e.toString());
}
}
......@@ -55,20 +58,23 @@ public class SnoozeServer implements Runnable {
@Override
public void run() {
try {
LOG.info("Connected: " + socket.getRemoteSocketAddress());
socket.setTcpNoDelay(true);
socket.setKeepAlive(true);
socket.setSoTimeout(SOCKET_TIMEOUT_MS);
DataInputStream in = new DataInputStream(socket.getInputStream());
OutputStream out = socket.getOutputStream();
int keepaliveIntervalSecs = Math.max(1, in.readUnsignedShort());
LOG.info("Keepalive interval " + keepaliveIntervalSecs + " seconds: " +
socket.getRemoteSocketAddress());
while (!socket.isClosed()) {
System.out.println("Writing: " + socket.getRemoteSocketAddress());
LOG.info("Writing: " + socket.getRemoteSocketAddress());
out.write(0);
sleep(keepaliveIntervalSecs * 1000);
}
System.out.println("Socket closed: " + socket.getRemoteSocketAddress());
LOG.info("Socket closed: " + socket.getRemoteSocketAddress());
} catch (IOException | InterruptedException e) {
System.err.println(e.toString() + " for " + socket.getRemoteSocketAddress());
LOG.warning(e.toString() + " for " + socket.getRemoteSocketAddress());
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment