Example usage for io.netty.channel.epoll EpollDomainSocketChannel pipeline

List of usage examples for io.netty.channel.epoll EpollDomainSocketChannel pipeline

Introduction

In this page you can find the example usage for io.netty.channel.epoll EpollDomainSocketChannel pipeline.

Prototype

ChannelPipeline pipeline();

Source Link

Document

Return the assigned ChannelPipeline .

Usage

From source file:net.petercashel.nettyCore.clientUDS.clientCoreUDS.java

License:Apache License

public static void initializeConnection(File socket) throws Exception {
    PacketRegistry.setupRegistry();//from   w ww.  ja  va 2  s .  c o m
    PacketRegistry.Side = side;

    group = new EpollEventLoopGroup();
    try {
        Bootstrap b = new BootstrapFactory<Bootstrap>() {
            @Override
            public Bootstrap newInstance() {
                return new Bootstrap().group(group).channel(EpollDomainSocketChannel.class)
                        .handler(new ChannelInitializer<EpollDomainSocketChannel>() {
                            @Override
                            protected void initChannel(EpollDomainSocketChannel ch) throws Exception {
                                ChannelPipeline p = ch.pipeline();
                                p.addLast("InboundOutboundClientHandler", new ClientUDSConnectionHander());
                            }
                        });
            }
        }.newInstance();

        // Make the connection attempt.
        ChannelFuture f = b.connect(newSocketAddress(socket)).sync();
        f.awaitUninterruptibly(2000, TimeUnit.MILLISECONDS);

        if (!f.isSuccess())
            throw new RuntimeException("Failed to connect");
        // if a wait option was selected and the connect did not fail,
        // the Date can now be sent.
        System.out.println("Client UDS Connected!");
        connection = f.channel();
        connClosed = false;

        // Send GetHistoryPacket
        PacketRegistry.pack(new GetHistoryPacket()).sendPacket(connection);

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
        System.out.println("Connection Closed");
        connClosed = true;
    }
}

From source file:net.petercashel.nettyCore.serverUDS.serverCoreUDS.java

License:Apache License

public static void initializeServer(File socket) throws Exception {
    clientConnectionMap = new HashMap<Channel, Channel>();
    PacketRegistry.setupRegistry();/* w ww . j  a v  a2s . co m*/
    PacketRegistry.Side = side;
    alive = true;
    bossGroup = new EpollEventLoopGroup(); // (1)
    workerGroup = new EpollEventLoopGroup();
    try {
        ServerBootstrap b = new BootstrapFactory<ServerBootstrap>() {
            @Override
            public ServerBootstrap newInstance() {
                return new ServerBootstrap().group(bossGroup, workerGroup)
                        .channel(EpollServerDomainSocketChannel.class)
                        .childHandler(new ChannelInitializer<EpollDomainSocketChannel>() {
                            @Override
                            public void initChannel(EpollDomainSocketChannel ch) throws Exception {
                                ChannelPipeline p = ch.pipeline();
                                p.addLast("readTimeoutHandler", new ReadTimeoutHandler(300));
                                p.addLast("InboundOutboundServerHandler", new ServerUDSConnectionHandler());
                            }
                        });
            }
        }.newInstance();

        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind(newSocketAddress(socket)).sync(); // (7)
        System.out.println("Server UDS Initalised!");
        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to
        // gracefully
        // shut down your server.
        c = f.channel();
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}