use of io.libp2p.core.Connection 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;
}
use of io.libp2p.core.Connection in project xdagj by XDagger.
the class Libp2pXdagProtocol method initChannel.
@NotNull
@Override
public CompletableFuture<Libp2pXdagController> initChannel(@NotNull P2PChannel p2PChannel, @NotNull String s) {
final Connection connection = ((io.libp2p.core.Stream) p2PChannel).getConnection();
libp2pChannel = new Libp2pChannel(connection, this, kernel);
channelManager.add(libp2pChannel);
blockHandler = new XdagBlockHandler(libp2pChannel);
blockHandler.setMessageFactory(new Xdag03MessageFactory());
channelManager.onChannelActive(libp2pChannel, libp2pChannel.getNode());
MessageCodes messageCodes = new MessageCodes();
libp2PXdagController = new Libp2pXdagController(kernel, libp2pChannel);
// add handler
p2PChannel.pushHandler(blockHandler);
p2PChannel.pushHandler(messageCodes);
p2PChannel.pushHandler(libp2PXdagController);
return libp2PXdagController.activeFuture;
}
use of io.libp2p.core.Connection 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);
}
});
}
use of io.libp2p.core.Connection in project teku by ConsenSys.
the class PeerManagerTest method shouldReportSuccessfulConnectionsToReputationManager.
@Test
public void shouldReportSuccessfulConnectionsToReputationManager() {
final Connection connection = mock(Connection.class);
final Session secureSession = new Session(PeerId.random(), PeerId.random(), EcdsaKt.generateEcdsaKeyPair().component2());
when(connection.secureSession()).thenReturn(secureSession);
when(connection.closeFuture()).thenReturn(new SafeFuture<>());
final Multiaddr multiaddr = Multiaddr.fromString("/ip4/127.0.0.1/tcp/9000");
final MultiaddrPeerAddress peerAddress = new MultiaddrPeerAddress(new MockNodeId(1), multiaddr);
final SafeFuture<Connection> connectionFuture = new SafeFuture<>();
when(network.connect(multiaddr)).thenReturn(connectionFuture);
final SafeFuture<Peer> result = peerManager.connect(peerAddress, network);
peerManager.handleConnection(connection);
connectionFuture.complete(connection);
assertThat(result).isCompleted();
verify(reputationManager).reportInitiatedConnectionSuccessful(peerAddress);
verify(reputationManager, never()).reportInitiatedConnectionFailed(peerAddress);
}
Aggregations