Example usage for io.netty.channel Channel config

List of usage examples for io.netty.channel Channel config

Introduction

In this page you can find the example usage for io.netty.channel Channel config.

Prototype

ChannelConfig config();

Source Link

Document

Returns the configuration of this channel.

Usage

From source file:de.jackwhite20.japs.server.network.initialize.ClusterPublisherChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(Channel channel) throws Exception {

    try {/*  www .  j  a v  a2  s .  c  o  m*/
        channel.config().setOption(ChannelOption.IP_TOS, 0x18);
    } catch (ChannelException e) {
        // Not supported
    }
    channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);

    channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    channel.pipeline().addLast(new JSONObjectDecoder());
    channel.pipeline().addLast(new LengthFieldPrepender(4));
    channel.pipeline().addLast(new JSONObjectEncoder());
    channel.pipeline().addLast(clusterPublisher);
}

From source file:de.jackwhite20.japs.server.network.initialize.ServerChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(Channel channel) throws Exception {

    try {/* w w  w  . j  ava2s .  c o m*/
        channel.config().setOption(ChannelOption.IP_TOS, 0x18);
    } catch (ChannelException e) {
        // Not supported
    }
    channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);

    channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    channel.pipeline().addLast(new JSONObjectDecoder());
    channel.pipeline().addLast(new LengthFieldPrepender(4));
    channel.pipeline().addLast(new JSONObjectEncoder());
    channel.pipeline().addLast(new Connection(jaPSServer, channel));
}

From source file:de.jackwhite20.japs.shared.pipeline.initialize.ClientChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(Channel channel) throws Exception {

    try {/*from  www  .ja  v  a  2 s.  c  om*/
        channel.config().setOption(ChannelOption.IP_TOS, 0x18);
    } catch (ChannelException e) {
        // Not supported
    }
    channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);

    channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    channel.pipeline().addLast(new JSONObjectDecoder());
    channel.pipeline().addLast(new LengthFieldPrepender(4));
    channel.pipeline().addLast(new JSONObjectEncoder());
    channel.pipeline().addLast(nioSocketClient);
}

From source file:io.grpc.netty.NettyServerTest.java

License:Apache License

@Test(timeout = 60000)
public void childChannelOptions() throws Exception {
    final int originalLowWaterMark = 2097169;
    final int originalHighWaterMark = 2097211;

    Map<ChannelOption<?>, Object> channelOptions = new HashMap<>();

    channelOptions.put(ChannelOption.WRITE_BUFFER_WATER_MARK,
            new WriteBufferWaterMark(originalLowWaterMark, originalHighWaterMark));

    final AtomicInteger lowWaterMark = new AtomicInteger(0);
    final AtomicInteger highWaterMark = new AtomicInteger(0);

    final CountDownLatch countDownLatch = new CountDownLatch(1);

    InetSocketAddress addr = new InetSocketAddress(0);
    NettyServer ns = new NettyServer(addr, Utils.DEFAULT_SERVER_CHANNEL_FACTORY, channelOptions,
            SharedResourcePool.forResource(Utils.DEFAULT_BOSS_EVENT_LOOP_GROUP),
            SharedResourcePool.forResource(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP),
            ProtocolNegotiators.plaintext(), Collections.<ServerStreamTracer.Factory>emptyList(),
            TransportTracer.getDefaultFactory(), 1, // ignore
            1, // ignore
            1, // ignore
            1, // ignore
            1, // ignore
            1, 1, // ignore
            1, 1, // ignore
            true, 0, // ignore
            channelz);/*from   ww w .  j a v a2s . co  m*/
    ns.start(new ServerListener() {
        @Override
        public ServerTransportListener transportCreated(ServerTransport transport) {
            Channel channel = ((NettyServerTransport) transport).channel();
            WriteBufferWaterMark writeBufferWaterMark = channel.config()
                    .getOption(ChannelOption.WRITE_BUFFER_WATER_MARK);
            lowWaterMark.set(writeBufferWaterMark.low());
            highWaterMark.set(writeBufferWaterMark.high());

            countDownLatch.countDown();

            return new NoopServerTransportListener();
        }

        @Override
        public void serverShutdown() {
        }
    });

    Socket socket = new Socket();
    socket.connect(ns.getListenSocketAddress(), /* timeout= */ 8000);
    countDownLatch.await();
    socket.close();

    assertThat(lowWaterMark.get()).isEqualTo(originalLowWaterMark);
    assertThat(highWaterMark.get()).isEqualTo(originalHighWaterMark);

    ns.shutdown();
}

From source file:io.grpc.netty.Utils.java

License:Apache License

