Example usage for io.netty.util.concurrent Future cause

List of usage examples for io.netty.util.concurrent Future cause

Introduction

In this page you can find the example usage for io.netty.util.concurrent Future 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:de.unipassau.isl.evs.ssh.core.network.UDPDiscoveryClient.java

License:Open Source License

/**
 * Schedule the next run of {@link #sendDiscoveryRequest()} if it hasn't been scheduled yet.
 *
 * @return the Future returned by {@link io.netty.channel.EventLoop#schedule(Callable, long, TimeUnit)}
 */// w ww.  ja v a 2s . c om
private synchronized ScheduledFuture<?> scheduleDiscoveryRetry() {
    Log.v(TAG, "scheduleDiscoveryRetry()");
    // don't schedule a second execution if one is already pending
    final boolean isExecutionPending = retryFuture != null && !retryFuture.isDone();
    if (isDiscoveryRunning && !isExecutionPending) {
        if (requireComponent(Client.KEY).isChannelOpen() && timeout == 0) {
            Log.d(TAG,
                    "scheduleDiscoveryRetry() running indefinitely, but Client Channel is open. Was stopDiscovery called?");
        }
        retryFuture = requireComponent(ExecutionServiceComponent.KEY).schedule(new Runnable() {
            @Override
            public void run() {
                if (timeout > 0 && System.currentTimeMillis() > timeout) {
                    Log.i(TAG, "Stopping discovery after timeout");
                    stopDiscovery();
                }
                if (isDiscoveryRunning) {
                    sendDiscoveryRequest();
                    /* Mark this future as completed, so that the next discovery request will be scheduled.
                     * Otherwise retryFuture.isDone() would be false until this method terminates and the following
                     * recursive call wouldn't schedule the next execution. */
                    retryFuture = null;
                    scheduleDiscoveryRetry();
                }
            }
        }, CLIENT_MILLIS_BETWEEN_BROADCASTS, TimeUnit.MILLISECONDS);
        retryFuture.addListener(new FutureListener<Object>() {
            @Override
            public void operationComplete(Future future) throws Exception {
                if (!future.isSuccess()) {
                    Log.w(TAG, "Could not reschedule execution of UDP discovery", future.cause());
                }
            }
        });
    } else {
        Log.d(TAG, "not scheduling another retry because " + "isDiscoveryRunning = " + isDiscoveryRunning
                + ", retryFuture = " + retryFuture);
    }
    return retryFuture;
}

From source file:de.unipassau.isl.evs.ssh.slave.handler.SlaveDoorHandler.java

License:Open Source License

private void handleUnlatchDoor(final DoorPayload payload, final Message.AddressedMessage message) {
    Key<DoorBuzzer> key = new Key<>(DoorBuzzer.class, payload.getModuleName());
    requireComponent(key).unlock(7000).addListener(new FutureListener<Void>() {
        @Override/*from   www  .j av  a2s.  c  o m*/
        public void operationComplete(Future<Void> future) throws Exception {
            if (future.isSuccess()) {
                sendReply(message, new Message(new DoorPayload(payload.getModuleName())));
            } else {
                sendReply(message, new Message(new ErrorPayload(future.cause())));
            }
        }
    });
}

From source file:de.unipassau.isl.evs.ssh.slave.handler.SlaveLightHandler.java

License:Open Source License

private void handleSet(LightPayload payload, final Message.AddressedMessage original) {
    final Key<EdimaxPlugSwitch> key = new Key<>(EdimaxPlugSwitch.class, payload.getModule().getName());
    final EdimaxPlugSwitch plugSwitch = requireComponent(key);
    final boolean setOn = payload.getOn();

    plugSwitch.setOnAsync(setOn).addListener(new FutureListener<Boolean>() {
        @Override/*  w w w .  j a  va 2s  . c  om*/
        public void operationComplete(Future<Boolean> future) throws Exception {
            if (future.isSuccess()) {
                if (future.get() == Boolean.TRUE) {
                    replyStatus(original, setOn);
                } else {
                    sendReply(original, new Message(new ErrorPayload("Cannot switch light")));
                }
            } else {
                sendReply(original, new Message(new ErrorPayload(future.cause())));
            }
        }
    });
}

