Java Code Examples for io.grpc.stub.ServerCallStreamObserver#setOnReadyHandler()

The following examples show how to use io.grpc.stub.ServerCallStreamObserver#setOnReadyHandler() . 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: ProtoReflectionService.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public StreamObserver<ServerReflectionRequest> serverReflectionInfo(
    final StreamObserver<ServerReflectionResponse> responseObserver) {
  final ServerCallStreamObserver<ServerReflectionResponse> serverCallStreamObserver =
      (ServerCallStreamObserver<ServerReflectionResponse>) responseObserver;
  ProtoReflectionStreamObserver requestObserver =
      new ProtoReflectionStreamObserver(updateIndexIfNecessary(), serverCallStreamObserver);
  serverCallStreamObserver.setOnReadyHandler(requestObserver);
  serverCallStreamObserver.disableAutoInboundFlowControl();
  serverCallStreamObserver.request(1);
  return requestObserver;
}
 
Example 2
Source File: BenchmarkGRpcServerServiceImpl.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public StreamObserver<Messages.SimpleRequest> streamingBothWays(StreamObserver<Messages.SimpleResponse> responseObserver) {
    final ServerCallStreamObserver<Messages.SimpleResponse> callStreamObserver = (ServerCallStreamObserver<Messages.SimpleResponse>) responseObserver;

    callStreamObserver.setOnReadyHandler(() -> {
        for (Messages.SimpleResponse response : responses) {
            if (callStreamObserver.isCancelled()) {
                return;
            }

            callStreamObserver.onNext(response);
        }

        if (!callStreamObserver.isCancelled()) {
            callStreamObserver.onCompleted();
        }
    });

    return new StreamObserver<Messages.SimpleRequest>() {
        @Override
        public void onNext(Messages.SimpleRequest value) {

        }

        @Override
        public void onError(Throwable t) {

        }

        @Override
        public void onCompleted() {

        }
    };
}
 
Example 3
Source File: ProtoReflectionService.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public StreamObserver<ServerReflectionRequest> serverReflectionInfo(
    final StreamObserver<ServerReflectionResponse> responseObserver) {
  final ServerCallStreamObserver<ServerReflectionResponse> serverCallStreamObserver =
      (ServerCallStreamObserver<ServerReflectionResponse>) responseObserver;
  ProtoReflectionStreamObserver requestObserver =
      new ProtoReflectionStreamObserver(getRefreshedIndex(), serverCallStreamObserver);
  serverCallStreamObserver.setOnReadyHandler(requestObserver);
  serverCallStreamObserver.disableAutoRequest();
  serverCallStreamObserver.request(1);
  return requestObserver;
}
 
Example 4
Source File: GrpcFlowControlTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public StreamObserver<SimpleRequest> clientBackPressure(
        StreamObserver<SimpleResponse> rawResponseObserver) {
    final AtomicInteger numRequests = new AtomicInteger();
    final AtomicInteger numResponses = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    final ServerCallStreamObserver<SimpleResponse> responseObserver =
            (ServerCallStreamObserver<SimpleResponse>) rawResponseObserver;
    responseObserver.setOnReadyHandler(() -> {
        if (numResponses.get() < TOTAL_NUM_MESSAGES && !done.get()) {
            numResponses.incrementAndGet();
            responseObserver.onNext(RESPONSE);
        }
    });
    for (int i = 0; i < TOTAL_NUM_MESSAGES; i++) {
        if (responseObserver.isReady()) {
            numResponses.incrementAndGet();
            responseObserver.onNext(RESPONSE);
        } else {
            break;
        }
    }
    return new StreamObserver<SimpleRequest>() {
        @Override
        public void onNext(SimpleRequest value) {
            numRequests.incrementAndGet();
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onCompleted() {
            done.set(true);
            assertThat(numRequests).hasValue(TOTAL_NUM_MESSAGES);
            assertThat(numResponses).hasValueBetween(CAPPED_NUM_MESSAGES, CAPPED_NUM_MESSAGES + 2);
            responseObserver.onCompleted();
        }
    };
}
 
Example 5
Source File: GrpcCommandService.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Override
public StreamObserver<PCmdMessage> handleCommand(StreamObserver<PCmdRequest> requestObserver) {
    final Long transportId = getTransportId();
    final AgentInfo agentInfo = getAgentInfo();

    final List<Integer> supportCommandCodeList = getSupportCommandCodeList();

    logger.debug("{} => local. handleCommand(). transportId:{}, supportCommandCodeList{}", agentInfo, transportId, supportCommandCodeList);

    final RequestManager requestManager = new RequestManager(timer, 3000);
    final PinpointGrpcServer pinpointGrpcServer = new PinpointGrpcServer(getRemoteAddress(), agentInfo, requestManager, requestObserver);

    final boolean registered = grpcServerRepository.registerIfAbsent(transportId, pinpointGrpcServer);
    if (!registered) {
        logger.warn("Duplicate PCmdRequestStream found. Terminate stream. {} transportId:{}", agentInfo, transportId);
        requestObserver.onError(new StatusException(Status.ALREADY_EXISTS));
        return DisabledStreamObserver.DISABLED_INSTANCE;
    }

    final ServerCallStreamObserver<PCmdRequest> serverCallStreamObserver = (ServerCallStreamObserver<PCmdRequest>) requestObserver;
    serverCallStreamObserver.setOnReadyHandler(new Runnable() {
        public void run() {
            if (serverCallStreamObserver.isReady()) {
                logger.info("{} => local. ready() transportId:{}", agentInfo.getAgentKey(), transportId);
                pinpointGrpcServer.connected();

                // new operation for handshake
                if (supportCommandCodeList == Header.SUPPORT_COMMAND_CODE_LIST_NOT_EXIST) {
                    return;
                }
                registerAgentCommandList(pinpointGrpcServer, supportCommandCodeList);
            }
        }
    });

    final StreamObserver<PCmdMessage> responseObserver = new StreamObserver<PCmdMessage>() {
        @Override
        public void onNext(PCmdMessage value) {
            // old operation for handshake
            if (value.hasHandshakeMessage()) {
                List<Integer> supportCommandServiceKeyList = value.getHandshakeMessage().getSupportCommandServiceKeyList();
                registerAgentCommandList(pinpointGrpcServer, supportCommandServiceKeyList);
            } else if (value.hasFailMessage()) {
                PCmdResponse failMessage = value.getFailMessage();
                pinpointGrpcServer.handleFail(failMessage);
            }
        }

        @Override
        public void onError(Throwable t) {
            final StatusError statusError = StatusErrors.throwable(t);
            if (statusError.isSimpleError()) {
                logger.info("Failed to command stream, {} => local, cause={}", getAgentInfo().getAgentKey(), statusError.getMessage());
            } else {
                logger.warn("Failed to command stream, {} => local, cause={}", getAgentInfo().getAgentKey(), statusError.getMessage(), statusError.getThrowable());
            }
            pinpointGrpcServer.disconnected();
        }

        @Override
        public void onCompleted() {
            if (logger.isDebugEnabled()) {
                logger.debug("{} => local. onCompleted", getAgentInfo().getAgentKey());
            }
            pinpointGrpcServer.disconnected();
        }

    };
    return responseObserver;
}