List of usage examples for io.netty.channel ChannelFutureListener ChannelFutureListener
ChannelFutureListener
From source file:it.jnrpe.JNRPE.java
License:Apache License
/** * Starts a new thread that listen for requests. The method is <b>not * blocking</b>//from www .j a va2 s .c o m * * @param address * The address to bind to * @param port * The listening port * @param useSSL * <code>true</code> if an SSL socket must be created. * @throws UnknownHostException * - */ public void listen(final String address, final int port, final boolean useSSL) throws UnknownHostException { // Bind and start to accept incoming connections. ChannelFuture cf = getServerBootstrap(useSSL).bind(address, port); cf.addListener(new ChannelFutureListener() { public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { context.getEventBus().post(new JNRPEStatusEvent(STATUS.STARTED, this, "JNRPE Server started")); LOG.info(context, "Listening on " + (useSSL ? "SSL/" : "") + address + ":" + port); } else { getExecutionContext().getEventBus() .post(new JNRPEStatusEvent(STATUS.FAILED, this, "JNRPE Server start failed")); LOG.error(context, "Unable to listen on " + (useSSL ? "SSL/" : "") + address + ":" + port, future.cause()); } } }); }
From source file:jj.http.server.websocket.WebSocketConnectionMaker.java
License:Apache License
private void doHandshake(final ChannelHandlerContext ctx, final FullHttpRequest request, final WebSocketServerHandshaker handshaker) { handshaker.handshake(ctx.channel(), request).addListener(new ChannelFutureListener() { private boolean isHandshakeFailure(ChannelFuture future) { return future.cause() != null && future.cause() instanceof WebSocketHandshakeException; }//w ww .j av a 2s .c om @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { URIMatch uriMatch = new URIMatch(request.getUri()); WebSocketConnectionHost host = null; for (Class<? extends WebSocketConnectionHost> hostClass : webSocketConnectionHostClasses) { host = resourceFinder.findResource(hostClass, AppLocation.Virtual, uriMatch.name); if (host != null) break; } if (host == null) { // 1011 indicates that a server is terminating the connection because // it encountered an unexpected condition that prevented it from // fulfilling the request. ctx.writeAndFlush(new CloseWebSocketFrame(1011, null)).addListener(CLOSE); // TODO: is closing here the right thing? or do we count on the client closing the connection // to avoid the time_wait state? } else if (!uriMatch.sha1.equals(host.sha1())) { ctx.writeAndFlush(new TextWebSocketFrame("jj-reload")) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { // 1001 indicates that an endpoint is "going away", such as a server // going down or a browser having navigated away from a page. ctx.writeAndFlush(new CloseWebSocketFrame(1001, null)).addListener(CLOSE); // TODO: is closing here the right thing? or do we count on the client closing the connection // to avoid the time_wait state? } }); } else { ctx.pipeline().replace(JJEngine.toString(), JJWebsocketHandler.toString(), handlerCreator.createHandler(handshaker, host)); } } else if (isHandshakeFailure(future)) { response.sendError(HttpResponseStatus.BAD_REQUEST); } else { ctx.close(); } } }); }
From source file:jj.http.server.websocket.WebSocketFrameHandler.java
License:Apache License
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { connectionTracker.addConnection(connection); connection.webSocketConnectionHost().connected(connection); executor.submit(connection, HostEvent.clientConnected.toString(), connection); ctx.channel().closeFuture().addListener(new ChannelFutureListener() { @Override/*from www . j av a2s. c om*/ public void operationComplete(ChannelFuture future) throws Exception { connectionTracker.removeConnection(connection); connection.webSocketConnectionHost().disconnected(connection); executor.submit(connection, HostEvent.clientDisconnected.toString(), connection); } }); }
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/*from ww w . ja v a2 s . c o m*/ 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 . ja v a 2s. c o 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 .j a va 2 s. c o 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 w w . j a va 2s . 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 ava2 s . 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. j a v a2 s.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//from w w w . j av a2 s. co m 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); } }); }