Search in sources :

Example 1 with Controller

use of tech.pegasys.teku.networking.p2p.libp2p.rpc.RpcHandler.Controller in project teku by ConsenSys.

the class RpcHandler method initChannel.

@NotNull
@Override
public SafeFuture<Controller<TOutgoingHandler>> initChannel(final P2PChannel channel, final String selectedProtocol) {
    final Connection connection = ((io.libp2p.core.Stream) channel).getConnection();
    final NodeId nodeId = new LibP2PNodeId(connection.secureSession().getRemoteId());
    final Controller<TOutgoingHandler> controller = new Controller<>(nodeId, channel);
    if (!channel.isInitiator()) {
        controller.setIncomingRequestHandler(rpcMethod.createIncomingRequestHandler(selectedProtocol));
    }
    channel.pushHandler(controller);
    return controller.activeFuture;
}
Also used : Connection(io.libp2p.core.Connection) LibP2PNodeId(tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId) NodeId(tech.pegasys.teku.networking.p2p.peer.NodeId) RpcStream(tech.pegasys.teku.networking.p2p.rpc.RpcStream) Stream(io.libp2p.core.Stream) LibP2PNodeId(tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId) Controller(tech.pegasys.teku.networking.p2p.libp2p.rpc.RpcHandler.Controller) RpcStreamController(tech.pegasys.teku.networking.p2p.rpc.RpcStreamController) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Controller

use of tech.pegasys.teku.networking.p2p.libp2p.rpc.RpcHandler.Controller in project teku by ConsenSys.

the class RpcHandler method sendRequest.

public SafeFuture<RpcStreamController<TOutgoingHandler>> sendRequest(Connection connection, TRequest request, TRespHandler responseHandler) {
    final Bytes initialPayload;
    try {
        initialPayload = rpcMethod.encodeRequest(request);
    } catch (Exception e) {
        return SafeFuture.failedFuture(e);
    }
    Interruptor closeInterruptor = SafeFuture.createInterruptor(connection.closeFuture(), PeerDisconnectedException::new);
    Interruptor timeoutInterruptor = SafeFuture.createInterruptor(asyncRunner.getDelayedFuture(TIMEOUT), () -> new StreamTimeoutException("Timed out waiting to initialize stream for protocol(s): " + String.join(",", rpcMethod.getIds())));
    return SafeFuture.notInterrupted(closeInterruptor).thenApply(__ -> connection.muxerSession().createStream(this)).thenWaitFor(StreamPromise::getStream).orInterrupt(closeInterruptor, timeoutInterruptor).thenCompose(streamPromise -> {
        final SafeFuture<String> protocolIdFuture = SafeFuture.of(streamPromise.getStream().thenCompose(Stream::getProtocol));
        // waiting for controller, writing initial payload or interrupt
        return SafeFuture.of(streamPromise.getController()).<String, RpcStreamController<TOutgoingHandler>>thenCombine(protocolIdFuture, (controller, protocolId) -> {
            final TOutgoingHandler handler = rpcMethod.createOutgoingRequestHandler(protocolId, request, responseHandler);
            controller.setOutgoingRequestHandler(handler);
            return controller;
        }).orInterrupt(closeInterruptor, timeoutInterruptor).thenWaitFor(controller -> controller.getRpcStream().writeBytes(initialPayload)).orInterrupt(closeInterruptor, timeoutInterruptor).whenException(err -> closeStreamAbruptly(streamPromise.getStream().join()));
    }).catchAndRethrow(err -> {
        if (ExceptionUtil.getCause(err, ConnectionClosedException.class).isPresent()) {
            throw new PeerDisconnectedException(err);
        }
    });
}
Also used : SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) Bytes(org.apache.tuweni.bytes.Bytes) Interruptor(tech.pegasys.teku.infrastructure.async.SafeFuture.Interruptor) RemoteWriteClosed(io.libp2p.etc.util.netty.mux.RemoteWriteClosed) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LibP2PNodeId(tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId) ByteBuf(io.netty.buffer.ByteBuf) RpcResponseHandler(tech.pegasys.teku.networking.p2p.rpc.RpcResponseHandler) Controller(tech.pegasys.teku.networking.p2p.libp2p.rpc.RpcHandler.Controller) P2PChannel(io.libp2p.core.P2PChannel) Duration(java.time.Duration) StreamPromise(io.libp2p.core.StreamPromise) ProtocolBinding(io.libp2p.core.multistream.ProtocolBinding) ProtocolDescriptor(io.libp2p.core.multistream.ProtocolDescriptor) RpcStream(tech.pegasys.teku.networking.p2p.rpc.RpcStream) AsyncRunner(tech.pegasys.teku.infrastructure.async.AsyncRunner) Connection(io.libp2p.core.Connection) Stream(io.libp2p.core.Stream) Throwables(com.google.common.base.Throwables) PeerDisconnectedException(tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException) RpcRequestHandler(tech.pegasys.teku.networking.p2p.rpc.RpcRequestHandler) FutureUtil.ignoreFuture(tech.pegasys.teku.infrastructure.async.FutureUtil.ignoreFuture) Consumer(java.util.function.Consumer) RpcMethod(tech.pegasys.teku.networking.p2p.rpc.RpcMethod) StreamClosedException(tech.pegasys.teku.networking.p2p.rpc.StreamClosedException) ExceptionUtil(tech.pegasys.teku.infrastructure.exceptions.ExceptionUtil) StreamTimeoutException(tech.pegasys.teku.networking.p2p.rpc.StreamTimeoutException) Logger(org.apache.logging.log4j.Logger) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull) LogManager(org.apache.logging.log4j.LogManager) NodeId(tech.pegasys.teku.networking.p2p.peer.NodeId) ConnectionClosedException(io.libp2p.core.ConnectionClosedException) RpcStreamController(tech.pegasys.teku.networking.p2p.rpc.RpcStreamController) Bytes(org.apache.tuweni.bytes.Bytes) Interruptor(tech.pegasys.teku.infrastructure.async.SafeFuture.Interruptor) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) ConnectionClosedException(io.libp2p.core.ConnectionClosedException) RpcStream(tech.pegasys.teku.networking.p2p.rpc.RpcStream) Stream(io.libp2p.core.Stream) PeerDisconnectedException(tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException) PeerDisconnectedException(tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException) StreamClosedException(tech.pegasys.teku.networking.p2p.rpc.StreamClosedException) StreamTimeoutException(tech.pegasys.teku.networking.p2p.rpc.StreamTimeoutException) ConnectionClosedException(io.libp2p.core.ConnectionClosedException) StreamTimeoutException(tech.pegasys.teku.networking.p2p.rpc.StreamTimeoutException)

