List of usage examples for io.netty.channel ChannelFuture isSuccess
boolean isSuccess();
From source file:net.tomp2p.connection.Sender.java
License:Apache License
/** * After sending, we check if the write was successful or if it was a fire * and forget./* ww w .jav a 2 s .co m*/ * * @param writeFuture * The future of the write operation. Can be UDP or TCP * @param futureResponse * The future to set the response * @param fireAndForget * True, if we don't expect a message */ private void afterSend(final ChannelFuture writeFuture, final FutureResponse futureResponse, final boolean fireAndForget) { final Cancel writeCancel = createCancel(writeFuture); writeFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { futureResponse.removeCancel(writeCancel); if (!future.isSuccess()) { futureResponse.failedLater(future.cause()); reportFailed(futureResponse, future.channel().close()); LOG.warn("Failed to write channel the request {} {}", futureResponse.request(), future.cause()); } if (fireAndForget) { futureResponse.responseLater(null); LOG.debug("fire and forget, close channel now {}, {}", futureResponse.request(), future.channel()); reportMessage(futureResponse, future.channel().close()); } } }); }
From source file:net.tomp2p.connection.TestChannelCreator.java
License:Apache License
/** * This test only works if the server calls "nc -lk 4000", that is done in @Before. It will open and close 1000 * * 1000 connections to localhost./*from w ww . j a v a 2s .c o m*/ * * @throws InterruptedException . */ @Test @Ignore public void testCreatorTCP() throws InterruptedException { long start = System.currentTimeMillis(); final int rounds = 10000; final int connections = 20; final int printOut = 100; EventLoopGroup ev = new NioEventLoopGroup(); final int timeout = 4000; for (int j = 0; j < rounds; j++) { ChannelClientConfiguration c = PeerBuilder.createDefaultChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); final ChannelCreator channelCreator2 = new ChannelCreator(ev, new FutureDone<Void>(), 0, connections, c); if (j % printOut == 0) { System.out.print(j + " "); } final CountDownLatch countDownLatch = new CountDownLatch(connections); final Map<String, Pair<EventExecutorGroup, ChannelHandler>> tmp = new HashMap<String, Pair<EventExecutorGroup, ChannelHandler>>(); final GenericFutureListener<ChannelFuture> handler = new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().close(); countDownLatch.countDown(); } else { future.cause().printStackTrace(); } } }; for (int i = 0; i < connections; i++) { final ChannelFuture channelFuture = channelCreator2.createTCP(SOCKET_ADDRESS, timeout, tmp, new FutureResponse(null)); channelFuture.addListener(handler); } countDownLatch.await(); channelCreator2.shutdown().awaitUninterruptibly(); } ev.shutdownGracefully().awaitUninterruptibly(); long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("\nBENCHMARK: opened and closed " + connections + " x " + rounds + " TCP connections to localhost in " + time + " ms. STAT: TCP open/close per sec:" + ((connections * rounds * mil) / time)); }
From source file:net.tomp2p.connection.TestChannelCreator.java
License:Apache License
/** * This test only works if the server calls "nc -lku 4000", that is done in @Before. * //w w w .java 2 s. co m * @throws InterruptedException . */ @Test @Ignore public void testCreatorUDP() throws InterruptedException { long start = System.currentTimeMillis(); final int rounds = 10000; final int connections = 20; final int printOut = 100; EventLoopGroup ev = new NioEventLoopGroup(); for (int j = 0; j < rounds; j++) { ChannelClientConfiguration c = PeerBuilder.createDefaultChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); final ChannelCreator channelCreator2 = new ChannelCreator(ev, new FutureDone<Void>(), connections, 0, c); if (j % printOut == 0) { System.out.print(j + " "); } final CountDownLatch countDownLatch = new CountDownLatch(connections); final Map<String, Pair<EventExecutorGroup, ChannelHandler>> tmp = new HashMap<String, Pair<EventExecutorGroup, ChannelHandler>>(); GenericFutureListener<ChannelFuture> handler = new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().writeAndFlush(Unpooled.wrappedBuffer(new byte[1])); future.channel().close(); countDownLatch.countDown(); } else { future.cause().printStackTrace(); } } }; for (int i = 0; i < connections; i++) { final ChannelFuture channelFuture = channelCreator2.createUDP(false, tmp, new FutureResponse(null)); channelFuture.addListener(handler); } countDownLatch.await(); channelCreator2.shutdown().awaitUninterruptibly(); } ev.shutdownGracefully().awaitUninterruptibly(); long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("\nBENCHMARK: opened and closed " + connections + " x " + rounds + " UDP connections to localhost in " + time + " ms. STAT: UDP open/close per sec:" + ((connections * rounds * mil) / time)); }
From source file:net.tomp2p.connection2.ChannelServer.java
License:Apache License
/** * Handles the waiting and returning the channel. * //from w w w .j ava2 s . c o m * @param future * The future to wait for * @return The channel or null if we failed to bind. */ private boolean handleFuture(final ChannelFuture future) { try { future.await(); } catch (InterruptedException e) { if (LOG.isWarnEnabled()) { LOG.warn("could not start UPD server", e); } return false; } boolean success = future.isSuccess(); if (success) { return true; } else { future.cause().printStackTrace(); return false; } }
From source file:net.tomp2p.connection2.Sender.java
License:Apache License
/** * After connecting, we check if the connect was successful. * //ww w .j a va 2 s. co m * @param futureResponse * The future to set the response * @param message * The message to send * @param channelFuture * the future of the connect * @param fireAndForget * True, if we don't expect a message */ private void afterConnect(final FutureResponse futureResponse, final Message2 message, final ChannelFuture channelFuture, final boolean fireAndForget) { final Cancel connectCancel = createCancel(channelFuture); futureResponse.addCancel(connectCancel); channelFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { futureResponse.removeCancel(connectCancel); if (future.isSuccess()) { futureResponse.setProgressHandler(new ProgresHandler() { @Override public void progres() { final ChannelFuture writeFuture = future.channel().writeAndFlush(message); afterSend(writeFuture, futureResponse, fireAndForget); } }); // this needs to be called first before all other progress futureResponse.progressFirst(); } else { futureResponse.setFailed("Channel creation failed " + future.cause()); LOG.warn("Channel creation failed ", future.cause()); } } }); }
From source file:net.tomp2p.connection2.Sender.java
License:Apache License
/** * After sending, we check if the write was successful or if it was a fire and forget. * //from ww w .j a v a2 s.co m * @param writeFuture * The future of the write operation. Can be UDP or TCP * @param futureResponse * The future to set the response * @param fireAndForget * True, if we don't expect a message */ private void afterSend(final ChannelFuture writeFuture, final FutureResponse futureResponse, final boolean fireAndForget) { final Cancel writeCancel = createCancel(writeFuture); writeFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { futureResponse.removeCancel(writeCancel); if (!future.isSuccess()) { futureResponse.setFailedLater(future.cause()); reportFailed(futureResponse, future.channel().close()); LOG.warn("Failed to write channel the request {}", futureResponse.getRequest(), future.cause()); } if (fireAndForget) { futureResponse.setResponseLater(null); LOG.debug("fire and forget, close channel now"); reportMessage(futureResponse, future.channel().close()); } } }); }
From source file:net.tomp2p.connection2.TestChannelCreator.java
License:Apache License
/** * This test only works if the server calls "nc -lk 4000", that is done in @Before. It will open and close 1000 * * 1000 connections to localhost.//from w ww . java2 s . c o m * * @throws InterruptedException . */ @Test @Ignore public void testCreatorTCP() throws InterruptedException { long start = System.currentTimeMillis(); final int rounds = 10000; final int connections = 20; final int printOut = 100; EventLoopGroup ev = new NioEventLoopGroup(); final int timeout = 4000; for (int j = 0; j < rounds; j++) { ChannelClientConfiguration c = new ChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); final ChannelCreator channelCreator2 = new ChannelCreator(ev, new FutureDone<Void>(), 0, connections, c); if (j % printOut == 0) { System.out.print(j + " "); } final CountDownLatch countDownLatch = new CountDownLatch(connections); final Map<String, ChannelHandler> tmp = new HashMap<String, ChannelHandler>(); final GenericFutureListener<ChannelFuture> handler = new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().close(); countDownLatch.countDown(); } else { future.cause().printStackTrace(); } } }; for (int i = 0; i < connections; i++) { final ChannelFuture channelFuture = channelCreator2.createTCP(SOCKET_ADDRESS, timeout, tmp); channelFuture.addListener(handler); } countDownLatch.await(); channelCreator2.shutdown().awaitUninterruptibly(); } ev.shutdownGracefully().awaitUninterruptibly(); long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("\nBENCHMARK: opened and closed " + connections + " x " + rounds + " TCP connections to localhost in " + time + " ms. STAT: TCP open/close per sec:" + ((connections * rounds * mil) / time)); }
From source file:net.tomp2p.connection2.TestChannelCreator.java
License:Apache License
/** * This test only works if the server calls "nc -lku 4000", that is done in @Before. * // w w w. j a v a2 s.c o m * @throws InterruptedException . */ @Test @Ignore public void testCreatorUDP() throws InterruptedException { long start = System.currentTimeMillis(); final int rounds = 10000; final int connections = 20; final int printOut = 100; EventLoopGroup ev = new NioEventLoopGroup(); for (int j = 0; j < rounds; j++) { ChannelClientConfiguration c = new ChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); final ChannelCreator channelCreator2 = new ChannelCreator(ev, new FutureDone<Void>(), connections, 0, c); if (j % printOut == 0) { System.out.print(j + " "); } final CountDownLatch countDownLatch = new CountDownLatch(connections); final Map<String, ChannelHandler> tmp = new HashMap<String, ChannelHandler>(); GenericFutureListener<ChannelFuture> handler = new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().writeAndFlush(Unpooled.wrappedBuffer(new byte[1])); future.channel().close(); countDownLatch.countDown(); } else { future.cause().printStackTrace(); } } }; for (int i = 0; i < connections; i++) { final ChannelFuture channelFuture = channelCreator2.createUDP(SOCKET_ADDRESS, false, tmp); channelFuture.addListener(handler); } countDownLatch.await(); channelCreator2.shutdown().awaitUninterruptibly(); } ev.shutdownGracefully().awaitUninterruptibly(); long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("\nBENCHMARK: opened and closed " + connections + " x " + rounds + " UDP connections to localhost in " + time + " ms. STAT: UDP open/close per sec:" + ((connections * rounds * mil) / time)); }
From source file:net.tomp2p.rpc.PingRPC.java
License:Apache License
/** * Ping a peer, and request the other peer to return the source port of our * socket used to send the message. This method needs to do the setup of a * ChannelFuture manually since we need to know the port number of the * assigned socket.//from ww w .jav a 2 s .c o m * * @param remotePeer * The destination peer * @param channelCreator * The channel creator where we create a UPD channel * @return The future that will be triggered when we receive an answer or * something fails. */ public FutureDone<List<PeerSocketAddress>> pingNATType(final PeerAddress remotePeer, final ChannelCreator channelCreator, final ConnectionConfiguration configuration, final Peer peer) { final FutureDone<List<PeerSocketAddress>> fDone = new FutureDone<List<PeerSocketAddress>>(); final List<PeerSocketAddress> peerSocketAddresses = new ArrayList<PeerSocketAddress>(2); final Message message = createMessage(remotePeer, RPC.Commands.PING.getNr(), Type.REQUEST_5); final FutureResponse futureResponse = new FutureResponse(message); final SimpleChannelInboundHandler<Message> inbound = new SimpleChannelInboundHandler<Message>() { @Override protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception { if (!msg.peerSocketAddresses().isEmpty() && msg.type() == Type.OK) { peerSocketAddresses.add(msg.peerSocketAddresses().get(0)); fDone.done(peerSocketAddresses); ctx.close(); } } }; Utils.addReleaseListener(channelCreator, futureResponse); final ChannelFuture cF = channelCreator.createUDP(false, peer.connectionBean().sender().configureHandlers(inbound, futureResponse, 30, false), futureResponse); cF.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { InetSocketAddress srcAddress = (InetSocketAddress) future.channel().localAddress(); peerSocketAddresses.add(new PeerSocketAddress(srcAddress.getAddress(), srcAddress.getPort(), srcAddress.getPort())); peer.connectionBean().sender().afterConnect(futureResponse, message, future, false); } } }); return fDone; }
From source file:Netty4.MQSource.NettyTest.common.RemotingUtil.java
License:Apache License
public static void closeChannel(Channel channel) { final String addrRemote = parseChannelRemoteAddr(channel); channel.close().addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { log.info("closeChannel: close the connection to remote address[{}] result: {}", addrRemote, future.isSuccess()); }/*from w w w. j a va2s .com*/ }); }