Permalink
Browse files

Changed SlimServer implementation to accept multiple connections with…

…out breaking a running test
  • Loading branch information...
boonelschenbroich committed Feb 10, 2011
1 parent 43c85b5 commit ea11b5ad81eb8da037662538312d12de62abbb8c
@@ -27,4 +27,9 @@ public void serve(Socket s, long requestTimeout) {
e.printStackTrace();
}
}
@Override
public SocketServer getInstance() {
return this;
}
}
@@ -9,14 +9,16 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class SlimServer implements SocketServer {
public static final String EXCEPTION_TAG = "__EXCEPTION__:";
public static final String EXCEPTION_STOP_TEST_TAG = "__EXCEPTION__:ABORT_SLIM_TEST:";
private StreamReader reader;
private BufferedWriter writer;
private ListExecutor executor;
public static final String EXCEPTION_TAG = "__EXCEPTION__:";
public static final String EXCEPTION_STOP_TEST_TAG = "__EXCEPTION__:ABORT_SLIM_TEST:";
private boolean verbose;
private SlimFactory slimFactory;
@@ -26,13 +28,18 @@ public SlimServer(boolean verbose, SlimFactory slimFactory) {
}
public void serve(Socket s) {
boolean byeCalled = false;
try {
tryProcessInstructions(s);
byeCalled = tryProcessInstructions(s);
} catch (Throwable e) {
e.printStackTrace();
} finally {
slimFactory.stop();
close();
closeEnclosingServiceInSeperateThread();
// we only want to stop the current thread and close the server socket only if bye is called
if(byeCalled) {
closeEnclosingServiceInSeperateThread();
}
}
}
@@ -48,11 +55,13 @@ public void run() {
).start();
}
private void tryProcessInstructions(Socket s) throws Exception {
private boolean tryProcessInstructions(Socket s) throws Exception {
initialize(s);
boolean more = true;
while (more)
more = processOneSetOfInstructions();
return !more;
}
private void initialize(Socket s) throws Exception {
@@ -108,4 +117,9 @@ private void close() {
}
}
@Override
public SocketServer getInstance() {
return new SlimServer(verbose, slimFactory);
}
}
@@ -12,7 +12,13 @@
public interface SocketServer {
public void serve(Socket s);
/**
* Get instance can return a new SocketServer instance, if necessary
* @return
*/
public SocketServer getInstance();
static class StreamUtility {
public static PrintStream GetPrintStream(Socket s) throws IOException {
OutputStream os = s.getOutputStream();
@@ -8,13 +8,16 @@
import java.net.SocketException;
import java.util.LinkedList;
import fitnesse.slim.SlimService;
public class SocketService {
private ServerSocket serverSocket = null;
private Thread serviceThread = null;
private volatile boolean running = false;
private SocketServer server = null;
private LinkedList<Thread> threads = new LinkedList<Thread>();
private volatile boolean everRan=false;
public SocketService(int port, SocketServer server) throws Exception {
this.server = server;
serverSocket = new ServerSocket(port);
@@ -33,7 +36,8 @@ public void close() throws Exception {
running = false;
serverSocket.close();
serviceThread.join();
waitForServerThreads();
// wait for server threads is now calling close, so this is not necessary any more
// waitForServerThreads();
}
private void waitForServiceThreadToStart() {
@@ -81,8 +85,14 @@ private void waitForServerThreads() throws InterruptedException {
}
t.join();
}
try {
// no closing of the server socket is fine, since all threads are done
close();
} catch (Exception e) {
e.printStackTrace();
}
}
private class ServerRunner implements Runnable {
private Socket socket;
@@ -92,7 +102,7 @@ private void waitForServerThreads() throws InterruptedException {
public void run() {
try {
server.serve(socket);
server.getInstance().serve(socket);
synchronized (threads) {
threads.remove(Thread.currentThread());
}
@@ -21,9 +21,16 @@
public SocketServiceTest() {
connectionCounter = new SocketServer() {
@Override
public void serve(Socket s) {
connections++;
}
@Override
public SocketServer getInstance() {
return this;
}
};
}
@@ -125,6 +132,11 @@ public void serve(Socket s) {
catch (IOException e) {
}
}
@Override
public SocketServer getInstance() {
return this;
}
}
class EchoService implements SocketServer {
@@ -138,4 +150,9 @@ public void serve(Socket s) {
catch (IOException e) {
}
}
@Override
public SocketServer getInstance() {
return this;
}
}

0 comments on commit ea11b5a

Please sign in to comment.