@Test public void connect() throws RpcException, URISyntaxException, IOException { RpcHost host = new RpcHost("127.0.0.1", 9600); MockSocket handshakeSocket = new MockSocket("\u0000\u0000" + AbstractRpcProtocol.R_ACCEPT + "\u0004"); MockSocket callbackSocket = new MockSocket(new byte[0]); MockServerSocket mockServerSocket = new MockServerSocket(callbackSocket); when(mockSocketFactory.createSocket(host)).thenReturn(handshakeSocket); when(mockSocketFactory.createServerSocket()).thenReturn(mockServerSocket); OldRpcProtocol protocol = new OldRpcProtocol(mockSocketFactory); Socket socket = protocol.connect(host, 2000, "123.45.67.89", "www.example.org"); Assert.assertSame(callbackSocket, socket); Assert.assertTrue(handshakeSocket.isClosed()); Assert.assertEquals(2000, handshakeSocket.getSoTimeout()); Assert.assertEquals(2000, mockServerSocket.getSoTimeout()); String out = new String(handshakeSocket.getBytesSent(), AbstractRpcProtocol.VISTA_CHARSET); Assert.assertTrue(out.contains(Integer.toString(mockServerSocket.getLocalPort()))); verify(mockSocketFactory).createSocket(host); verify(mockSocketFactory).createServerSocket(); }
@Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { try { Socket handshakeSocket = null; ServerSocket callbackSocket = null; try { handshakeSocket = socketFactory.createSocket(host); callbackSocket = socketFactory.createServerSocket(); } catch (ConnectException e) { throw new ServerUnavailableException(host); } catch (PortUnreachableException e) { throw new ServerUnavailableException(host); } catch (NoRouteToHostException e) { throw new ServerNotFoundException(host); } catch (IOException e) { throw e; } try { handshakeSocket.setSoTimeout(timeout); callbackSocket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", handshakeSocket.getRemoteHostName(), handshakeSocket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(handshakeSocket); writer.writeStartConnection(clientHostName != null ? clientHostName : handshakeSocket.getLocalHostName(), clientAddress != null ? clientAddress : handshakeSocket.getLocalHostAddress(), callbackSocket.getLocalPort()); writer.flush(); String response = createReader(handshakeSocket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } try { return callbackSocket.accept(); } catch (IOException e) { throw new RpcException("error creating callback socket", e); } finally { try { handshakeSocket.close(); } catch (IOException e) { log.warn("error closing handshake socket", e); } } } catch (RpcException e) { log.error("unable to connect to " + host, e); throw e; } }
OldRpcProtocol extends AbstractRpcProtocol implements RpcProtocol { @Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { try { Socket handshakeSocket = null; ServerSocket callbackSocket = null; try { handshakeSocket = socketFactory.createSocket(host); callbackSocket = socketFactory.createServerSocket(); } catch (ConnectException e) { throw new ServerUnavailableException(host); } catch (PortUnreachableException e) { throw new ServerUnavailableException(host); } catch (NoRouteToHostException e) { throw new ServerNotFoundException(host); } catch (IOException e) { throw e; } try { handshakeSocket.setSoTimeout(timeout); callbackSocket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", handshakeSocket.getRemoteHostName(), handshakeSocket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(handshakeSocket); writer.writeStartConnection(clientHostName != null ? clientHostName : handshakeSocket.getLocalHostName(), clientAddress != null ? clientAddress : handshakeSocket.getLocalHostAddress(), callbackSocket.getLocalPort()); writer.flush(); String response = createReader(handshakeSocket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } try { return callbackSocket.accept(); } catch (IOException e) { throw new RpcException("error creating callback socket", e); } finally { try { handshakeSocket.close(); } catch (IOException e) { log.warn("error closing handshake socket", e); } } } catch (RpcException e) { log.error("unable to connect to " + host, e); throw e; } } }
OldRpcProtocol extends AbstractRpcProtocol implements RpcProtocol { @Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { try { Socket handshakeSocket = null; ServerSocket callbackSocket = null; try { handshakeSocket = socketFactory.createSocket(host); callbackSocket = socketFactory.createServerSocket(); } catch (ConnectException e) { throw new ServerUnavailableException(host); } catch (PortUnreachableException e) { throw new ServerUnavailableException(host); } catch (NoRouteToHostException e) { throw new ServerNotFoundException(host); } catch (IOException e) { throw e; } try { handshakeSocket.setSoTimeout(timeout); callbackSocket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", handshakeSocket.getRemoteHostName(), handshakeSocket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(handshakeSocket); writer.writeStartConnection(clientHostName != null ? clientHostName : handshakeSocket.getLocalHostName(), clientAddress != null ? clientAddress : handshakeSocket.getLocalHostAddress(), callbackSocket.getLocalPort()); writer.flush(); String response = createReader(handshakeSocket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } try { return callbackSocket.accept(); } catch (IOException e) { throw new RpcException("error creating callback socket", e); } finally { try { handshakeSocket.close(); } catch (IOException e) { log.warn("error closing handshake socket", e); } } } catch (RpcException e) { log.error("unable to connect to " + host, e); throw e; } } OldRpcProtocol(SocketFactory socketFactory); }
OldRpcProtocol extends AbstractRpcProtocol implements RpcProtocol { @Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { try { Socket handshakeSocket = null; ServerSocket callbackSocket = null; try { handshakeSocket = socketFactory.createSocket(host); callbackSocket = socketFactory.createServerSocket(); } catch (ConnectException e) { throw new ServerUnavailableException(host); } catch (PortUnreachableException e) { throw new ServerUnavailableException(host); } catch (NoRouteToHostException e) { throw new ServerNotFoundException(host); } catch (IOException e) { throw e; } try { handshakeSocket.setSoTimeout(timeout); callbackSocket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", handshakeSocket.getRemoteHostName(), handshakeSocket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(handshakeSocket); writer.writeStartConnection(clientHostName != null ? clientHostName : handshakeSocket.getLocalHostName(), clientAddress != null ? clientAddress : handshakeSocket.getLocalHostAddress(), callbackSocket.getLocalPort()); writer.flush(); String response = createReader(handshakeSocket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } try { return callbackSocket.accept(); } catch (IOException e) { throw new RpcException("error creating callback socket", e); } finally { try { handshakeSocket.close(); } catch (IOException e) { log.warn("error closing handshake socket", e); } } } catch (RpcException e) { log.error("unable to connect to " + host, e); throw e; } } OldRpcProtocol(SocketFactory socketFactory); @Override Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName); RpcMessageReader createReader(Reader r); RpcMessageWriter createWriter(Writer w); }
OldRpcProtocol extends AbstractRpcProtocol implements RpcProtocol { @Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { try { Socket handshakeSocket = null; ServerSocket callbackSocket = null; try { handshakeSocket = socketFactory.createSocket(host); callbackSocket = socketFactory.createServerSocket(); } catch (ConnectException e) { throw new ServerUnavailableException(host); } catch (PortUnreachableException e) { throw new ServerUnavailableException(host); } catch (NoRouteToHostException e) { throw new ServerNotFoundException(host); } catch (IOException e) { throw e; } try { handshakeSocket.setSoTimeout(timeout); callbackSocket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", handshakeSocket.getRemoteHostName(), handshakeSocket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(handshakeSocket); writer.writeStartConnection(clientHostName != null ? clientHostName : handshakeSocket.getLocalHostName(), clientAddress != null ? clientAddress : handshakeSocket.getLocalHostAddress(), callbackSocket.getLocalPort()); writer.flush(); String response = createReader(handshakeSocket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } try { return callbackSocket.accept(); } catch (IOException e) { throw new RpcException("error creating callback socket", e); } finally { try { handshakeSocket.close(); } catch (IOException e) { log.warn("error closing handshake socket", e); } } } catch (RpcException e) { log.error("unable to connect to " + host, e); throw e; } } OldRpcProtocol(SocketFactory socketFactory); @Override Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName); RpcMessageReader createReader(Reader r); RpcMessageWriter createWriter(Writer w);  }