Package com.facebook.presto.jdbc.internal.netty.channel

Examples of com.facebook.presto.jdbc.internal.netty.channel.ChannelFuture


            }
            return Channels.succeededFuture(ctx.getChannel());
        }

        ChannelBuffer footer;
        ChannelFuture future;
        synchronized (z) {
            try {
                // Configure input.
                z.next_in = EMPTY_ARRAY;
                z.next_in_index = 0;
                z.avail_in = 0;

                // Configure output.
                byte[] out = new byte[32]; // room for ADLER32 + ZLIB / CRC32 + GZIP header
                z.next_out = out;
                z.next_out_index = 0;
                z.avail_out = out.length;

                // Write the ADLER32 checksum (stream footer).
                int resultCode = z.deflate(JZlib.Z_FINISH);
                if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) {
                    future = Channels.failedFuture(
                            ctx.getChannel(),
                            ZlibUtil.exception(z, "compression failure", resultCode));
                    footer = null;
                } else if (z.next_out_index != 0) {
                    future = Channels.future(ctx.getChannel());
                    footer =
                        ctx.getChannel().getConfig().getBufferFactory().getBuffer(
                                out, 0, z.next_out_index);
                } else {
                    // Note that we should never use a SucceededChannelFuture
                    // here just in case any downstream handler or a sink wants
                    // to notify a write error.
                    future = Channels.future(ctx.getChannel());
                    footer = ChannelBuffers.EMPTY_BUFFER;
                }
            } finally {
                z.deflateEnd();

                // Deference the external references explicitly to tell the VM that
                // the allocated byte arrays are temporary so that the call stack
                // can be utilized.
                // I'm not sure if the modern VMs do this optimization though.
                z.next_in = null;
                z.next_out = null;
            }
        }

        if (footer != null) {
            Channels.write(ctx, future, footer);
        }

        if (evt != null) {
            future.addListener(new ChannelFutureListener() {
                public void operationComplete(ChannelFuture future) throws Exception {
                    ctx.sendDownstream(evt);
                }
            });
        }
View Full Code Here


        super.handleDownstream(ctx, evt);
    }

    private ChannelFuture finishEncode(final ChannelHandlerContext ctx, final ChannelEvent evt) {
        ChannelFuture future = Channels.succeededFuture(ctx.getChannel());

        if (!finished.compareAndSet(false, true)) {
            if (evt != null) {
                ctx.sendDownstream(evt);
            }
            return future;
        }

        ChannelBuffer footer = ChannelBuffers.dynamicBuffer(ctx.getChannel().getConfig().getBufferFactory());
        synchronized (deflater) {
            deflater.finish();
            while (!deflater.finished()) {
                int numBytes = deflater.deflate(out, 0, out.length);
                footer.writeBytes(out, 0, numBytes);
            }
            if (gzip) {
                int crcValue = (int) crc.getValue();
                int uncBytes = deflater.getTotalIn();
                footer.writeByte(crcValue);
                footer.writeByte(crcValue >>> 8);
                footer.writeByte(crcValue >>> 16);
                footer.writeByte(crcValue >>> 24);
                footer.writeByte(uncBytes);
                footer.writeByte(uncBytes >>> 8);
                footer.writeByte(uncBytes >>> 16);
                footer.writeByte(uncBytes >>> 24);
            }
            deflater.end();
        }

        if (footer.readable()) {
            future = Channels.future(ctx.getChannel());
            Channels.write(ctx, future, footer);
        }

        if (evt != null) {
            future.addListener(new ChannelFutureListener() {
                public void operationComplete(ChannelFuture future) throws Exception {
                    ctx.sendDownstream(evt);
                }
            });
        }
View Full Code Here

        if (msg instanceof HttpRequest) {

            HttpRequest httpRequest = (HttpRequest) msg;
            SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpRequest);
            currentStreamId = spdySynStreamFrame.getStreamId();
            ChannelFuture future = getMessageFuture(ctx, e, currentStreamId, httpRequest);
            Channels.write(ctx, future, spdySynStreamFrame, e.getRemoteAddress());

        } else if (msg instanceof HttpResponse) {

            HttpResponse httpResponse = (HttpResponse) msg;
            if (httpResponse.containsHeader(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID)) {
                SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpResponse);
                currentStreamId = spdySynStreamFrame.getStreamId();
                ChannelFuture future = getMessageFuture(ctx, e, currentStreamId, httpResponse);
                Channels.write(ctx, future, spdySynStreamFrame, e.getRemoteAddress());
            } else {
                SpdySynReplyFrame spdySynReplyFrame = createSynReplyFrame(httpResponse);
                currentStreamId = spdySynReplyFrame.getStreamId();
                ChannelFuture future = getMessageFuture(ctx, e, currentStreamId, httpResponse);
                Channels.write(ctx, future, spdySynReplyFrame, e.getRemoteAddress());
            }

        } else if (msg instanceof HttpChunk) {
View Full Code Here

                spdyDataFrame.setLast(true);
                Channels.write(ctx, future, spdyDataFrame, remoteAddress);
            }
        } else {
            SpdyDataFrame[] spdyDataFrames = createSpdyDataFrames(streamId, chunk.getContent());
            ChannelFuture dataFuture = getDataFuture(ctx, future, spdyDataFrames, remoteAddress);

            // Trigger a write
            dataFuture.setSuccess();
        }
    }
