Example usage for io.netty.channel.socket.nio NioDatagramChannel pipeline

List of usage examples for io.netty.channel.socket.nio NioDatagramChannel pipeline

Introduction

In this page you can find the example usage for io.netty.channel.socket.nio NioDatagramChannel pipeline.

Prototype

@Override
    public ChannelPipeline pipeline() 

Source Link

Usage

From source file:UdpServer.java

License:Open Source License

public void run() throws Exception {
    final NioEventLoopGroup group = new NioEventLoopGroup();
    //Create Actor System
    final ActorSystem system = ActorSystem.create("CapwapFSMActorSystem");
    final ActorRef PacketProcessorActor = system
            .actorOf(Props.create(CapwapMsgProcActor.class, "MessageProcessorActor"), "MessageProcessorActor");
    try {//  ww  w.ja  va  2s .c o  m
        final Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true)
                .handler(new ChannelInitializer<NioDatagramChannel>() {
                    @Override
                    public void initChannel(final NioDatagramChannel ch) throws Exception {

                        ChannelPipeline p = ch.pipeline();
                        createActorAndHandler(p);
                        //p.addLast(new IncommingPacketHandler(PacketProcessorActor));
                    }
                });

        // Bind and start to accept incoming connections.
        Integer pPort = port;
        InetAddress address = InetAddress.getLoopbackAddress();
        System.out.printf("\n\nwaiting for message %s %s\n\n", String.format(pPort.toString()),
                String.format(address.toString()));
        b.bind(address, port).sync().channel().closeFuture().await();

    } finally {
        System.out.print("In Server Finally");
    }
}

From source file:FSMTester.java

License:Open Source License

public void run() throws Exception {
    final NioEventLoopGroup group = new NioEventLoopGroup();
    //Create Actor System
    final ActorSystem system = ActorSystem.create("CapwapFSMActorSystem");
    final ActorRef PacketProcessorActor = system
            .actorOf(Props.create(CapwapMsgProcActor.class, "MessageProcessorActor"), "MessageProcessorActor");
    try {/*from   w w  w .j av  a 2  s.c  o m*/
        final Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true)
                .handler(new ChannelInitializer<NioDatagramChannel>() {
                    @Override
                    public void initChannel(final NioDatagramChannel ch) throws Exception {

                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new IncommingPacketFSMHandler(PacketProcessorActor));
                    }
                });

        // Bind and start to accept incoming connections.
        Integer pPort = port;
        InetAddress address = InetAddress.getLoopbackAddress();
        System.out.printf("\n\nwaiting for message %s %s\n\n", String.format(pPort.toString()),
                String.format(address.toString()));
        b.bind(address, port).sync().channel().closeFuture().await();

    } finally {
        System.out.print("In Server Finally");
    }
}

From source file:co.rsk.net.discovery.UDPServer.java

License:Open Source License

private Bootstrap createBootstrap(EventLoopGroup group) {
    return new Bootstrap().group(group).channel(NioDatagramChannel.class)
            .handler(new ChannelInitializer<NioDatagramChannel>() {
                @Override/* w  w  w  .ja v  a  2 s. c o m*/
                public void initChannel(NioDatagramChannel ch) throws Exception {
                    ch.pipeline().addLast(new PacketDecoder());
                    UDPChannel udpChannel = new UDPChannel(ch, peerExplorer);
                    peerExplorer.setUDPChannel(udpChannel);
                    ch.pipeline().addLast(udpChannel);
                }
            });
}

From source file:com.ibasco.agql.protocols.valve.source.query.logger.SourceLogListenService.java

License:Open Source License

/**
 * <p>Creates a new service using the specified {@link InetSocketAddress} to listen on and utilizing
 * the callback specified to notify listeners of source log events</p>
 *
 * @param listenAddress//  w  w w .  ja v  a 2 s .c o m
 *         An {@link InetSocketAddress} where the listen service will bind or listen on
 * @param logEventCallback
 *         A {@link Consumer} callback that will be called once a log event has been received
 */
public SourceLogListenService(InetSocketAddress listenAddress, Consumer<SourceLogEntry> logEventCallback) {
    this.listenAddress = listenAddress;
    bootstrap = new Bootstrap().localAddress(this.listenAddress).channel(NioDatagramChannel.class)
            .group(listenWorkGroup).handler(new ChannelInitializer<NioDatagramChannel>() {
                @Override
                protected void initChannel(NioDatagramChannel ch) throws Exception {
                    ch.pipeline().addLast(new SourceLogListenHandler(logEventCallback));
                }
            });

    SourceLogListenService service = this;
    //Add shutdown hook
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                log.debug("Service Interrupted. Shutting down gracefully.");
                service.shutdown();
            } catch (InterruptedException e) {
                log.error(e.getMessage(), e);
            }
        }
    });
}

From source file:dorkbox.network.pipeline.discovery.ClientDiscoverHostInitializer.java

License:Apache License

