@Override public ListenableFuture<SocketAddress> openConnection(SocketAddress serverAddress, StreamConnection connection) { try { if (!isRunning()) throw new IllegalStateException(); return new BlockingClient(serverAddress, connection, connectTimeoutMillis, socketFactory, clients).getConnectFuture(); } catch (IOException e) { throw new RuntimeException(e); // This should only happen if we are, eg, out of system resources } }
@Override protected void shutDown() throws Exception { synchronized (clients) { for (BlockingClient client : clients) client.closeConnection(); } }
@Override public void run() { Context.propagate(context); if (clientSet != null) clientSet.add(BlockingClient.this); try { socket.connect(serverAddress, connectTimeoutMillis); connection.connectionOpened(); connectFuture.set(serverAddress); InputStream stream = socket.getInputStream(); runReadLoop(stream, connection); } catch (Exception e) { if (!vCloseRequested) { log.error("Error trying to open/read from connection: {}: {}", serverAddress, e.getMessage()); connectFuture.setException(e); } } finally { try { socket.close(); } catch (IOException e1) { // At this point there isn't much we can do, and we can probably assume the channel is closed } if (clientSet != null) clientSet.remove(BlockingClient.this); connection.connectionClosed(); } } };
private MessageWriteTarget openConnection(SocketAddress addr, ProtobufConnection<TwoWayChannelMessage> parser) throws Exception { if (clientType == 0 || clientType == 1) { channels.openConnection(addr, parser); if (parser.writeTarget.get() == null) Thread.sleep(100); return parser.writeTarget.get(); } else if (clientType == 2) return new NioClient(addr, parser, 100); else if (clientType == 3) return new BlockingClient(addr, parser, 100, SocketFactory.getDefault(), null); else throw new RuntimeException(); }
@Override public void run() { Context.propagate(context); if (clientSet != null) clientSet.add(BlockingClient.this); try { socket.connect(serverAddress, connectTimeoutMillis); connection.connectionOpened(); connectFuture.set(serverAddress); InputStream stream = socket.getInputStream(); runReadLoop(stream, connection); } catch (Exception e) { if (!vCloseRequested) { log.error("Error trying to open/read from connection: {}: {}", serverAddress, e.getMessage()); connectFuture.setException(e); } } finally { try { socket.close(); } catch (IOException e1) { // At this point there isn't much we can do, and we can probably assume the channel is closed } if (clientSet != null) clientSet.remove(BlockingClient.this); connection.connectionClosed(); } } };
new NioClient(new InetSocketAddress("127.0.0.1", 2000), peer, 100); else if (clientType == ClientType.BLOCKING_CLIENT) new BlockingClient(new InetSocketAddress("127.0.0.1", 2000), peer, 100, SocketFactory.getDefault(), null); else throw new RuntimeException();
@Override public ListenableFuture<SocketAddress> openConnection(SocketAddress serverAddress, StreamConnection connection) { try { if (!isRunning()) throw new IllegalStateException(); return new BlockingClient(serverAddress, connection, connectTimeoutMillis, socketFactory, clients).getConnectFuture(); } catch (IOException e) { throw new RuntimeException(e); // This should only happen if we are, eg, out of system resources } }
@Override protected void shutDown() throws Exception { synchronized (clients) { for (BlockingClient client : clients) client.closeConnection(); } }
@Override public void run() { Context.propagate(context); if (clientSet != null) clientSet.add(BlockingClient.this); try { socket.connect(serverAddress, connectTimeoutMillis); connection.connectionOpened(); connectFuture.set(serverAddress); InputStream stream = socket.getInputStream(); runReadLoop(stream, connection); } catch (Exception e) { if (!vCloseRequested) { log.error("Error trying to open/read from connection: {}: {}", serverAddress, e.getMessage()); connectFuture.setException(e); } } finally { try { socket.close(); } catch (IOException e1) { // At this point there isn't much we can do, and we can probably assume the channel is closed } if (clientSet != null) clientSet.remove(BlockingClient.this); connection.connectionClosed(); } } };
@Override public ListenableFuture<SocketAddress> openConnection(SocketAddress serverAddress, StreamConnection connection) { try { if (!isRunning()) throw new IllegalStateException(); return new BlockingClient(serverAddress, connection, connectTimeoutMillis, socketFactory, clients).getConnectFuture(); } catch (IOException e) { throw new RuntimeException(e); // This should only happen if we are, eg, out of system resources } }
@Override protected void shutDown() throws Exception { synchronized (clients) { for (BlockingClient client : clients) client.closeConnection(); } }
@Override public void run() { Context.propagate(context); if (clientSet != null) clientSet.add(BlockingClient.this); try { socket.connect(serverAddress, connectTimeoutMillis); connection.connectionOpened(); connectFuture.set(serverAddress); InputStream stream = socket.getInputStream(); runReadLoop(stream, connection); } catch (Exception e) { if (!vCloseRequested) { log.error("Error trying to open/read from connection: {}: {}", serverAddress, e.getMessage()); connectFuture.setException(e); } } finally { try { socket.close(); } catch (IOException e1) { // At this point there isn't much we can do, and we can probably assume the channel is closed } if (clientSet != null) clientSet.remove(BlockingClient.this); connection.connectionClosed(); } } };
@Override public ListenableFuture<SocketAddress> openConnection(SocketAddress serverAddress, StreamConnection connection) { try { if (!isRunning()) throw new IllegalStateException(); return new BlockingClient(serverAddress, connection, connectTimeoutMillis, socketFactory, clients).getConnectFuture(); } catch (IOException e) { throw new RuntimeException(e); // This should only happen if we are, eg, out of system resources } }
@Override protected void shutDown() throws Exception { synchronized (clients) { for (BlockingClient client : clients) client.closeConnection(); } }
@Override public synchronized void writeBytes(byte[] message) throws IOException { try { OutputStream stream = socket.getOutputStream(); stream.write(message); stream.flush(); } catch (IOException e) { log.error("Error writing message to connection, closing connection", e); closeConnection(); throw e; } }
@Override public synchronized void writeBytes(byte[] message) throws IOException { try { OutputStream stream = socket.getOutputStream(); stream.write(message); stream.flush(); } catch (IOException e) { log.error("Error writing message to connection, closing connection", e); closeConnection(); throw e; } }
@Override public synchronized void writeBytes(byte[] message) throws IOException { try { OutputStream stream = socket.getOutputStream(); stream.write(message); stream.flush(); } catch (IOException e) { log.error("Error writing message to connection, closing connection", e); closeConnection(); throw e; } }
@Override public synchronized void writeBytes(byte[] message) throws IOException { try { OutputStream stream = socket.getOutputStream(); stream.write(message); stream.flush(); } catch (IOException e) { log.error("Error writing message to connection, closing connection", e); closeConnection(); throw e; } }
@Override public void closeConnections(int n) { if (!isRunning()) throw new IllegalStateException(); synchronized (clients) { Iterator<BlockingClient> it = clients.iterator(); while (n-- > 0 && it.hasNext()) it.next().closeConnection(); } } }
@Override public void closeConnections(int n) { if (!isRunning()) throw new IllegalStateException(); synchronized (clients) { Iterator<BlockingClient> it = clients.iterator(); while (n-- > 0 && it.hasNext()) it.next().closeConnection(); } } }