List of usage examples for io.netty.channel ChannelFutureListener ChannelFutureListener
ChannelFutureListener
From source file:evanq.game.net.netty.FactorialClientHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext arg0, final BigInteger msg1) throws Exception { receivedMessages++;/*from www.j a v a2s .co m*/ if (receivedMessages == count) { // Offer the answer after closing the connection. ctx.channel().close().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { boolean offered = answer.offer(msg1); assert offered; } }); } }
From source file:fr.letroll.ttorrentandroid.client.io.PeerClient.java
License:Apache License
@Nonnull public ChannelFuture connect(@Nonnull final PeerConnectionListener listener, @Nonnull final byte[] infoHash, @Nonnull final SocketAddress remoteAddress) { final ChannelFuture future; synchronized (lock) { // connect -> initAndRegister grabs this, so we can safely synchronize here. bootstrap.handler(new PeerClientHandshakeHandler(listener, infoHash, client.getLocalPeerId())); future = bootstrap.connect(remoteAddress); }/* w w w . j a va 2 s.c o m*/ future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { try { LOG.trace("Succeeded: {}", future.get()); } catch (Exception e) { LOG.error("Connection to " + remoteAddress + " failed.", e); listener.handlePeerConnectionFailed(remoteAddress, e); } } }); return future; }
From source file:gedi.remote.RemoteConnections.java
License:Apache License
/** * Connects to the given server; the mechanics is as follows: * <br/>/*from w ww . j a v a2s . c o m*/ * The initChannel consumer is supposed to register channel handlers (called upon channel.registered) * <br/> * Depending on the outcome of the Bootrap.connect method, either the errorHandler or the connectedHandler is called. * <br/> * If the connection is lost, the closedHandler is called. * <br/> * If you want to cancel the connection attempt, invoke cancel on the returned ChannelFuture. If you want to terminate the connection, use the * channel object from the connectedHandler. * <br /> * If the channel is unregistered, the added flag is reset for all handlers bound to the channel pipeline (important if you want to reuse * the handlers added by the initChannel consumer). * * @param uri * @param initChannel * @param errorHandler * @param connectedHandler * @param closedHandler * @return */ public ChannelFuture connect(URI uri, Consumer<SocketChannel> initChannel, Consumer<Throwable> errorHandler, Consumer<SocketChannel> connectedHandler, Runnable closedHandler) { final Protocol protocol = ProtocolExtensionPoint.getInstance().get(ExtensionContext.emptyContext(), uri.getScheme()); if (protocol == null) throw new RuntimeException("Protocol " + uri.getScheme() + " unknown!"); EventLoopGroup group = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); protocol.setCodecs(pipeline); initChannel.accept(ch); pipeline.addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { pipeline.addLast(new ConfigLoggingHandler(ConfigLoggingHandler.LogLevel.INFO)); connectedHandler.accept(ch); super.channelActive(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); closedHandler.run(); } @Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { ctx.pipeline().iterator().forEachRemaining((e) -> Workarounds.removeAdded(e.getValue())); super.channelUnregistered(ctx); } }); } }); // Make a new connection and wait until closed. ChannelFuture f = b.connect(uri.getHost(), uri.getPort() == -1 ? protocol.getDefaultPort() : uri.getPort()) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { Throwable cause = future.cause(); if (cause != null) { log.log(Level.INFO, "Connection failed to server " + uri + ": " + cause.getMessage()); try { errorHandler.accept(cause); } finally { group.shutdownGracefully(); } } else { log.log(Level.INFO, "Client connected to server " + uri); future.channel().closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { log.log(Level.INFO, "Connection closed to server " + uri); group.shutdownGracefully(); } }); } } }); return f; }
From source file:groovyx.gpars.remote.netty.discovery.DiscoveryClient.java
License:Apache License
public void start() { channelFuture.addListener(new ChannelFutureListener() { @Override/*from www . ja v a 2s .c o m*/ public void operationComplete(ChannelFuture future) throws Exception { future.channel().closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { group.shutdownGracefully(); } }); } }); }
From source file:groovyx.gpars.remote.netty.discovery.DiscoveryClient.java
License:Apache License
public void stop() { channelFuture.addListener(new ChannelFutureListener() { @Override// w w w.j av a2 s. com public void operationComplete(ChannelFuture future) throws Exception { future.channel().close(); } }); }
From source file:groovyx.gpars.remote.netty.discovery.DiscoveryClient.java
License:Apache License
public Promise<InetSocketAddress> ask(String actorUrl) { DataflowVariable<InetSocketAddress> promise = new DataflowVariable<InetSocketAddress>(); registeredPromises.putIfAbsent(actorUrl, promise); final DiscoveryRequest request = new DiscoveryRequest(actorUrl); channelFuture.addListener(new ChannelFutureListener() { @Override/*from w ww . ja v a 2 s . c o m*/ public void operationComplete(ChannelFuture future) throws Exception { future.channel().writeAndFlush(request); } }); return registeredPromises.get(actorUrl); }
From source file:groovyx.gpars.remote.netty.NettyClient.java
License:Apache License
/** * Connects the client to server/*w w w. ja v a 2 s. co m*/ * Note: method does not block */ public void start() { if (channelFuture == null) { channelFuture = bootstrap.connect(); channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { future.channel().closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { workerGroup.shutdownGracefully(); } }); } }); } }
From source file:groovyx.gpars.remote.netty.NettyClient.java
License:Apache License
/** * Closes connection to server/*from w w w . ja va 2 s . c o m*/ * Note: method does not block */ public void stop() { if (channelFuture == null) { throw new IllegalStateException("Client has not been started"); } channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { future.channel().close(); } }); }
From source file:groovyx.gpars.remote.netty.NettyServer.java
License:Apache License
/** * Starts the server.//from w w w . j av a2s. c o m * Note: method does not block */ public void start() { if (channelFuture == null) { channelFuture = bootstrap.bind(); channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { future.channel().closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }); } }); } }
From source file:groovyx.gpars.remote.netty.NettyServer.java
License:Apache License
/** * Stops the server.//from ww w . ja v a2 s .c o m * Note: method does not block */ public void stop() { if (channelFuture == null) { throw new IllegalStateException("Server has not been started"); } channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { future.channel().close(); } }); }