Example usage for io.netty.channel.oio OioEventLoopGroup OioEventLoopGroup

List of usage examples for io.netty.channel.oio OioEventLoopGroup OioEventLoopGroup

Introduction

In this page you can find the example usage for io.netty.channel.oio OioEventLoopGroup OioEventLoopGroup.

Prototype

public OioEventLoopGroup() 

Source Link

Document

Create a new OioEventLoopGroup with no limit in place.

Usage

From source file:cc.ly.mc.client.netty.SocketClient.java

License:Apache License

public void run() {
    // Configure the server.
    worker = new OioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(worker).channel(OioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.SO_REUSEADDR, true).handler(new SocketClientInitializer());
    // Start the client.
    channel = b.connect(host, port).channel();
}

From source file:cc.ly.mc.core.client.io.SocketClient.java

License:Apache License

public void run() {
    // Configure the server.
    worker = new OioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(worker).channel(OioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.SO_REUSEADDR, true)
            .handler(new SocketClientInitializer(connectedListeners, disconnectedListeners));
    // Start the client.
    channel = b.connect(host, port).channel();
}

From source file:com.android.tools.idea.diagnostics.crash.LocalTestServer.java

License:Apache License

public void start() throws Exception {
    ServerBootstrap b = new ServerBootstrap();
    myEventLoopGroup = new OioEventLoopGroup();
    b.group(myEventLoopGroup).channel(OioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override/*from   www. ja va2 s.  co m*/
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    p.addLast(new HttpServerCodec());
                    // Note: Netty's decompressor uses jcraft jzlib, which is not exported as a library
                    // p.addLast(new HttpContentDecompressor());
                    p.addLast(new HttpObjectAggregator(32 * 1024)); // big enough to collect a full thread dump
                    p.addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                            ctx.flush();
                        }

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            if (!(msg instanceof FullHttpRequest)) {
                                return;
                            }

                            FullHttpResponse response = myResponseSupplier.apply((FullHttpRequest) msg);
                            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
                            response.headers().set(HttpHeaderNames.CONTENT_LENGTH,
                                    response.content().readableBytes());
                            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                                throws Exception {
                            ctx.write(cause.toString()).addListener(ChannelFutureListener.CLOSE);
                        }
                    });
                }
            });

    myChannel = b.bind(myPort).sync().channel();
}

From source file:com.cloudhopper.smpp.impl.DefaultSmppServer.java

License:Apache License

/**
 * Creates a new default SmppServer. Window monitoring and automatic
 * expiration of requests will be disabled with no monitorExecutors.
 * A "cachedDaemonThreadPool" will be used for IO worker threads.
 * @param configuration The server configuration to create this server with
 * @param serverHandler The handler implementation for handling bind requests
 *      and creating/destroying sessions.
 *//* w w  w  . j ava  2 s  . c  o m*/
public DefaultSmppServer(SmppServerConfiguration configuration, SmppServerHandler serverHandler) {
    this(configuration, serverHandler, null,
            configuration.isNonBlockingSocketsEnabled() ? new NioEventLoopGroup() : new OioEventLoopGroup(),
            configuration.isNonBlockingSocketsEnabled() ? new NioEventLoopGroup() : new OioEventLoopGroup());
}