static InternalChannelz.SocketOptions getSocketOptions(Channel channel) {
    ChannelConfig config = channel.config();
    InternalChannelz.SocketOptions.Builder b = new InternalChannelz.SocketOptions.Builder();

    // The API allows returning null but not sure if it can happen in practice.
    // Let's be paranoid and do null checking just in case.
    Integer lingerSeconds = config.getOption(SO_LINGER);
    if (lingerSeconds != null) {
        b.setSocketOptionLingerSeconds(lingerSeconds);
    }/*from   ww w. ja va2  s  . c  o  m*/

    Integer timeoutMillis = config.getOption(SO_TIMEOUT);
    if (timeoutMillis != null) {
        // in java, SO_TIMEOUT only applies to receiving
        b.setSocketOptionTimeoutMillis(timeoutMillis);
    }

    for (Entry<ChannelOption<?>, Object> opt : config.getOptions().entrySet()) {
        ChannelOption<?> key = opt.getKey();
        // Constants are pooled, so there should only be one instance of each constant
        if (key.equals(SO_LINGER) || key.equals(SO_TIMEOUT)) {
            continue;
        }
        Object value = opt.getValue();
        // zpencer: Can a netty option be null?
        b.addOption(key.name(), String.valueOf(value));
    }

    NativeSocketOptions nativeOptions = NettySocketSupport.getNativeSocketOptions(channel);
    if (nativeOptions != null) {
        b.setTcpInfo(nativeOptions.tcpInfo); // may be null
        for (Entry<String, String> entry : nativeOptions.otherInfo.entrySet()) {
            b.addOption(entry.getKey(), entry.getValue());
        }
    }
    return b.build();
}

From source file:io.grpc.netty.UtilsTest.java

License:Apache License

@Test
public void channelOptionsTest_noLinger() {
    Channel channel = new EmbeddedChannel();
    assertNull(channel.config().getOption(ChannelOption.SO_LINGER));
    InternalChannelz.SocketOptions socketOptions = Utils.getSocketOptions(channel);
    assertNull(socketOptions.lingerSeconds);
}

From source file:io.grpc.netty.UtilsTest.java

License:Apache License

private static InternalChannelz.SocketOptions setAndValidateGeneric(Channel channel) {
    channel.config().setOption(ChannelOption.SO_LINGER, 3);
    // only applicable for OIO channels:
    channel.config().setOption(ChannelOption.SO_TIMEOUT, 250);
    // Test some arbitrarily chosen options with a non numeric values
    channel.config().setOption(ChannelOption.SO_KEEPALIVE, true);
    WriteBufferWaterMark writeBufWaterMark = new WriteBufferWaterMark(10, 20);
    channel.config().setOption(ChannelOption.WRITE_BUFFER_WATER_MARK, writeBufWaterMark);

    InternalChannelz.SocketOptions socketOptions = Utils.getSocketOptions(channel);
    assertEquals(3, (int) socketOptions.lingerSeconds);
    assertEquals("true", socketOptions.others.get("SO_KEEPALIVE"));
    assertEquals(writeBufWaterMark.toString(),
            socketOptions.others.get(ChannelOption.WRITE_BUFFER_WATER_MARK.toString()));
    return socketOptions;
}

From source file:io.hekate.network.netty.NettyServerClient.java

License:Apache License

private void pauseReceiver(boolean pause, Consumer<NetworkEndpoint<Object>> callback) {
    ChannelHandlerContext localCtx = this.handlerCtx;

    if (localCtx != null) {
        if (debug) {
            if (pause) {
                log.debug("Pausing inbound receiver [from={}, protocol={}]", address(), protocol);
            } else {
                log.debug("Resuming Pausing inbound receiver [from={}, protocol={}]", address(), protocol);
            }/*from www.ja va  2  s.  c  o  m*/
        }

        Channel channel = localCtx.channel();
        EventLoop eventLoop = channel.eventLoop();

        if (eventLoop.inEventLoop()) {
            channel.config().setAutoRead(!pause);

            notifyOnReceivePause(pause, callback, channel);
        } else {
            eventLoop.execute(() -> {
                channel.config().setAutoRead(!pause);

                notifyOnReceivePause(pause, callback, channel);
            });
        }
    } else if (callback != null) {
        callback.accept(this);
    }
}

From source file:io.nodyn.tcp.TCPWrap.java

License:Apache License

public void listen(int backlog) {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(this.process.getEventLoop().getEventLoopGroup());
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(new ChannelInitializer<Channel>() {
        @Override/* w  ww . java 2s.  c o m*/
        protected void initChannel(Channel ch) throws Exception {
            ch.config().setAutoRead(false);
            //                ch.pipeline().addLast("debug", new DebugHandler("server"));
            ch.pipeline().addLast("emit.connection",
                    new ConnectionEventHandler(TCPWrap.this.process, TCPWrap.this));
            ch.pipeline().addLast("handle", new UnrefHandler(TCPWrap.this));
        }
    });
    this.channelFuture = bootstrap.bind(this.addr, this.port);
    this.channelFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            // TODO callback error
        }
    });

    ref();
}

From source file:io.nodyn.tcp.TCPWrap.java

License:Apache License

public void connect(String addr, int port) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(this.process.getEventLoop().getEventLoopGroup());
    bootstrap.channel(NioSocketChannel.class);
    if (this.port >= 0) {
        if (this.addr != null) {
            bootstrap.localAddress(this.addr, this.port);
        } else {/* w ww.java  2  s .c  om*/
            bootstrap.localAddress(this.port);
        }
    }

    bootstrap.handler(new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.config().setAutoRead(false);
            //ch.pipeline().addLast("debug", new DebugHandler("client"));
            ch.pipeline().addLast("emit.afterConnect",
                    new AfterConnectEventHandler(TCPWrap.this.process, TCPWrap.this));
            ch.pipeline().addLast("emit.eof", new EOFEventHandler(TCPWrap.this.process, TCPWrap.this));
            ch.pipeline().addLast("handle", new UnrefHandler(TCPWrap.this));
        }
    });

    this.channelFuture = bootstrap.connect(addr, port);
    this.channelFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            // TODO callback error
        }
    });
    ref();
}