Example usage for io.netty.channel ChannelFuture cause

List of usage examples for io.netty.channel ChannelFuture cause

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture cause.

Prototype

Throwable cause();

Source Link

Document

Returns the cause of the failed I/O operation if the I/O operation has failed.

Usage

From source file:org.jupiter.transport.netty.channel.NettyChannel.java

License:Apache License

@Override
public JChannel close(final JFutureListener<JChannel> listener) {
    final JChannel jChannel = this;
    channel.close().addListener(new ChannelFutureListener() {

        @Override//from w  w w  .  j  a  va 2s . c  o m
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                listener.operationSuccess(jChannel);
            } else {
                listener.operationFailure(jChannel, future.cause());
            }
        }
    });
    return jChannel;
}

From source file:org.jupiter.transport.netty.channel.NettyChannel.java

License:Apache License

@Override
public JChannel write(Object msg, final JFutureListener<JChannel> listener) {
    final JChannel jChannel = this;
    channel.writeAndFlush(msg).addListener(new ChannelFutureListener() {

        @Override/*w  w w .  ja va 2  s  .  c om*/
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                listener.operationSuccess(jChannel);
            } else {
                listener.operationFailure(jChannel, future.cause());
            }
        }
    });
    return jChannel;
}

From source file:org.kaaproject.kaa.server.transports.tcp.transport.netty.KaaTcpEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof MqttFrame)) {
        LOG.warn("Message is not a {}", MqttFrame.class.getSimpleName());
        super.write(ctx, msg, promise);
    } else {//from  ww w . j  a v  a  2s  . c o  m
        MqttFrame frame = (MqttFrame) msg;
        byte[] data = frame.getFrame().array();
        if (LOG.isTraceEnabled()) {
            LOG.trace("Sending {} data for frame {}", Arrays.toString(data), frame);
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace(
                    "Channel promise before writeAndFlush isSuccess [{}] isDone [{}]"
                            + " isCancelled [{}] for frame {}",
                    promise.isSuccess(), promise.isDone(), promise.isCancelled(), frame);
        }
        ChannelFuture future = ctx.writeAndFlush(data, promise);
        if (LOG.isTraceEnabled()) {
            LOG.trace(
                    "Returned future [{}] isSuccess [{}] isDone [{}] isCancelled [{}] cause [{}]"
                            + " for frame {}",
                    future, future.isSuccess(), future.isDone(), future.isCancelled(), future.cause(), frame);
            if (future.cause() != null) {
                LOG.trace("Write operation failed due to:", future.cause());
            }
        }
        if (frame.isNeedCloseConnection()) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
}

From source file:org.lanternpowered.pingy.Pingy.java

License:MIT License

/**
 * Starts the pingy server.//  ww  w  .  j av  a 2 s.co m
 *
 * @throws IOException
 */
public void start() throws IOException {
    boolean epoll = false;

    if (this.properties.isUseEpollWhenAvailable()) {
        if (Epoll.isAvailable()) {
            debugInfo("Epoll is available");
            epoll = true;
        } else {
            debugWarn(
                    "Epoll is unavailable (The following exception is only used to print the cause why it's unavailable, "
                            + "it won't affect the functionality.)");
            //noinspection ThrowableResultOfMethodCallIgnored
            debug(() -> Epoll.unavailabilityCause().printStackTrace());
        }
    }

    final ServerBootstrap bootstrap = new ServerBootstrap();
    final EventLoopGroup group = epoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();

    final ChannelFuture future = bootstrap.group(group)
            .channel(epoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ReadTimeoutHandler(20))
                            .addLast(new PingyLegacyHandler(properties)).addLast(new PingyFramingHandler())
                            .addLast(new PingyHandler(properties));
                }
            }).childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true)
            .bind(getBindAddress(this.properties.getIp(), this.properties.getPort()));
    final Channel channel = future.awaitUninterruptibly().channel();
    if (!channel.isActive()) {
        final Throwable cause = future.cause();
        if (cause instanceof BindException) {
            throw (BindException) cause;
        }
        throw new RuntimeException("Failed to bind to address", cause);
    }
    info("Successfully bound to: " + channel.localAddress());
}

From source file:org.lanternpowered.server.LanternServer.java

License:MIT License

