Java Code Examples for io.netty.channel.ChannelPromise#setSuccess()

The following examples show how to use io.netty.channel.ChannelPromise#setSuccess() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OioSctpServerChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture unbindAddress(final InetAddress localAddress, final ChannelPromise promise) {
    if (eventLoop().inEventLoop()) {
        try {
            sch.unbindAddress(localAddress);
            promise.setSuccess();
        } catch (Throwable t) {
            promise.setFailure(t);
        }
    } else {
        eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                unbindAddress(localAddress, promise);
            }
        });
    }
    return promise;
}
 
Example 2
Source File: AbstractEpollStreamChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static void shutdownDone(ChannelFuture shutdownOutputFuture,
                          ChannelFuture shutdownInputFuture,
                          ChannelPromise promise) {
    Throwable shutdownOutputCause = shutdownOutputFuture.cause();
    Throwable shutdownInputCause = shutdownInputFuture.cause();
    if (shutdownOutputCause != null) {
        if (shutdownInputCause != null) {
            logger.debug("Exception suppressed because a previous exception occurred.",
                    shutdownInputCause);
        }
        promise.setFailure(shutdownOutputCause);
    } else if (shutdownInputCause != null) {
        promise.setFailure(shutdownInputCause);
    } else {
        promise.setSuccess();
    }
}
 
Example 3
Source File: ClientTest.java    From xio with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitialConnectedWrites() throws Exception {
  ChannelPromise connectPromise = channel.newPromise();
  when(connectionManager.connectionState()).thenReturn(ClientConnectionState.NOT_CONNECTED);
  when(connectionManager.connect()).thenReturn(connectPromise);
  Request request1 = requestFactory("req1");
  Optional<ChannelFuture> result1Future = subject.write(request1);

  verify(connectionManager).connect();

  Request proxiedRequest1BeforeConnection = channel.readOutbound();
  assertNull(proxiedRequest1BeforeConnection);

  connectPromise.setSuccess();

  Request proxiedRequest1AfterConnectionComplete = channel.readOutbound();
  assertEquals(proxiedRequest1AfterConnectionComplete.path(), request1.path());

  assertTrue(result1Future.get().isDone() && result1Future.get().isSuccess());
}
 
Example 4
Source File: OioSocketChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static void shutdownDone(ChannelFuture shutdownOutputFuture,
                                 ChannelFuture shutdownInputFuture,
                                 ChannelPromise promise) {
    Throwable shutdownOutputCause = shutdownOutputFuture.cause();
    Throwable shutdownInputCause = shutdownInputFuture.cause();
    if (shutdownOutputCause != null) {
        if (shutdownInputCause != null) {
            logger.debug("Exception suppressed because a previous exception occurred.",
                    shutdownInputCause);
        }
        promise.setFailure(shutdownOutputCause);
    } else if (shutdownInputCause != null) {
        promise.setFailure(shutdownInputCause);
    } else {
        promise.setSuccess();
    }
}
 
Example 5
Source File: NioSctpServerChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture unbindAddress(final InetAddress localAddress, final ChannelPromise promise) {
    if (eventLoop().inEventLoop()) {
        try {
            javaChannel().unbindAddress(localAddress);
            promise.setSuccess();
        } catch (Throwable t) {
            promise.setFailure(t);
        }
    } else {
        eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                unbindAddress(localAddress, promise);
            }
        });
    }
    return promise;
}
 
Example 6
Source File: NioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture unbindAddress(final InetAddress localAddress, final ChannelPromise promise) {
    if (eventLoop().inEventLoop()) {
        try {
            javaChannel().unbindAddress(localAddress);
            promise.setSuccess();
        } catch (Throwable t) {
            promise.setFailure(t);
        }
    } else {
        eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                unbindAddress(localAddress, promise);
            }
        });
    }
    return promise;
}
 
