Example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

Introduction

In this page you can find the example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup.

Prototype

public NioEventLoopGroup() 

Source Link

Document

Create a new instance using the default number of threads, the default ThreadFactory and the SelectorProvider which is returned by SelectorProvider#provider() .

Usage

From source file:com.agrn.senqm.network.connection.Connection.java

License:Open Source License

public Connection(String host) {
    this.host = host;

    workerGroup = new NioEventLoopGroup();
    bootstrap = getBootstrap(workerGroup);

    connect();
}

From source file:com.ahanda.techops.noty.clientTest.Client.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (args.length == 1) {
        System.setProperty("PINT.conf", args[0]);
    }//from  ww  w  .ja v a  2 s.  c o m

    if (System.getProperty("PINT.conf") == null)
        throw new IllegalArgumentException();

    JsonNode config = null;
    Config cf = Config.getInstance();
    cf.setupConfig();

    String scheme = "http";
    String host = cf.getHttpHost();
    int port = cf.getHttpPort();

    if (port == -1) {
        port = 8080;
    }

    if (!"http".equalsIgnoreCase(scheme)) {
        l.warn("Only HTTP is supported.");
        return;
    }

    // Configure SSL context if necessary.
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new HttpClientCodec());
                // p.addLast( "decoder", new HttpResponseDecoder());
                // p.addLast( "encoder", new HttpRequestEncoder());

                // Remove the following line if you don't want automatic content decompression.
                // p.addLast(new HttpContentDecompressor());

                // Uncomment the following line if you don't want to handle HttpContents.
                p.addLast(new HttpObjectAggregator(10485760));

                p.addLast(new ClientHandler());
            }
        });

        // Make the connection attempt.
        Channel ch = b.connect(host, port).sync().channel();

        ClientHandler client = new ClientHandler();
        client.login(ch, ClientHandler.credential);
        // ClientHandler.pubEvent( ch, ClientHandler.event );

        // Wait for the server to close the connection.
        ch.closeFuture().sync();
        l.info("Closing Client side Connection !!!");
    } finally {
        // Shut down executor threads to exit.
        group.shutdownGracefully();
    }
}

From source file:com.alexkasko.netty.ftp.FtpServerTest.java

License:Apache License

@Test
public void test() throws IOException, InterruptedException {
    final DefaultCommandExecutionTemplate defaultCommandExecutionTemplate = new DefaultCommandExecutionTemplate(
            new ConsoleReceiver());
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {

                @Override//  www. j  a  v  a2  s. com
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipe = ch.pipeline();
                    pipe.addLast("decoder", new CrlfStringDecoder());
                    pipe.addLast("handler", new FtpServerHandler(defaultCommandExecutionTemplate));
                }

            });
    b.localAddress(2121).bind();
    FTPClient client = new FTPClient();
    //        https://issues.apache.org/jira/browse/NET-493

    client.setBufferSize(0);
    client.connect("127.0.0.1", 2121);
    assertEquals(230, client.user("anonymous"));

    // active
    assertTrue(client.setFileType(FTP.BINARY_FILE_TYPE));
    assertEquals("/", client.printWorkingDirectory());
    assertTrue(client.changeWorkingDirectory("/foo"));
    assertEquals("/foo", client.printWorkingDirectory());
    assertTrue(client.listFiles("/foo").length == 0);
    assertTrue(client.storeFile("bar", new ByteArrayInputStream("content".getBytes())));
    assertTrue(client.rename("bar", "baz"));
    //  assertTrue(client.deleteFile("baz"));

    // passive
    assertTrue(client.setFileType(FTP.BINARY_FILE_TYPE));
    client.enterLocalPassiveMode();
    assertEquals("/foo", client.printWorkingDirectory());
    assertTrue(client.changeWorkingDirectory("/foo"));
    assertEquals("/foo", client.printWorkingDirectory());

    //TODO make a virtual filesystem that would work with directory
    //assertTrue(client.listFiles("/foo").length==1);

    assertTrue(client.storeFile("bar", new ByteArrayInputStream("content".getBytes())));
    assertTrue(client.rename("bar", "baz"));
    // client.deleteFile("baz");

    assertEquals(221, client.quit());
    try {
        client.noop();
        fail("Should throw exception");
    } catch (IOException e) {
        //expected;
    }

}

