List of usage examples for io.netty.channel ChannelFuture isSuccess
boolean isSuccess();
From source file:deven.monitor.client.MonitorClient.java
License:Apache License
public boolean write(ClusterMonitor msg) { if (msg == null) return false; else if (channel == null) throw new RuntimeException("missing channel"); ChannelFuture cf = connect().writeAndFlush(msg); if (cf.isDone() && !cf.isSuccess()) { System.out.println("failed to send message to server"); return false; }/*ww w.j a va 2 s.c om*/ return true; }
From source file:divconq.api.HyperSession.java
License:Open Source License
public Channel allocateHttpChannel(final ChannelHandler handler, OperationResult or) { final AtomicReference<Future<Channel>> sslready = new AtomicReference<>(); Bootstrap b = new Bootstrap(); b.group(Hub.instance.getEventLoopGroup()).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) .handler(new ChannelInitializer<SocketChannel>() { @Override//from www . j a v a 2 s . c o m public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (HyperSession.this.info.isSecurel()) { SslHandler sh = new SslHandler(HyperSession.this.sslfac.getClientEngine()); sslready.set(sh.handshakeFuture()); pipeline.addLast("ssl", sh); } pipeline.addLast("decoder", new HttpResponseDecoder()); pipeline.addLast("encoder", new HttpRequestEncoder()); // TODO maybe //pipeline.addLast("deflater", new HttpContentCompressor()); pipeline.addLast("handler", handler); } }); or.info("Web Client connecting"); try { // must wait here to make sure we don't release connectLock too soon // we want channel init (above) to complete before we try connect again ChannelFuture f = b.connect(this.info.getAddress()).sync(); if (!f.isSuccess()) { or.error(1, "Web Client unable to successfully connect: " + f.cause()); } // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent // that lets wait for the handshake to be done for sure if (sslready.get() != null) { Future<Channel> sf = sslready.get().sync(); if (!sf.isSuccess()) { or.error(1, "Web Client unable to securely connect: " + sf.cause()); } } if (handler instanceof ClientHandler) ((ClientHandler) handler).waitConnect(); return f.channel(); } catch (InterruptedException x) { or.error(1, "Web Client interrupted while connecting: " + x); } catch (Exception x) { or.error(1, "Web Client unable to connect: " + x); } return null; }
From source file:divconq.api.HyperSession.java
License:Open Source License
public Channel allocateWsChannel(final ChannelHandler handler, OperationResult or) { final AtomicReference<Future<Channel>> sslready = new AtomicReference<>(); Bootstrap b = new Bootstrap(); b.group(Hub.instance.getEventLoopGroup()).option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) .channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override// w ww. j a v a 2 s .c om public void initChannel(SocketChannel ch) throws Exception { HttpHeaders customHeaders = new DefaultHttpHeaders(); customHeaders.add("x-DivConq-Mode", Hub.instance.getResources().getMode()); WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker( HyperSession.this.info.getUri(), WebSocketVersion.V13, null, false, customHeaders); ChannelPipeline pipeline = ch.pipeline(); if (HyperSession.this.info.isSecurel()) { SslHandler sh = new SslHandler(HyperSession.this.sslfac.getClientEngine()); sslready.set(sh.handshakeFuture()); pipeline.addLast("ssl", sh); } pipeline.addLast("http-codec", new HttpClientCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("ws-handler", new WebSocketClientProtocolHandler(handshaker)); pipeline.addLast("handler", handler); /* pipeline.addLast("handler", new SimpleChannelInboundHandler<Object>() { @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("read: " + msg); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { super.userEventTriggered(ctx, evt); Logger.debug("ue: " + evt); } }); */ } }); or.info("Web Client connecting"); try { // must wait here to make sure we don't release connectLock too soon // we want channel init (above) to complete before we try connect again ChannelFuture f = b.connect(this.info.getAddress()).sync(); if (!f.isSuccess()) { or.error(1, "Web Client unable to successfully connect: " + f.cause()); } // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent // that lets wait for the handshake to be done for sure if (sslready.get() != null) { Future<Channel> sf = sslready.get().sync(); if (!sf.isSuccess()) { or.error(1, "Web Client unable to securely connect: " + sf.cause()); } } if (handler instanceof ClientHandler) ((ClientHandler) handler).waitConnect(); return f.channel(); } catch (InterruptedException x) { or.error(1, "Web Client interrupted while connecting: " + x); } catch (Exception x) { or.error(1, "Web Client unable to connect: " + x); } return null; }
From source file:divconq.api.internal.DownloadHandler.java
License:Open Source License
public Channel allocateChannel(final HyperSession parent, OperationResult or) { final AtomicReference<Future<Channel>> sslready = new AtomicReference<>(); Bootstrap b = new Bootstrap(); b.group(Hub.instance.getEventLoopGroup()).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) .handler(new ChannelInitializer<SocketChannel>() { @Override//from www . j av a 2s . c o m public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (parent.getInfo().isSecurel()) { SslHandler sh = new SslHandler(parent.getSsl().getClientEngine()); sslready.set(sh.handshakeFuture()); pipeline.addLast("ssl", sh); } pipeline.addLast("codec", new HttpClientCodec()); //pipeline.addLast("decoder", new HttpResponseDecoder()); //pipeline.addLast("encoder", new HttpRequestEncoder()); pipeline.addLast("handler", DownloadHandler.this); } }); or.info("Web Data Client connecting"); try { // must wait here to make sure we don't release connectLock too soon // we want chanel init (above) to complete before we try connect again ChannelFuture f = b.connect(parent.getInfo().getAddress()).sync(); if (!f.isSuccess()) { or.error(1, "Web Data Client unable to successfully connect: " + f.cause()); } // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent // that lets wait for the handshake to be done for sure if (sslready.get() != null) { Future<Channel> sf = sslready.get().sync(); if (!sf.isSuccess()) { or.error(1, "Web Data Client unable to securely connect: " + sf.cause()); } } return f.channel(); } catch (InterruptedException x) { or.error(1, "Web Data Client interrupted while connecting: " + x); } catch (Exception x) { or.error(1, "Web Data Client unable to connect: " + x); } return null; }
From source file:divconq.api.internal.UploadPostHandler.java
License:Open Source License
public Channel allocateChannel(final HyperSession parent, OperationResult or) { final AtomicReference<Future<Channel>> sslready = new AtomicReference<>(); Bootstrap b = new Bootstrap(); b.group(Hub.instance.getEventLoopGroup()).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) .handler(new ChannelInitializer<SocketChannel>() { @Override//from w w w .jav a 2 s . c o m public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (parent.getInfo().isSecurel()) { SslHandler sh = new SslHandler(parent.getSsl().getClientEngine()); sslready.set(sh.handshakeFuture()); pipeline.addLast("ssl", sh); } pipeline.addLast("codec", new HttpClientCodec()); // Remove the following line if you don't want automatic content decompression. //pipeline.addLast("inflater", new HttpContentDecompressor()); // to be used since huge file transfer pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); // so we can get the upload response (200 or not) pipeline.addLast("handler", UploadPostHandler.this); } }); System.out.println("Web Data Client connecting"); try { // must wait here to make sure we don't release connectLock too soon // we want chanel init (above) to complete before we try connect again ChannelFuture f = b.connect(parent.getInfo().getAddress()).sync(); if (!f.isSuccess()) { or.error(1, "Web Client unable to successfully connect: " + f.cause()); System.out.println("Web Client unable to successfully connect: " + f.cause()); } // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent // that lets wait for the handshake to be done for sure if (sslready.get() != null) { Future<Channel> sf = sslready.get().sync(); if (!sf.isSuccess()) { or.error(1, "Web Client unable to securely connect: " + sf.cause()); System.out.println("Web Client unable to securely connect: " + sf.cause()); } } return f.channel(); } catch (InterruptedException x) { or.error(1, "Web Client interrupted while connecting: " + x); System.out.println("Web Client interrupted while connecting: " + x); } catch (Exception x) { or.error(1, "Web Client unable to connect: " + x); System.out.println("Web Client unable to connect: " + x); } return null; }
From source file:divconq.api.internal.UploadPutHandler.java
License:Open Source License
public Channel allocateChannel(final HyperSession parent, OperationResult or) { final AtomicReference<Future<Channel>> sslready = new AtomicReference<>(); Bootstrap b = new Bootstrap(); b.group(Hub.instance.getEventLoopGroup()).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) .handler(new ChannelInitializer<SocketChannel>() { @Override//www. j a v a 2 s . c o m public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (parent.getInfo().isSecurel()) { SslHandler sh = new SslHandler(parent.getSsl().getClientEngine()); sslready.set(sh.handshakeFuture()); pipeline.addLast("ssl", sh); } pipeline.addLast("codec", new HttpClientCodec()); // so we can get the upload response (200 or not) pipeline.addLast("handler", UploadPutHandler.this); } }); or.info("Web Data Client connecting"); try { // must wait here to make sure we don't release connectLock too soon // we want chanel init (above) to complete before we try connect again ChannelFuture f = b.connect(parent.getInfo().getAddress()).sync(); if (!f.isSuccess()) { or.error(1, "Web Client unable to successfully connect: " + f.cause()); } // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent // that lets wait for the handshake to be done for sure if (sslready.get() != null) { Future<Channel> sf = sslready.get().sync(); if (!sf.isSuccess()) { or.error(1, "Web Client unable to securely connect: " + sf.cause()); } } return f.channel(); } catch (InterruptedException x) { or.error(1, "Web Client interrupted while connecting: " + x); } catch (Exception x) { or.error(1, "Web Client unable to connect: " + x); } return null; }
From source file:divconq.bus.Bus.java
License:Open Source License
public void connect() { // never try to connect until init has run if (Hub.instance.isStopping()) return;// w w w . jav a 2 s. c om // if connect method is already running then skip - it will try again later if (!this.connectLock.tryLock()) return; try { // ========================================================================== // Add client connections when not enough // ========================================================================== for (final SocketInfo info : this.connectors.values()) { HubRouter router = this.allocateOrGetHub(info.getHubId(), info.isGateway()); if (router.isLocal()) continue; // ------------------------------------------------- // message port // ------------------------------------------------- int conncount = router.getCountSessions(info); // add a coonection only once per call to connect (should be between 2 - 15 seconds between calls) if (conncount < info.getCount()) { Bootstrap b = new Bootstrap(); b.group(this.getEventLoopGroup()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 250) .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (info.isUseSsl()) pipeline.addLast("ssl", new SslHandler(SslContextFactory.getClientEngine())); pipeline.addLast("http-codec", new HttpClientCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(8192)); // TODO is this too small? pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(60)); // TODO config pipeline.addLast("writeTimeoutHandler", new WriteTimeoutHandler(30)); // TODO config pipeline.addLast("ws-handler", new ClientHandler(info)); } }); Logger.debug("dcBus Client connecting"); try { // must wait here to make sure we don't release connectLock too soon // we want channel init (above) to complete before we try connect again b.connect(info.getAddress()).sync(); } catch (InterruptedException x) { Logger.warn("dcBus Client interrupted while connecting: " + x); } catch (Exception x) { Logger.debug("dcBus Client unable to connect: " + x); } } // ------------------------------------------------- // stream port // ------------------------------------------------- conncount = router.getCountStreamSessions(info); // add a coonection only once per call to connect (should be between 2 - 15 seconds between calls) if (conncount < info.getStreamCount()) { Bootstrap b = new Bootstrap(); b.group(this.getEventLoopGroup()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 250) .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (info.isUseSsl()) pipeline.addLast("ssl", new SslHandler(SslContextFactory.getClientEngine())); // TODO consider compression pipeline.addLast("decoder", new StreamDecoder()); pipeline.addLast("encoder", new StreamEncoder()); pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(60)); // TODO config pipeline.addLast("writeTimeoutHandler", new WriteTimeoutHandler(30)); // TODO config pipeline.addLast("handler", new StreamHandler(info, false)); } }); Logger.debug("dcBus Client stream connecting"); try { // must wait here to make sure we don't release connectLock too soon // we want chanel init (above) to complete before we try connect again b.connect(info.getStreamAddress()).addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture cf) throws Exception { if (!cf.isSuccess()) { Logger.debug("dcBus Stream unable to connect: " + cf.cause()); return; } // client starts the HELLO thing once connected! StreamMessage icmd = Hub.instance.getBus().getLocalHub() .buildStreamHello(info.getHubId()); cf.channel().writeAndFlush(icmd); } }).sync(); } catch (InterruptedException x) { Logger.warn("dcBus Client stream interrupted while connecting: " + x); } catch (Exception x) { Logger.debug("dcBus Client stream unable to connect: " + x); } } } // ========================================================================== // Add server binding when missing // ========================================================================== for (final SocketInfo info : this.listeners) { // only if not currently bound if (this.activelisteners.containsKey(info)) continue; // ------------------------------------------------- // message port // ------------------------------------------------- ServerBootstrap b = new ServerBootstrap(); b.group(this.getEventLoopGroup()).channel(NioServerSocketChannel.class) .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) //.option(ChannelOption.SO_BACKLOG, 125) // this is probably not needed but serves as note to research .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (info.isUseSsl()) pipeline.addLast("ssl", new SslHandler(SslContextFactory.getServerEngine())); pipeline.addLast("codec-http", new HttpServerCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(60)); // TODO config pipeline.addLast("writeTimeoutHandler", new WriteTimeoutHandler(30)); // TODO config pipeline.addLast("handler", new ServerHandler(info)); } }); try { // must wait here, both to keep the activelisteners listeners up to date // but also to make sure we don't release connectLock too soon ChannelFuture bfuture = b.bind(info.getAddress()).sync(); if (bfuture.isSuccess()) { Logger.info("dcBus Message Server listening - now listening for dcMessages on TCP port " + info.getPort()); this.activelisteners.put(info, bfuture.channel()); } else Logger.error("dcBus Server unable to bind: " + bfuture.cause()); } catch (InterruptedException x) { Logger.warn("dcBus Server interrupted while binding: " + x); } catch (Exception x) { Logger.error("dcBus Server unable to bind: " + x); } // ------------------------------------------------- // stream port // ------------------------------------------------- b = new ServerBootstrap(); b.group(this.getEventLoopGroup()).channel(NioServerSocketChannel.class) .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) //.option(ChannelOption.SO_BACKLOG, 125) // this is probably not needed but serves as note to research .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (info.isUseSsl()) pipeline.addLast("ssl", new SslHandler(SslContextFactory.getServerEngine())); // TODO consider compression pipeline.addLast("decoder", new StreamDecoder()); pipeline.addLast("encoder", new StreamEncoder()); pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(60)); // TODO config pipeline.addLast("writeTimeoutHandler", new WriteTimeoutHandler(30)); // TODO config pipeline.addLast("handler", new StreamHandler(info, true)); } }); try { // must wait here, both to keep the activelisteners listeners up to date // but also to make sure we don't release connectLock too soon ChannelFuture bfuture = b.bind(info.getStreamAddress()).sync(); if (bfuture.isSuccess()) { Logger.info("dcBus Stream Server listening - now listening for dcStreams on TCP port " + info.getPort()); this.activestreamlisteners.put(info, bfuture.channel()); } else Logger.error("dcBus Stream Server unable to bind: " + bfuture.cause()); } catch (InterruptedException x) { Logger.warn("dcBus Stream Server interrupted while binding: " + x); } catch (Exception x) { Logger.error("dcBus Stream Server unable to bind: " + x); } } // ========================================================================== // Remove server binding as needed // ========================================================================== for (final SocketInfo info : this.activelisteners.keySet()) { // all is well if in the listeners list if (this.listeners.contains(info)) continue; // otherwise we don't want to bind anymore this.stopSocketListener(info); } } finally { this.connectLock.unlock(); } }
From source file:divconq.bus.Bus.java
License:Open Source License
protected void stopSocketListener(SocketInfo info) { // tear down message port Channel ch = this.activelisteners.remove(info); try {//from w w w. j a v a 2 s.c o m // must wait here, both to keep the activelisteners listeners up to date // but also to make sure we don't release connectLock too soon ChannelFuture bfuture = ch.close().sync(); if (bfuture.isSuccess()) System.out.println("dcBus Server unbound"); else System.out.println("dcBus Server unable to unbind: " + bfuture.cause()); } catch (InterruptedException x) { System.out.println("dcBus Server unable to unbind: " + x); } // tear down stream port ch = this.activestreamlisteners.remove(info); try { if (ch != null) { ChannelFuture bfuture = ch.close().sync(); if (bfuture.isSuccess()) System.out.println("dcBus Stream Server unbound"); else System.out.println("dcBus Stream Server unable to unbind: " + bfuture.cause()); } else System.out.println("dcBus Stream Server missing channel"); } catch (InterruptedException x) { System.out.println("dcBus Stream Server unable to unbind: " + x); } }
From source file:divconq.bus.net.StreamSession.java
License:Open Source License
public boolean write(StreamMessage m) { try {/* w w w . ja va 2s. c o m*/ if (this.chan != null) { Logger.trace("Stream session " + this + " sending message : " + m); //m.retain(); ChannelFuture cf = this.chan.writeAndFlush(m).sync(); // we overrun the queue if we don't sync Logger.trace("Stream session " + this + " sent message : " + m); // record how much we wrote to this channel if (cf.isSuccess() && (m.getData() != null)) this.written += m.getData().writerIndex(); // if we get here there is a decent chance the message was sent (no proof, just good chance) return true; } } catch (Exception x) { Logger.error("Error writing stream message: " + m); Logger.error("Error writing stream message: " + x); x.printStackTrace(); // TODO close channel ? } finally { //m.release(); // if the data is on the network we don't need the buffer anymore -- release in stream encoder } Logger.error("Could not write stream message"); return false; }
From source file:divconq.ctp.net.CtpServices.java
License:Open Source License
public void connect() { // never try to connect until init has run if (Hub.instance.isStopping()) return;// ww w . j a v a 2s.c om // if connect method is already running then skip - it will try again later if (!this.connectLock.tryLock()) return; try { // ========================================================================== // Add server binding when missing // ========================================================================== for (SocketInfo info : this.listeners) { // only if not currently bound if (this.activelisteners.containsKey(info)) continue; // ------------------------------------------------- // stream port // ------------------------------------------------- ServerBootstrap b = new ServerBootstrap().group(Hub.instance.getEventLoopGroup()) .channel(NioServerSocketChannel.class) .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) //.option(ChannelOption.SO_BACKLOG, 125) // this is probably not needed but serves as note to research .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (info.isUseSsl()) pipeline.addLast("ssl", new SslHandler(SslContextFactory.getServerEngine())); // TODO this should be the external SSL not the BUS one pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(60)); // TODO config pipeline.addLast("writeTimeoutHandler", new WriteTimeoutHandler(45)); // TODO config //pipeline.addLast("logger", new LoggingHandler(LogLevel.INFO)); // start as guest until authenticated CtpAdapter adapter = new CtpAdapter(OperationContext.allocateGuest()); adapter.setHandler(new CtpsHandler()); pipeline.addLast("ctp", new CtpHandler(adapter, true)); } }); try { // must wait here, both to keep the activelisteners listeners up to date // and also to make sure we don't release connectLock too soon ChannelFuture bfuture = b.bind(info.getAddress()).sync(); if (bfuture.isSuccess()) { Logger.info("dcCtp Server listening"); this.activelisteners.put(info, bfuture.channel()); } else Logger.error("dcCtp Server unable to bind: " + bfuture.cause()); } catch (InterruptedException x) { Logger.warn("dcCtp Server interrupted while binding: " + x); } catch (Exception x) { Logger.error("dcCtp Server unable to bind: " + x); } } // ========================================================================== // Remove server binding as needed // ========================================================================== for (final SocketInfo info : this.activelisteners.keySet()) { // all is well if in the listeners list if (this.listeners.contains(info)) continue; // otherwise we don't want to bind anymore this.stopSocketListener(info); } } finally { this.connectLock.unlock(); } }