/** * Constructor for Server * * @param hostname The host name of the remote server * @param port Port number used to connect to the server */ Server(String name, String hostname, int port) { this.name = name + "_" + this.name; InetSocketAddress address = new InetSocketAddress(hostname, port); this.addr = address; this.hostname = hostname; this.port = port; connectionRelay = new ConnectionRelay(this, false); connectionRelay.start(); }
/** * Callback used by the Connection.terminate when it has closed a connection. * * @param conn */ void connectionClosed(Connection conn) { connections.remove(conn); connectionRelay.connectionClosed(conn); if (sfLog().isInfoEnabled()) sfLog() .debug( "Server Connection closed to " + getHostname() + ": " + getPort() + ". Remaining: " + connections.size()); }
/** * Close all open connections to the server, stop realaying data to the server, and stop all * associated threads. After this method has been called, the Server will not accept new * connections. */ void close() { closing = true; Vector tempConnections = (Vector) connections.clone(); // Close all open connections for (Enumeration connectionEnum = tempConnections.elements(); connectionEnum.hasMoreElements(); ) { Connection connection = (Connection) connectionEnum.nextElement(); connection.terminate(); } // Stop sending data if (connectionRelay != null) { connectionRelay.stop(); connectionRelay = null; } }
/** * Create a new Connection instance for the new connection and start to handle data movement * between the client and the server. * * @param clientChannel Socket channel to the connected client * @param serverChannel Socket channel to the corresponding server */ void addNewConnection(SocketChannel clientChannel, SocketChannel serverChannel) { if (!closing) { // Create data structure to associate the objects, and add to the dataMover Connection conn = new Connection(clientChannel, serverChannel, this); connections.add(conn); connectionRelay.addConnection(conn); } else { // The server has been removed while waiting to connect, so just close sockets try { clientChannel.close(); } catch (IOException ioe) { if (sfLog().isErrorEnabled()) sfLog().error("Could not close client socket " + ioe.getMessage(), ioe); } try { serverChannel.close(); } catch (IOException ioe) { if (sfLog().isErrorEnabled()) sfLog().error("Could not close server socket " + ioe.getMessage()); } } }