@Test public void connect() throws RpcException, URISyntaxException, IOException { RpcHost host = new RpcHost("127.0.0.1", 9600); mockSocket = new MockSocket("\u0000\u0000" + AbstractRpcProtocol.R_ACCEPT + "\u0004"); when(mockSocketFactory.createSocket(host)).thenReturn(mockSocket); NewRpcProtocol protocol = new NewRpcProtocol(this.mockSocketFactory); Assert.assertSame(mockSocket, protocol.connect(host, 2000, "123.45.67.89", "www.example.org")); Assert.assertEquals(2000, mockSocket.getSoTimeout()); verify(mockSocketFactory).createSocket(host); }
@Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { Socket socket = null; try { try { socket = socketFactory.createSocket(host); } 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 { socket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", socket.getRemoteHostName(), socket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(socket); writer.writeStartConnection(clientHostName != null ? clientHostName :socket.getLocalHostName(), clientAddress != null ? clientAddress : socket.getLocalHostAddress(), socket.getLocalPort()); writer.flush(); String response = createReader(socket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } return socket; } catch (RpcException e) { if (socket != null) { try { socket.close(); } catch (IOException ioe) { } } throw e; } }
NewRpcProtocol extends AbstractRpcProtocol implements RpcProtocol { @Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { Socket socket = null; try { try { socket = socketFactory.createSocket(host); } 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 { socket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", socket.getRemoteHostName(), socket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(socket); writer.writeStartConnection(clientHostName != null ? clientHostName :socket.getLocalHostName(), clientAddress != null ? clientAddress : socket.getLocalHostAddress(), socket.getLocalPort()); writer.flush(); String response = createReader(socket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } return socket; } catch (RpcException e) { if (socket != null) { try { socket.close(); } catch (IOException ioe) { } } throw e; } } }
NewRpcProtocol extends AbstractRpcProtocol implements RpcProtocol { @Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { Socket socket = null; try { try { socket = socketFactory.createSocket(host); } 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 { socket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", socket.getRemoteHostName(), socket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(socket); writer.writeStartConnection(clientHostName != null ? clientHostName :socket.getLocalHostName(), clientAddress != null ? clientAddress : socket.getLocalHostAddress(), socket.getLocalPort()); writer.flush(); String response = createReader(socket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } return socket; } catch (RpcException e) { if (socket != null) { try { socket.close(); } catch (IOException ioe) { } } throw e; } } NewRpcProtocol(SocketFactory socketFactory); }
NewRpcProtocol extends AbstractRpcProtocol implements RpcProtocol { @Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { Socket socket = null; try { try { socket = socketFactory.createSocket(host); } 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 { socket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", socket.getRemoteHostName(), socket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(socket); writer.writeStartConnection(clientHostName != null ? clientHostName :socket.getLocalHostName(), clientAddress != null ? clientAddress : socket.getLocalHostAddress(), socket.getLocalPort()); writer.flush(); String response = createReader(socket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } return socket; } catch (RpcException e) { if (socket != null) { try { socket.close(); } catch (IOException ioe) { } } throw e; } } NewRpcProtocol(SocketFactory socketFactory); @Override Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName); }
NewRpcProtocol extends AbstractRpcProtocol implements RpcProtocol { @Override public Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName) throws IOException { Socket socket = null; try { try { socket = socketFactory.createSocket(host); } 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 { socket.setSoTimeout(timeout); } catch (SocketException e) { throw new RpcException("error setting socket timeout millis to " + timeout, e); } log.debug("Starting connection at {}/{}", socket.getRemoteHostName(), socket.getRemoteHostAddress()); RpcMessageWriter writer = createWriter(socket); writer.writeStartConnection(clientHostName != null ? clientHostName :socket.getLocalHostName(), clientAddress != null ? clientAddress : socket.getLocalHostAddress(), socket.getLocalPort()); writer.flush(); String response = createReader(socket).readResponse().toString(); if (!R_ACCEPT.equalsIgnoreCase(response)) { throw new RpcException("error starting connection, response was: " + response); } return socket; } catch (RpcException e) { if (socket != null) { try { socket.close(); } catch (IOException ioe) { } } throw e; } } NewRpcProtocol(SocketFactory socketFactory); @Override Socket connect(RpcHost host, int timeout, String clientAddress, String clientHostName);  }