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:jlibs.wamp4j.netty.NettyWebSocketClient.java

License:Apache License

@Override
public void connect(final URI uri, final ConnectListener listener, final String... subProtocols) {
    String protocol = uri.getScheme();
    if (!protocol.equals("ws"))
        throw new IllegalArgumentException("invalid protocol: " + protocol);
    int port = uri.getPort();
    if (port == -1)
        port = 80;/* www  .  j  a  v  a 2 s  .c o m*/

    Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri,
                            WebSocketVersion.V13, Util.toString(subProtocols), false, new DefaultHttpHeaders());
                    ch.pipeline().addLast(new HttpClientCodec(), new HttpObjectAggregator(8192),
                            new WebSocketClientProtocolHandler(handshaker),
                            new HandshakeListener(handshaker, listener));
                }
            });
    bootstrap.connect(uri.getHost(), port).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                assert !future.channel().isOpen();
                listener.onError(future.cause());
            }
        }
    });
}

From source file:jlibs.wamp4j.netty.NettyWebSocketServer.java

License:Apache License

@Override
public void bind(final URI uri, final String subProtocols[], final AcceptListener listener) {
    int port = uri.getPort();
    if (port == -1)
        port = 80;//from  ww w . j a v a2  s.c om
    ServerBootstrap bootstrap = new ServerBootstrap().group(eventLoopGroup)
            .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new HttpServerCodec(), new HttpObjectAggregator(65536),
                            new Handshaker(uri, listener, subProtocols));
                }
            });
    bootstrap.bind(uri.getHost(), port).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                channel = future.channel();
                channel.attr(ACCEPT_LISTENER).set(listener);
                listener.onBind(NettyWebSocketServer.this);
            } else
                listener.onError(future.cause());
        }
    });
}

From source file:jlibs.wamp4j.netty.NettyWebSocketServer.java

License:Apache License

@Override
public void close() {
    channel.close().addListener(new ChannelFutureListener() {
        @Override//from   www . j ava 2s.  com
        public void operationComplete(ChannelFuture future) throws Exception {
            AcceptListener acceptListener = channel.attr(ACCEPT_LISTENER).get();
            if (!future.isSuccess())
                acceptListener.onError(future.cause());
            acceptListener.onClose(NettyWebSocketServer.this);
        }
    });
}

From source file:me.ferrybig.javacoding.teamspeakconnector.TeamspeakApi.java

License:Open Source License

public Future<TeamspeakConnection> connect(SocketAddress addr) {
    Promise<TeamspeakConnection> prom = this.group.next().newPromise();
    ChannelFuture channel = openChannel(addr, new TeamspeakConnectionInitizer(prom, 20000), 20000);
    channel.addListener(future -> {/*  w  w w  . jav a 2s  .co  m*/
        if (!channel.isSuccess()) {
            prom.setFailure(new TeamspeakException("Connection failed", channel.cause()));
        }
    });
    return prom;
}

From source file:me.ferrybig.javacoding.webmapper.MainServer.java

private synchronized Listener addListener0(String host, int port, SslContext sslCtx) throws ListenerException {
    try {// ww w.ja v  a2 s.co m
        Listener listener = new Listener(host, port, sslCtx != null);
        if (this.listeners.containsKey(listener)) {
            return listener;
        }

        WebServerInitializer init;
        if (sslCtx != null) {
            init = new WebSslServerInitializer(sslCtx, this, sessions, mapper, listener);
        } else {
            init = new WebServerInitializer(this, sessions, mapper, listener);
        }
        b.childHandler(init);
        ChannelFuture f;
        if (listener.getHost() == null)
            f = b.bind(port);
        else
            f = b.bind(host, port);
        Channel ch = f.sync().channel();
        if (f.cause() != null) {
            throw new ListenerException("Unable to bind listener", f.cause());
        }
        LOGGER.log(Level.INFO, "Started listener on: {0}", listener.toURL());
        this.listeners.put(listener, ch);
        return listener;
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new ListenerException(ex);
    }
}

From source file:me.jtalk.socketconnector.io.TCPManager.java

License:Open Source License

