use of org.apache.ignite.internal.network.handshake.HandshakeManager in project ignite-3 by apache.
the class NettyServerTest method testHandshakeManagerInvoked.
/**
* Tests that handshake manager is invoked upon a client connecting to a server.
*
* @throws Exception If failed.
*/
@Test
public void testHandshakeManagerInvoked() throws Exception {
HandshakeManager handshakeManager = mock(HandshakeManager.class);
when(handshakeManager.handshakeFuture()).thenReturn(CompletableFuture.completedFuture(mock(NettySender.class)));
HandshakeResult noOp = HandshakeResult.noOp();
when(handshakeManager.init(any())).thenReturn(noOp);
when(handshakeManager.onConnectionOpen(any())).thenReturn(noOp);
when(handshakeManager.onMessage(any(), any())).thenReturn(noOp);
MessageSerializationRegistry registry = mock(MessageSerializationRegistry.class);
when(registry.createDeserializer(anyShort(), anyShort())).thenReturn(new MessageDeserializer<>() {
/**
* {@inheritDoc}
*/
@Override
public boolean readMessage(MessageReader reader) throws MessageMappingException {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public Class<NetworkMessage> klass() {
return NetworkMessage.class;
}
/**
* {@inheritDoc}
*/
@Override
public NetworkMessage getMessage() {
return mock(NetworkMessage.class);
}
});
bootstrapFactory = new NettyBootstrapFactory(serverCfg, "");
bootstrapFactory.start();
server = new NettyServer(serverCfg.value(), () -> handshakeManager, sender -> {
}, (message) -> {
}, new SerializationService(registry, mock(UserObjectSerializationContext.class)), bootstrapFactory);
server.start().get(3, TimeUnit.SECONDS);
CompletableFuture<Channel> connectFut = NettyUtils.toChannelCompletableFuture(new Bootstrap().channel(NioSocketChannel.class).group(new NioEventLoopGroup()).handler(new ChannelInitializer<>() {
/**
* {@inheritDoc}
*/
@Override
protected void initChannel(Channel ch) throws Exception {
// No-op.
}
}).connect(server.address()));
Channel channel = connectFut.get(3, TimeUnit.SECONDS);
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
// One message only.
for (int i = 0; i < (NetworkMessage.MSG_TYPE_SIZE_BYTES + 1); i++) {
buffer.writeByte(1);
}
channel.writeAndFlush(buffer).get(3, TimeUnit.SECONDS);
channel.close().get(3, TimeUnit.SECONDS);
InOrder order = Mockito.inOrder(handshakeManager);
order.verify(handshakeManager, timeout()).init(any());
order.verify(handshakeManager, timeout()).handshakeFuture();
order.verify(handshakeManager, timeout()).onConnectionOpen(any());
order.verify(handshakeManager, timeout()).onMessage(any(), any());
}
use of org.apache.ignite.internal.network.handshake.HandshakeManager in project ignite-3 by apache.
the class NettyServer method start.
/**
* Starts the server.
*
* @return Future that resolves when the server is successfully started.
*/
public CompletableFuture<Void> start() {
synchronized (startStopLock) {
if (stopped) {
throw new IgniteInternalException("Attempted to start an already stopped server");
}
if (serverStartFuture != null) {
throw new IgniteInternalException("Attempted to start an already started server");
}
ServerBootstrap bootstrap = bootstrapFactory.createServerBootstrap();
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
/**
* {@inheritDoc}
*/
@Override
public void initChannel(SocketChannel ch) {
var sessionSerializationService = new PerSessionSerializationService(serializationService);
// Get handshake manager for the new channel.
HandshakeManager manager = handshakeManager.get();
ch.pipeline().addLast(/*
* Decoder that uses the MessageReader
* to read chunked data.
*/
new InboundDecoder(sessionSerializationService), // Handshake handler.
new HandshakeHandler(manager, (consistentId) -> new MessageHandler(messageListener, consistentId, sessionSerializationService)), /*
* Encoder that uses the MessageWriter
* to write chunked data.
*/
new ChunkedWriteHandler(), // Converts NetworkMessage to a ChunkedNetworkMessageInput
new OutboundEncoder(sessionSerializationService), new IoExceptionSuppressingHandler());
manager.handshakeFuture().thenAccept(newConnectionListener);
}
});
int port = configuration.port();
int portRange = configuration.portRange();
var bindFuture = new CompletableFuture<Channel>();
tryBind(bootstrap, port, port + portRange, port, bindFuture);
serverStartFuture = bindFuture.handle((channel, err) -> {
synchronized (startStopLock) {
if (channel != null) {
serverCloseFuture = NettyUtils.toCompletableFuture(channel.closeFuture());
}
this.channel = (ServerChannel) channel;
if (err != null || stopped) {
Throwable stopErr = err != null ? err : new CancellationException("Server was stopped");
return CompletableFuture.<Void>failedFuture(stopErr);
} else {
return CompletableFuture.<Void>completedFuture(null);
}
}
}).thenCompose(Function.identity());
return serverStartFuture;
}
}
Aggregations