public void init(ChannelPipelineFactory factory) throws Exception { id = String.format("%1$020d", Math.abs(new Random(System.currentTimeMillis()).nextLong())) .getBytes(); group = new OioEventLoopGroup(); connectionlessBootstrap = new Bootstrap(); connectionlessBootstrap.group(group); connectionlessBootstrap.option(ChannelOption.SO_BROADCAST, true); connectionlessBootstrap.handler(factory); connectionlessBootstrap.channel(OioDatagramChannel.class); ; datagramChannel = (DatagramChannel) connectionlessBootstrap .bind(new InetSocketAddress(mcastGroupPort)).sync().channel(); multicastAddress = new InetSocketAddress(mcastGroupIp, mcastGroupPort); NetworkInterface networkInterface = NetworkInterface .getByInetAddress(InetAddress.getByName(bindAddress)); // for (Enumeration nifs = NetworkInterface.getNetworkInterfaces(); // nifs.hasMoreElements(); ) datagramChannel.joinGroup(multicastAddress, null);// (NetworkInterface) // nifs.nextElement()); init = true; if (debug) factory.debug(); }
public static Bootstrap createUDPBootstrap(final ChannelType channelType) throws UnsupportedOperationException { Bootstrap bootstrap = new Bootstrap(); switch (channelType) { case NIO: bootstrap.group(new NioEventLoopGroup()); bootstrap.channel(NioDatagramChannel.class); return bootstrap; case OIO: bootstrap.group(new OioEventLoopGroup()); bootstrap.channel(OioDatagramChannel.class); return bootstrap; default: throw new UnsupportedOperationException("Failed to create Bootstrap, " + channelType + " not supported!"); } }
public static Bootstrap createServerBootstrap(final ChannelType channelType) throws UnsupportedOperationException { Bootstrap serverBootstrap = new Bootstrap(); switch (channelType) { case NIO: serverBootstrap.group(new NioEventLoopGroup(Runtime.getRuntime().availableProcessors())); serverBootstrap.channel(NioDatagramChannel.class); // serverBootstrap.localAddress(new InetSocketAddress(port)) // .handler(packetHandler); return serverBootstrap; case OIO: serverBootstrap.group(new OioEventLoopGroup(Runtime.getRuntime().availableProcessors())); serverBootstrap.channel(OioDatagramChannel.class); return serverBootstrap; default: throw new UnsupportedOperationException("Failed to create ServerBootstrap, " + channelType + " not supported!"); } }
public List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() { // Make the list of Bootstrap factories. List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList( new BootstrapFactory<Bootstrap>() { @Override public Bootstrap newInstance() { return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() { @Override public Channel newChannel() { return new NioDatagramChannel(InternetProtocolFamily.IPv4); } @Override public String toString() { return NioDatagramChannel.class.getSimpleName() + ".class"; } }); } }, new BootstrapFactory<Bootstrap>() { @Override public Bootstrap newInstance() { return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class); } } ); // Populare the combinations. return combo(bfs, bfs); }
public void testMulticast(Bootstrap sb, Bootstrap cb) throws Throwable { MulticastTestHandler mhandler = new MulticastTestHandler(); sb.handler(new SimpleChannelInboundHandler<Object>() { @Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { // Nothing will be sent. } }); cb.handler(mhandler); sb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF); sb.option(ChannelOption.SO_REUSEADDR, true); cb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF); cb.option(ChannelOption.SO_REUSEADDR, true); cb.localAddress(addr.getPort()); Channel sc = sb.bind().sync().channel(); if (sc instanceof OioDatagramChannel) { // skip the test for OIO, as it fails because of // No route to host which makes no sense. // Maybe a JDK bug ? sc.close().awaitUninterruptibly(); return; } DatagramChannel cc = (DatagramChannel) cb.bind().sync().channel(); String group = "230.0.0.1"; InetSocketAddress groupAddress = new InetSocketAddress(group, addr.getPort()); cc.joinGroup(groupAddress, NetUtil.LOOPBACK_IF).sync(); sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync(); assertTrue(mhandler.await()); // leave the group cc.leaveGroup(groupAddress, NetUtil.LOOPBACK_IF).sync(); // sleep a second to make sure we left the group Thread.sleep(1000); // we should not receive a message anymore as we left the group before sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync(); mhandler.await(); sc.close().awaitUninterruptibly(); cc.close().awaitUninterruptibly(); }
static List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> datagram() { List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> list = new ArrayList<Entry<Factory<Bootstrap>, Factory<Bootstrap>>>(); // Make the list of Bootstrap factories. List<Factory<Bootstrap>> bfs = new ArrayList<Factory<Bootstrap>>(); bfs.add(new Factory<Bootstrap>() { @Override public Bootstrap newInstance() { return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() { @Override public Channel newChannel() { return new NioDatagramChannel(InternetProtocolFamily.IPv4); } @Override public String toString() { return NioDatagramChannel.class.getSimpleName() + ".class"; } }); } }); bfs.add(new Factory<Bootstrap>() { @Override public Bootstrap newInstance() { return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class); } }); // Populate the combinations for (Factory<Bootstrap> sbf: bfs) { for (Factory<Bootstrap> cbf: bfs) { final Factory<Bootstrap> sbf0 = sbf; final Factory<Bootstrap> cbf0 = cbf; list.add(new Entry<Factory<Bootstrap>, Factory<Bootstrap>>() { @Override public Factory<Bootstrap> getKey() { return sbf0; } @Override public Factory<Bootstrap> getValue() { return cbf0; } @Override public Factory<Bootstrap> setValue(Factory<Bootstrap> value) { throw new UnsupportedOperationException(); } }); } } return list; }
static List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> datagram() { List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> list = new ArrayList<Entry<Factory<Bootstrap>, Factory<Bootstrap>>>(); // Make the list of Bootstrap factories. List<Factory<Bootstrap>> bfs = new ArrayList<Factory<Bootstrap>>(); bfs.add(new Factory<Bootstrap>() { @Override public Bootstrap newInstance() { return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() { @Override public Channel newChannel(EventLoop eventLoop) { return new NioDatagramChannel(eventLoop, InternetProtocolFamily.IPv4); } @Override public String toString() { return NioDatagramChannel.class.getSimpleName() + ".class"; } }); } }); bfs.add(new Factory<Bootstrap>() { @Override public Bootstrap newInstance() { return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class); } }); // Populate the combinations for (Factory<Bootstrap> sbf: bfs) { for (Factory<Bootstrap> cbf: bfs) { final Factory<Bootstrap> sbf0 = sbf; final Factory<Bootstrap> cbf0 = cbf; list.add(new Entry<Factory<Bootstrap>, Factory<Bootstrap>>() { @Override public Factory<Bootstrap> getKey() { return sbf0; } @Override public Factory<Bootstrap> getValue() { return cbf0; } @Override public Factory<Bootstrap> setValue(Factory<Bootstrap> value) { throw new UnsupportedOperationException(); } }); } } return list; }
public void testMulticast(Bootstrap sb, Bootstrap cb) throws Throwable { MulticastTestHandler mhandler = new MulticastTestHandler(); sb.handler(new SimpleChannelInboundHandler<Object>() { @Override public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception { // Nothing will be sent. } }); cb.handler(mhandler); sb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF); sb.option(ChannelOption.SO_REUSEADDR, true); cb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF); cb.option(ChannelOption.SO_REUSEADDR, true); cb.localAddress(addr.getPort()); Channel sc = sb.bind().sync().channel(); if (sc instanceof OioDatagramChannel) { // skip the test for OIO, as it fails because of // No route to host which makes no sense. // Maybe a JDK bug ? sc.close().awaitUninterruptibly(); return; } DatagramChannel cc = (DatagramChannel) cb.bind().sync().channel(); String group = "230.0.0.1"; InetSocketAddress groupAddress = new InetSocketAddress(group, addr.getPort()); cc.joinGroup(groupAddress, NetUtil.LOOPBACK_IF).sync(); sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync(); assertTrue(mhandler.await()); // leave the group cc.leaveGroup(groupAddress, NetUtil.LOOPBACK_IF).sync(); // sleep a second to make sure we left the group Thread.sleep(1000); // we should not receive a message anymore as we left the group before sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync(); mhandler.await(); sc.close().awaitUninterruptibly(); cc.close().awaitUninterruptibly(); }
@Override protected Class<? extends Channel> getChannelClass() { return OioDatagramChannel.class; }
@Override public OioDatagramChannel newChannel() { return new OioDatagramChannel(); }