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

The following examples show how to use io.grpc.stub.ServerCallStreamObserver#onNext() . 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: BenchmarkGRpcServerServiceImpl.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void streamingFromServer(Messages.SimpleRequest request, StreamObserver<Messages.SimpleResponse> responseObserver) {
    final ServerCallStreamObserver<Messages.SimpleResponse> callStreamObserver = (ServerCallStreamObserver<Messages.SimpleResponse>) responseObserver;

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

        callStreamObserver.onNext(response);
    }

    if (!callStreamObserver.isCancelled()) {
        callStreamObserver.onCompleted();
    }
}
 
Example 2
Source File: MemoryCAS.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
@Override
public void get(
    Digest digest,
    long offset,
    long limit,
    ServerCallStreamObserver<ByteString> blobObserver,
    RequestMetadata requestMetadata) {
  Blob blob = get(digest);
  if (blob == null) {
    if (delegate != null) {
      // FIXME change this to a read-through get
      delegate.get(digest, offset, limit, blobObserver, requestMetadata);
    } else {
      blobObserver.onError(io.grpc.Status.NOT_FOUND.asException());
    }
  } else {
    blobObserver.onNext(blob.getData());
    blobObserver.onCompleted();
  }
}
 
Example 3
Source File: AsyncServer.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public StreamObserver<Messages.SimpleRequest> streamingCall(
    final StreamObserver<Messages.SimpleResponse> observer) {
  final ServerCallStreamObserver<Messages.SimpleResponse> responseObserver =
      (ServerCallStreamObserver<Messages.SimpleResponse>) observer;
  // TODO(spencerfang): flow control to stop reading when !responseObserver.isReady
  return new StreamObserver<Messages.SimpleRequest>() {
    @Override
    public void onNext(Messages.SimpleRequest value) {
      if (shutdown.get()) {
        responseObserver.onCompleted();
        return;
      }
      responseObserver.onNext(Utils.makeResponse(value));
    }

    @Override
    public void onError(Throwable t) {
      // other side closed with non OK
      responseObserver.onError(t);
    }

    @Override
    public void onCompleted() {
      // other side closed with OK
      responseObserver.onCompleted();
    }
  };
}
 
Example 4
Source File: ExecutionService.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
KeepaliveWatcher createWatcher(
    ServerCallStreamObserver<Operation> serverCallStreamObserver,
    RequestMetadata requestMetadata) {
  return new KeepaliveWatcher(serverCallStreamObserver) {
    @Override
    void deliver(Operation operation) {
      if (operation != null) {
        metricsPublisher.publishRequestMetadata(operation, requestMetadata);
      }
      serverCallStreamObserver.onNext(operation);
    }
  };
}
 
Example 5
Source File: ByteStreamService.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
ServerCallStreamObserver<ReadResponse> onErrorLogReadObserver(
    String name, long offset, ServerCallStreamObserver<ReadResponse> delegate) {
  return new UniformDelegateServerCallStreamObserver<ReadResponse>(delegate) {
    long responseCount = 0;
    long responseBytes = 0;

    @Override
    public void onNext(ReadResponse response) {
      delegate.onNext(response);
      responseCount++;
      responseBytes += response.getData().size();
    }

    @Override
    public void onError(Throwable t) {
      Status status = Status.fromThrowable(t);
      if (status.getCode() != Code.NOT_FOUND) {
        java.util.logging.Level level = Level.SEVERE;
        if (responseCount > 0 && status.getCode() == Code.DEADLINE_EXCEEDED
            || status.getCode() == Code.CANCELLED) {
          level = Level.WARNING;
        }
        String message = format("error reading %s at offset %d", name, offset);
        if (responseCount > 0) {
          message +=
              format(" after %d responses and %d bytes of content", responseCount, responseBytes);
        }
        logger.log(level, message, t);
      }
      delegate.onError(t);
    }

    @Override
    public void onCompleted() {
      delegate.onCompleted();
    }
  };
}
 
Example 6
Source File: ByteStreamService.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
ServerCallStreamObserver<ByteString> newChunkObserver(
    ServerCallStreamObserver<ReadResponse> responseObserver) {
  return new DelegateServerCallStreamObserver<ByteString, ReadResponse>(responseObserver) {
    @Override
    public void onNext(ByteString data) {
      while (!data.isEmpty()) {
        ByteString slice;
        if (data.size() > CHUNK_SIZE) {
          slice = data.substring(0, CHUNK_SIZE);
          data = data.substring(CHUNK_SIZE);
        } else {
          slice = data;
          data = ByteString.EMPTY;
        }
        responseObserver.onNext(ReadResponse.newBuilder().setData(slice).build());
      }
    }

    @Override
    public void onError(Throwable t) {
      responseObserver.onError(t);
    }

    @Override
    public void onCompleted() {
      responseObserver.onCompleted();
    }
  };
}
 
Example 7
Source File: GrpcFlowControlTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public StreamObserver<SimpleRequest> serverBackPressure(
        StreamObserver<SimpleResponse> rawResponseObserver) {
    final ServerCallStreamObserver<SimpleResponse> responseObserver =
            (ServerCallStreamObserver<SimpleResponse>) rawResponseObserver;
    responseObserver.disableAutoInboundFlowControl();
    final AtomicInteger numRequests = new AtomicInteger();
    responseObserver.request(1);
    return new StreamObserver<SimpleRequest>() {
        @Override
        public void onNext(SimpleRequest value) {
            if (numRequests.incrementAndGet() < CAPPED_NUM_MESSAGES) {
                responseObserver.request(1);
            } else {
                for (int i = 0; i < TOTAL_NUM_MESSAGES; i++) {
                    responseObserver.onNext(SimpleResponse.getDefaultInstance());
                }
                responseObserver.onCompleted();
            }
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onCompleted() {
        }
    };
}
 
Example 8
Source File: CompressingHelloWorldServerPerMethod.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> plainResponseObserver) {
  ServerCallStreamObserver<HelloReply> responseObserver =
      (ServerCallStreamObserver<HelloReply>) plainResponseObserver;
  /* This line by itself enables compression for the response */
  responseObserver.setCompression("gzip");
  HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
  responseObserver.onNext(reply);
  responseObserver.onCompleted();
}
 
Example 9
Source File: AsyncServer.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public StreamObserver<Messages.SimpleRequest> streamingCall(
    final StreamObserver<Messages.SimpleResponse> observer) {
  final ServerCallStreamObserver<Messages.SimpleResponse> responseObserver =
      (ServerCallStreamObserver<Messages.SimpleResponse>) observer;
  // TODO(spencerfang): flow control to stop reading when !responseObserver.isReady
  return new StreamObserver<Messages.SimpleRequest>() {
    @Override
    public void onNext(Messages.SimpleRequest value) {
      if (shutdown.get()) {
        responseObserver.onCompleted();
        return;
      }
      responseObserver.onNext(Utils.makeResponse(value));
    }

    @Override
    public void onError(Throwable t) {
      // other side closed with non OK
      responseObserver.onError(t);
    }

    @Override
    public void onCompleted() {
      // other side closed with OK
      responseObserver.onCompleted();
    }
  };
}
 
Example 10
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();
        }
    };
}