@Test public void backwardsCompatibility() throws IOException { MockSocket mockNoResponseSocket = new MockSocket(new byte[0]); MockSocket mockHandshakeSocket = new MockSocket(getAcceptMessage()); MockSocket mockCallbackSocket = new MockSocket(getSignOnSetupMessage() + getIntroMessage() + getBrokerInfoMessage() + getAVCodeMessage() + getUserInfoMessage() + getDivisionGetMessage() + getDivisionSetMessage()); when(mockSocketFactory.createSocket(host)) .thenReturn(mockNoResponseSocket) .thenReturn(mockHandshakeSocket); when(mockSocketFactory.createServerSocket()).thenReturn(new MockServerSocket(mockCallbackSocket)); BrokerConnectionFactory cf = new BrokerConnectionFactory(mockSocketFactory); Connection c = cf.getConnection(host, new AccessVerifyConnectionSpec("960", "foo", "bar", "123.45.67.89", "www.example.org")); assertNotNull(c); verify(mockSocketFactory, times(2)).createSocket(host); verify(mockSocketFactory).createServerSocket(); Assert.assertTrue(mockNoResponseSocket.getBytesSentAsString().startsWith(NewRpcMessageWriter.PREFIX)); Assert.assertTrue(mockNoResponseSocket.isClosed()); Assert.assertTrue(mockHandshakeSocket.getBytesSentAsString().startsWith(OldRpcMessageWriter.PREFIX)); Assert.assertTrue(mockHandshakeSocket.isClosed()); }
public synchronized Connection getConnection(RpcHost host, ConnectionSpec auth) throws RpcException { if (auth == null) throw new IllegalArgumentException("connectionSpec must not be null"); if (!(auth instanceof AccessVerifyConnectionSpec) && !(auth instanceof AnonymousConnectionSpec) && !(auth instanceof AppHandleConnectionSpec)) throw new UnsupportedOperationException("only access/verify, app handle and anonymous authentication currently supported"); long start = System.currentTimeMillis(); log.debug("Creating connection to " + host.toString()); RpcProtocol protocol = isOldProtocolOnly() ? new OldRpcProtocol(socketFactory) : new NewRpcProtocol(socketFactory); Socket socket = null; try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (ServiceTemporarilyDownException e) { throw e; } catch (IOException e) { throw new RpcException(e); } catch (EOFException e) { if (protocol instanceof NewRpcProtocol && isBackwardsCompatible()) { protocol = new OldRpcProtocol(socketFactory); try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (IOException ex) { throw new RpcException(ex); } } else { throw new UnsupportedProtocolException(); } } catch (RpcException e) { throw e; } if (socket == null) throw new RpcException("unable to connect"); BrokerConnection connection = new BrokerConnection(host, socket, protocol); try { connection.getSystemInfo(); if (auth instanceof AccessVerifyConnectionSpec) { connection.authenticate((AccessVerifyConnectionSpec) auth); } else if (auth instanceof AppHandleConnectionSpec) { connection.authenticate((AppHandleConnectionSpec) auth); } } catch (RpcException e) { connection.close(); throw e; } connection.setElapsedMillis(System.currentTimeMillis() - start); return connection; }
BrokerConnectionFactory implements ConnectionFactory { public synchronized Connection getConnection(RpcHost host, ConnectionSpec auth) throws RpcException { if (auth == null) throw new IllegalArgumentException("connectionSpec must not be null"); if (!(auth instanceof AccessVerifyConnectionSpec) && !(auth instanceof AnonymousConnectionSpec) && !(auth instanceof AppHandleConnectionSpec)) throw new UnsupportedOperationException("only access/verify, app handle and anonymous authentication currently supported"); long start = System.currentTimeMillis(); log.debug("Creating connection to " + host.toString()); RpcProtocol protocol = isOldProtocolOnly() ? new OldRpcProtocol(socketFactory) : new NewRpcProtocol(socketFactory); Socket socket = null; try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (ServiceTemporarilyDownException e) { throw e; } catch (IOException e) { throw new RpcException(e); } catch (EOFException e) { if (protocol instanceof NewRpcProtocol && isBackwardsCompatible()) { protocol = new OldRpcProtocol(socketFactory); try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (IOException ex) { throw new RpcException(ex); } } else { throw new UnsupportedProtocolException(); } } catch (RpcException e) { throw e; } if (socket == null) throw new RpcException("unable to connect"); BrokerConnection connection = new BrokerConnection(host, socket, protocol); try { connection.getSystemInfo(); if (auth instanceof AccessVerifyConnectionSpec) { connection.authenticate((AccessVerifyConnectionSpec) auth); } else if (auth instanceof AppHandleConnectionSpec) { connection.authenticate((AppHandleConnectionSpec) auth); } } catch (RpcException e) { connection.close(); throw e; } connection.setElapsedMillis(System.currentTimeMillis() - start); return connection; } }
BrokerConnectionFactory implements ConnectionFactory { public synchronized Connection getConnection(RpcHost host, ConnectionSpec auth) throws RpcException { if (auth == null) throw new IllegalArgumentException("connectionSpec must not be null"); if (!(auth instanceof AccessVerifyConnectionSpec) && !(auth instanceof AnonymousConnectionSpec) && !(auth instanceof AppHandleConnectionSpec)) throw new UnsupportedOperationException("only access/verify, app handle and anonymous authentication currently supported"); long start = System.currentTimeMillis(); log.debug("Creating connection to " + host.toString()); RpcProtocol protocol = isOldProtocolOnly() ? new OldRpcProtocol(socketFactory) : new NewRpcProtocol(socketFactory); Socket socket = null; try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (ServiceTemporarilyDownException e) { throw e; } catch (IOException e) { throw new RpcException(e); } catch (EOFException e) { if (protocol instanceof NewRpcProtocol && isBackwardsCompatible()) { protocol = new OldRpcProtocol(socketFactory); try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (IOException ex) { throw new RpcException(ex); } } else { throw new UnsupportedProtocolException(); } } catch (RpcException e) { throw e; } if (socket == null) throw new RpcException("unable to connect"); BrokerConnection connection = new BrokerConnection(host, socket, protocol); try { connection.getSystemInfo(); if (auth instanceof AccessVerifyConnectionSpec) { connection.authenticate((AccessVerifyConnectionSpec) auth); } else if (auth instanceof AppHandleConnectionSpec) { connection.authenticate((AppHandleConnectionSpec) auth); } } catch (RpcException e) { connection.close(); throw e; } connection.setElapsedMillis(System.currentTimeMillis() - start); return connection; } BrokerConnectionFactory();  BrokerConnectionFactory(SocketFactory socketFactory); }
BrokerConnectionFactory implements ConnectionFactory { public synchronized Connection getConnection(RpcHost host, ConnectionSpec auth) throws RpcException { if (auth == null) throw new IllegalArgumentException("connectionSpec must not be null"); if (!(auth instanceof AccessVerifyConnectionSpec) && !(auth instanceof AnonymousConnectionSpec) && !(auth instanceof AppHandleConnectionSpec)) throw new UnsupportedOperationException("only access/verify, app handle and anonymous authentication currently supported"); long start = System.currentTimeMillis(); log.debug("Creating connection to " + host.toString()); RpcProtocol protocol = isOldProtocolOnly() ? new OldRpcProtocol(socketFactory) : new NewRpcProtocol(socketFactory); Socket socket = null; try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (ServiceTemporarilyDownException e) { throw e; } catch (IOException e) { throw new RpcException(e); } catch (EOFException e) { if (protocol instanceof NewRpcProtocol && isBackwardsCompatible()) { protocol = new OldRpcProtocol(socketFactory); try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (IOException ex) { throw new RpcException(ex); } } else { throw new UnsupportedProtocolException(); } } catch (RpcException e) { throw e; } if (socket == null) throw new RpcException("unable to connect"); BrokerConnection connection = new BrokerConnection(host, socket, protocol); try { connection.getSystemInfo(); if (auth instanceof AccessVerifyConnectionSpec) { connection.authenticate((AccessVerifyConnectionSpec) auth); } else if (auth instanceof AppHandleConnectionSpec) { connection.authenticate((AppHandleConnectionSpec) auth); } } catch (RpcException e) { connection.close(); throw e; } connection.setElapsedMillis(System.currentTimeMillis() - start); return connection; } BrokerConnectionFactory();  BrokerConnectionFactory(SocketFactory socketFactory); boolean isBackwardsCompatible(); void setBackwardsCompatible(boolean backwardsCompatible); boolean isOldProtocolOnly(); void setOldProtocolOnly(boolean oldProtocolOnly); synchronized Connection getConnection(RpcHost host, ConnectionSpec auth); }
BrokerConnectionFactory implements ConnectionFactory { public synchronized Connection getConnection(RpcHost host, ConnectionSpec auth) throws RpcException { if (auth == null) throw new IllegalArgumentException("connectionSpec must not be null"); if (!(auth instanceof AccessVerifyConnectionSpec) && !(auth instanceof AnonymousConnectionSpec) && !(auth instanceof AppHandleConnectionSpec)) throw new UnsupportedOperationException("only access/verify, app handle and anonymous authentication currently supported"); long start = System.currentTimeMillis(); log.debug("Creating connection to " + host.toString()); RpcProtocol protocol = isOldProtocolOnly() ? new OldRpcProtocol(socketFactory) : new NewRpcProtocol(socketFactory); Socket socket = null; try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (ServiceTemporarilyDownException e) { throw e; } catch (IOException e) { throw new RpcException(e); } catch (EOFException e) { if (protocol instanceof NewRpcProtocol && isBackwardsCompatible()) { protocol = new OldRpcProtocol(socketFactory); try { socket = protocol.connect(host, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, auth.getClientAddress(), auth.getClientHostName()); } catch (IOException ex) { throw new RpcException(ex); } } else { throw new UnsupportedProtocolException(); } } catch (RpcException e) { throw e; } if (socket == null) throw new RpcException("unable to connect"); BrokerConnection connection = new BrokerConnection(host, socket, protocol); try { connection.getSystemInfo(); if (auth instanceof AccessVerifyConnectionSpec) { connection.authenticate((AccessVerifyConnectionSpec) auth); } else if (auth instanceof AppHandleConnectionSpec) { connection.authenticate((AppHandleConnectionSpec) auth); } } catch (RpcException e) { connection.close(); throw e; } connection.setElapsedMillis(System.currentTimeMillis() - start); return connection; } BrokerConnectionFactory();  BrokerConnectionFactory(SocketFactory socketFactory); boolean isBackwardsCompatible(); void setBackwardsCompatible(boolean backwardsCompatible); boolean isOldProtocolOnly(); void setOldProtocolOnly(boolean oldProtocolOnly); synchronized Connection getConnection(RpcHost host, ConnectionSpec auth);  }