From source file:de.unipassau.isl.evs.ssh.slave.handler.SlaveLightHandler.java

License:Open Source License

private void handleGet(LightPayload payload, final Message.AddressedMessage original) {
    final Key<EdimaxPlugSwitch> key = new Key<>(EdimaxPlugSwitch.class, payload.getModule().getName());
    requireComponent(key).isOnAsync().addListener(new FutureListener<Boolean>() {
        @Override//w w  w  .  jav a 2s.com
        public void operationComplete(Future<Boolean> future) throws Exception {
            if (future.isSuccess()) {
                replyStatus(original, future.get());
            } else {
                sendReply(original, new Message(new ErrorPayload(future.cause())));
            }
        }
    });
}

From source file:divconq.api.HyperSession.java

License:Open Source License

public Channel allocateHttpChannel(final ChannelHandler handler, OperationResult or) {
    final AtomicReference<Future<Channel>> sslready = new AtomicReference<>();

    Bootstrap b = new Bootstrap();

    b.group(Hub.instance.getEventLoopGroup()).channel(NioSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator())
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override/* ww  w. j a v  a 2s.  c  om*/
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();

                    if (HyperSession.this.info.isSecurel()) {
                        SslHandler sh = new SslHandler(HyperSession.this.sslfac.getClientEngine());
                        sslready.set(sh.handshakeFuture());
                        pipeline.addLast("ssl", sh);
                    }

                    pipeline.addLast("decoder", new HttpResponseDecoder());
                    pipeline.addLast("encoder", new HttpRequestEncoder());

                    // TODO maybe
                    //pipeline.addLast("deflater", new HttpContentCompressor());

                    pipeline.addLast("handler", handler);
                }
            });

    or.info("Web Client connecting");

    try {
        // must wait here to make sure we don't release connectLock too soon
        // we want channel init (above) to complete before we try connect again
        ChannelFuture f = b.connect(this.info.getAddress()).sync();

        if (!f.isSuccess()) {
            or.error(1, "Web Client unable to successfully connect: " + f.cause());
        }

        // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent
        // that lets wait for the handshake to be done for sure
        if (sslready.get() != null) {
            Future<Channel> sf = sslready.get().sync();

            if (!sf.isSuccess()) {
                or.error(1, "Web Client unable to securely connect: " + sf.cause());
            }
        }

        if (handler instanceof ClientHandler)
            ((ClientHandler) handler).waitConnect();

        return f.channel();
    } catch (InterruptedException x) {
        or.error(1, "Web Client interrupted while connecting: " + x);
    } catch (Exception x) {
        or.error(1, "Web Client unable to connect: " + x);
    }

    return null;
}

From source file:divconq.api.HyperSession.java

License:Open Source License