Example 7
Source File: Lz4FrameEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelPromise promise) {
    if (finished) {
        promise.setSuccess();
        return promise;
    }
    finished = true;

    final ByteBuf footer = ctx.alloc().heapBuffer(
            compressor.maxCompressedLength(buffer.readableBytes()) + HEADER_LENGTH);
    flushBufferedData(footer);

    final int idx = footer.writerIndex();
    footer.setLong(idx, MAGIC_NUMBER);
    footer.setByte(idx + TOKEN_OFFSET, (byte) (BLOCK_TYPE_NON_COMPRESSED | compressionLevel));
    footer.setInt(idx + COMPRESSED_LENGTH_OFFSET, 0);
    footer.setInt(idx + DECOMPRESSED_LENGTH_OFFSET, 0);
    footer.setInt(idx + CHECKSUM_OFFSET, 0);

    footer.writerIndex(idx + HEADER_LENGTH);

    return ctx.writeAndFlush(footer, promise);
}
 
Example 8
Source File: OioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture bindAddress(final InetAddress localAddress, final ChannelPromise promise) {
    if (eventLoop().inEventLoop()) {
        try {
            ch.bindAddress(localAddress);
            promise.setSuccess();
        } catch (Throwable t) {
            promise.setFailure(t);
        }
    } else {
        eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                bindAddress(localAddress, promise);
            }
        });
    }
    return promise;
}
 
Example 9
Source File: NioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture bindAddress(final InetAddress localAddress, final ChannelPromise promise) {
    if (eventLoop().inEventLoop()) {
        try {
            javaChannel().bindAddress(localAddress);
            promise.setSuccess();
        } catch (Throwable t) {
            promise.setFailure(t);
        }
    } else {
        eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                bindAddress(localAddress, promise);
            }
        });
    }
    return promise;
}
 
Example 10
Source File: TokenClientPromiseHolder.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
public static <T> boolean completePromise(int xid, ClusterResponse<T> response) {
    if (!PROMISE_MAP.containsKey(xid)) {
        return false;
    }
    SimpleEntry<ChannelPromise, ClusterResponse> entry = PROMISE_MAP.get(xid);
    if (entry != null) {
        ChannelPromise promise = entry.getKey();
        if (promise.isDone() || promise.isCancelled()) {
            return false;
        }
        entry.setValue(response);
        promise.setSuccess();
        return true;
    }
    return false;
}
 
Example 11
Source File: HttpResponseHandler.java    From netty-cookbook with Apache License 2.0 6 votes vote down vote up
@Override
protected void messageReceived(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
    Integer streamId = msg.headers().getInt(HttpUtil.ExtensionHeaderNames.STREAM_ID.text());
    if (streamId == null) {
        System.err.println("HttpResponseHandler unexpected message received: " + msg);
        return;
    }

    ChannelPromise promise = streamidPromiseMap.get(streamId);
    if (promise == null) {
        System.err.println("Message received for unknown stream id " + streamId);
    } else {
        // Do stuff with the message (for now just print it)
        ByteBuf content = msg.content();
        if (content.isReadable()) {
            int contentLength = content.readableBytes();
            byte[] arr = new byte[contentLength];
            content.readBytes(arr);
            System.out.println(new String(arr, 0, contentLength, CharsetUtil.UTF_8));
        }

        promise.setSuccess();
    }
}
 
Example 12
Source File: NettyClientHandler.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
/**
 * Cancels this stream.
 */
private void cancelStream(ChannelHandlerContext ctx, CancelClientStreamCommand cmd,
    ChannelPromise promise) {
  NettyClientStream.TransportState stream = cmd.stream();
  PerfMark.startTask("NettyClientHandler.cancelStream", stream.tag());
  PerfMark.linkIn(cmd.getLink());
  try {
    Status reason = cmd.reason();
    if (reason != null) {
      stream.transportReportStatus(reason, true, new Metadata());
    }
    if (!cmd.stream().isNonExistent()) {
      encoder().writeRstStream(ctx, stream.id(), Http2Error.CANCEL.code(), promise);
    } else {
      promise.setSuccess();
    }
  } finally {
    PerfMark.stopTask("NettyClientHandler.cancelStream", stream.tag());
  }
}
 
