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

The following examples show how to use io.grpc.stub.ServerCallStreamObserver#setCompression() . 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: TestServiceImpl.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Immediately responds with a payload of the type and size specified in the request.
 */
@Override
public void unaryCall(SimpleRequest req, StreamObserver<SimpleResponse> responseObserver) {
  ServerCallStreamObserver<SimpleResponse> obs =
      (ServerCallStreamObserver<SimpleResponse>) responseObserver;
  SimpleResponse.Builder responseBuilder = SimpleResponse.newBuilder();
  try {
    if (req.hasResponseCompressed() && req.getResponseCompressed().getValue()) {
      obs.setCompression("gzip");
    } else {
      obs.setCompression("identity");
    }
  } catch (IllegalArgumentException e) {
    obs.onError(Status.UNIMPLEMENTED
        .withDescription("compression not supported.")
        .withCause(e)
        .asRuntimeException());
    return;
  }

  if (req.getResponseSize() != 0) {
    // For consistency with the c++ TestServiceImpl, use a random offset for unary calls.
    // TODO(wonderfly): whether or not this is a good approach needs further discussion.
    int offset = random.nextInt(compressableBuffer.size());
    ByteString payload = generatePayload(compressableBuffer, offset, req.getResponseSize());
    responseBuilder.setPayload(
        Payload.newBuilder()
            .setBody(payload));
  }

  if (req.hasResponseStatus()) {
    obs.onError(Status.fromCodeValue(req.getResponseStatus().getCode())
        .withDescription(req.getResponseStatus().getMessage())
        .asRuntimeException());
    return;
  }

  responseObserver.onNext(responseBuilder.build());
  responseObserver.onCompleted();
}
 
Example 2
Source File: GrpcServiceServerTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public void staticUnaryCallSetsMessageCompression(SimpleRequest request,
                                                  StreamObserver<SimpleResponse> responseObserver) {
    if (!request.equals(REQUEST_MESSAGE)) {
        responseObserver.onError(new IllegalArgumentException("Unexpected request: " + request));
        return;
    }
    final ServerCallStreamObserver<SimpleResponse> callObserver =
            (ServerCallStreamObserver<SimpleResponse>) responseObserver;
    callObserver.setCompression("gzip");
    callObserver.setMessageCompression(true);
    responseObserver.onNext(RESPONSE_MESSAGE);
    responseObserver.onCompleted();
}
 
Example 3
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 4
Source File: TestServiceImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Immediately responds with a payload of the type and size specified in the request.
 */
@Override
public void unaryCall(SimpleRequest req, StreamObserver<SimpleResponse> responseObserver) {
  ServerCallStreamObserver<SimpleResponse> obs =
      (ServerCallStreamObserver<SimpleResponse>) responseObserver;
  SimpleResponse.Builder responseBuilder = SimpleResponse.newBuilder();
  try {
    if (req.hasResponseCompressed() && req.getResponseCompressed().getValue()) {
      obs.setCompression("gzip");
    } else {
      obs.setCompression("identity");
    }
  } catch (IllegalArgumentException e) {
    obs.onError(Status.UNIMPLEMENTED
        .withDescription("compression not supported.")
        .withCause(e)
        .asRuntimeException());
    return;
  }

  if (req.getResponseSize() != 0) {
    // For consistency with the c++ TestServiceImpl, use a random offset for unary calls.
    // TODO(wonderfly): whether or not this is a good approach needs further discussion.
    int offset = random.nextInt(compressableBuffer.size());
    ByteString payload = generatePayload(compressableBuffer, offset, req.getResponseSize());
    responseBuilder.setPayload(
        Payload.newBuilder()
            .setBody(payload));
  }

  if (req.hasResponseStatus()) {
    obs.onError(Status.fromCodeValue(req.getResponseStatus().getCode())
        .withDescription(req.getResponseStatus().getMessage())
        .asRuntimeException());
    return;
  }

  responseObserver.onNext(responseBuilder.build());
  responseObserver.onCompleted();
}
 
Example 5
Source File: TestServiceImpl.java    From armeria with Apache License 2.0 4 votes vote down vote up
/**
 * Immediately responds with a payload of the type and size specified in the request.
 */
@Override
public void unaryCall(SimpleRequest req, StreamObserver<SimpleResponse> responseObserver) {
    final ServerCallStreamObserver<SimpleResponse> obs =
            (ServerCallStreamObserver<SimpleResponse>) responseObserver;
    final SimpleResponse.Builder responseBuilder = SimpleResponse.newBuilder();
    try {
        switch (req.getResponseCompression()) {
            case DEFLATE:
                // fallthrough, just use gzip
            case GZIP:
                obs.setCompression("gzip");
                break;
            case NONE:
                obs.setCompression("identity");
                break;
            case UNRECOGNIZED:
                // fallthrough
            default:
                obs.onError(Status.INVALID_ARGUMENT
                                    .withDescription("Unknown: " + req.getResponseCompression())
                                    .asRuntimeException());
                return;
        }
    } catch (IllegalArgumentException e) {
        obs.onError(Status.UNIMPLEMENTED
                            .withDescription("compression not supported.")
                            .withCause(e)
                            .asRuntimeException());
        return;
    }

    if (req.getResponseSize() != 0) {
        final boolean compressable = compressableResponse(req.getResponseType());
        final ByteString dataBuffer = compressable ? compressableBuffer : uncompressableBuffer;
        // For consistency with the c++ TestServiceImpl, use a random offset for unary calls.
        // TODO(wonderfly): whether or not this is a good approach needs further discussion.
        final int offset = random.nextInt(
                compressable ? compressableBuffer.size() : uncompressableBuffer.size());
        final ByteString payload = generatePayload(dataBuffer, offset, req.getResponseSize());
        responseBuilder.getPayloadBuilder()
                       .setType(compressable ? PayloadType.COMPRESSABLE : PayloadType.UNCOMPRESSABLE)
                       .setBody(payload);
    }

    if (req.hasResponseStatus()) {
        obs.onError(Status.fromCodeValue(req.getResponseStatus().getCode())
                          .withDescription(req.getResponseStatus().getMessage())
                          .asRuntimeException());
        return;
    }

    responseObserver.onNext(responseBuilder.build());
    responseObserver.onCompleted();
}