Example usage for io.netty.handler.codec.compression JZlibEncoder JZlibEncoder

List of usage examples for io.netty.handler.codec.compression JZlibEncoder JZlibEncoder

Introduction

In this page you can find the example usage for io.netty.handler.codec.compression JZlibEncoder JZlibEncoder.

Prototype

public JZlibEncoder() 

Source Link

Document

Creates a new zlib encoder with the default compression level ( 6 ), default window bits ( 15 ), default memory level ( 8 ), and the default wrapper ( ZlibWrapper#ZLIB ).

Usage

From source file:waazdoh.cp2p.network.NodeConnectionFactory.java

License:Open Source License

public synchronized Bootstrap getBootstrap() {
    if (NodeConnectionFactory._bootstrap == null) {
        NodeConnectionFactory._bootstrap = new Bootstrap(); // (1)
        _bootstrap.group(workerGroup); // (2)
        _bootstrap.channel(OioSocketChannel.class); // (3)
        _bootstrap.option(ChannelOption.SO_KEEPALIVE, true); // (4)
        _bootstrap.option(ChannelOption.TCP_NODELAY, true); // (4)

        _bootstrap.handler(new ChannelInitializer<SocketChannel>() {
            @Override//from  w  w w  . j  a va 2 s .c  o m
            public void initChannel(SocketChannel ch) throws Exception {
                log.info("init client channel " + ch);

                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("zipencoder", new JZlibEncoder());
                pipeline.addLast("zipdecoder", new JZlibDecoder());
                pipeline.addLast("messageencoder", new MessageEncoder());
                pipeline.addLast("messagedecoder", new MessageDecoder());
                pipeline.addLast("channels", new NodeHandler());

                ch.writeAndFlush("<init>" + System.currentTimeMillis() + "</init>");
            }
        });
    }
    return _bootstrap;
}

From source file:waazdoh.cp2p.network.TCPListener.java

License:Open Source License

public void start() {
    if (!isClosed()) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override/*from   ww  w .  j a v a  2s . co  m*/
                    protected void initChannel(SocketChannel ch) throws Exception {
                        if (!isClosed()) {
                            log.info("initChannel " + ch);
                            ChannelPipeline pipe = ch.pipeline();
                            pipe.addLast("zipencoder", new JZlibEncoder());
                            pipe.addLast("zipdecoder", new JZlibDecoder());
                            pipe.addLast("messageencoder", new MessageEncoder());
                            pipe.addLast("messagedecoder", new MessageDecoder());
                            pipe.addLast("server", new MServerHandler());
                            //
                            List<MMessage> mlist = new LinkedList<MMessage>();
                            mlist.add(messenger.getMessage("hello"));
                            ch.writeAndFlush(mlist);
                        } else {
                            log.info("InitChannel on closed listener. Closing channel.");
                            ch.close();
                        }
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        //
        port = preferences.getInteger(WPreferences.NETWORK_SERVER_PORT, DEFAULT_PORT);
        //
        try {
            while (!isClosed() && bind == null && port < 65000) {
                startListening(bootstrap);
                if (bind == null) {
                    port++;
                }
            }
        } catch (InterruptedException e) {
            log.error(e);
        }

        log.info("listening " + port + " messager:" + this.messenger);
        //
        MMessage b = this.messenger.getMessage("newnode");
        b.addAttribute("port", port);
        //
        this.messenger.broadcastMessage(b);
    }
}