public void run() {
try {
log.info("Starting forwarding listener thread for '" + name + "'");
//
// ServerSocket server = new ServerSocket(getPortToBind(), 50, InetAddress.getByName(getAddressToBind()));
//server = new ServerSocket(getPortToBind(), 50, InetAddress.getByName(getAddressToBind()));
Socket socket;
while (state.getValue() == StartStopState.STARTED) {
listening = true;
socket = server.accept();
if ((state.getValue() == StartStopState.STOPPED) ||
(socket == null)) {
break;
}
log.info("Connection accepted, creating forwarding channel");
try {
ForwardingSocketChannel channel = createChannel(hostToConnect,
portToConnect, socket);
channel.bindSocket(socket);
if (connection.openChannel(channel)) {
log.info("Forwarding channel for '" + name +
"' is open");
} else {
log.warn("Failed to open forwarding chanel " + name);
socket.close();
}
} catch (Exception ex) {
log.warn("Failed to open forwarding chanel " + name, ex);
try {
socket.close();
} catch (IOException ioe) {
}
}
}
} catch (IOException ioe) {
/* only warn if the forwarding has not been stopped */
if (state.getValue() == StartStopState.STARTED) {
log.warn("Local forwarding listener to " + hostToConnect + ":" +
String.valueOf(portToConnect) + " has failed", ioe);
}
} finally {
stop();
}
}
|