List of usage examples for io.netty.channel ChannelFuture isSuccess
boolean isSuccess();
From source file:darks.grid.network.GridRemoteSession.java
License:Apache License
@Override public boolean sendSyncMessage(Object msg, boolean failRetry) { if (channel == null || !channel.isActive()) return false; try {// w w w . j ava 2s . co m boolean ret = false; int count = 0; do { if (!channel.isActive() || !channel.isWritable()) return false; ChannelFuture future = channel.writeAndFlush(msg).sync(); ret = future.isSuccess(); if (log.isDebugEnabled()) log.debug("Send " + msg + " to " + channel.remoteAddress() + " ret:" + ret); if (ret) break; count++; } while (count < failRetryCount && failRetry); return ret; } catch (Exception e) { log.error("Fail to send remote sync message.[" + channel.remoteAddress() + "/active:" + channel.isActive() + "] Cause " + e.getMessage(), e); return false; } }
From source file:de.cubeisland.engine.core.webapi.WebSocketRequestHandler.java
License:Open Source License
public void doHandshake(ChannelHandlerContext ctx, FullHttpRequest message) { WebSocketServerHandshakerFactory handshakerFactory = new WebSocketServerHandshakerFactory( "ws://" + message.headers().get(HOST) + "/" + this.WEBSOCKET_ROUTE, null, false); this.handshaker = handshakerFactory.newHandshaker(message); if (handshaker == null) { this.log.info("client is incompatible!"); WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel()); return;//from w ww. j a v a2 s. c om } this.log.debug("handshaking now..."); this.handshaker.handshake(ctx.channel(), message).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { log.debug("Success!"); } else { log.debug("Failed!"); } } }); }
From source file:de.felix_klauke.pegasus.client.network.NettyClient.java
License:Apache License
/** * * You can send a new Object (would be cool when you choose a * {@link de.felix_klauke.pegasus.protocol.Packet}). * * @param object the object to send through our pipeline *///from w w w . j a v a 2s .c o m public void send(Object object) { System.out.println("Sending a packet"); ChannelFuture future = getChannel().writeAndFlush(object); future.addListener(new GenericFutureListener<Future<? super Void>>() { public void operationComplete(Future<? super Void> future) throws Exception { if (!future.isSuccess()) { future.cause().printStackTrace(); } } }); }
From source file:de.felix_klauke.pegasus.server.handler.listener.PacketMessageListener.java
License:Apache License
/** * Handle any incoming PacketMessage./* ww w .j ava 2 s.c om*/ * * @param channel the channel the message cam from * @param packet the packet */ public void handlePacket(Channel channel, PacketMessage packet) { PacketMessage packetMessage = new PacketMessage(packet.getMessage()); packetMessage.setAuthor(userManager.getUser(channel).getUsername()); for (User user : userManager.getUsers()) { if (user.getChannel().id() == channel.id()) continue; System.out.println("Sending Packet to: " + user.getUsername() + " -> " + packetMessage.getMessage()); ChannelFuture future = user.getChannel().writeAndFlush(packetMessage); future.addListener(new GenericFutureListener<Future<? super Void>>() { public void operationComplete(Future<? super Void> future) throws Exception { if (!future.isSuccess()) { future.cause().printStackTrace(); } } }); } }
From source file:de.felix_klauke.pegasus.server.handler.PacketHandler.java
License:Apache License
/** * * The Method everything is about. All incoming data will be handled by this method. * It will check all received data. When the object containing this data is an instance * of {@link de.felix_klauke.pegasus.protocol.Packet}. * * This is the Main Handler for Handshakes. * * The other packets will be/*from w w w .j a v a2s .c om*/ * passed to the method that will handle all incoming packets: * {@link de.felix_klauke.pegasus.server.handler.PacketHandler#handlePacket(Channel, Packet)} * * @param ctx the context of the channel that received the data * @param msg the data the channel received * @throws Exception the exception that occurs when receiving data fails */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { logger.info("Handling incoming data."); User user = userManager.getUser(ctx.pipeline().channel()); if (user == null) { System.out.println(msg.getClass()); if (msg instanceof PacketHandshake) { PacketHandshake packetHandshake = (PacketHandshake) msg; logger.info("Authenticating: " + packetHandshake.getUsername() + " with password " + packetHandshake.getPassword()); boolean success = userManager.authUser(packetHandshake.getUsername(), packetHandshake.getPassword()); PacketHandshakeResponse response = new PacketHandshakeResponse(); response.setResult(success ? HandshakeResult.SUCCESS : HandshakeResult.FAILURE); if (success) { userManager.createUser(packetHandshake.getUsername(), ctx.channel()); } ChannelFuture future = ctx.channel().writeAndFlush(response); future.addListener(new GenericFutureListener<Future<? super Void>>() { public void operationComplete(Future<? super Void> future) throws Exception { if (!future.isSuccess()) { future.cause().printStackTrace(); } } }); return; } ctx.pipeline().channel().close(); return; } if (msg instanceof Packet) { handlePacket(ctx.pipeline().channel(), (Packet) msg); } }
From source file:de.jackwhite20.japs.shared.nio.NioSocketClient.java
License:Open Source License
public boolean connect(String host, int port) { ChannelFuture channelFuture = new Bootstrap().group(PipelineUtils.newEventLoopGroup(1)) .channel(PipelineUtils.getChannel()).handler(new ClientChannelInitializer(this)) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT).connect(host, port); channelFuture.awaitUninterruptibly(); channel = channelFuture.channel();//from w ww . j av a 2s.co m CountDownLatch countDownLatch = new CountDownLatch(1); channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { connected = channelFuture.isSuccess(); countDownLatch.countDown(); } }); try { countDownLatch.await(2, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } return connected; }
From source file:de.ocarthon.core.network.tcp.TCPClient.java
License:Apache License
public boolean connect(String host, int port) throws InterruptedException { if (bootstrap != null) { ChannelFuture cf = bootstrap.connect(host, port); cf.sync();// www . jav a 2 s . c o m if (cf.isSuccess()) { this.channel = cf.channel(); } return cf.isSuccess(); } else { throw new IllegalStateException("TCPClient#setup() must be called first!"); } }
From source file:de.ocarthon.core.network.tcp.TCPServer.java
License:Apache License
public boolean bind(String host, int port) throws InterruptedException { ChannelFuture cf = this.bootstrap.bind(host, port); cf.sync();//from w w w.j av a 2s. c o m serverChannel = cf.channel(); return cf.isSuccess(); }
From source file:de.saxsys.synchronizefx.netty.base.client.NettyBasicClient.java
License:Open Source License
@Override public void connect() throws SynchronizeFXException { this.eventLoopGroup = new NioEventLoopGroup(); BasicChannelInitializerClient channelInitializer = createChannelInitializer(); channelInitializer.setTopologyCallback(callback); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, UnpooledByteBufAllocator.DEFAULT) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, TIMEOUT).handler(channelInitializer); LOG.info("Connecting to server"); try {// w w w .j a v a 2s. co m ChannelFuture future = bootstrap.connect(address); if (!future.await(TIMEOUT)) { disconnect(); throw new SynchronizeFXException("Timeout while trying to connect to the server."); } if (!future.isSuccess()) { disconnect(); throw new SynchronizeFXException("Connection to the server failed.", future.cause()); } this.channel = future.channel(); channel.closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() { @Override public void operationComplete(final Future<? super Void> future) throws Exception { // stop the event loop eventLoopGroup.shutdownGracefully(); } }); } catch (InterruptedException e) { disconnect(); throw new SynchronizeFXException(e); } }
From source file:de.unipassau.isl.evs.ssh.core.network.Client.java
License:Open Source License
/** * Tries to establish a TCP connection to the Server with the given host and port. * If the connect ist successful, {@link #getHandshakeHandler()} is used to add the * required Handlers to the pipeline.//from w w w. java 2s . c o m * If the connection fails, {@link #channelClosed(Channel)} is called until to many retries are made and the Client * switches to searching the master via UDP discovery using the {@link UDPDiscoveryClient}. */ private void connectClient(InetSocketAddress address) { Log.i(TAG, "Client connecting to " + address); notifyClientConnecting(address.getHostString(), address.getPort()); // TCP Connection Bootstrap b = new Bootstrap().group(requireComponent(ExecutionServiceComponent.KEY)) .channel(NioSocketChannel.class).handler(getHandshakeHandler()) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) TimeUnit.SECONDS.toMillis(5)); // Wait for the start of the client channelFuture = b.connect(address); channelFuture.addListener(new ChannelFutureListener() { /** * Called once the operation completes, either because the connect was successful or because of an error. */ @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { Log.v(TAG, "Channel open"); channelOpen(future.channel()); } else { Log.v(TAG, "Channel open failed"); channelClosed(future.channel()); } } }); channelFuture.channel().closeFuture().addListener(new ChannelFutureListener() { /** * Called once the connection is closed. */ @Override public void operationComplete(ChannelFuture future) throws Exception { Log.v(TAG, "Channel closed"); channelClosed(future.channel()); } }); }