private DefaultServiceClient( GraphName nodeName, ServiceDeclaration serviceDeclaration, MessageFactory messageFactory, ScheduledExecutorService executorService) throws IOException { this.serviceDeclaration = serviceDeclaration; this.messageFactory = messageFactory; messageBufferPool = new MessageBufferPool(); responseListeners = new LinkedList<ServiceResponseListener<S>>(); connectionHeader = new ConnectionHeader(); connectionHeader.addField(ConnectionHeaderFields.CALLER_ID, nodeName.toString()); // TODO(damonkohler): Support non-persistent connections. connectionHeader.addField(ConnectionHeaderFields.PERSISTENT, "1"); connectionHeader.merge(serviceDeclaration.toConnectionHeader()); tcpClientManager = TcpClientManager.getInstance(executorService); ServiceClientHandshakeHandler<T, S> serviceClientHandshakeHandler = new ServiceClientHandshakeHandler<T, S>( connectionHeader, responseListeners, executorService); handshakeLatch = new HandshakeLatch(); serviceClientHandshakeHandler.addListener(handshakeLatch); tcpClientManager.addNamedChannelHandler(serviceClientHandshakeHandler); }
@Override public void connect(InetSocketAddress inetSocketAddress) throws Exception { assert (inetSocketAddress != null) : "Address must be specified."; // assert(inetSocketAddress.getScheme().equals("rosrpc")) : "Invalid service URI."; assert (tcpClient == null) : "Already connected once."; handshakeLatch.reset(); try { tcpClient = tcpClientManager.connect(toString(), inetSocketAddress); } catch (IOException e1) { throw new RosRuntimeException( "TcpClient failed to connect to remote " + toString() + " " + inetSocketAddress, e1); } try { if (!handshakeLatch.await(1, TimeUnit.SECONDS)) { throw new RosRuntimeException(handshakeLatch.getErrorMessage()); } } catch (InterruptedException e) { throw new RosRuntimeException("Handshake timed out."); } }
@Override public void shutdown() { assert (tcpClient != null) : "Not connected."; tcpClientManager.shutdown(); }