From source file:com.cmz.rxtx.RxtxClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {/*from  w ww .ja  v a2  s .com*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(RxtxChannel.class).handler(new ChannelInitializer<RxtxChannel>() {
            @Override
            public void initChannel(RxtxChannel ch) throws Exception {
                ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(),
                        new StringDecoder(), new RxtxClientHandler());
            }
        });

        ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();

        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.github.mrstampy.kitchensync.netty.Bootstrapper.java

License:Open Source License

/**
 * Gets the event loop group based upon the class name. If the class is
 * neither an <a href=//from  www .j  av  a 2s . com
 * "http://netty.io/4.0/api/io/netty/channel/socket/nio/NioDatagramChannel.html"
 * >NioDatagramChannel</a> nor <a href=
 * "http://netty.io/4.0/api/io/netty/channel/socket/oio/OioDatagramChannel.html"
 * >OioDatagramChannel</a> (or on Linux, <a href=
 * "http://netty.io/4.0/api/io/netty/channel/epoll/EpollDatagramChannel.html"
 * >EpollDatagramChannel</a>) then an exception is thrown.
 *
 * @param <CHANNEL>
 *          the generic type
 * @param clazz
 *          the clazz
 * @return the event loop group
 */
protected <CHANNEL extends DatagramChannel> EventLoopGroup getEventLoopGroup(Class<? extends CHANNEL> clazz) {
    if (NioDatagramChannel.class.equals(clazz))
        return new NioEventLoopGroup();

    if (OioDatagramChannel.class.equals(clazz))
        return new OioEventLoopGroup();

    // Linux only
    if (EpollDatagramChannel.class.equals(clazz))
        return new EpollEventLoopGroup();

    throw new UnsupportedOperationException("No default event loop group defined for " + clazz.getName());
}

From source file:com.hxr.javatone.concurrency.netty.official.rxtx.RxtxClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {/*  w w w .jav a  2s.  c o m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(RxtxChannel.class).handler(new ChannelInitializer<RxtxChannel>() {
            @Override
            public void initChannel(RxtxChannel ch) throws Exception {
                ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(),
                        new StringDecoder(), new RxtxClientHandler());
            }
        });

        ChannelFuture f = b.connect(new RxtxDeviceAddress("/dev/ttyUSB0")).sync();

        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.ning.http.client.providers.netty_4.NettyAsyncHttpProvider.java

License:Apache License

public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {

    if (config.getAsyncHttpProviderConfig() != null && NettyAsyncHttpProviderConfig.class
            .isAssignableFrom(config.getAsyncHttpProviderConfig().getClass())) {
        asyncHttpProviderConfig = NettyAsyncHttpProviderConfig.class.cast(config.getAsyncHttpProviderConfig());
    } else {/*from  w ww. java2s.  c o  m*/
        asyncHttpProviderConfig = new NettyAsyncHttpProviderConfig();
    }

    if (asyncHttpProviderConfig.isUseBlockingIO()) {
        socketChannelFactory = OioSocketChannel.class;
        this.allowReleaseSocketChannelFactory = true;
    } else {
        // check if external NioClientSocketChannelFactory is defined
        Class<? extends SocketChannel> scf = asyncHttpProviderConfig.getSocketChannel();
        if (scf != null) {
            this.socketChannelFactory = scf;

            // cannot allow releasing shared channel factory
            this.allowReleaseSocketChannelFactory = false;
        } else {
            socketChannelFactory = NioSocketChannel.class;
            eventLoop = asyncHttpProviderConfig.getEventLoopGroup();
            if (eventLoop == null) {
                if (socketChannelFactory == OioSocketChannel.class) {
                    eventLoop = new OioEventLoopGroup();
                } else if (socketChannelFactory == NioSocketChannel.class) {
                    eventLoop = new NioEventLoopGroup();
                } else {
                    throw new IllegalArgumentException(
                            "No set event loop compatbile with socket channel " + scf);
                }
            }
            int numWorkers = config.getIoThreadMultiplier() * Runtime.getRuntime().availableProcessors();
            log.debug("Number of application's worker threads is {}", numWorkers);
            this.allowReleaseSocketChannelFactory = true;
        }
    }
    plainBootstrap = new Bootstrap().channel(socketChannelFactory).group(eventLoop);
    secureBootstrap = new Bootstrap().channel(socketChannelFactory).group(eventLoop);
    ;
    webSocketBootstrap = new Bootstrap().channel(socketChannelFactory).group(eventLoop);
    ;
    secureWebSocketBootstrap = new Bootstrap().channel(socketChannelFactory).group(eventLoop);
    ;
    configureNetty();

    this.config = config;

    // This is dangerous as we can't catch a wrong typed ConnectionsPool
    ConnectionsPool<String, Channel> cp = (ConnectionsPool<String, Channel>) config.getConnectionsPool();
    if (cp == null && config.getAllowPoolingConnection()) {
        cp = new NettyConnectionsPool(this);
    } else if (cp == null) {
        cp = new NonConnectionsPool();
    }
    this.connectionsPool = cp;

    if (config.getMaxTotalConnections() != -1) {
        trackConnections = true;
        freeConnections = new Semaphore(config.getMaxTotalConnections());
    } else {
        trackConnections = false;
    }

    useRawUrl = config.isUseRawUrl();
}

From source file:com.whizzosoftware.wzwave.controller.netty.NettyZWaveController.java

License:Open Source License

public void start() {
    if (channel == null) {
        // set up Netty bootstrap
        bootstrap = new Bootstrap();
        bootstrap.group(new OioEventLoopGroup());
        bootstrap.channel(RxtxChannel.class);
        bootstrap.handler(new ChannelInitializer<RxtxChannel>() {
            @Override/*  ww  w  . j  a  v  a2 s  .c o  m*/
            protected void initChannel(RxtxChannel channel) throws Exception {
                NettyZWaveController.this.channel = channel;
                channel.config().setBaudrate(115200);
                channel.config().setDatabits(RxtxChannelConfig.Databits.DATABITS_8);
                channel.config().setParitybit(RxtxChannelConfig.Paritybit.NONE);
                channel.config().setStopbits(RxtxChannelConfig.Stopbits.STOPBITS_1);
                channel.pipeline().addLast("decoder", new ZWaveFrameDecoder());
                channel.pipeline().addLast("ack", new ACKInboundHandler());
                channel.pipeline().addLast("encoder", new ZWaveFrameEncoder());
                channel.pipeline().addLast("writeQueue", new FrameQueueHandler());
                channel.pipeline().addLast("transaction", new TransactionInboundHandler());
                channel.pipeline().addLast("handler", inboundHandler);
            }
        });

        bootstrap.connect(new RxtxDeviceAddress(serialPort)).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    sendDataFrame(new Version());
                    sendDataFrame(new MemoryGetId());
                    sendDataFrame(new InitData());
                } else {
                    onZWaveConnectionFailure(future.cause());
                }
            }
        });
    }
}

From source file:controllers.WebSocketClient.java

License:Apache License

public void run(final ByteBuf bytes) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {//w w w . j a  v a 2s  .  c  om
        Bootstrap b = new Bootstrap();
        String protocol = uri.getScheme();
        if (!"ws".equals(protocol)) {
            throw new IllegalArgumentException("Unsupported protocol: " + protocol);
        }

        HttpHeaders customHeaders = new DefaultHttpHeaders();
        //customHeaders.add("MyHeader", "MyValue");

        // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
        // If you change it to V00, ping is not supported and remember to change
        // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
        final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory
                .newHandshaker(uri, WebSocketVersion.V13, null, false, customHeaders));

        b.group(group).channel(OioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("http-codec", new HttpClientCodec());
                pipeline.addLast("aggregator", new HttpObjectAggregator(8192));
                pipeline.addLast("ws-handler", handler);
            }
        });

        // connect
        Channel ch = b.connect(uri.getHost(), uri.getPort()).sync().channel();
        handler.handshakeFuture().sync();

        for (int i = 0; i < 10; i++) {
            //ch.writeAndFlush(new BinaryWebSocketFrame(bytes.copy()));
            ch.writeAndFlush(new TextWebSocketFrame("Test Message #" + i));
        }

        ch.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}