private void bind() throws BindException {
    final InetSocketAddress address = this.getBindAddress(this.game.getGlobalConfig().getServerPort());
    final boolean useEpollWhenAvailable = this.game.getGlobalConfig().useServerEpollWhenAvailable();

    ProtocolState.init();//from   w  w  w  .  jav a  2  s . c om
    final ChannelFuture future = this.networkManager.init(address, useEpollWhenAvailable);
    final Channel channel = future.awaitUninterruptibly().channel();
    if (!channel.isActive()) {
        final Throwable cause = future.cause();
        if (cause instanceof BindException) {
            throw (BindException) cause;
        }
        throw new RuntimeException("Failed to bind to address", cause);
    }

    this.logger.info("Successfully bound to: " + channel.localAddress());
}

From source file:org.mobicents.media.server.ctrl.rtsp.stack.RtspClientStackImpl.java

License:Open Source License

public void start() throws IOException {

    Bootstrap b = new Bootstrap();
    b.group(workerGroup);//from w  w w  . j  a v a2  s.c om
    b.channel(NioSocketChannel.class);
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            // httpResponse??HttpResponseDecoder?
            ch.pipeline().addLast(new RtspResponseDecoder());
            // ??httprequest?HttpRequestEncoder?
            ch.pipeline().addLast(new RtspRequestEncoder());
            // ch.pipeline().addLast(new HttpObjectAggregator(1024 * 64));
            ch.pipeline().addLast("handler", new RtspResponseHandler(RtspClientStackImpl.this));
        }
    });

    // Start the client.
    ChannelFuture f = b.connect(getHost(), getPort());
    try {
        f.sync();
        channel = f.channel();

        InetSocketAddress bindAddress = new InetSocketAddress(this.host, this.port);

        logger.info("Mobicents RTSP Client started and bound to " + bindAddress.toString());

    } catch (InterruptedException e) {
        throw new IOException(f.cause());
    }
}

From source file:org.mobicents.media.server.ctrl.rtsp.stack.RtspClientStackImpl.java

License:Open Source License

public void sendRequest(HttpRequest rtspRequest, String remoteHost, int remotePort) {

    ChannelFuture future = null;
    if (channel == null || (channel != null && !channel.isOpen())) {
        // Start the connection attempt.
        future = bootstrap.connect(new InetSocketAddress(remoteHost, remotePort));

        // Wait until the connection attempt succeeds or fails.
        channel = future.awaitUninterruptibly().channel();
        if (!future.isSuccess()) {
            future.cause().printStackTrace();
            // bootstrap.releaseExternalResources();
            return;
        }// w ww. j a v  a 2 s.co  m
    }

    channel.writeAndFlush(rtspRequest);
}

From source file:org.mobicents.media.server.rtsp.stack.RtspClientStackImpl.java

License:Open Source License

public void connect() throws IOException {

    Bootstrap b = new Bootstrap();
    b.group(workerGroup);//  www. j av  a  2s  .  c om
    b.channel(NioSocketChannel.class);
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            // ??httpResponse???HttpResponseDecoder?
            ch.pipeline().addLast(new RtspResponseDecoder());
            // ?ttprequest?HttpRequestEncoder?
            ch.pipeline().addLast(new RtspRequestEncoder());
            // ch.pipeline().addLast(new HttpObjectAggregator(1024 * 64));
            ch.pipeline().addLast("handler", new RtspResponseHandler(RtspClientStackImpl.this));
        }
    });

    // Start the client.
    ChannelFuture f = b.connect(getHost(), getPort());
    try {
        f.sync();
        channel = f.channel();

        InetSocketAddress bindAddress = new InetSocketAddress(this.host, this.port);

        logger.info("Mobicents RTSP Client started and bound to " + bindAddress.toString());

        RtspResponseHandler handler = (RtspResponseHandler) f.channel().pipeline().get("handler");
        handler.connect();
    } catch (InterruptedException e) {
        throw new IOException(f.cause());
    }
}

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 w  w  w  .  ja  va2s .  c om*/
                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.onesec.raven.rtp.NettyRtpConnector.java

License:Apache License

private Channel getChannel(ChannelFuture future) throws IOException {
    if (future.isDone()) {
        if (future.isSuccess())
            return future.channel();
        else//from w  w  w.  j av a2  s.c  o m
            throw new IOException(future.cause());
    } else
        try {
            return getChannel(future.await());
        } catch (InterruptedException e) {
            throw new IOException(e);
        }
}