scriptSocket.close();
break;
case ACCEPT_SERVER_SOCKET:
log.info("accepting server socket on port: " + ++nextBindPort);
mss = new MasterServerSocket(nextBindPort);
os.write(i);
mss.acceptServerSocketConnection();
mss.close();
break;
case CONNECT_TO_CLIENT:
try
{
socketToClient = new VirtualSocket(clientServerSocketHost, socket.getPort());
is_client = socketToClient.getInputStream();
os_client = socketToClient.getOutputStream();
}
catch(IOException e)
{
log.error("unable to create VirtualSocket back to client", e);
socketToClient = null;
}
break;
case CONNECT_TO_CLIENT_VSS:
socketToClient = null;
nextRemotePort++;
// Since we're reusing ports, the ServerSocket might not be available yet.
for(int j = 0; j < 5; j++)
{
try
{
log.info("connecting to (" + clientServerSocketHost + ", " + nextRemotePort + ")");
socketToClient = new VirtualSocket(clientServerSocketHost, nextRemotePort);
is_client = socketToClient.getInputStream();
os_client = socketToClient.getOutputStream();
break;
}
catch(IOException e)
{
log.error("unable to create VirtualSocket back to client: trying again", e);
Thread.sleep(1000);
}
}
if(socketToClient == null)
{
log.error("giving up attempt to create VirtualSocket back to client");
}
break;
case CONNECT_TO_CLIENT_MSS:
socketToClient = null;
nextRemotePort++;
log.info("connecting to (" + clientServerSocketHost + ", " + nextRemotePort + ")");
// Since we're reusing ports, the ServerSocket might not be available yet.
for(int j = 0; j < 5; j++)
{
try
{
socketToClient = new VirtualSocket(clientServerSocketHost, nextRemotePort);
is_client = socketToClient.getInputStream();
os_client = socketToClient.getOutputStream();
break;
}
catch(IOException e)
{
log.info("unable to create VirtualSocket back to client: trying again", e);
Thread.sleep(1000);
}
}
if(socketToClient == null)
{
log.error("giving up attempt to create VirtualSocket back to client");
}
break;
case READ_FROM_CLIENT:
if(socketToClient == null)
{
log.error("unable to read from client: socket has not been opened");
}
else
{
b = is_client.read();
}
break;
case WRITE_TO_CLIENT:
if(socketToClient == null)
{
log.error("unable to write to client: socket has not been opened");
}
else
{
os_client.write(i);
}
break;
case CLOSE_CLIENT_SOCKET:
if(socketToClient == null)
{
log.error("unable to close socket to client: not open");
}
else
{
socketToClient.close();
socketToClient = null;
}
break;
case RUN_VIRTUALSERVERSOCKET:
mss = new MasterServerSocket(basicBehaviorServerPort + 1);
os.write(3);
Socket virtualSocket1 = mss.accept();
Socket virtualSocket2 = mss.accept();
int localPort = virtualSocket2.getLocalPort();
log.info("VirtualServerSocket binding to port: " + localPort);
vss = new VirtualServerSocket(localPort);
os.write(5);
Socket virtualSocket3 = vss.accept();
DataOutputStream vos = new DataOutputStream(virtualSocket3.getOutputStream());
vos.writeInt(localPort);
virtualSocket1.close();
virtualSocket2.close();
virtualSocket3.close();
mss.close();
vss.close();
break;
case RUN_SERVER_TIMEOUT_TEST:
Socket virtualSocket4 = null;
Socket virtualSocket5 = null;
Socket virtualSocket6 = null;
Socket virtualSocket7 = null;
mss = new MasterServerSocket(basicBehaviorServerPort + 100);
mss.setSoTimeout(10000);
os.write(3);
Thread.sleep(1000);
try
{
virtualSocket4 = mss.accept();
log.info("accepted virtualSocket4");
}
catch(SocketTimeoutException e)
{
log.info(e);
}
try
{
virtualSocket5 = mss.accept();
log.info("accepted virtualSocket5");
}
catch(SocketTimeoutException e)
{
log.info("timed out waiting to accept virtualSocket5");
}
is.read();
Thread.sleep(1000);
vss = new VirtualServerSocket(basicBehaviorServerPort + 101);
SocketAddress address1 = new InetSocketAddress(clientServerSocketHost, clientServerSocketPort + 101);
vss.connect(address1);
os.write(5);
Thread.sleep(1000);
vss.setSoTimeout(10000);
try
{
virtualSocket6 = vss.accept();
log.info("accepted virtualSocket6");
}
catch(SocketTimeoutException e)
{
log.info(e);
}
try
{
virtualSocket7 = vss.accept();
log.info("accepted virtualSocket7");
}
catch(SocketTimeoutException e)
{
log.info(e);
}
if(virtualSocket4 != null)
{
virtualSocket4.close();
}
if(virtualSocket5 != null)
{
virtualSocket5.close();
}
if(virtualSocket6 != null)
{
virtualSocket6.close();
}
if(virtualSocket7 != null)
{
virtualSocket7.close();
}
mss.close();
vss.close();
break;
case RUN_VSS_TO_VSS:
mss = new MasterServerSocket(++nextBindPort);
log.info("MasterServerSocket accepting connections on: " + nextBindPort);
os.write(3);
// int port = mss.acceptServerSocketConnection().getSocket().getLocalPort();
int port = mss.acceptServerSocketConnection();
vss = new VirtualServerSocket(port);