View Full Code Here

    private static ChannelFuture getDataFuture(
            ChannelHandlerContext ctx, ChannelFuture future,
            SpdyDataFrame[] spdyDataFrames, SocketAddress remoteAddress) {

        ChannelFuture dataFuture = future;
        for (int i = spdyDataFrames.length; --i >= 0;) {
            future = Channels.future(ctx.getChannel());
            future.addListener(new SpdyFrameWriter(ctx, new DownstreamMessageEvent(
                    ctx.getChannel(), dataFuture, spdyDataFrames[i], remoteAddress)));
            dataFuture = future;
View Full Code Here

            throws Exception {

        long timeoutMillis = getTimeoutMillis(e);
        if (timeoutMillis > 0) {
            // Set timeout only when getTimeoutMillis() returns a positive value.
            ChannelFuture future = e.getFuture();
            final Timeout timeout = timer.newTimeout(
                    new WriteTimeoutTask(ctx, future),
                    timeoutMillis, TimeUnit.MILLISECONDS);

            future.addListener(new TimeoutCanceller(timeout));
        }

        super.writeRequested(ctx, e);
    }
View Full Code Here

        for (int i = 0; i < data.length; i ++) {
            data[i] = (ChannelBuffer) pendingWrites.get(i).getMessage();
        }

        ChannelBuffer composite = ChannelBuffers.wrappedBuffer(data);
        ChannelFuture future = Channels.future(ctx.getChannel());
        future.addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future)
                    throws Exception {
                if (future.isSuccess()) {
                    for (MessageEvent e: pendingWrites) {
                        e.getFuture().setSuccess();
                    }
                } else {
                    Throwable cause = future.getCause();
                    for (MessageEvent e: pendingWrites) {
                        e.getFuture().setFailure(cause);
                    }
                }
            }
View Full Code Here

                                // ChunkedInput.nextChunk() returned null and it has
                                // not reached at the end of input.  Let's wait until
                                // more chunks arrive.  Nothing to write or notify.
                                break;
                            } else {
                                ChannelFuture writeFuture;
                                if (endOfInput) {
                                    this.currentEvent = null;
                                    writeFuture = currentEvent.getFuture();

                                    // Register a listener which will close the input once the write
                                    // is complete. This is needed because the Chunk may have some
                                    // resource bound that can not be closed before its not written
                                    //
                                    // See https://github.com/netty/netty/issues/303
                                    writeFuture.addListener(new ChannelFutureListener() {

                                        public void operationComplete(ChannelFuture future) throws Exception {
                                            closeInput(chunks);
                                        }
                                    });
                                } else {
                                    writeFuture = future(channel);
                                    writeFuture.addListener(new ChannelFutureListener() {
                                        public void operationComplete(ChannelFuture future) throws Exception {
                                            if (!future.isSuccess()) {
                                                currentEvent.getFuture().setFailure(future.getCause());
                                                closeInput((ChunkedInput) currentEvent.getMessage());
                                            }
View Full Code Here

        }
    }

    private void openConnection(boolean isSsl, InetSocketAddress remoteAddress, ConnectionCallback connectionCallback)
    {
        ChannelFuture future = bootstrap.connect(remoteAddress);
        if (isSsl) {
            future.addListener(new SslConnectionListener(remoteAddress, connectionCallback, openChannels));
        }
        else {
            future.addListener(new CallbackConnectionListener(remoteAddress, connectionCallback, openChannels));
        }
    }
View Full Code Here

                sslEngine.setSSLParameters(sslParameters);
                sslEngine.setUseClientMode(true);

                SslHandler sslHandler = new SslHandler(sslEngine);
                future.getChannel().getPipeline().addBefore("codec", "ssl", sslHandler);
                ChannelFuture handshakeFuture = sslHandler.handshake();
                handshakeFuture.addListener(callbackConnectionListener);
            }
            else {
                callbackConnectionListener.operationComplete(future);
            }
        }
View Full Code Here

TOP

Related Classes of com.facebook.presto.jdbc.internal.netty.channel.ChannelFuture

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.