public Channel allocateWsChannel(final ChannelHandler handler, OperationResult or) {
    final AtomicReference<Future<Channel>> sslready = new AtomicReference<>();

    Bootstrap b = new Bootstrap();

    b.group(Hub.instance.getEventLoopGroup()).option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator())
            .channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
                @Override//from   ww w  .ja va 2  s. c  o m
                public void initChannel(SocketChannel ch) throws Exception {
                    HttpHeaders customHeaders = new DefaultHttpHeaders();
                    customHeaders.add("x-DivConq-Mode", Hub.instance.getResources().getMode());

                    WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(
                            HyperSession.this.info.getUri(), WebSocketVersion.V13, null, false, customHeaders);

                    ChannelPipeline pipeline = ch.pipeline();

                    if (HyperSession.this.info.isSecurel()) {
                        SslHandler sh = new SslHandler(HyperSession.this.sslfac.getClientEngine());
                        sslready.set(sh.handshakeFuture());
                        pipeline.addLast("ssl", sh);
                    }

                    pipeline.addLast("http-codec", new HttpClientCodec());
                    pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
                    pipeline.addLast("ws-handler", new WebSocketClientProtocolHandler(handshaker));

                    pipeline.addLast("handler", handler);

                    /*
                    pipeline.addLast("handler", new SimpleChannelInboundHandler<Object>() {
                            
                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
                     System.out.println("read: " + msg);
                    }
                            
                            
                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                      super.userEventTriggered(ctx, evt);
                              
                      Logger.debug("ue: " + evt);
                    }
                    });
                    */
                }
            });

    or.info("Web Client connecting");

    try {
        // must wait here to make sure we don't release connectLock too soon
        // we want channel init (above) to complete before we try connect again
        ChannelFuture f = b.connect(this.info.getAddress()).sync();

        if (!f.isSuccess()) {
            or.error(1, "Web Client unable to successfully connect: " + f.cause());
        }

        // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent
        // that lets wait for the handshake to be done for sure
        if (sslready.get() != null) {
            Future<Channel> sf = sslready.get().sync();

            if (!sf.isSuccess()) {
                or.error(1, "Web Client unable to securely connect: " + sf.cause());
            }
        }

        if (handler instanceof ClientHandler)
            ((ClientHandler) handler).waitConnect();

        return f.channel();
    } catch (InterruptedException x) {
        or.error(1, "Web Client interrupted while connecting: " + x);
    } catch (Exception x) {
        or.error(1, "Web Client unable to connect: " + x);
    }

    return null;
}

From source file:divconq.api.internal.DownloadHandler.java

License:Open Source License

public Channel allocateChannel(final HyperSession parent, OperationResult or) {
    final AtomicReference<Future<Channel>> sslready = new AtomicReference<>();

    Bootstrap b = new Bootstrap();

    b.group(Hub.instance.getEventLoopGroup()).channel(NioSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator())
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override/*  w w w . j a v  a2  s  .  co  m*/
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();

                    if (parent.getInfo().isSecurel()) {
                        SslHandler sh = new SslHandler(parent.getSsl().getClientEngine());
                        sslready.set(sh.handshakeFuture());
                        pipeline.addLast("ssl", sh);
                    }

                    pipeline.addLast("codec", new HttpClientCodec());
                    //pipeline.addLast("decoder", new HttpResponseDecoder());
                    //pipeline.addLast("encoder", new HttpRequestEncoder());

                    pipeline.addLast("handler", DownloadHandler.this);
                }
            });

    or.info("Web Data Client connecting");

    try {
        // must wait here to make sure we don't release connectLock too soon
        // we want chanel init (above) to complete before we try connect again
        ChannelFuture f = b.connect(parent.getInfo().getAddress()).sync();

        if (!f.isSuccess()) {
            or.error(1, "Web Data Client unable to successfully connect: " + f.cause());
        }

        // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent
        // that lets wait for the handshake to be done for sure
        if (sslready.get() != null) {
            Future<Channel> sf = sslready.get().sync();

            if (!sf.isSuccess()) {
                or.error(1, "Web Data Client unable to securely connect: " + sf.cause());
            }
        }

        return f.channel();
    } catch (InterruptedException x) {
        or.error(1, "Web Data Client interrupted while connecting: " + x);
    } catch (Exception x) {
        or.error(1, "Web Data Client unable to connect: " + x);
    }

    return null;
}

From source file:divconq.api.internal.UploadPostHandler.java

License:Open Source License

