X-Git-Url: http://dolda2000.com/gitweb/?p=jsvc.git;a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Fscgi%2FServer.java;fp=src%2Fdolda%2Fjsvc%2Fscgi%2FServer.java;h=f8d46371a867a4205e92cec6bd9d06237b058b21;hp=f01eae9bb738aa9f783ea5f59f69210df4a23f69;hb=0de513742ae180a370c7836aa285cc82a8e95f22;hpb=ad522c7697632eea5751fd6adffbaa73ace97407 diff --git a/src/dolda/jsvc/scgi/Server.java b/src/dolda/jsvc/scgi/Server.java index f01eae9..f8d4637 100644 --- a/src/dolda/jsvc/scgi/Server.java +++ b/src/dolda/jsvc/scgi/Server.java @@ -8,6 +8,7 @@ import java.util.*; public abstract class Server implements Runnable { private final ServerSocket sk; private final Logger logger = Logger.getLogger("dolda.jsvc.scgi"); + private boolean running = false; public String headcs = "UTF-8"; public Server(ServerSocket sk) { @@ -97,7 +98,12 @@ public abstract class Server implements Runnable { public void run() { try { try { - while(true) { + synchronized(this) { + if(running) + throw(new IllegalStateException("SCGI server is already running")); + running = true; + } + while(running) { Socket nsk = sk.accept(); serve(nsk); } @@ -105,7 +111,26 @@ public abstract class Server implements Runnable { sk.close(); } } catch(IOException e) { - logger.log(Level.SEVERE, "SCGI server encountered I/O error", e); + if((e instanceof SocketException) && !running) { + /* Assume that stop() has closed the socket. */ + } else { + logger.log(Level.SEVERE, "SCGI server encountered I/O error", e); + } + } finally { + shutdown(); + running = false; } } + + public void stop() { + try { + running = false; + sk.close(); + } catch(IOException e) { + throw(new RuntimeException(e)); + } + } + + protected void shutdown() { + } }