use of org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler in project atomix by atomix.
the class NettyBroadcastService method bootstrapServer.
private CompletableFuture<Void> bootstrapServer() {
Bootstrap serverBootstrap = new Bootstrap().group(group).channelFactory(() -> new NioDatagramChannel(InternetProtocolFamily.IPv4)).handler(new SimpleChannelInboundHandler<Object>() {
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// Nothing will be sent.
}
}).option(ChannelOption.IP_MULTICAST_IF, iface).option(ChannelOption.SO_REUSEADDR, true);
CompletableFuture<Void> future = new CompletableFuture<>();
serverBootstrap.bind(localAddress).addListener((ChannelFutureListener) f -> {
if (f.isSuccess()) {
serverChannel = f.channel();
future.complete(null);
} else {
future.completeExceptionally(f.cause());
}
});
return future;
}
use of org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler in project atomix by atomix.
the class NettyBroadcastService method bootstrapClient.
private CompletableFuture<Void> bootstrapClient() {
Bootstrap clientBootstrap = new Bootstrap().group(group).channelFactory(() -> new NioDatagramChannel(InternetProtocolFamily.IPv4)).handler(new SimpleChannelInboundHandler<DatagramPacket>() {
@Override
protected void channelRead0(ChannelHandlerContext context, DatagramPacket packet) throws Exception {
byte[] payload = new byte[packet.content().readInt()];
packet.content().readBytes(payload);
Message message = SERIALIZER.decode(payload);
Set<Consumer<byte[]>> listeners = NettyBroadcastService.this.listeners.get(message.subject());
if (listeners != null) {
for (Consumer<byte[]> listener : listeners) {
listener.accept(message.payload());
}
}
}
}).option(ChannelOption.IP_MULTICAST_IF, iface).option(ChannelOption.SO_REUSEADDR, true).localAddress(localAddress.getPort());
CompletableFuture<Void> future = new CompletableFuture<>();
clientBootstrap.bind().addListener((ChannelFutureListener) f -> {
if (f.isSuccess()) {
clientChannel = (DatagramChannel) f.channel();
log.info("{} joining multicast group {} on port {}", localAddress.getHostName(), groupAddress.getHostName(), groupAddress.getPort());
clientChannel.joinGroup(groupAddress, iface).addListener(f2 -> {
if (f2.isSuccess()) {
log.info("{} successfully joined multicast group {} on port {}", localAddress.getHostName(), groupAddress.getHostName(), groupAddress.getPort());
future.complete(null);
} else {
log.info("{} failed to join group {} on port {}", localAddress.getHostName(), groupAddress.getHostName(), groupAddress.getPort());
future.completeExceptionally(f2.cause());
}
});
} else {
future.completeExceptionally(f.cause());
}
});
return future;
}
use of org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler in project atomix by atomix.
the class NettyUnicastService method bootstrap.
private CompletableFuture<Void> bootstrap() {
Bootstrap serverBootstrap = new Bootstrap().group(group).channel(NioDatagramChannel.class).handler(new SimpleChannelInboundHandler<DatagramPacket>() {
@Override
protected void channelRead0(ChannelHandlerContext context, DatagramPacket packet) throws Exception {
byte[] payload = new byte[packet.content().readInt()];
packet.content().readBytes(payload);
Message message = SERIALIZER.decode(payload);
Map<BiConsumer<Address, byte[]>, Executor> listeners = NettyUnicastService.this.listeners.get(message.subject());
if (listeners != null) {
listeners.forEach((consumer, executor) -> executor.execute(() -> consumer.accept(message.source(), message.payload())));
}
}
}).option(ChannelOption.RCVBUF_ALLOCATOR, new DefaultMaxBytesRecvByteBufAllocator()).option(ChannelOption.SO_BROADCAST, true).option(ChannelOption.SO_REUSEADDR, true);
return bind(serverBootstrap);
}
Aggregations