public void run() throws Exception { final ThreadFactory connectFactory = new DefaultThreadFactory("rendezvous"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER); try { final Bootstrap bootstrap = new Bootstrap(); bootstrap.group(connectGroup) .channelFactory(NioUdtProvider.BYTE_RENDEZVOUS) .handler(new ChannelInitializer<UdtChannel>() { @Override protected void initChannel(UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new ByteEchoPeerHandler(messageSize)); } }); final ChannelFuture future = bootstrap.connect(peerAddress, myAddress).sync(); future.channel().closeFuture().sync(); } finally { connectGroup.shutdownGracefully(); } }
/** * verify factory */ @Test public void provideFactory() { // bytes assertNotNull(NioUdtProvider.BYTE_ACCEPTOR.newChannel()); assertNotNull(NioUdtProvider.BYTE_CONNECTOR.newChannel()); assertNotNull(NioUdtProvider.BYTE_RENDEZVOUS.newChannel()); // message assertNotNull(NioUdtProvider.MESSAGE_ACCEPTOR.newChannel()); assertNotNull(NioUdtProvider.MESSAGE_CONNECTOR.newChannel()); assertNotNull(NioUdtProvider.MESSAGE_RENDEZVOUS.newChannel()); // acceptor types assertTrue(NioUdtProvider.BYTE_ACCEPTOR.newChannel() instanceof UdtServerChannel); assertTrue(NioUdtProvider.MESSAGE_ACCEPTOR.newChannel() instanceof UdtServerChannel); }
public void run() throws Exception { final ThreadFactory connectFactory = new UtilThreadFactory("rendezvous"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER); try { final Bootstrap bootstrap = new Bootstrap(); bootstrap.group(connectGroup) .channelFactory(NioUdtProvider.BYTE_RENDEZVOUS) .handler(new ChannelInitializer<UdtChannel>() { @Override protected void initChannel(UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new ByteEchoPeerHandler(messageSize)); } }); final ChannelFuture future = bootstrap.connect(peerAddress, myAddress).sync(); future.channel().closeFuture().sync(); } finally { connectGroup.shutdownGracefully(); } }
/** * verify factory */ @Test public void provideFactory() { EventLoop loop = new NioEventLoopGroup().next(); EventLoopGroup childGroup = new NioEventLoopGroup(); // bytes assertNotNull(NioUdtProvider.BYTE_ACCEPTOR.newChannel(loop, childGroup)); assertNotNull(NioUdtProvider.BYTE_CONNECTOR.newChannel(loop)); assertNotNull(NioUdtProvider.BYTE_RENDEZVOUS.newChannel(loop)); // message assertNotNull(NioUdtProvider.MESSAGE_ACCEPTOR.newChannel(loop, childGroup)); assertNotNull(NioUdtProvider.MESSAGE_CONNECTOR.newChannel(loop)); assertNotNull(NioUdtProvider.MESSAGE_RENDEZVOUS.newChannel(loop)); // acceptor types assertTrue(NioUdtProvider.BYTE_ACCEPTOR.newChannel(loop, childGroup) instanceof UdtServerChannel); assertTrue(NioUdtProvider.MESSAGE_ACCEPTOR.newChannel(loop, childGroup) instanceof UdtServerChannel); }
@Override public ChannelFactory<? extends Channel> getChannelFactory() { if (CC.mp.net.tcpGateway()) return super.getChannelFactory(); if (CC.mp.net.udtGateway()) return NioUdtProvider.BYTE_CONNECTOR; if (CC.mp.net.sctpGateway()) return NioSctpChannel::new; return super.getChannelFactory(); }
@Override public SelectorProvider getSelectorProvider() { if (CC.mp.net.tcpGateway()) return super.getSelectorProvider(); if (CC.mp.net.udtGateway()) return NioUdtProvider.BYTE_PROVIDER; if (CC.mp.net.sctpGateway()) return super.getSelectorProvider(); return super.getSelectorProvider(); }
@Override public ChannelFactory<? extends ServerChannel> getChannelFactory() { if (CC.mp.net.tcpGateway()) return super.getChannelFactory(); if (CC.mp.net.udtGateway()) return NioUdtProvider.BYTE_ACCEPTOR; if (CC.mp.net.sctpGateway()) return NioSctpServerChannel::new; return super.getChannelFactory(); }
public static void main(String[] args) throws Exception { // Configure the client. final ThreadFactory connectFactory = new DefaultThreadFactory("connect"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER); try { final Bootstrap boot = new Bootstrap(); boot.group(connectGroup) .channelFactory(NioUdtProvider.MESSAGE_CONNECTOR) .handler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new MsgEchoClientHandler()); } }); // Start the client. final ChannelFuture f = boot.connect(HOST, PORT).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. connectGroup.shutdownGracefully(); } }
public static void main(String[] args) throws Exception { final ThreadFactory acceptFactory = new DefaultThreadFactory("accept"); final ThreadFactory connectFactory = new DefaultThreadFactory("connect"); final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.MESSAGE_PROVIDER); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER); // Configure the server. try { final ServerBootstrap boot = new ServerBootstrap(); boot.group(acceptGroup, connectGroup) .channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR) .option(ChannelOption.SO_BACKLOG, 10) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new MsgEchoServerHandler()); } }); // Start the server. final ChannelFuture future = boot.bind(PORT).sync(); // Wait until the server socket is closed. future.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. acceptGroup.shutdownGracefully(); connectGroup.shutdownGracefully(); } }
public static void main(String[] args) throws Exception { // Configure the client. final ThreadFactory connectFactory = new DefaultThreadFactory("connect"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER); try { final Bootstrap boot = new Bootstrap(); boot.group(connectGroup) .channelFactory(NioUdtProvider.BYTE_CONNECTOR) .handler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new ByteEchoClientHandler()); } }); // Start the client. final ChannelFuture f = boot.connect(HOST, PORT).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. connectGroup.shutdownGracefully(); } }
public static void main(String[] args) throws Exception { final ThreadFactory acceptFactory = new DefaultThreadFactory("accept"); final ThreadFactory connectFactory = new DefaultThreadFactory("connect"); final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER); // Configure the server. try { final ServerBootstrap boot = new ServerBootstrap(); boot.group(acceptGroup, connectGroup) .channelFactory(NioUdtProvider.BYTE_ACCEPTOR) .option(ChannelOption.SO_BACKLOG, 10) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new ByteEchoServerHandler()); } }); // Start the server. final ChannelFuture future = boot.bind(PORT).sync(); // Wait until the server socket is closed. future.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. acceptGroup.shutdownGracefully(); connectGroup.shutdownGracefully(); } }
public void run() throws Exception { // Configure the peer. final ThreadFactory connectFactory = new DefaultThreadFactory("rendezvous"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER); try { final Bootstrap boot = new Bootstrap(); boot.group(connectGroup) .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS) .handler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new MsgEchoPeerHandler(messageSize)); } }); // Start the peer. final ChannelFuture f = boot.connect(peer, self).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. connectGroup.shutdownGracefully(); } }
@Override protected Future<?> execute() { Bootstrap cb = new Bootstrap().group(proxyServer.getProxyToServerWorkerFor(transportProtocol)); switch (transportProtocol) { case TCP: LOG.debug("Connecting to server with TCP"); cb.channelFactory(new ChannelFactory<Channel>() { @Override public Channel newChannel() { return new NioSocketChannel(); } }); break; case UDT: LOG.debug("Connecting to server with UDT"); cb.channelFactory(NioUdtProvider.BYTE_CONNECTOR) .option(ChannelOption.SO_REUSEADDR, true); break; default: throw new UnknownTransportProtocolException(transportProtocol); } cb.handler(new ChannelInitializer<Channel>() { protected void initChannel(Channel ch) throws Exception { initChannelPipeline(ch.pipeline(), initialRequest); }; }); cb.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, proxyServer.getConnectTimeout()); if (localAddress != null) { return cb.connect(remoteAddress, localAddress); } else { return cb.connect(remoteAddress); } }
/** * Determines if UDT is available on the classpath. * * @return true if UDT is available */ public static boolean isUdtAvailable() { try { return NioUdtProvider.BYTE_PROVIDER != null; } catch (NoClassDefFoundError e) { return false; } }
@Override protected Future<?> execute() { Bootstrap cb = new Bootstrap().group(proxyServer .getProxyToServerWorkerFor(transportProtocol)); switch (transportProtocol) { case TCP: LOG.debug("Connecting to server with TCP"); cb.channelFactory(new ChannelFactory<Channel>() { @Override public Channel newChannel() { return new NioSocketChannel(); } }); break; case UDT: LOG.debug("Connecting to server with UDT"); cb.channelFactory(NioUdtProvider.BYTE_CONNECTOR) .option(ChannelOption.SO_REUSEADDR, true); break; default: throw new UnknownTransportProtocolError(transportProtocol); } cb.handler(new ChannelInitializer<Channel>() { protected void initChannel(Channel ch) throws Exception { initChannelPipeline(ch.pipeline(), initialRequest); }; }); cb.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, proxyServer.getConnectTimeout()); if (localAddress != null) { return cb.connect(remoteAddress, localAddress); } else { return cb.connect(remoteAddress); } }
private void initializeTransport(TransportProtocol transportProtocol) { SelectorProvider selectorProvider = null; switch (transportProtocol) { case TCP: selectorProvider = SelectorProvider.provider(); break; case UDT: selectorProvider = NioUdtProvider.BYTE_PROVIDER; break; default: throw new UnknownTransportProtocolError(transportProtocol); } NioEventLoopGroup inboundAcceptorGroup = new NioEventLoopGroup( INCOMING_ACCEPTOR_THREADS, new CategorizedThreadFactory("ClientToProxyAcceptor"), selectorProvider); NioEventLoopGroup inboundWorkerGroup = new NioEventLoopGroup( INCOMING_WORKER_THREADS, new CategorizedThreadFactory("ClientToProxyWorker"), selectorProvider); inboundWorkerGroup.setIoRatio(90); NioEventLoopGroup outboundWorkerGroup = new NioEventLoopGroup( OUTGOING_WORKER_THREADS, new CategorizedThreadFactory("ProxyToServerWorker"), selectorProvider); outboundWorkerGroup.setIoRatio(90); this.clientToProxyBossPools.put(transportProtocol, inboundAcceptorGroup); this.clientToProxyWorkerPools.put(transportProtocol, inboundWorkerGroup); this.proxyToServerWorkerPools.put(transportProtocol, outboundWorkerGroup); }
@Override public void run() { final Bootstrap boot = new Bootstrap(); final ThreadFactory clientFactory = new DefaultThreadFactory("client"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, clientFactory, NioUdtProvider.BYTE_PROVIDER); try { boot.group(connectGroup) .channelFactory(NioUdtProvider.BYTE_CONNECTOR) .handler(new ChannelInitializer<UdtChannel>() { @Override protected void initChannel(final UdtChannel ch) throws Exception { final ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast("decoder", new StringDecoder( CharsetUtil.UTF_8)); pipeline.addLast("encoder", new StringEncoder( CharsetUtil.UTF_8)); pipeline.addLast("handler", new ClientHandler()); } }); channel = boot.connect(host, port).sync().channel(); isRunning = true; log.info("Client ready."); waitForRunning(false); log.info("Client closing..."); channel.close().sync(); isShutdown = true; log.info("Client is done."); } catch (final Throwable e) { log.error("Client failed.", e); } finally { connectGroup.shutdownGracefully().syncUninterruptibly(); } }
public void run() throws Exception { // Configure the client. final ThreadFactory connectFactory = new UtilThreadFactory("connect"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER); try { final Bootstrap boot = new Bootstrap(); boot.group(connectGroup) .channelFactory(NioUdtProvider.MESSAGE_CONNECTOR) .handler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new MsgEchoClientHandler(messageSize)); } }); // Start the client. final ChannelFuture f = boot.connect(host, port).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. connectGroup.shutdownGracefully(); } }
public void run() throws Exception { final ThreadFactory acceptFactory = new UtilThreadFactory("accept"); final ThreadFactory connectFactory = new UtilThreadFactory("connect"); final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.MESSAGE_PROVIDER); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER); // Configure the server. try { final ServerBootstrap boot = new ServerBootstrap(); boot.group(acceptGroup, connectGroup) .channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR) .option(ChannelOption.SO_BACKLOG, 10) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new MsgEchoServerHandler()); } }); // Start the server. final ChannelFuture future = boot.bind(port).sync(); // Wait until the server socket is closed. future.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. acceptGroup.shutdownGracefully(); connectGroup.shutdownGracefully(); } }
public void run() throws Exception { // Configure the client. final ThreadFactory connectFactory = new UtilThreadFactory("connect"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER); try { final Bootstrap boot = new Bootstrap(); boot.group(connectGroup) .channelFactory(NioUdtProvider.BYTE_CONNECTOR) .handler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new ByteEchoClientHandler(messageSize)); } }); // Start the client. final ChannelFuture f = boot.connect(host, port).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. connectGroup.shutdownGracefully(); } }
public void run() throws Exception { final ThreadFactory acceptFactory = new UtilThreadFactory("accept"); final ThreadFactory connectFactory = new UtilThreadFactory("connect"); final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER); // Configure the server. try { final ServerBootstrap boot = new ServerBootstrap(); boot.group(acceptGroup, connectGroup) .channelFactory(NioUdtProvider.BYTE_ACCEPTOR) .option(ChannelOption.SO_BACKLOG, 10) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new ByteEchoServerHandler()); } }); // Start the server. final ChannelFuture future = boot.bind(port).sync(); // Wait until the server socket is closed. future.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. acceptGroup.shutdownGracefully(); connectGroup.shutdownGracefully(); } }
public void run() throws Exception { // Configure the peer. final ThreadFactory connectFactory = new UtilThreadFactory("rendezvous"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER); try { final Bootstrap boot = new Bootstrap(); boot.group(connectGroup) .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS) .handler(new ChannelInitializer<UdtChannel>() { @Override public void initChannel(final UdtChannel ch) throws Exception { ch.pipeline().addLast( new LoggingHandler(LogLevel.INFO), new MsgEchoPeerHandler(messageSize)); } }); // Start the peer. final ChannelFuture f = boot.connect(peer, self).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. connectGroup.shutdownGracefully(); } }
/** * Start listening as a server at the given address.. * * @param addr the address to listen at * @param port the port number to listen at * @param bindAllNetworkIfs whether to bind on all network interfaces * @return true if listening was started * @throws ChannelException in case binding failed */ private boolean bindUdtPort(InetAddress addr, int port, boolean bindAllNetworkIfs) { if (udtPortsToSockets.containsKey(port)) { return true; } ThreadFactory bossFactory = new UtilThreadFactory("boss"); ThreadFactory workerFactory = new UtilThreadFactory("worker"); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, bossFactory, NioUdtProvider.BYTE_PROVIDER); NioEventLoopGroup workerGroup = new NioEventLoopGroup(1, workerFactory, NioUdtProvider.BYTE_PROVIDER); NettyUdtServerHandler handler = new NettyUdtServerHandler(component); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channelFactory(NioUdtProvider.BYTE_ACCEPTOR) .childHandler(new NettyInitializer<UdtChannel>(handler, msgDecoderClass)) .option(ChannelOption.SO_REUSEADDR, true); try { if (bindAllNetworkIfs) { bootstrap.bind(new InetSocketAddress(port)).sync(); } else { bootstrap.bind(new InetSocketAddress(addr, port)).sync(); } logger.debug("Successfully bound to ip:port {}:{}", addr, port); } catch (InterruptedException e) { logger.warn("Problem when trying to bind to {}:{}", addr.getHostAddress(), port); trigger(new Fault(e.getCause()), control); return false; } InetSocketAddress iAddr = new InetSocketAddress(addr, port); udtPortsToSockets.put(port, iAddr); udtSocketsToServerBootstraps.put(iAddr, bootstrap); return true; }
/** * Connect to a UDT server. * * @param remoteAddress the remote address * @param localAddress the local address to bind to * @return true if connection succeeded * @throws ChannelException if connecting failed */ private boolean connectUdt(Address remoteAddress, Address localAddress) { InetSocketAddress remote = address2SocketAddress(remoteAddress); InetSocketAddress local = address2SocketAddress(localAddress); if (udtSocketsToBootstraps.containsKey(remote)) { return true; } ThreadFactory workerFactory = new UtilThreadFactory("clientWorker"); NioEventLoopGroup workerGroup = new NioEventLoopGroup(1, workerFactory, NioUdtProvider.BYTE_PROVIDER); NettyStreamHandler handler = new NettyStreamHandler(component, Transport.UDT); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workerGroup).channelFactory(NioUdtProvider.BYTE_CONNECTOR) .handler(new NettyInitializer<UdtChannel>(handler, msgDecoderClass)) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT_MS) .option(ChannelOption.SO_REUSEADDR, true); try { UdtChannel c = (UdtChannel) bootstrap.connect(remote, local).sync().channel(); addLocalSocket(remote, c); logger.debug("Successfully connected to ip:port {}", remote.toString()); } catch (InterruptedException e) { logger.warn("Problem when trying to connect to {}", remote.toString()); trigger(new Fault(e.getCause()), control); return false; } udtSocketsToBootstraps.put(remote, bootstrap); return true; }