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(); } }
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(); } }
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 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; }
/** * Send a message to using UDT. Connects to a server on demand. * * @param msg the message to be sent * @throws ChannelException in case of connection problems */ private void sendUdt(RewriteableMsg msg) { InetSocketAddress dst = address2SocketAddress(msg.getDestination()); UdtChannel channel = udtSocketsToChannels.get(dst); if (channel == null) { if (connectUdt(msg.getDestination(), msg.getSource()) == false) { logger.warn("Channel was null when trying to write msg of type: " + msg.getClass().getCanonicalName() + " with dst address: " + dst.toString()); trigger(new Fault(new IllegalStateException( "Could not send messge because connection could not be established to " + dst.toString())), control); return; } channel = udtSocketsToChannels.get(dst); } try { logger.trace("Sending " + msg.getClass().getCanonicalName() + " from {} to {} ", msg.getSource().getId(), msg.getDestination().getId()); channel.writeAndFlush(msg); totalWrittenBytes += msg.getSize(); } catch (Exception ex) { ex.printStackTrace(); logger.warn("Problem trying to write msg of type: " + msg.getClass().getCanonicalName() + " with dst address: " + dst.toString()); throw new RuntimeException(ex.getMessage()); } }
@Override public void channelActive(ChannelHandlerContext ctx) { super.channelActive(ctx); UdtChannel channel = (UdtChannel) ctx.channel(); component.channels.addLocalSocket(channel); InetSocketAddress other = channel.remoteAddress(); channel.writeAndFlush(new DisambiguateConnection(component.self, new NettyAddress(other), protocol, component.boundUDTPort, true)); }
void disambiguate(DisambiguateConnection msg, Channel c) { synchronized (this) { if (c.isActive()) { // might have been closed by the time we get the lock? component.setCustomMDC(); try { component.extLog.debug("Handling Disamb: {} on {}", msg, c); if (c instanceof SocketChannel) { SocketChannel sc = (SocketChannel) c; address4Remote.put(sc.remoteAddress(), msg.getSource().asSocket()); tcpChannels.put(msg.getSource().asSocket(), sc); component.networkStatus(ConnectionStatus.established(msg.getSource(), Transport.TCP)); if (!tcpChannels.get(msg.getSource().asSocket()).isEmpty()) { // don't add if we don't have a TCP channel since host is most likely dead udtBoundPorts.put(msg.getSource().asSocket(), new InetSocketAddress(msg.getSource().getIp(), msg.udtPort)); } component.trigger(new SendDelayed(msg.getSource(), Transport.TCP)); if (waitingForCreationUDT.remove(msg.getSource().asSocket())) { component.extLog.debug("Requesting creation of outstanding UDT channel to {}", msg.getSource()); createUDTChannel(msg.getSource(), component.bootstrapUDTClient); } } else if (c instanceof UdtChannel) { UdtChannel uc = (UdtChannel) c; address4Remote.put(uc.remoteAddress(), msg.getSource().asSocket()); udtChannels.put(msg.getSource().asSocket(), uc); component.networkStatus(ConnectionStatus.established(msg.getSource(), Transport.UDT)); if (!tcpChannels.get(msg.getSource().asSocket()).isEmpty()) { // don't add if we don't have a TCP channel since host is most likely dead udtBoundPorts.put(msg.getSource().asSocket(), new InetSocketAddress(msg.getSource().getIp(), msg.udtPort)); } component.trigger(new SendDelayed(msg.getSource(), Transport.UDT)); } } finally { MDC.clear(); } } } }
void checkUDTChannel(Msg msg, UdtChannel c) { // Ignore some messages if (msg instanceof CheckChannelActive) { return; } if (msg instanceof CloseChannel) { return; } if (msg instanceof ChannelClosed) { return; } if (!c.equals(udtActiveChannels.get(msg.getSource().asSocket()))) { synchronized (this) { component.setCustomMDC(); try { UdtChannel activeC = udtActiveChannels.get(msg.getSource().asSocket()); udtActiveChannels.put(msg.getSource().asSocket(), c); udtChannels.put(msg.getSource().asSocket(), c); if (activeC != null && !activeC.equals(c)) { component.extLog.warn("Duplicate TCP channel between {} and {}: local {}, remote {}", new Object[]{msg.getSource(), msg.getDestination(), c.localAddress(), c.remoteAddress()}); UdtChannel minsc = minChannel(udtChannels.get(msg.getSource().asSocket())); minsc.writeAndFlush(new MessageNotify.Req(new CheckChannelActive(component.self, msg.getSource(), Transport.UDT))); } } finally { MDC.clear(); } } component.trigger(new SendDelayed(msg.getSource(), Transport.UDT)); } }
private int channel2Id(Channel c) { if (c instanceof SocketChannel) { return channel2Id((SocketChannel) c); } if (c instanceof UdtChannel) { return channel2Id((UdtChannel) c); } throw new NotImplementedException(); }
void monitor() { component.extLog.debug("Monitoring UDT channels:"); for (UdtChannel c : udtChannelsByRemote.values()) { SocketUDT socket = NioUdtProvider.socketUDT(c); if (socket != null) { component.extLog.debug("UDT Stats: \n {} \n {}", new Object[]{socket.toStringMonitor(), socket.toStringOptions()}); try { socket.updateMonitor(true); // reset statistics } catch (ExceptionUDT ex) { component.extLog.warn("Couldn't reset UDT monitoring stats."); } } } }
@Override public void run() { final ServerBootstrap boot = new ServerBootstrap(); final ThreadFactory acceptFactory = new DefaultThreadFactory("accept"); final ThreadFactory serverFactory = new DefaultThreadFactory("server"); final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, serverFactory, NioUdtProvider.BYTE_PROVIDER); try { boot.group(acceptGroup, connectGroup) .channelFactory(NioUdtProvider.BYTE_ACCEPTOR) .childHandler(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 ServerHandler( group)); } }); channel = boot.bind(port).sync().channel(); isRunning = true; log.info("Server ready."); waitForRunning(false); log.info("Server closing acceptor..."); channel.close().sync(); log.info("Server closing connectors..."); group.close().sync(); isShutdown = true; log.info("Server is done."); } catch (final Throwable e) { log.error("Server failure.", e); } finally { acceptGroup.shutdownGracefully(); connectGroup.shutdownGracefully(); acceptGroup.terminationFuture().syncUninterruptibly(); connectGroup.terminationFuture().syncUninterruptibly(); } }
@Override public void channelActive(ChannelHandlerContext ctx) { UdtChannel channel = (UdtChannel) ctx.channel(); getComponent().addLocalSocket(channel.remoteAddress(), channel); super.channelActive(ctx); }
@Override protected void messageReceived(ChannelHandlerContext ctx, Msg msg) throws Exception { component.channels.checkUDTChannel(msg, (UdtChannel) ctx.channel()); super.messageReceived(ctx, msg); }