public boolean isActive() { return connection != null && connection.getChannel() != null && connection.getChannel().isActive(); }
@Override public boolean isActive() { return super.isActive(); }
@Override public void setChannelCloseHandler(ChannelFutureListener closeHandler) { // wrap the passed handler in a proxyCloseHandler so we can later wrap the original handler into another one proxyCloseHandler = new ProxyCloseHandler(closeHandler); super.setChannelCloseHandler(proxyCloseHandler); }
void send(RpcOutcomeListener<RECEIVE> listener, C connection, T rpcType, SEND protobufBody, Class<RECEIVE> clazz, boolean allowInEventLoop, ByteBuf... dataBodies) { Preconditions.checkArgument(allowInEventLoop || !connection.inEventLoop(), "You attempted to send while inside the rpc event thread. This isn't allowed because sending will block if the channel is backed up."); if (!allowInEventLoop && !connection.blockOnNotWritable(listener)) { ChannelListenerWithCoordinationId futureListener = connection.createNewRpcListener(listener, clazz); OutboundRpcMessage m = new OutboundRpcMessage(RpcMode.REQUEST, rpcType, futureListener.getCoordinationId(), protobufBody, dataBodies); ChannelFuture channelFuture = connection.getChannel().writeAndFlush(m); channelFuture.addListener(futureListener); channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
/** * Initialize the {@code SocketChannel}. * * This method initializes a new channel created by the {@code ServerBootstrap} * * The default implementation create a remote connection, configures a default pipeline * which handles coding/decoding messages, handshaking, timeout and error handling based * on {@code RpcConfig} instance provided at construction time. * * On each call to this method, every handler added must be a new instance. As of now, the * handlers cannot be shared across connections. * * Subclasses can override it to add extra handlers if needed. * * @param ch the socket channel */ protected void initChannel(final SocketChannel ch) throws SSLException { C connection = initRemoteConnection(ch); connection.setChannelCloseHandler(newCloseListener(ch, connection)); final ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(PROTOCOL_ENCODER, new RpcEncoder("s-" + rpcConfig.getName())); pipeline.addLast(MESSAGE_DECODER, newDecoder(connection.getAllocator())); pipeline.addLast(HANDSHAKE_HANDLER, newHandshakeHandler(connection)); if (rpcConfig.hasTimeout()) { pipeline.addLast(TIMEOUT_HANDLER, new LoggingReadTimeoutHandler(connection, rpcConfig.getTimeout())); } pipeline.addLast(MESSAGE_HANDLER, new InboundHandler(connection)); pipeline.addLast(EXCEPTION_HANDLER, new RpcExceptionHandler<>(connection)); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if(!ctx.channel().isOpen() || cause.getMessage().equals("Connection reset by peer")){ logger.warn("Exception occurred with closed channel. Connection: {}", connection.getName(), cause); return; }else{ logger.error("Exception in RPC communication. Connection: {}. Closing connection.", connection.getName(), cause); ctx.close(); } }
public void setAutoRead(boolean enableAutoRead) { connection.setAutoRead(enableAutoRead); }
@Override protected void initChannel(SocketChannel ch) throws Exception { connection = initRemoteConnection(ch); // each handler in the pipeline is created per connection ch.closeFuture().addListener(newCloseListener(ch, connection)); final ChannelPipeline pipe = ch.pipeline(); pipe.addLast(PROTOCOL_ENCODER, new RpcEncoder("c-" + rpcConfig.getName())); pipe.addLast(PROTOCOL_DECODER, newDecoder(connection.getAllocator())); pipe.addLast(HANDSHAKE_HANDLER, new ClientHandshakeHandler()); if (timeoutInMillis != -1) { pipe.addLast(IDLE_STATE_HANDLER, new IdlePingHandler(timeoutInMillis)); } pipe.addLast(MESSAGE_HANDLER, new InboundHandler(connection)); pipe.addLast(EXCEPTION_HANDLER, new RpcExceptionHandler<>(connection)); } });
public RemoteConnection(SocketChannel channel, String name, boolean blockOnSocket) { super(); this.channel = channel; this.clientName = name; this.writeManager = new WriteManager(); this.requestIdMap = new RequestIdMap(getName()); if(!blockOnSocket){ writeManager.disable(); } channel.pipeline() .addLast(BACK_PRESSURE_HANDLER, new BackPressureHandler()); }
@Override public void close() { logger.debug("Closing client"); try { if (connection != null) { connection.getChannel().close().sync(); } } catch (final InterruptedException e) { logger.warn("Failure while shutting {}", this.getClass().getName(), e); // Preserve evidence that the interruption occurred so that code higher up on the call stack can learn of the // interruption and respond to it if it wants to. Thread.currentThread().interrupt(); } }
if (connection.isActive()) { cmd.connectionAvailable(connection);
protected RemoteConnection(RemoteConnection connection){ this.channel = connection.channel; this.clientName = connection.clientName; this.writeManager = new WriteManager(); this.requestIdMap = new RequestIdMap(getName()); writeManager.disable(); }
@Override public void close() { if (closed.getAndSet(true)) { // Connection was already closed. Let's print out logger.info("Attempting to close connection again"); } CONNECTION_TYPE c = connectionHolder.getAndSet(null); if (c != null) { try { c.getChannel().close().sync(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } }
@Override public void run() { try { closeLatch.await(); ((RemoteConnection)connection).getChannel().close(); } catch (InterruptedException e) { } }
@Override public void handle(final PhysicalConnection connection, int rpcType, ByteString pBody, ByteBuf dBody, ResponseSender sender) throws RpcException { switch(rpcType){ case 1: sender.send(new Response(new FakeEnum(1), expectedD)); new Thread(){ @Override public void run() { try { closeLatch.await(); ((RemoteConnection)connection).getChannel().close(); } catch (InterruptedException e) { } } }.start(); return; case 2: ((RemoteConnection)connection).getChannel().close(); return; default: throw new UnsupportedOperationException(); } }