public long connect(InetSocketAddress target) throws ResourceException {
    try {// w  w w  . j  a  va  2s  .c  o m
        log.finest(String.format("Connection initialization to %s: starting", target));

        ChannelFuture completed = this.client.connect(target).sync();
        if (!completed.isSuccess()) {
            throw new EISSystemException("Connection failed", completed.cause());
        }
        Receiver handler = completed.channel().pipeline().get(Receiver.class);
        long connId = handler.getId();

        return connId;

    } catch (InterruptedException e) {
        throw new EISSystemException("Execution interrupted during connecting to remote client", e);
    }
}

From source file:me.jtalk.socketconnector.io.TCPManager.java

License:Open Source License

public long listen(InetSocketAddress local) throws ResourceException {
    try {//ww  w. j a v a  2  s  .  c om
        log.finest(String.format("Listening to %s: starting", local));

        ChannelFuture completed = this.server.bind(local).sync();
        if (!completed.isSuccess()) {
            throw new EISSystemException("Listening failed", completed.cause());
        }
        long connId = this.ids.incrementAndGet();

        log.finest(String.format("Listening to %s: connection id %d", local, connId));
        this.register(connId, completed.channel(), true);

        return connId;

    } catch (InterruptedException e) {
        throw new EISSystemException("Execution interrupted during listening setup", e);
    }
}

From source file:mmo.server.Server.java

License:Open Source License

public void run(String host, int port) {
    parentGroup = new NioEventLoopGroup();
    childGroup = new NioEventLoopGroup();

    new ServerBootstrap().group(parentGroup, childGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new HttpServerCodec(), //
                            new HttpObjectAggregator(65536), //
                            routeHandlerProvider.get());

                }/*from   w  w  w . jav  a2 s .  c  o  m*/
            }).option(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(16384)).bind(host, port)
            .addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        L.error("Error setting up server channel: {}", future.cause(), null);
                        new Thread(() -> { // TODO shutdown program
                            // gracefuller
                            try {
                                shutdown();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } finally {
                                System.exit(1);
                            }
                        }).start();
                    }
                }
            });
}

From source file:nenea.client.operation.UptimeClient.java

License:Apache License

static void connect(Bootstrap b) {
    b.connect().addListener(new ChannelFutureListener() {
        @Override/* w  w w. ja  va 2  s  .  co  m*/
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.cause() != null) {
                uptimeHandler.startTime = -1;
                uptimeHandler.println("Failed to connect: " + future.cause());
            }
        }
    });
}

From source file:net.hasor.rsf.remoting.transport.customer.InnerClientManager.java

License:Apache License

private synchronized AbstractRsfClient connSocket(final Address hostAddress) {
    final URL hostURL = hostAddress.getAddress();
    Bootstrap boot = new Bootstrap();
    boot.group(this.rsfContext.getLoopGroup());
    boot.channel(NioSocketChannel.class);
    boot.option(ChannelOption.SO_KEEPALIVE, true);
    boot.handler(new ChannelInitializer<SocketChannel>() {
        public void initChannel(SocketChannel ch) throws Exception {
            Channel channel = ch.pipeline().channel();
            NetworkConnection.initConnection(hostURL, channel);
            LoggerHelper.logInfo("initConnection connect %s.", hostURL);
            ////from   w  w w  . j  a va 2s.  c om
            ch.pipeline().addLast(new RSFCodec(), new InnerRsfCustomerHandler(getRequestManager()));
        }
    });
    ChannelFuture future = null;
    SocketAddress remote = new InetSocketAddress(hostURL.getHost(), hostURL.getPort());
    LoggerHelper.logInfo("connect to %s ...", hostURL);
    future = boot.connect(remote);
    try {
        future.await();
    } catch (InterruptedException e) {
        LoggerHelper.logSevere("connect to %s failure , %s", hostURL, e.getMessage());
        return null;
    }
    if (future.isSuccess() == true) {
        LoggerHelper.logInfo("remote %s connected.", hostURL);
        NetworkConnection conn = NetworkConnection.getConnection(future.channel());
        return new InnerRsfClient(this.getRequestManager(), conn);
    }
    //
    try {
        LoggerHelper.logSevere("connect to %s failure , %s", hostURL, future.cause().getMessage());
        future.channel().close().await();
    } catch (InterruptedException e) {
        LoggerHelper.logSevere("close connect(%s) failure , %s", hostURL, e.getMessage());
    }
    return null;
}