List of usage examples for io.netty.util.concurrent GenericFutureListener GenericFutureListener
GenericFutureListener
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./*w ww . j a va 2 s . c o 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.Sender.java
License:Apache License
/** * Report a failure after the channel was closed. * //from ww w .j a v a 2s . co m * @param futureResponse * The future to set the response * @param close * The close future */ private void reportFailed(final FutureResponse futureResponse, final ChannelFuture close) { close.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture arg0) throws Exception { futureResponse.responseNow(); } }); }
From source file:net.tomp2p.connection.Sender.java
License:Apache License
/** * Report a successful response after the channel was closed. * //from www .j a v a 2s . c o m * @param futureResponse * The future to set the response * @param close * The close future */ private void reportMessage(final FutureResponse futureResponse, final ChannelFuture close) { close.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture arg0) throws Exception { futureResponse.responseNow(); } }); }
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./*ww w. ja v a2 s . c om*/ * * @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. * //from w w w . ja v a2 s . com * @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.connection.TestReservation.java
License:Apache License
/** * Test the TCP connection reservation./* www. j ava2s . c om*/ * * @throws InterruptedException . */ @Test public void testReservationTCP() throws InterruptedException { long start = System.currentTimeMillis(); final int round = 10; final int inner = 200; final int conn = 50; final int tcpMax = 1000; for (int i = 0; i < round; i++) { ChannelClientConfiguration c = PeerBuilder.createDefaultChannelClientConfiguration(); c.maxPermitsTCP(tcpMax); c.pipelineFilter(new MyPipeLine()); Reservation r = new Reservation(workerGroup, c); List<FutureChannelCreator> fcc = new ArrayList<FutureChannelCreator>(); for (int j = 0; j < inner; j++) { FutureChannelCreator fc = r.create(0, conn); fc.addListener(new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { final ChannelCreator cc = future.channelCreator(); final int timeout = 2000; final CountDownLatch countDownLatch = new CountDownLatch(conn); for (int k = 0; k < conn; k++) { ChannelFuture channelFuture = cc.createTCP(SOCKET_ADDRESS, timeout, new HashMap<String, Pair<EventExecutorGroup, ChannelHandler>>(), new FutureResponse(null)); channelFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { future.channel().close(); countDownLatch.countDown(); } }); } countDownLatch.await(); cc.shutdown().awaitListenersUninterruptibly(); } }); fcc.add(fc); } for (FutureChannelCreator fcc1 : fcc) { fcc1.awaitListeners(); } r.shutdown().awaitUninterruptibly(); } //ev.shutdownGracefully().awaitUninterruptibly(); long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("BENCHMARK: opened and closed " + round + " x " + inner + " x " + conn + " TCP connections to localhost in " + time + " ms. STAT: TCP res open/close per sec:" + ((round * inner * conn * mil) / time)); }
From source file:net.tomp2p.connection.TestReservation.java
License:Apache License
/** * Test the TCP connection reservation. This will throw many * "TomP2PSinglePacketUDP - did not get the complete packet" exceptions, * since we are not having any proper headers. * // w ww . jav a 2 s. c o m * @throws InterruptedException . */ @Test public void testReservationUDP() throws InterruptedException { long start = System.currentTimeMillis(); final int round = 10; final int inner = 200; final int conn = 50; final int udpMax = 1000; for (int i = 0; i < round; i++) { ChannelClientConfiguration c = PeerBuilder.createDefaultChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); c.maxPermitsUDP(udpMax); Reservation r = new Reservation(workerGroup, c); List<FutureChannelCreator> fcc = new ArrayList<FutureChannelCreator>(); for (int j = 0; j < inner; j++) { FutureChannelCreator fc = r.create(conn, 0); fc.addListener(new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { final ChannelCreator cc = future.channelCreator(); final CountDownLatch countDownLatch = new CountDownLatch(conn); for (int k = 0; k < conn; k++) { ChannelFuture channelFuture = cc.createUDP(false, new HashMap<String, Pair<EventExecutorGroup, ChannelHandler>>() { }, new FutureResponse(null)); channelFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { future.channel().writeAndFlush(Unpooled.wrappedBuffer(new byte[1])); future.channel().close(); countDownLatch.countDown(); } }); } countDownLatch.await(); cc.shutdown().awaitUninterruptibly(); } }); fcc.add(fc); } for (FutureChannelCreator fcc1 : fcc) { fcc1.awaitListeners(); } r.shutdown().awaitUninterruptibly(); } long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("BENCHMARK: opened and closed " + round + " x " + inner + " x " + conn + " UDP connections to localhost in " + time + " ms. STAT: UDP res open/close per sec:" + ((round * inner * conn * mil) / time)); }
From source file:net.tomp2p.connection.TestReservation.java
License:Apache License
/** * Test an unclean shutdown, that means the reservation is shutdown, but the * connectioncreation is not.//from w ww . j ava 2s. com * * @throws InterruptedException . */ @Test public void testReservationTCPNonCleanShutdown() throws InterruptedException { long start = System.currentTimeMillis(); final int round = 100; final int inner = 100; final int conn = 5; final int tcpMax = 500; for (int i = 0; i < round; i++) { ChannelClientConfiguration c = PeerBuilder.createDefaultChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); c.maxPermitsTCP(tcpMax); Reservation r = new Reservation(workerGroup, c); List<FutureChannelCreator> fcc = new ArrayList<FutureChannelCreator>(); for (int j = 0; j < inner; j++) { FutureChannelCreator fc = r.create(0, conn); fc.addListener(new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isFailed()) { return; } final ChannelCreator cc = future.channelCreator(); final int timeout = 2000; for (int k = 0; k < conn; k++) { ChannelFuture channelFuture = cc.createTCP(SOCKET_ADDRESS, timeout, new HashMap<String, Pair<EventExecutorGroup, ChannelHandler>>() { }, new FutureResponse(null)); if (channelFuture == null) { return; } channelFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { future.channel().close(); } }); } } }); fcc.add(fc); } for (FutureChannelCreator fcc1 : fcc) { fcc1.awaitListeners(); } r.shutdown().awaitListenersUninterruptibly(); } long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("BENCHMARK: opened and closed " + round + " x " + inner + " x " + conn + " TCP connections to localhost in " + time + " ms. STAT: TCP unclean open/close per sec:" + ((round * inner * conn * mil) / time)); }
From source file:net.tomp2p.connection.TestReservation.java
License:Apache License
/** * Unclean shutdown of pending connections. * // w w w . j a v a 2 s. c om * @throws InterruptedException . */ @Test public void testReservationTCPNonCleanShutdown2() throws InterruptedException { EventLoopGroup ev = new NioEventLoopGroup(); final int round = 100; final int inner = 100; final int conn = 5; final int tcpMax = 500; for (int i = 0; i < round; i++) { ChannelClientConfiguration c = PeerBuilder.createDefaultChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); c.maxPermitsTCP(tcpMax); Reservation r = new Reservation(ev, c); List<FutureChannelCreator> fcc = new ArrayList<FutureChannelCreator>(); for (int j = 0; j < inner; j++) { FutureChannelCreator fc = r.create(0, conn); fc.addListener(new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isFailed()) { return; } final ChannelCreator cc = future.channelCreator(); final int timeout = 2000; for (int k = 0; k < conn; k++) { ChannelFuture channelFuture = cc.createTCP(SOCKET_ADDRESS, timeout, new HashMap<String, Pair<EventExecutorGroup, ChannelHandler>>() { }, new FutureResponse(null)); if (channelFuture == null) { return; } channelFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { future.channel().close(); } }); } } }); fcc.add(fc); } r.shutdown().awaitListenersUninterruptibly(); } Future<?> f = ev.shutdownGracefully().awaitUninterruptibly(); f.awaitUninterruptibly(); }
From source file:net.tomp2p.connection2.ChannelCreator.java
License:Apache License
/** * When a channel is closed, the semaphore is released an other channel can be created. * /*www. ja v a 2s .com*/ * @param channelFuture * The channel future * @param semaphore * The semaphore to decrease * @return The same future that was passed as an argument */ private ChannelFuture setupCloseListener(final ChannelFuture channelFuture, final Semaphore semaphore) { channelFuture.channel().closeFuture().addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { semaphore.release(); } }); recipients.add(channelFuture.channel()); return channelFuture; }