@Override
public void initChannel(final NioDatagramChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast("discoverHostHandler", clientDiscoverHostHandler);
}

From source file:nearenough.examples.NettyClient.java

License:Open Source License

public static void main(String[] args) throws InterruptedException {
    InetSocketAddress addr = new InetSocketAddress(INT08H_SERVER_HOST, INT08H_SERVER_PORT);

    System.out.printf("Sending request to %s\n", addr);

    // Below is Netty boilerplate for setting-up an event loop and registering a handler
    NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap().group(nioEventLoopGroup).remoteAddress(addr)
            .channel(NioDatagramChannel.class).handler(new ChannelInitializer<NioDatagramChannel>() {
                @Override/*  w  ww .  j a v a  2s.c om*/
                protected void initChannel(NioDatagramChannel ch) {
                    ch.pipeline().addLast(new ReadTimeoutHandler(5)).addLast(new RequestHandler(addr));
                }
            });

    ChannelFuture connectFuture = bootstrap.connect();
    connectFuture.addListener(future -> {
        if (!future.isSuccess()) {
            System.out.println("Connect fail:");
            System.out.println(future.cause().getMessage());
        }
    });

    connectFuture.channel().closeFuture().sync();
    nioEventLoopGroup.shutdownGracefully();
}

From source file:org.beykery.wormhole.WormRegistry.java

License:Apache License

public WormRegistry(int port, int workerSize) {
    if (port <= 0 || workerSize <= 0) {
        throw new IllegalArgumentException("??");
    }//w w  w .jav a  2  s . co  m
    services = new HashMap<>();
    workers = new NioEventLoopGroup(workerSize);
    final NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.channel(NioDatagramChannel.class);
    bootstrap.group(nioEventLoopGroup);
    bootstrap.handler(new ChannelInitializer<NioDatagramChannel>() {

        @Override
        protected void initChannel(NioDatagramChannel ch) throws Exception {
            ChannelPipeline cp = ch.pipeline();
            cp.addLast(new ChannelInboundHandlerAdapter() {
                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    DatagramPacket dp = (DatagramPacket) msg;
                    WormRegistry.this.onMessage(dp);
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    WormRegistry.this.onException(cause);
                }
            });
        }
    });
    ChannelFuture sync = bootstrap.bind(port).syncUninterruptibly();
    channel = (NioDatagramChannel) sync.channel();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            nioEventLoopGroup.shutdownGracefully();
        }
    }));
}

From source file:org.ethereum.net.UdpTest.java

License:Open Source License

public Channel create(String bindAddr, int port) throws InterruptedException {
    NioEventLoopGroup group = new NioEventLoopGroup(1);

    Bootstrap b = new Bootstrap();
    b.group(group).channel(NioDatagramChannel.class).handler(new ChannelInitializer<NioDatagramChannel>() {
        @Override/*from  www.ja  va2s  .  c om*/
        public void initChannel(NioDatagramChannel ch) throws Exception {
            ch.pipeline().addLast(new PacketDecoder());
            SimpleMessageHandler messageHandler = new SimpleMessageHandler(ch, nodeManager);
            nodeManager.setMessageSender(messageHandler);
            ch.pipeline().addLast(messageHandler);
        }
    });

    return b.bind(bindAddr, port).sync().channel();
}

From source file:org.onosproject.lisp.ctl.impl.LispChannelInitializer.java

License:Apache License

@Override
protected void initChannel(NioDatagramChannel channel) throws Exception {
    ChannelPipeline pipeline = channel.pipeline();

    LispChannelHandler handler = new LispChannelHandler();

    pipeline.addLast(LISP_MESSAGE_DECODER, new LispMessageDecoder());
    pipeline.addLast(LISP_MESSAGE_ENCODER, new LispMessageEncoder());
    pipeline.addLast(LISP_CHANNEL_HANDLER, handler);
}

From source file:org.opendaylight.capwap.ODLCapwapACServer.java

License:Open Source License

@Override
public void start() throws Exception {

    final Bootstrap b = new Bootstrap();
    b.group(group);/* w  w  w. j av a 2 s.c o  m*/
    b.channel(NioDatagramChannel.class);
    b.option(ChannelOption.SO_BROADCAST, true);
    b.handler(new ChannelInitializer<NioDatagramChannel>() {
        @Override
        public void initChannel(final NioDatagramChannel ch) throws Exception {

            ChannelPipeline p = ch.pipeline();
            if (security == SecurityType.DTLS) {
                p.addLast(new LoggingHandler("CapwapACServer Log 2", LogLevel.TRACE));
                ChannelHandler dtlsHandler = UscPluginUdp.getSecureServerHandler(ch);
                p.addLast(dtlsHandler);
            }
            p.addLast(new LoggingHandler("CapwapACServer Log 1", LogLevel.TRACE));
            p.addLast(new CapwapPacketHandler());
        }
    });
    b.bind(port).sync().channel().closeFuture().await();
}