Java 类io.netty.channel.socket.oio.OioDatagramChannel 实例源码

项目:yajsw    文件:MulticastEndpoint.java   
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();
}
项目:distributeTemplate    文件:BootstrapFactory.java   
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!");
    }
}
项目:distributeTemplate    文件:ServerUDPBootstrapFactory.java   
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!");
    }
}
项目:netty4.0.27Learn    文件:SocketTestPermutation.java   
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);
}
项目:netty4.0.27Learn    文件:DatagramMulticastTest.java   
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();
}
项目:netty4study    文件:SocketTestPermutation.java   
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;
}
项目:netty4study    文件:DatagramMulticastTest.java   
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();
}
项目:netty-netty-5.0.0.Alpha1    文件:SocketTestPermutation.java   
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;
}
项目:netty-netty-5.0.0.Alpha1    文件:DatagramMulticastTest.java   
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();
}
项目:statsd-netty    文件:NettyUDPServer.java   
@Override
protected Class<? extends Channel> getChannelClass() {
    return OioDatagramChannel.class;
}
项目:dnd    文件:TCPUDPServerManager.java   
@Override
public OioDatagramChannel newChannel() {
    return new OioDatagramChannel();
}