public Channel allocateChannel(final HyperSession parent, OperationResult or) {
    final AtomicReference<Future<Channel>> sslready = new AtomicReference<>();

    Bootstrap b = new Bootstrap();

    b.group(Hub.instance.getEventLoopGroup()).channel(NioSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator())
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override//ww w.ja v  a 2  s. com
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();

                    if (parent.getInfo().isSecurel()) {
                        SslHandler sh = new SslHandler(parent.getSsl().getClientEngine());
                        sslready.set(sh.handshakeFuture());
                        pipeline.addLast("ssl", sh);
                    }

                    pipeline.addLast("codec", new HttpClientCodec());

                    // Remove the following line if you don't want automatic content decompression.
                    //pipeline.addLast("inflater", new HttpContentDecompressor());

                    // to be used since huge file transfer
                    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());

                    // so we can get the upload response (200 or not)
                    pipeline.addLast("handler", UploadPostHandler.this);
                }
            });

    System.out.println("Web Data Client connecting");

    try {
        // must wait here to make sure we don't release connectLock too soon
        // we want chanel init (above) to complete before we try connect again
        ChannelFuture f = b.connect(parent.getInfo().getAddress()).sync();

        if (!f.isSuccess()) {
            or.error(1, "Web Client unable to successfully connect: " + f.cause());
            System.out.println("Web Client unable to successfully connect: " + f.cause());
        }

        // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent
        // that lets wait for the handshake to be done for sure
        if (sslready.get() != null) {
            Future<Channel> sf = sslready.get().sync();

            if (!sf.isSuccess()) {
                or.error(1, "Web Client unable to securely connect: " + sf.cause());
                System.out.println("Web Client unable to securely connect: " + sf.cause());
            }
        }

        return f.channel();
    } catch (InterruptedException x) {
        or.error(1, "Web Client interrupted while connecting: " + x);
        System.out.println("Web Client interrupted while connecting: " + x);
    } catch (Exception x) {
        or.error(1, "Web Client unable to connect: " + x);
        System.out.println("Web Client unable to connect: " + x);
    }

    return null;
}

From source file:divconq.api.internal.UploadPutHandler.java

License:Open Source License

public Channel allocateChannel(final HyperSession parent, OperationResult or) {
    final AtomicReference<Future<Channel>> sslready = new AtomicReference<>();

    Bootstrap b = new Bootstrap();

    b.group(Hub.instance.getEventLoopGroup()).channel(NioSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator())
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override//from  www .  j ava 2s.  c  o  m
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();

                    if (parent.getInfo().isSecurel()) {
                        SslHandler sh = new SslHandler(parent.getSsl().getClientEngine());
                        sslready.set(sh.handshakeFuture());
                        pipeline.addLast("ssl", sh);
                    }

                    pipeline.addLast("codec", new HttpClientCodec());

                    // so we can get the upload response (200 or not)
                    pipeline.addLast("handler", UploadPutHandler.this);
                }
            });

    or.info("Web Data Client connecting");

    try {
        // must wait here to make sure we don't release connectLock too soon
        // we want chanel init (above) to complete before we try connect again
        ChannelFuture f = b.connect(parent.getInfo().getAddress()).sync();

        if (!f.isSuccess()) {
            or.error(1, "Web Client unable to successfully connect: " + f.cause());
        }

        // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent
        // that lets wait for the handshake to be done for sure
        if (sslready.get() != null) {
            Future<Channel> sf = sslready.get().sync();

            if (!sf.isSuccess()) {
                or.error(1, "Web Client unable to securely connect: " + sf.cause());
            }
        }

        return f.channel();
    } catch (InterruptedException x) {
        or.error(1, "Web Client interrupted while connecting: " + x);
    } catch (Exception x) {
        or.error(1, "Web Client unable to connect: " + x);
    }

    return null;
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    if (!startTls && engine.getUseClientMode()) {
        // issue and handshake and add a listener to it which will fire an exception event if
        // an exception was thrown while doing the handshake
        handshake().addListener(new GenericFutureListener<Future<Channel>>() {
            @Override/*  www  . j av  a 2  s. c om*/
            public void operationComplete(Future<Channel> future) throws Exception {
                if (!future.isSuccess()) {
                    logger.debug("Failed to complete handshake", future.cause());
                    ctx.close();
                }
            }
        });
    }
    ctx.fireChannelActive();
}