From source file:com.alexkasko.netty.ftp.StartServer.java

License:Apache License

public static void main(String... args) throws Exception {
    final DefaultCommandExecutionTemplate defaultCommandExecutionTemplate = new DefaultCommandExecutionTemplate(
            new ConsoleReceiver());

    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {

                @Override/*w w w. jav  a2s.  c  o m*/
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipe = ch.pipeline();
                    pipe.addLast("decoder", new CrlfStringDecoder());
                    pipe.addLast("handler", new FtpServerHandler(defaultCommandExecutionTemplate));
                }

            });
    b.localAddress(2121).bind().channel().closeFuture().sync();
}

From source file:com.allanbank.mongodb.netty.NettyTransportFactory.java

License:Apache License

/**
 * Creates a new NettyTransportFactory.
 */
public NettyTransportFactory() {
    this(new NioEventLoopGroup(), PooledByteBufAllocator.DEFAULT);
}

From source file:com.alltobid.quotabid.BidClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    bidClientHandler.addListener(new BidClientHandlerAdapter() {
        @Override//  ww  w. j a v a  2s.  c  o  m
        public void messageReceived(ReceivedMessage receivedMessage) {
            receivedMessageQueue.add(receivedMessage);
        }
    });

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new BidClientInitializer());

        MonitorThread monitorThread = new MonitorThread();
        monitorThread.setName("BidClient Monitor");
        monitorThread.start();

        while (true) {
            try {
                // Start the client.
                Channel ch = b.connect(HOST, PORT).sync().channel();

                // Wait until the connection is closed.
                ch.closeFuture().sync();
            } catch (Exception ex) {
                logger.error("Channel Error", ex);
                Thread.sleep(5000);
            }
        }
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:com.ancun.netty.common.NettyBootstrapFactory.java

License:Apache License

private Bootstrap newNioClientBootstrap() {
    workerGroup = new NioEventLoopGroup();

    return new Bootstrap().group(workerGroup).channel(NioSocketChannel.class);
}

From source file:com.andrewkroh.cisco.rtp.NettyRtpSessionTest.java

License:Apache License

/**
 * Creates the test client and binds it to a random IPv4 address.
 *//*from  w w w . j a  v a2 s .c  om*/
@Before
public void beforeTest() throws InterruptedException, UnknownHostException {
    clientHandler = new TestHandler();
    clientBootstrap = new Bootstrap();
    clientBootstrap.group(new NioEventLoopGroup()).channel(NioDatagramChannel.class)
            .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.IP_MULTICAST_IF, multicastInterface)
            .localAddress(TestUtils.getFreePort()).handler(clientHandler);

    clientChannel = (DatagramChannel) clientBootstrap.bind().sync().channel();

    rtpPacket = new RtpPacket();
    rtpPacket.setPayloadType(PAYLOAD_TYPE);
    rtpPacket.setRtpPayloadData(new byte[] { PACKET_PAYLOAD_DATA });
    rtpPacket.setTimestamp(TIMESTAMP);
}

From source file:com.andrewkroh.cisco.xmlservices.DefaultXmlPushService.java

License:Apache License

@Override
protected void doStart() {
    ChannelInboundHandler handler = new XmlResponseChannelHandler(PHONE_KEY, PUSH_RESP_KEY);

    bootstrap = new Bootstrap();
    bootstrap.group(new NioEventLoopGroup()).channel(NioSocketChannel.class)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeoutMs)
            .handler(new HttpClientInitializer(handler)).validate();

    notifyStarted();/*w w  w  .  ja v  a2  s .c  o  m*/

    LOGGER.info("Service is now STARTED.");
}

From source file:com.antsdb.saltedfish.server.mysql.MysqlClient.java

License:Open Source License

public static void start() {
    if (client == null) {
        synchronized (MysqlClient.class) {
            if (client == null) {
                client = new MysqlClient();
            }//from  w ww . jav  a  2  s.  c  om
        }
    }
    if (client != null) {
        synchronized (isRunning) {
            if (!isRunning) {
                NioEventLoopGroup pool = new NioEventLoopGroup();
                try {
                    client.run(pool, new MysqlClientHandler());
                    isRunning = true;
                } catch (Exception e) {
                    _log.error("", e);
                    throw new CodingError("Failed to start replication");
                }
            }
        }
    }

}