use of io.dingodb.net.netty.channel.ConnectionSubChannel in project dingo by dingodb.
the class GenericMessageHandler method handle.
@Override
public void handle(Connection<Message> connection, Packet<Message> packet) {
switch(packet.header().type()) {
case PING:
connection.genericSubChannel().send(MessagePacket.pong(0));
return;
case PONG:
break;
case CONNECT_CHANNEL:
ConnectionSubChannel<Message> channel = connection.openSubChannel(packet.header().targetChannelId());
channel.send(MessagePacket.ack(channel.channelId(), channel.targetChannelId(), channel.nextSeq()));
break;
case DIS_CONNECT_CHANNEL:
connection.closeSubChannel(packet.header().channelId());
break;
case ACK:
Optional.ofNullable(ackFuture.get(connection)).map(futureMap -> futureMap.remove(packet.header().channelId())).ifPresent(future -> future.complete(packet));
break;
case HANDSHAKE_ERROR:
ErrorMessage errorMessage = ErrorMessage.builder().build().load(packet.content().toBytes());
log.error("Handshake failed, msg is: {}", errorMessage.getDetailMessage());
try {
connection.close();
} catch (Exception e) {
log.error("Connection close error.", e);
}
break;
default:
throw new IllegalStateException("Unexpected value: " + packet.header().type());
}
}
use of io.dingodb.net.netty.channel.ConnectionSubChannel in project dingo by dingodb.
the class NetServiceTest method hello.
// @Test
public void hello() throws Exception {
String hello = "hello";
Tag tag = SimpleTag.builder().tag("TEST".getBytes(StandardCharsets.UTF_8)).build();
ServiceLoader<NetServiceProvider> loader = ServiceLoader.load(NetServiceProvider.class);
NettyNetService netService = (NettyNetService) loader.iterator().next().get();
netService.listenPort(19199);
netService.registerMessageListenerProvider(tag, () -> (msg, ch) -> assertThat(new String(msg.toBytes())).isEqualTo(hello));
netService.registerMessageListenerProvider(tag, () -> (msg, ch) -> System.out.println(String.format("%s %s %s", new String(msg.toBytes()), ((ConnectionSubChannel) ch).channelId(), StackTraces.stack(2))));
Channel channel = netService.newChannel(NetAddress.builder().host("localhost").port(19199).build());
Message helloMsg = SimpleMessage.builder().tag(tag).content(hello.getBytes()).build();
channel.send(helloMsg);
Thread.sleep(100000);
}
Aggregations