List of usage examples for io.netty.channel ChannelFuture isSuccess
boolean isSuccess();
From source file:jj.repl.ReplIntegrationTest.java
License:Apache License
@Test public void test() throws Throwable { assertTrue("timed out waiting for init", latch.await(500, MILLISECONDS)); // well... it started so that's something // connect to config.port() and send in some commands? why not latch = new CountDownLatch(1); final AtomicReference<Throwable> failure = new AtomicReference<>(); final StringBuilder response = new StringBuilder().append("Welcome to JibbrJabbr\n>") .append("ReferenceError: \"whatever\" is not defined.\n").append(" at repl-console:1\n") .append(" at base-repl-system.js:8 ($$print)\n").append(" at repl-console:1\n").append("\n>"); bootstrap = new Bootstrap().group(new NioEventLoopGroup(1)).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override//from ww w . j a v a 2s. co m protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new StringEncoder(US_ASCII)).addLast(new StringDecoder(US_ASCII)) .addLast(new SimpleChannelInboundHandler<String>() { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { if (msg.equals(response.substring(0, msg.length()))) { response.delete(0, msg.length()); } if (response.length() == 0) { latch.countDown(); } } }); } }); bootstrap.connect("localhost", config.port()).addListener((ChannelFuture future) -> { if (future.isSuccess()) { future.channel().writeAndFlush("whatever\n"); } else { failure.set(future.cause()); } }); assertTrue("timed out waiting for response", latch.await(1, SECONDS)); if (failure.get() != null) { throw failure.get(); } }
From source file:jj.repl.ReplServer.java
License:Apache License
private void start() { port = (configuration.port() < 1023 || configuration.port() > 65535) ? DEFAULT_PORT : configuration.port(); final ServerBootstrap bootstrap = new ServerBootstrap() .group(new NioEventLoopGroup(1, bossThreadFactory), new NioEventLoopGroup(1, workerThreadFactory)) .channel(NioServerSocketChannel.class).childHandler(channelInitializer); bootstrap.bind("localhost", port).addListener(new ChannelFutureListener() { @Override//ww w.j ava 2 s . c om public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { publisher.publish(new ReplListening(port)); server = bootstrap; } else { publisher.publish(new Emergency("could not start the REPL server", future.cause())); bootstrap.group().shutdownGracefully(0, 0, SECONDS); bootstrap.childGroup().shutdownGracefully(0, 0, SECONDS); } } }); }
From source file:jlibs.wamp4j.netty.NettyClientEndpoint.java
License:Apache License
@Override public void connect(final URI uri, final ConnectListener listener, final String... subProtocols) { final SslContext sslContext; if ("wss".equals(uri.getScheme())) { try {/*from ww w . j ava2s. co m*/ if (sslSettings == null) { sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); } else { sslContext = SslContextBuilder.forClient().trustManager(sslSettings.trustCertChainFile) .keyManager(sslSettings.certificateFile, sslSettings.keyFile, sslSettings.keyPassword) .build(); } } catch (Throwable thr) { listener.onError(thr); return; } } else if ("ws".equals(uri.getScheme())) sslContext = null; else throw new IllegalArgumentException("invalid protocol: " + uri.getScheme()); final int port = uri.getPort() == -1 ? (sslContext == null ? 80 : 443) : uri.getPort(); Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.MAX_MESSAGES_PER_READ, 50000).option(ChannelOption.WRITE_SPIN_COUNT, 50000) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { if (sslContext != null) ch.pipeline().addLast(sslContext.newHandler(ch.alloc(), uri.getHost(), port)); WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, Util.toString(subProtocols), false, new DefaultHttpHeaders()); ch.pipeline().addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), new WebSocketClientProtocolHandler(handshaker) { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); listener.onError(cause); } }, new HandshakeListener(handshaker, listener)); } }); bootstrap.connect(uri.getHost(), port).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { assert !future.channel().isOpen(); listener.onError(future.cause()); } } }); }
From source file:jlibs.wamp4j.netty.NettyServerEndpoint.java
License:Apache License
@Override public void bind(final URI uri, final String subProtocols[], final AcceptListener listener) { final SslContext sslContext; if ("wss".equals(uri.getScheme())) { try {//from w w w . ja va 2s . co m if (sslSettings == null) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslSettings = new SSLSettings().keyFile(ssc.privateKey()).certificateFile(ssc.certificate()); } ClientAuth clientAuth = ClientAuth.values()[sslSettings.clientAuthentication.ordinal()]; sslContext = SslContextBuilder .forServer(sslSettings.certificateFile, sslSettings.keyFile, sslSettings.keyPassword) .clientAuth(clientAuth).trustManager(sslSettings.trustCertChainFile).build(); } catch (Throwable thr) { listener.onError(thr); return; } } else if ("ws".equals(uri.getScheme())) sslContext = null; else throw new IllegalArgumentException("invalid protocol: " + uri.getScheme()); int port = uri.getPort(); if (port == -1) port = sslContext == null ? 80 : 443; ServerBootstrap bootstrap = new ServerBootstrap().group(eventLoopGroup) .channel(NioServerSocketChannel.class) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.MAX_MESSAGES_PER_READ, 50000) .childOption(ChannelOption.WRITE_SPIN_COUNT, 50000) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { if (sslContext != null) ch.pipeline().addLast(sslContext.newHandler(ch.alloc())); ch.pipeline().addLast(new HttpServerCodec(), new HttpObjectAggregator(65536), new Handshaker(uri, listener, subProtocols)); } }); bootstrap.bind(uri.getHost(), port).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { channel = future.channel(); channel.attr(ACCEPT_LISTENER).set(listener); listener.onBind(NettyServerEndpoint.this); } else listener.onError(future.cause()); } }); }
From source file:jlibs.wamp4j.netty.NettyServerEndpoint.java
License:Apache License
@Override public void close() { channel.close().addListener(new ChannelFutureListener() { @Override/*from w ww .j av a2 s . c o m*/ public void operationComplete(ChannelFuture future) throws Exception { AcceptListener acceptListener = channel.attr(ACCEPT_LISTENER).get(); if (!future.isSuccess()) acceptListener.onError(future.cause()); acceptListener.onClose(NettyServerEndpoint.this); } }); }
From source file:jlibs.wamp4j.netty.NettyWebSocketClient.java
License:Apache License
@Override public void connect(final URI uri, final ConnectListener listener, final String... subProtocols) { String protocol = uri.getScheme(); if (!protocol.equals("ws")) throw new IllegalArgumentException("invalid protocol: " + protocol); int port = uri.getPort(); if (port == -1) port = 80;/* w w w .j a va 2s .c o m*/ Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, Util.toString(subProtocols), false, new DefaultHttpHeaders()); ch.pipeline().addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), new WebSocketClientProtocolHandler(handshaker), new HandshakeListener(handshaker, listener)); } }); bootstrap.connect(uri.getHost(), port).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { assert !future.channel().isOpen(); listener.onError(future.cause()); } } }); }
From source file:jlibs.wamp4j.netty.NettyWebSocketServer.java
License:Apache License
@Override public void bind(final URI uri, final String subProtocols[], final AcceptListener listener) { int port = uri.getPort(); if (port == -1) port = 80;//w w w . java 2s . c o m ServerBootstrap bootstrap = new ServerBootstrap().group(eventLoopGroup) .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpServerCodec(), new HttpObjectAggregator(65536), new Handshaker(uri, listener, subProtocols)); } }); bootstrap.bind(uri.getHost(), port).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { channel = future.channel(); channel.attr(ACCEPT_LISTENER).set(listener); listener.onBind(NettyWebSocketServer.this); } else listener.onError(future.cause()); } }); }
From source file:jlibs.wamp4j.netty.NettyWebSocketServer.java
License:Apache License
@Override public void close() { channel.close().addListener(new ChannelFutureListener() { @Override//ww w. j a va 2s.c om public void operationComplete(ChannelFuture future) throws Exception { AcceptListener acceptListener = channel.attr(ACCEPT_LISTENER).get(); if (!future.isSuccess()) acceptListener.onError(future.cause()); acceptListener.onClose(NettyWebSocketServer.this); } }); }
From source file:lunarion.node.requester.LunarDBClient.java
License:Open Source License
public ChannelFuture connect(String host, int port) throws Exception { group = new NioEventLoopGroup(); client_handler = new ClientHandler(internal); try {/*from w w w. j a v a 2 s . c o m*/ Bootstrap client_bootstrap = new Bootstrap(); client_bootstrap.group(group).channel(NioSocketChannel.class) .option(ChannelOption.SO_SNDBUF, 1024 * 1024) .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 1024, 65536 * 512)) //.option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_KEEPALIVE, true).handler(new ClientChannelInitializer(client_handler)); //ChannelFuture cf = client_bootstrap.connect(host, port).sync(); ChannelFuture cf = client_bootstrap.connect(host, port); channel_list.add(cf); cf.addListener(new ChannelFutureListener() { public void operationComplete(final ChannelFuture channelFuture) throws Exception { if (channelFuture.isSuccess()) { ClientHandler handler = channelFuture.channel().pipeline().get(ClientHandler.class); client_handler = handler; } } }); connected.set(true); connected_host_ip = host; connected_port = port; return cf; } finally { // group.shutdownGracefully(); } }
From source file:malcolm.HttpProxyFrontendHandler.java
License:Apache License
private Channel proxyRequest(final ChannelHandlerContext ctx, final HttpRequest req, final ChannelFutureListener onWriteComplete) { final Endpoint endpoint = HttpUtil.getEndpoint(req) .orElseThrow(() -> new IllegalArgumentException("Missing host header")); return backendChannelBootstap(ctx.channel()).connect(endpoint.getHost(), endpoint.getPort()) .addListener((final ChannelFuture f) -> { if (f.isSuccess()) { f.channel().writeAndFlush(req).addListener(onWriteComplete); } else { logger.debug("connect failed"); // TODO bad gateway? }/*from w ww.ja v a 2s.c o m*/ }).channel(); }