public RpcBus(RpcConfig rpcConfig) { this.rpcConfig = rpcConfig; this.sendDurations = Metrics.getInstance() .histogram(rpcConfig.getName() + "-send-durations-ms"); }
@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)); } });
/** * 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 handle(PhysicalConnection connection, int rpcType, ByteString pBody, ByteBuf dBody, ResponseSender sender) throws RpcException { MessageLite defaultInstance = defaultRequestInstances[rpcType]; try{ MessageLite value = defaultInstance.getParserForType().parseFrom(pBody); SentResponseMessage<MessageLite> response = handlers[rpcType].handle(value, (ArrowBuf) dBody); sender.send(new Response(new PseudoEnum(rpcType), response.getBody(), response.getBuffers())); } catch(Exception e){ final String fail = String.format("Failure consuming message for protocol[%d], request[%d] in the %s rpc layer.", getProtocolId(), rpcType, getConfig().getName()); throw new UserRpcException(NodeEndpoint.getDefaultInstance(), fail, e); } } }
public FabricConnectionManager( final RpcConfig rpcConfig, final BufferAllocator allocator, final FabricIdentity remoteIdentity, final FabricIdentity localIdentity, final EventLoopGroup eventLoop, final FabricMessageHandler handler, Optional<SSLEngineFactory> engineFactory ) { super( rpcConfig.getName(), FabricHandshake.newBuilder() .setRpcVersion(FabricRpcConfig.RPC_VERSION) .setIdentity(localIdentity) .build(), remoteIdentity.getAddress(), remoteIdentity.getPort()); this.rpcConfig = rpcConfig; this.eventLoop = eventLoop; this.allocator = allocator; this.remoteIdentity = remoteIdentity; this.localIdentity = localIdentity; this.handler = handler; this.engineFactory = engineFactory; }