Skip to content

Commit

Permalink
SocketFactory use can be switched off and is switched off by default …
Browse files Browse the repository at this point in the history
…as suggested by Kai Moritz
  • Loading branch information
WolfgangFahl committed Oct 9, 2012
1 parent ec84301 commit 3398f26
Showing 1 changed file with 85 additions and 33 deletions.
118 changes: 85 additions & 33 deletions src/main/java/com/meterware/pseudoserver/PseudoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
**/
public class PseudoServer {

/**
* allow factory use to be switched on and off
* by default the factory is not used any more since there were problems with the test cases as of 2012-10-09
*/
public static final boolean useFactory=false;

private static final int DEFAULT_SOCKET_TIMEOUT = 1000;

private static final int INPUT_POLL_INTERVAL = 10;
Expand Down Expand Up @@ -467,7 +473,15 @@ private WebResource getResource( PseudoServlet servlet, HttpRequest request ) th

private ServerSocket getServerSocket() throws IOException {
synchronized (this) {
if (_serverSocket == null) _serverSocket = ServerSocketFactory.newServerSocket();
// if the socket has not been initialized yet
if (_serverSocket == null) {
// create one - either using the factory (reusing sockets)
// or with no factory - getting a new socket each time
if (useFactory)
_serverSocket = ServerSocketFactory.newServerSocket();
else
_serverSocket = ServerSocketFactory.createNewServerSocket();
}
}
return _serverSocket;
}
Expand Down Expand Up @@ -591,40 +605,78 @@ private void sendText( String text ) {
}



/**
* Factory for Server sockets
*/
class ServerSocketFactory {

static private ArrayList _sockets = new ArrayList();

static private int _outstandingSockets;

static private Object _releaseSemaphore = new Object();

static synchronized ServerSocket newServerSocket() throws IOException {
if (_sockets.isEmpty() && _outstandingSockets > PseudoServer.getWaitThreshhold()) {
try { synchronized( _releaseSemaphore) {_releaseSemaphore.wait( PseudoServer.getSocketReleaseWaitTime() ); } } catch (InterruptedException e) {};
}
_outstandingSockets++;
if (!_sockets.isEmpty()) {
return (ServerSocket) _sockets.remove(0);
} else {
ServerSocket serverSocket = new ServerSocket(0);
serverSocket.setSoTimeout( 1000 );
return serverSocket;
}
}

static synchronized void release( ServerSocket serverSocket ) throws IOException {
if (_sockets.size() >= 2 * PseudoServer.getWaitThreshhold()) {
serverSocket.close();
} else {
_sockets.add( serverSocket );
_outstandingSockets--;
synchronized (_releaseSemaphore) { _releaseSemaphore.notify(); }
}
}
}

// the default timeout in millisecs
static public int DEFAULT_TIMEOUT=1000;

// the list of sockets that have been created by the factory
static private ArrayList _sockets = new ArrayList();

static private int _outstandingSockets;

// synchronization object
static private Object _releaseSemaphore = new Object();

/**
* create a new ServerSocket
* @return a new ServerSocket
* @throws IOException
*/
static synchronized ServerSocket createNewServerSocket() throws IOException {
ServerSocket serverSocket = new ServerSocket(0);
serverSocket.setSoTimeout(DEFAULT_TIMEOUT);
return serverSocket;
}

/**
* create a new Server Socket
* @return a Server Socket
* @throws IOException
*/
static synchronized ServerSocket newServerSocket() throws IOException {

if (_sockets.isEmpty()
&& _outstandingSockets > PseudoServer.getWaitThreshhold()) {
try {
synchronized (_releaseSemaphore) {
_releaseSemaphore.wait(PseudoServer
.getSocketReleaseWaitTime());
}
} catch (InterruptedException e) {
}
;
}
_outstandingSockets++;
if (!_sockets.isEmpty()) {
return (ServerSocket) _sockets.remove(0);
} else {
ServerSocket serverSocket=createNewServerSocket();
return serverSocket;
}
}

/**
* release the given server Socket
* @param serverSocket
* @throws IOException
*/
static synchronized void release(ServerSocket serverSocket)
throws IOException {
if (_sockets.size() >= 2 * PseudoServer.getWaitThreshhold()) {
serverSocket.close();
} else {
_sockets.add(serverSocket);
_outstandingSockets--;
synchronized (_releaseSemaphore) {
_releaseSemaphore.notify();
}
}
}
} // ServerSocketFactory

class RecordingOutputStream extends OutputStream {

Expand Down

0 comments on commit 3398f26

Please sign in to comment.