*/
@Override
public void dispatch(final FrameInfo frameInfo) throws FramingException {
Preconditions.checkNotNull(frameSender, "Must define frameSender before calling dispatch");
try {
final SocketDataInfo socketDataInfo = SocketDataInfo.parseFrom(frameInfo.getPayload());
final long connectionId = socketDataInfo.getConnectionId();
// Handle incoming start request.
if (socketDataInfo.getState() == SocketDataInfo.State.START) {
LOG.info("Starting new connection. ID " + connectionId);
final Socket socket = socketFactory.createSocket();
socket.connect(new InetSocketAddress(localHostAddress, localConf.getSocksServerPort()));
final ConnectionRemover connectionRemoverCallback = new ConnectionRemover();
// TODO(rayc) Create a pool of connectors instead of making a new instance each time.
final InputStreamConnector inputStreamConnector =
injector.getInstance(InputStreamConnector.class);
inputStreamConnector.setConnectionId(connectionId);
inputStreamConnector.setInputStream(socket.getInputStream());
inputStreamConnector.setFrameSender(frameSender);
inputStreamConnector.setConnectorStateCallback(connectionRemoverCallback);
inputStreamConnector.setName("Inputconnector-" + connectionId);
// TODO(rayc) Create a pool of connectors instead of making a new instance each time.
final OutputStreamConnector outputStreamConnector =
injector.getInstance(OutputStreamConnector.class);
outputStreamConnector.setConnectionId(connectionId);
outputStreamConnector.setOutputStream(socket.getOutputStream());
outputStreamConnector.setConnectorStateCallback(connectionRemoverCallback);
outputStreamConnector.setName("Outputconnector-" + connectionId);
outputQueueMap.put(connectionId, outputStreamConnector.getQueue());
// Start threads
threadPoolExecutor.execute(inputStreamConnector);
threadPoolExecutor.execute(outputStreamConnector);
LOG.debug("active thread count = " + Thread.activeCount());
// Deal with continuing connections or close connections.
} else if (socketDataInfo.getState() == SocketDataInfo.State.CONTINUE ||
socketDataInfo.getState() == SocketDataInfo.State.CLOSE) {
if (outputQueueMap.containsKey(socketDataInfo.getConnectionId())) {
outputQueueMap.get(connectionId).put(socketDataInfo);
}
// Unknown states.
} else {
throw new FramingException("Unknown State: " + socketDataInfo.getState() +
" received while dispatching");
}
} catch (InvalidProtocolBufferException e) {
throw new FramingException(e);
} catch (IOException e) {