@Test public void testCreateServer() throws Exception { HandlerFactory handlerFactory = HandlerFactory.instanceAddDecoder(MessageDecoder.createServerProfile(sxpNode), HandlerFactory.Position.END); SxpConnection connectionMock = mock(SxpConnection.class); when(connectionMock.getPassword()).thenReturn("passwd"); when(connectionMock.getDestination()).thenReturn(new InetSocketAddress("0.0.0.0", 64999)); when(connectionMock.getSecurityType()).thenReturn(SecurityType.Default); Mockito.when(sxpNode.getAllConnections()).thenReturn(Collections.singletonList(connectionMock)); Channel channel = ConnectFacade.createServer(sxpNode, handlerFactory, ConnectFacade.collectAllPasswords(sxpNode)).channel(); assertTrue(channel.isOpen()); assertTrue(channel.isWritable()); channel.close().get(); }
public static ChannelFuture createServer(SxpNode node, HandlerFactory hf, Map<InetAddress, byte[]> keyMapping) { if (!Epoll.isAvailable()) { throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause()); } LOG.trace("{} Scheduling server creation with registered passwords {}", node, keyMapping); Optional<SslContext> serverSslContext = node.getSslContextFactory().getServerContext(); keyMapping.remove(node.getSourceIp()); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.channel(EpollServerSocketChannel.class) .option(EpollChannelOption.TCP_MD5SIG, keyMapping) .option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.SO_KEEPALIVE, true) .group(BOSS_GROUP, EVENT_LOOP_GROUP); if (Configuration.NETTY_LOGGER_HANDLER) { bootstrap.handler(new LoggingHandler(LogLevel.INFO)); } bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { SxpConnection connection = node.getConnection(ch.remoteAddress()); if (connection == null) { LOG.error("{} Could not find a connection for the peer: {}, closing channel: {}", node, ch.remoteAddress(), ch); throw new IllegalStateException("Could not find a connection for the peer " + ch.remoteAddress()); } if (connection.getSecurityType() == SecurityType.TLS) { if (serverSslContext.isPresent()) { ch.pipeline().addLast(serverSslContext.get().newHandler(ch.alloc())); } else { LOG.error("{} - Closing {} as the SSL context is not available", node, ch); throw new IllegalStateException("SSL connection specified, but no serverSSLContext found"); } } ch.pipeline().addLast(hf.getDecoders()); ch.pipeline().addLast(hf.getEncoders()); } }); return bootstrap.bind(node.getSourceIp(), node.getServerPort()); }
ConnectFacade { public static ChannelFuture createServer(SxpNode node, HandlerFactory hf, Map<InetAddress, byte[]> keyMapping) { if (!Epoll.isAvailable()) { throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause()); } LOG.trace("{} Scheduling server creation with registered passwords {}", node, keyMapping); Optional<SslContext> serverSslContext = node.getSslContextFactory().getServerContext(); keyMapping.remove(node.getSourceIp()); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.channel(EpollServerSocketChannel.class) .option(EpollChannelOption.TCP_MD5SIG, keyMapping) .option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.SO_KEEPALIVE, true) .group(BOSS_GROUP, EVENT_LOOP_GROUP); if (Configuration.NETTY_LOGGER_HANDLER) { bootstrap.handler(new LoggingHandler(LogLevel.INFO)); } bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { SxpConnection connection = node.getConnection(ch.remoteAddress()); if (connection == null) { LOG.error("{} Could not find a connection for the peer: {}, closing channel: {}", node, ch.remoteAddress(), ch); throw new IllegalStateException("Could not find a connection for the peer " + ch.remoteAddress()); } if (connection.getSecurityType() == SecurityType.TLS) { if (serverSslContext.isPresent()) { ch.pipeline().addLast(serverSslContext.get().newHandler(ch.alloc())); } else { LOG.error("{} - Closing {} as the SSL context is not available", node, ch); throw new IllegalStateException("SSL connection specified, but no serverSSLContext found"); } } ch.pipeline().addLast(hf.getDecoders()); ch.pipeline().addLast(hf.getEncoders()); } }); return bootstrap.bind(node.getSourceIp(), node.getServerPort()); } }
ConnectFacade { public static ChannelFuture createServer(SxpNode node, HandlerFactory hf, Map<InetAddress, byte[]> keyMapping) { if (!Epoll.isAvailable()) { throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause()); } LOG.trace("{} Scheduling server creation with registered passwords {}", node, keyMapping); Optional<SslContext> serverSslContext = node.getSslContextFactory().getServerContext(); keyMapping.remove(node.getSourceIp()); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.channel(EpollServerSocketChannel.class) .option(EpollChannelOption.TCP_MD5SIG, keyMapping) .option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.SO_KEEPALIVE, true) .group(BOSS_GROUP, EVENT_LOOP_GROUP); if (Configuration.NETTY_LOGGER_HANDLER) { bootstrap.handler(new LoggingHandler(LogLevel.INFO)); } bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { SxpConnection connection = node.getConnection(ch.remoteAddress()); if (connection == null) { LOG.error("{} Could not find a connection for the peer: {}, closing channel: {}", node, ch.remoteAddress(), ch); throw new IllegalStateException("Could not find a connection for the peer " + ch.remoteAddress()); } if (connection.getSecurityType() == SecurityType.TLS) { if (serverSslContext.isPresent()) { ch.pipeline().addLast(serverSslContext.get().newHandler(ch.alloc())); } else { LOG.error("{} - Closing {} as the SSL context is not available", node, ch); throw new IllegalStateException("SSL connection specified, but no serverSSLContext found"); } } ch.pipeline().addLast(hf.getDecoders()); ch.pipeline().addLast(hf.getEncoders()); } }); return bootstrap.bind(node.getSourceIp(), node.getServerPort()); } private  ConnectFacade(); }
ConnectFacade { public static ChannelFuture createServer(SxpNode node, HandlerFactory hf, Map<InetAddress, byte[]> keyMapping) { if (!Epoll.isAvailable()) { throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause()); } LOG.trace("{} Scheduling server creation with registered passwords {}", node, keyMapping); Optional<SslContext> serverSslContext = node.getSslContextFactory().getServerContext(); keyMapping.remove(node.getSourceIp()); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.channel(EpollServerSocketChannel.class) .option(EpollChannelOption.TCP_MD5SIG, keyMapping) .option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.SO_KEEPALIVE, true) .group(BOSS_GROUP, EVENT_LOOP_GROUP); if (Configuration.NETTY_LOGGER_HANDLER) { bootstrap.handler(new LoggingHandler(LogLevel.INFO)); } bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { SxpConnection connection = node.getConnection(ch.remoteAddress()); if (connection == null) { LOG.error("{} Could not find a connection for the peer: {}, closing channel: {}", node, ch.remoteAddress(), ch); throw new IllegalStateException("Could not find a connection for the peer " + ch.remoteAddress()); } if (connection.getSecurityType() == SecurityType.TLS) { if (serverSslContext.isPresent()) { ch.pipeline().addLast(serverSslContext.get().newHandler(ch.alloc())); } else { LOG.error("{} - Closing {} as the SSL context is not available", node, ch); throw new IllegalStateException("SSL connection specified, but no serverSSLContext found"); } } ch.pipeline().addLast(hf.getDecoders()); ch.pipeline().addLast(hf.getEncoders()); } }); return bootstrap.bind(node.getSourceIp(), node.getServerPort()); } private  ConnectFacade(); static ChannelFuture createClient(SxpNode node, SxpConnection connection, HandlerFactory hf); static ChannelFuture createServer(SxpNode node, HandlerFactory hf, Map<InetAddress, byte[]> keyMapping); static Map<InetAddress, byte[]> collectAllPasswords(SxpNode node); }
ConnectFacade { public static ChannelFuture createServer(SxpNode node, HandlerFactory hf, Map<InetAddress, byte[]> keyMapping) { if (!Epoll.isAvailable()) { throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause()); } LOG.trace("{} Scheduling server creation with registered passwords {}", node, keyMapping); Optional<SslContext> serverSslContext = node.getSslContextFactory().getServerContext(); keyMapping.remove(node.getSourceIp()); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.channel(EpollServerSocketChannel.class) .option(EpollChannelOption.TCP_MD5SIG, keyMapping) .option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.SO_KEEPALIVE, true) .group(BOSS_GROUP, EVENT_LOOP_GROUP); if (Configuration.NETTY_LOGGER_HANDLER) { bootstrap.handler(new LoggingHandler(LogLevel.INFO)); } bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { SxpConnection connection = node.getConnection(ch.remoteAddress()); if (connection == null) { LOG.error("{} Could not find a connection for the peer: {}, closing channel: {}", node, ch.remoteAddress(), ch); throw new IllegalStateException("Could not find a connection for the peer " + ch.remoteAddress()); } if (connection.getSecurityType() == SecurityType.TLS) { if (serverSslContext.isPresent()) { ch.pipeline().addLast(serverSslContext.get().newHandler(ch.alloc())); } else { LOG.error("{} - Closing {} as the SSL context is not available", node, ch); throw new IllegalStateException("SSL connection specified, but no serverSSLContext found"); } } ch.pipeline().addLast(hf.getDecoders()); ch.pipeline().addLast(hf.getEncoders()); } }); return bootstrap.bind(node.getSourceIp(), node.getServerPort()); } private  ConnectFacade(); static ChannelFuture createClient(SxpNode node, SxpConnection connection, HandlerFactory hf); static ChannelFuture createServer(SxpNode node, HandlerFactory hf, Map<InetAddress, byte[]> keyMapping); static Map<InetAddress, byte[]> collectAllPasswords(SxpNode node);  }