Example 13
Source File: HttpResponseHandler.java    From http2-examples with Apache License 2.0 6 votes vote down vote up
@Override
protected void messageReceived(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
    Integer streamId = msg.headers().getInt(HttpUtil.ExtensionHeaderNames.STREAM_ID.text());
    if (streamId == null) {
        System.err.println("HttpResponseHandler unexpected message received: " + msg);
        return;
    }

    ChannelPromise promise = streamidPromiseMap.get(streamId);
    if (promise == null) {
        System.err.println("Message received for unknown stream id " + streamId);
    } else {
        // Do stuff with the message (for now just print it)
        ByteBuf content = msg.content();
        if (content.isReadable()) {
            int contentLength = content.readableBytes();
            byte[] arr = new byte[contentLength];
            content.readBytes(arr);
            System.out.println("After " + StopWatch.getInstance().currentTimeInSeconds() + " seconds: " + new String(arr, 0, contentLength));
        }

        promise.setSuccess();
    }
}
 
Example 14
Source File: OioSctpServerChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture bindAddress(final InetAddress localAddress, final ChannelPromise promise) {
    if (eventLoop().inEventLoop()) {
        try {
            sch.bindAddress(localAddress);
            promise.setSuccess();
        } catch (Throwable t) {
            promise.setFailure(t);
        }
    } else {
        eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                bindAddress(localAddress, promise);
            }
        });
    }
    return promise;
}
 
Example 15
Source File: OioSocketChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void shutdownInput0(ChannelPromise promise) {
    try {
        socket.shutdownInput();
        promise.setSuccess();
    } catch (Throwable t) {
        promise.setFailure(t);
    }
}
 
Example 16
Source File: OioDatagramChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelFuture leaveGroup(InetAddress multicastAddress, ChannelPromise promise) {
    try {
        socket.leaveGroup(multicastAddress);
        promise.setSuccess();
    } catch (IOException e) {
        promise.setFailure(e);
    }
    return promise;
}
 
Example 17
Source File: MockChannelHandlerContext.java    From karyon with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise) {
    promise.setSuccess();
    return promise;
}
 
Example 18
Source File: EmbeddedChannelTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
    queue.add(DISCONNECT);
    promise.setSuccess();
}
 
Example 19
Source File: MockChannelHandlerContext.java    From karyon with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelFuture disconnect(ChannelPromise promise) {
    promise.setSuccess();
    return promise;
}
 
Example 20
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public void close(final ChannelPromise promise) {
    if (!promise.setUncancellable()) {
        return;
    }
    if (closeInitiated) {
        if (closePromise.isDone()) {
            // Closed already.
            promise.setSuccess();
        } else if (!(promise instanceof VoidChannelPromise)) { // Only needed if no VoidChannelPromise.
            // This means close() was called before so we just register a listener and return
            closePromise.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    promise.setSuccess();
                }
            });
        }
        return;
    }
    closeInitiated = true;

    closePending = false;
    fireChannelReadPending = false;

    // Only ever send a reset frame if the connection is still alive as otherwise it makes no sense at
    // all anyway.
    if (parent().isActive() && !streamClosedWithoutError && isStreamIdValid(stream().id())) {
        Http2StreamFrame resetFrame = new DefaultHttp2ResetFrame(Http2Error.CANCEL).stream(stream());
        write(resetFrame, unsafe().voidPromise());
        flush();
    }

    if (inboundBuffer != null) {
        for (;;) {
            Object msg = inboundBuffer.poll();
            if (msg == null) {
                break;
            }
            ReferenceCountUtil.release(msg);
        }
    }

    // The promise should be notified before we call fireChannelInactive().
    outboundClosed = true;
    closePromise.setSuccess();
    promise.setSuccess();

    pipeline().fireChannelInactive();
    if (isRegistered()) {
        deregister(unsafe().voidPromise());
    }
}