Example 3 with Controller

use of tech.pegasys.teku.networking.p2p.libp2p.rpc.RpcHandler.Controller in project teku by ConsenSys.

the class RpcHandlerTest method sendRequest_streamClosedRightBeforeCreateStreamShouldThrowPeerDisconnectedException.

@Test
void sendRequest_streamClosedRightBeforeCreateStreamShouldThrowPeerDisconnectedException() {
    when(session.createStream((ProtocolBinding<Controller<RpcRequestHandler>>) any())).thenThrow(new ConnectionClosedException());
    SafeFuture<RpcStreamController<RpcRequestHandler>> future = rpcHandler.sendRequest(connection, request, responseHandler);
    assertThatSafeFuture(future).isCompletedExceptionallyWith(PeerDisconnectedException.class);
}
Also used : RpcStreamController(tech.pegasys.teku.networking.p2p.rpc.RpcStreamController) ConnectionClosedException(io.libp2p.core.ConnectionClosedException) Controller(tech.pegasys.teku.networking.p2p.libp2p.rpc.RpcHandler.Controller) RpcStreamController(tech.pegasys.teku.networking.p2p.rpc.RpcStreamController) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Controller (tech.pegasys.teku.networking.p2p.libp2p.rpc.RpcHandler.Controller)3 RpcStreamController (tech.pegasys.teku.networking.p2p.rpc.RpcStreamController)3 Connection (io.libp2p.core.Connection)2 ConnectionClosedException (io.libp2p.core.ConnectionClosedException)2 Stream (io.libp2p.core.Stream)2 NotNull (org.jetbrains.annotations.NotNull)2 LibP2PNodeId (tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId)2 NodeId (tech.pegasys.teku.networking.p2p.peer.NodeId)2 RpcStream (tech.pegasys.teku.networking.p2p.rpc.RpcStream)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Throwables (com.google.common.base.Throwables)1 P2PChannel (io.libp2p.core.P2PChannel)1 StreamPromise (io.libp2p.core.StreamPromise)1 ProtocolBinding (io.libp2p.core.multistream.ProtocolBinding)1 ProtocolDescriptor (io.libp2p.core.multistream.ProtocolDescriptor)1 RemoteWriteClosed (io.libp2p.etc.util.netty.mux.RemoteWriteClosed)1 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 Duration (java.time.Duration)1