List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup
public NioEventLoopGroup()
From source file:at.yawk.accordion.netty.NettyConnector.java
License:Mozilla Public License
@Override public Server listen(SocketAddress inf) { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, BACKLOG_VALUE).childOption(ChannelOption.SO_KEEPALIVE, true); NettyServer server = new NettyServer(bootstrap, inf); server.init();//w w w .jav a 2s .c om return server; }
From source file:at.yawk.dbus.protocol.DbusConnector.java
/** * Connect to the dbus server at the given {@link SocketAddress}. *//*from w w w . jav a 2s. c om*/ public DbusChannel connect(SocketAddress address) throws Exception { Bootstrap localBootstrap = bootstrap.clone(); if (address instanceof DomainSocketAddress) { localBootstrap.group(new EpollEventLoopGroup()); localBootstrap.channel(EpollDomainSocketChannel.class); } else { localBootstrap.group(new NioEventLoopGroup()); localBootstrap.channel(NioSocketChannel.class); } Channel channel = localBootstrap.connect(address).sync().channel(); AuthClient authClient = new AuthClient(); if (LoggingInboundAdapter.isEnabled()) { channel.pipeline().addLast(new LoggingInboundAdapter()); } channel.pipeline().addLast("auth", authClient); channel.config().setAutoRead(true); log.trace("Pipeline is now {}", channel.pipeline()); // I really don't get why dbus does this channel.write(Unpooled.wrappedBuffer(new byte[] { 0 })); if (authMechanism == null) { authMechanism = new ExternalAuthMechanism(); } CompletionStage<?> completionPromise = authClient.startAuth(channel, authMechanism); SwappableMessageConsumer swappableConsumer = new SwappableMessageConsumer(initialConsumer); completionPromise.toCompletableFuture().thenRun(() -> { channel.pipeline().replace("auth", "main", new DbusMainProtocol(swappableConsumer)); log.trace("Pipeline is now {}", channel.pipeline()); }).get(); DbusChannelImpl dbusChannel = new DbusChannelImpl(channel, swappableConsumer); dbusChannel.write(MessageFactory.methodCall("/", "org.freedesktop.DBus", "org.freedesktop.DBus", "Hello")); return dbusChannel; }
From source file:at.yawk.votifier.VotifierServerImpl.java
License:Mozilla Public License
public void init() { if (!initialized.compareAndSet(false, true)) { return;/*from w w w . j av a 2 s . com*/ } logger.info("Initializing votifier server..."); EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { logger.fine("Client disconnected."); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { handleError(cause); } @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new VoteDecrypter(key)).addLast(new LineSplitter()) .addLast(new VoteDecoder()).addLast(new VersionEncoder()) .addLast(new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof Operation)) { return; } Operation operation = (Operation) msg; if (operation.getOperation().equals("VOTE")) { listener.accept( new VoteEvent(operation.getUsername(), operation.getService(), operation.getAddress(), operation.getTimestamp())); ctx.channel().close(); } else { throw new UnsupportedOperationException(operation.getOperation()); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { handleError(cause); } }); logger.info("Client connected: Sending version packet."); ch.writeAndFlush(version); } }); }
From source file:basic.TimeClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO// w w w .ja v a 2 s .c o m EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeClientHandler()); } }); // ?? ChannelFuture f = b.connect(host, port).sync(); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:basic.TimeServer.java
License:Apache License
public void bind(int port) throws Exception { // ??NIO/* w w w .j a v a2 s . c om*/ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler()); // ??? ChannelFuture f = b.bind(port).sync(); // ??? f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:be.mil.ChatServer.netty.server.TCPServer.java
License:Apache License
public void start() { EventLoopGroup producer = new NioEventLoopGroup(); EventLoopGroup consumer = new NioEventLoopGroup(); try {//from w ww . jav a2s. c o m Bootstrap bootstrapClient = new Bootstrap().group(new NioEventLoopGroup()) .channel(NioSocketChannel.class).handler(new ClientAdapterInitializer()); ServerBootstrap bootstrap = new ServerBootstrap().group(producer, consumer) .channel(NioServerSocketChannel.class).childHandler(serverAdapterInitializer); System.out.println("Server started"); bootstrap.bind(port).sync().channel().closeFuture().sync(); try { channel = bootstrapClient.connect(server, port).sync().channel(); try { channel.write("Hi\n"); channel.write("Hi\n"); channel.flush(); } catch (Exception e) { e.printStackTrace(); } finally { bootstrap.group().shutdownGracefully(); } } catch (InterruptedException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { producer.shutdownGracefully(); consumer.shutdownGracefully(); } }
From source file:be.yildizgames.module.network.netty.factory.NettyFactory.java
License:MIT License
/** * Create a new client for Netty.//ww w . j ava 2 s .c om * * @return A client implementation. */ static Client createClientNetty() { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class); ClientNetty client = new WebSocketClientNetty(bootstrap); bootstrap.handler( new NettyChannelInitializer(new SimpleClientHandlerFactory(client, DecoderEncoder.WEBSOCKET))); return client; }
From source file:be.yildizgames.module.network.netty.factory.NettyFactory.java
License:MIT License
/** * Create a new client for Netty./*from ww w . j a v a 2 s. com*/ * * @return A client implementation. */ static Client createSimpleClientNetty() { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class); ClientNetty client = new SimpleClientNetty(bootstrap); bootstrap.handler( new NettyChannelInitializer(new SimpleClientHandlerFactory(client, DecoderEncoder.STRING))); return client; }
From source file:be.yildizgames.module.network.netty.server.ServerNetty.java
License:MIT License
/** * Create a new Netty server./* w w w. j a v a 2 s . c o m*/ * */ //@requires bootstrap != null. //@requires port > 0 < 65535. private ServerNetty() { super(); EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); this.bootstrap = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class); this.bootstrap.option(ChannelOption.TCP_NODELAY, true); this.bootstrap.option(ChannelOption.SO_KEEPALIVE, true); }
From source file:bean.lee.demo.netty.learn.http.file.HttpStaticFileServer.java
License:Apache License
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*w ww.jav a 2s . co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new HttpStaticFileServerInitializer()); b.bind(port).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }