/** * Start listening for incoming connections and forward to remote host as a channel. * * @throws IOException */ public void listen() throws IOException { listen(Thread.currentThread()); }
/** * Create a {@link LocalPortForwarder} that will listen based on {@code parameters} using the bound * {@code serverSocket} and forward incoming connections to the server; which will further forward them to * {@code host:port}. * <p/> * The returned forwarder's {@link LocalPortForwarder#listen() listen()} method should be called to actually start * listening, this method just creates an instance. * * @param parameters parameters for the forwarding setup * @param serverSocket bound server socket * * @return a {@link LocalPortForwarder} */ public LocalPortForwarder newLocalPortForwarder(LocalPortForwarder.Parameters parameters, ServerSocket serverSocket) { LocalPortForwarder forwarder = new LocalPortForwarder(conn, parameters, serverSocket, loggerFactory); forwarders.add(forwarder); return forwarder; }
/** * Disconnects from the connected SSH server. {@code SSHClient} objects are not reusable therefore it is incorrect * to attempt connection after this method has been called. * <p/> * This method should be called from a {@code finally} construct after connection is established; so that proper * cleanup is done and the thread spawned by the transport layer for dealing with incoming packets is stopped. */ @Override public void disconnect() throws IOException { for (LocalPortForwarder forwarder : forwarders) { try { forwarder.close(); } catch (IOException e) { log.warn("Error closing forwarder", e); } } forwarders.clear(); trans.disconnect(); super.disconnect(); }
/** * Start listening for incoming connections and forward to remote host as a channel and ensure that the thread is registered. * This is useful if for instance {@link #close() is called from another thread} * * @throws IOException */ public void listen(Thread runningThread) throws IOException { this.runningThread = runningThread; log.info("Listening on {}", serverSocket.getLocalSocketAddress()); while (!runningThread.isInterrupted()) { try { final Socket socket = serverSocket.accept(); log.debug("Got connection from {}", socket.getRemoteSocketAddress()); startChannel(socket); } catch (SocketException e) { if (!serverSocket.isClosed()) { throw e; } } } if (serverSocket.isClosed()) { log.debug("LocalPortForwarder closed"); } else { log.debug("LocalPortForwarder interrupted!"); } }
/** * Start listening for incoming connections and forward to remote host as a channel. * * @throws IOException */ public void listen() throws IOException { log.info("Listening on {}", serverSocket.getLocalSocketAddress()); while (!Thread.currentThread().isInterrupted()) { final Socket socket = serverSocket.accept(); log.debug("Got connection from {}", socket.getRemoteSocketAddress()); openChannel(socket).start(); } log.debug("Interrupted!"); }
/** * Start listening for incoming connections and forward to remote host as a channel and ensure that the thread is registered. * This is useful if for instance {@link #close() is called from another thread} * * @throws IOException */ public void listen(Thread runningThread) throws IOException { this.runningThread = runningThread; log.info("Listening on {}", serverSocket.getLocalSocketAddress()); while (!runningThread.isInterrupted()) { try { final Socket socket = serverSocket.accept(); log.debug("Got connection from {}", socket.getRemoteSocketAddress()); startChannel(socket); } catch (SocketException e) { if (!serverSocket.isClosed()) { throw e; } } } if (serverSocket.isClosed()) { log.debug("LocalPortForwarder closed"); } else { log.debug("LocalPortForwarder interrupted!"); } }
@Override public void run() { try { forwarder.listen(); } catch (IOException ignore) { } } }, "SSH port forwarder thread from local port " + localPort + " to " + remoteHostName + ":" + remotePort);
/** * Create a {@link LocalPortForwarder} that will listen based on {@code parameters} using the bound * {@code serverSocket} and forward incoming connections to the server; which will further forward them to * {@code host:port}. * <p/> * The returned forwarder's {@link LocalPortForwarder#listen() listen()} method should be called to actually start * listening, this method just creates an instance. * * @param parameters parameters for the forwarding setup * @param serverSocket bound server socket * * @return a {@link LocalPortForwarder} */ public LocalPortForwarder newLocalPortForwarder(LocalPortForwarder.Parameters parameters, ServerSocket serverSocket) { return new LocalPortForwarder(conn, parameters, serverSocket); }
/** * Disconnects from the connected SSH server. {@code SSHClient} objects are not reusable therefore it is incorrect * to attempt connection after this method has been called. * <p/> * This method should be called from a {@code finally} construct after connection is established; so that proper * cleanup is done and the thread spawned by the transport layer for dealing with incoming packets is stopped. */ @Override public void disconnect() throws IOException { for (LocalPortForwarder forwarder : forwarders) { try { forwarder.close(); } catch (IOException e) { log.warn("Error closing forwarder", e); } } forwarders.clear(); trans.disconnect(); super.disconnect(); }
@Override public void run() { try { forwarder.listen(); } catch (IOException ignore) { } } }, "SSH port forwarder thread from local port " + localPort + " to " + actualItestHost.getHostName() + ":" + remotePort);
/** * Create a {@link LocalPortForwarder} that will listen based on {@code parameters} using the bound * {@code serverSocket} and forward incoming connections to the server; which will further forward them to * {@code host:port}. * <p/> * The returned forwarder's {@link LocalPortForwarder#listen() listen()} method should be called to actually start * listening, this method just creates an instance. * * @param parameters parameters for the forwarding setup * @param serverSocket bound server socket * * @return a {@link LocalPortForwarder} */ public LocalPortForwarder newLocalPortForwarder(LocalPortForwarder.Parameters parameters, ServerSocket serverSocket) { LocalPortForwarder forwarder = new LocalPortForwarder(conn, parameters, serverSocket, loggerFactory); forwarders.add(forwarder); return forwarder; }
/** * Start listening for incoming connections and forward to remote host as a channel. * * @throws IOException */ public void listen() throws IOException { listen(Thread.currentThread()); }
try { latch.countDown(); forwarder.listen(); } catch (IOException ignore) {/* OK. */}