@Override public void close() { channel.close().syncUninterruptibly(); group.shutdownGracefully(); }
/** * Reset the command-handler to the initial not-connected state. */ public void initialState() { commandBuffer.clear(); Channel currentChannel = this.channel; if (currentChannel != null) { ChannelFuture close = currentChannel.close(); if (currentChannel.isOpen()) { close.syncUninterruptibly(); } } }
@Override public void close() { Channel rawChannel = rawChannel(); if ( rawChannel.isOpen() ) { rawChannel.close().syncUninterruptibly(); } }
@Override public void start() { LOG.info("binding to port {}", localAddress); parentChannel = bootstrap.bind(localAddress).syncUninterruptibly().channel(); // Port changes after bind() if the original port was 0, update // localAddress to get the real port. localAddress = (InetSocketAddress) parentChannel.localAddress(); LOG.info("bound to port " + getLocalPort()); }
public void reconfigure(InetSocketAddress addr) { Channel oldChannel = parentChannel; try { LOG.info("binding to port {}", addr); parentChannel = bootstrap.bind(addr).syncUninterruptibly().channel(); // Port changes after bind() if the original port was 0, update // localAddress to get the real port. localAddress = (InetSocketAddress) parentChannel.localAddress(); LOG.info("bound to port " + getLocalPort()); } catch (Exception e) { LOG.error("Error while reconfiguring", e); } finally { oldChannel.close(); } }
@Override public void disconnect() { if (channel != null) { Channel nettyChannel = channel.channel(); if (nettyChannel != null) { nettyChannel.close().syncUninterruptibly(); } } }
@Override public void disconnect() { if (channel != null) { Channel nettyChannel = channel.channel(); if (nettyChannel != null) { nettyChannel.close().syncUninterruptibly(); } } }
public RpcResponseState send(String key, RpcCmd cmd) throws RpcException { Channel channel = getChannel(key); ChannelFuture future = channel.writeAndFlush(cmd).syncUninterruptibly(); return future.isSuccess() ? RpcResponseState.success : RpcResponseState.fail; }
@Override public void stop() { final Channel channel = channelReference.get(); if (channel != null) { channel.close(); channel.closeFuture().syncUninterruptibly(); } childChannels.close().syncUninterruptibly(); if (childEventLoopGroup != null) { childEventLoopGroup.shutdownGracefully(); } bootstrap = null; }
public ChannelFuture start(InetSocketAddress address) { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(group) .channel(NioServerSocketChannel.class) .childHandler(createInitializer(channelGroup)); ChannelFuture future = bootstrap.bind(address); future.syncUninterruptibly(); channel = future.channel(); return future; }
private void doStop() { if (future != null) { future.channel().close().syncUninterruptibly(); future = null; } if (group != null) { Future<?> groupFuture = group.shutdownGracefully(0, 0, TimeUnit.SECONDS); try { groupFuture.get(); } catch (Exception e) { throw new IllegalStateException(e); } group = null; } } }
public Channel bind() { return bootstrap.bind().syncUninterruptibly().channel(); }
/** * Listing 8.3 Incompatible Channel and EventLoopGroup * */ public void bootstrap() { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(OioSocketChannel.class) .handler(new SimpleChannelInboundHandler<ByteBuf>() { @Override protected void channelRead0( ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { System.out.println("Received data"); } }); ChannelFuture future = bootstrap.connect( new InetSocketAddress("www.manning.com", 80)); future.syncUninterruptibly(); } }
@Override public void run() { try { InetSocketAddress inetSocketAddress; if (Strings.isNullOrEmpty(localBoundIP)) { inetSocketAddress = new InetSocketAddress(portToBind); } else { inetSocketAddress = new InetSocketAddress(localBoundIP, portToBind); } serverBootstrap .bind(inetSocketAddress) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { channelOpened.set(future.channel()); } else { channelOpened.setException(future.cause()); } } }) .channel().closeFuture().syncUninterruptibly(); } catch (Exception e) { throw new RuntimeException("Exception while binding MockServer to port " + portToBind, e); } } }, "MockServer thread for port: " + portToBind).start();
@Override void cleanup() { connectLock.lock(); try { if (connectFuture != null) { connectFuture.cancel(false); connectFuture = null; } if (channel != null) { channel.close().syncUninterruptibly(); channel = null; } } finally { connectLock.unlock(); } Iterator<Packet> iter = outgoingQueue.iterator(); while (iter.hasNext()) { Packet p = iter.next(); if (p == WakeupPacket.getInstance()) { iter.remove(); } } }
public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Please give port as argument"); System.exit(1); } int port = Integer.parseInt(args[0]); final ChatServer endpoint = new ChatServer(); ChannelFuture future = endpoint.start( new InetSocketAddress(port)); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { endpoint.destroy(); } }); future.channel().closeFuture().syncUninterruptibly(); } }
channelFuture.syncUninterruptibly(); channel = channelFuture.channel();
@Override public void launch(final MessageInput input) throws MisfireException { try { bootstrap = getBootstrap(input); bootstrap.bind(socketAddress) .addListener(new InputLaunchListener(channelReference, input, getRecvBufferSize())) .syncUninterruptibly(); } catch (Exception e) { throw new MisfireException(e); } }
public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Please give port as argument"); System.exit(1); } int port = Integer.parseInt(args[0]); SelfSignedCertificate cert = new SelfSignedCertificate(); SslContext context = SslContext.newServerContext( cert.certificate(), cert.privateKey()); final SecureChatServer endpoint = new SecureChatServer(context); ChannelFuture future = endpoint.start(new InetSocketAddress(port)); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { endpoint.destroy(); } }); future.channel().closeFuture().syncUninterruptibly(); } }
@Override public void launch(final MessageInput input) throws MisfireException { try { bootstrap = getBootstrap(input); final NettyTransportType transportType = nettyTransportConfiguration.getType(); int numChannels = (transportType == NettyTransportType.EPOLL || transportType == NettyTransportType.KQUEUE) ? workerThreads : 1; for (int i = 0; i < numChannels; i++) { LOG.debug("Starting channel on {}", socketAddress); bootstrap.bind(socketAddress) .addListener(new InputLaunchListener(channels, input, getRecvBufferSize())) .syncUninterruptibly(); } } catch (Exception e) { throw new MisfireException(e); } }