List of usage examples for io.netty.channel ChannelFutureListener ChannelFutureListener
ChannelFutureListener
From source file:org.maodian.flyingcat.netty.handler.XmppXMLStreamHandler.java
License:Apache License
@Override public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception { // discard xml declaration if (StringUtils.startsWith(msg, "<?xml ")) { return;//from w w w . j a va2s .c o m } // deal with </stream:stream> if (StringUtils.contains(msg, ":stream") && StringUtils.contains(msg, "</")) { xmppContext.destroy(); if (initCloseingStream) { log.info("Close Stream and underhood socket due to requested by server"); ctx.channel().close(); } else if (ctx.channel().isOpen()) { ctx.write("</stream:stream>").addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { log.info("Close Stream and underhood socket due to requested by client"); future.channel().close(); } }); } else { log.info( "Won't respond client's close stream request since the channel has been closed (may because ssl has been closed)"); } return; } xmppContext.parseXML(msg); }
From source file:org.maodian.flyingcat.xmpp.state.DefaultElementVisitor.java
License:Apache License
@Override public State handleTLS(XmppContext xmppCtx, TLS tls) throws XMLStreamException { ChannelHandlerContext ctx = xmppCtx.getNettyChannelHandlerContext(); SSLEngine engine = SecureSslContextFactory.getServerContext().createSSLEngine(); engine.setUseClientMode(false);//from ww w. ja v a2s . co m SslHandler sslHandler = new SslHandler(engine, true); sslHandler.sslCloseFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { log.info("Close the socket since SSL connection has been closed by client"); future.channel().close(); } }); ctx.pipeline().addFirst("ssl", sslHandler); StringWriter writer = new StringWriter(); XMLStreamWriter xmlsw = XMLOutputFactoryHolder.getXMLOutputFactory().createXMLStreamWriter(writer); xmlsw.writeEmptyElement("", "proceed", XmppNamespace.TLS); xmlsw.setPrefix("", XmppNamespace.TLS); xmlsw.writeNamespace("", XmppNamespace.TLS); xmlsw.writeEndDocument(); xmppCtx.flush(writer.toString()); return xmppCtx.getGlobalContext().getTlsStreamState(); }
From source file:org.msgpack.rpc.impl.netty.NettyTcpClientTransport.java
License:Apache License
public void sendMessage(final Message msg) { if (_writables.isEmpty() && _availables.getAndDecrement() > 0) { startConnection().addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { final Channel connected = future.channel(); sendMessageChannel(connected, msg); connected.closeFuture().addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture channelFuture) throws Exception { _availables.incrementAndGet(); }//from w ww.j a v a 2s. co m }); } }); } else { final Channel writable = _writables.poll(); if (writable != null) { sendMessageChannel(writable, msg); } else { Thread.yield(); sendMessage(msg); } } }
From source file:org.msgpack.rpc.impl.netty.NettyTcpClientTransport.java
License:Apache License
protected ChannelFuture sendMessageChannel(Channel c, Object msg) { return c.writeAndFlush(msg).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { _writables.offer(future.channel()); }/* w ww .ja v a 2s . c om*/ }); }
From source file:org.msrpenabler.server.net.NioMsrpSockServerBootStrap.java
License:Apache License
public void run() throws Exception { // Configure the server. ServerBootstrap b = new ServerBootstrap(); b = b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 500) // maximum queue length sock waiting to be accepted .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() { // Create a new socket / channel / pipeline for each new Cnx @Override/*from ww w. j a v a 2 s.c o m*/ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); ch.pipeline().addLast(new NioMsrpMsgDecoder()); ch.pipeline().addLast(new NioMsrpMsgEncoder()); ch.pipeline().addLast("msrpHandler", new MsgRcvHandler(addrServ)); } }); // Start the server. futureBind = b.bind(addrServ.inetAddr, addrServ.port); futureBind.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { logger.info(" Bind success on {}:{}", addrServ.inetAddr, addrServ.port); } else { logger.error(" Bind failed on {}:{}", addrServ.inetAddr, addrServ.port, future.cause()); } } }); }
From source file:org.opendaylight.controller.netconf.client.NetconfClientSessionNegotiator.java
License:Open Source License
/** * Initiates exi communication by sending start-exi message and waiting for positive/negative response. * * @param startExiMessage/*from w w w. j av a 2 s . c o m*/ */ void tryToInitiateExi(final NetconfClientSession session, final NetconfStartExiMessage startExiMessage) { channel.pipeline().addAfter(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, ExiConfirmationInboundHandler.EXI_CONFIRMED_HANDLER, new ExiConfirmationInboundHandler(session, startExiMessage)); session.sendMessage(startExiMessage).addListener(new ChannelFutureListener() { @Override public void operationComplete(final ChannelFuture f) { if (!f.isSuccess()) { LOG.warn("Failed to send start-exi message {} on session {}", startExiMessage, this, f.cause()); channel.pipeline().remove(ExiConfirmationInboundHandler.EXI_CONFIRMED_HANDLER); } else { LOG.trace("Start-exi message {} sent to socket on session {}", startExiMessage, this); } } }); }
From source file:org.opendaylight.controller.netconf.nettyutil.handler.ssh.SshHandler.java
License:Open Source License
@Override public void connect(final ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {/*from w w w .ja v a2 s . com*/ ctx.connect(remoteAddress, localAddress, promise); promise.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture channelFuture) { sshClientAdapter.start(ctx); } }); }
From source file:org.opendaylight.controller.netconf.util.handler.ssh.SshHandler.java
License:Open Source License
@Override public void connect(final ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { ctx.connect(remoteAddress, localAddress, promise); promise.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture channelFuture) throws Exception { sshClientAdapter.start(ctx); }/*from ww w. j a v a2 s .c om*/ }); }
From source file:org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin.java
License:Open Source License
public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer, final MessageType packetType) { InetAddress ip = InetAddresses.forString(new String(address.getIpAddress().getValue())); InetSocketAddress recipient = new InetSocketAddress(ip, address.getPort().getValue()); // the wrappedBuffer() method doesn't copy data, so this conversion shouldn't hurt performance ByteBuf data = wrappedBuffer(outBuffer.array()); DatagramPacket packet = new DatagramPacket(data, recipient); LOG.debug("Sending {} on port {} to address: {}", packetType, address.getPort().getValue(), ip); if (LOG.isTraceEnabled()) { LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data)); }//from w ww. j a va2 s .c om channel.write(packet).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { LOG.trace("Success"); statistics.incrementTx(packetType.getIntValue()); } else { LOG.warn("Failed to send packet"); statistics.incrementTxErrors(); } } }); channel.flush(); }
From source file:org.opendaylight.netconf.impl.NetconfServerSession.java
License:Open Source License
@Override public ChannelFuture sendMessage(final NetconfMessage netconfMessage) { final ChannelFuture channelFuture = super.sendMessage(netconfMessage); if (netconfMessage instanceof NetconfNotification) { outNotification++;/*from w ww. j a v a 2 s. c o m*/ sessionListener.onNotification(this, (NetconfNotification) netconfMessage); } // delayed close was set, close after the message was sent if (delayedClose) { channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(final ChannelFuture future) throws Exception { close(); } }); } return channelFuture; }