Search in sources :

Example 1 with Listener

use of io.grpc.ServerCall.Listener in project grpc-java by grpc.

the class TransportCompressionTest method startServer.

/** Start server. */
@BeforeClass
public static void startServer() {
    compressors.register(FZIPPER);
    compressors.register(Codec.Identity.NONE);
    startStaticServer(NettyServerBuilder.forPort(0).maxMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).compressorRegistry(compressors).decompressorRegistry(decompressors), new ServerInterceptor() {

        @Override
        public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            Listener<ReqT> listener = next.startCall(call, headers);
            // TODO(carl-mastrangelo): check that encoding was set.
            call.setMessageCompression(true);
            return listener;
        }
    });
}
Also used : ForwardingClientCallListener(io.grpc.ForwardingClientCallListener) Listener(io.grpc.ServerCall.Listener) ServerInterceptor(io.grpc.ServerInterceptor) Metadata(io.grpc.Metadata) BeforeClass(org.junit.BeforeClass)

Example 2 with Listener

use of io.grpc.ServerCall.Listener in project grpc-java by grpc.

the class TransportCompressionTest method createChannel.

@Override
protected ManagedChannel createChannel() {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", getPort()).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).decompressorRegistry(decompressors).compressorRegistry(compressors).intercept(new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            final ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
            return new ForwardingClientCall<ReqT, RespT>() {

                @Override
                protected ClientCall<ReqT, RespT> delegate() {
                    return call;
                }

                @Override
                public void start(final ClientCall.Listener<RespT> responseListener, Metadata headers) {
                    ClientCall.Listener<RespT> listener = new ForwardingClientCallListener<RespT>() {

                        @Override
                        protected io.grpc.ClientCall.Listener<RespT> delegate() {
                            return responseListener;
                        }

                        @Override
                        public void onHeaders(Metadata headers) {
                            super.onHeaders(headers);
                            if (expectFzip) {
                                String encoding = headers.get(GrpcUtil.MESSAGE_ENCODING_KEY);
                                assertEquals(encoding, FZIPPER.getMessageEncoding());
                            }
                        }
                    };
                    super.start(listener, headers);
                    setMessageCompression(true);
                }
            };
        }
    }).usePlaintext(true);
    io.grpc.internal.TestingAccessor.setStatsContextFactory(builder, getClientStatsFactory());
    return builder.build();
}
Also used : ForwardingClientCallListener(io.grpc.ForwardingClientCallListener) Listener(io.grpc.ServerCall.Listener) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) ForwardingClientCall(io.grpc.ForwardingClientCall) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) ByteString(com.google.protobuf.ByteString) MethodDescriptor(io.grpc.MethodDescriptor) ClientCall(io.grpc.ClientCall) ForwardingClientCall(io.grpc.ForwardingClientCall) ClientInterceptor(io.grpc.ClientInterceptor) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) ForwardingClientCallListener(io.grpc.ForwardingClientCallListener)

Example 3 with Listener

use of io.grpc.ServerCall.Listener in project motan by weibocom.

the class MotanServerCallHandler method unaryCall.

private <ReqT, RespT> Listener<ReqT> unaryCall(final ServerCall<ReqT, RespT> call, final Metadata headers) {
    final ServerCallStreamObserverImpl<ReqT, RespT> responseObserver = new ServerCallStreamObserverImpl<ReqT, RespT>(call);
    // see ServerCalls
    call.request(2);
    return new ServerCall.Listener<ReqT>() {

        ReqT request;

        @Override
        public void onMessage(ReqT request) {
            this.request = request;
        }

        @SuppressWarnings("unchecked")
        @Override
        public void onHalfClose() {
            if (request != null) {
                DefaultRequest motanRequest = getBaseMotanRequest(headers);
                String ip = NetUtils.getHostName(call.attributes().get(ServerCall.REMOTE_ADDR_KEY));
                if (ip != null) {
                    motanRequest.setAttachment(URLParamType.host.getName(), ip);
                }
                if (responseStream) {
                    motanRequest.setArguments(new Object[] { request, responseObserver });
                } else {
                    motanRequest.setArguments(new Object[] { request });
                }
                Response response = null;
                try {
                    response = provider.call(motanRequest);
                    if (response.getValue() != null) {
                        responseObserver.onNext((RespT) response.getValue());
                        responseObserver.onCompleted();
                    }
                } catch (Exception e) {
                    responseObserver.onError(e);
                    return;
                }
                responseObserver.freeze();
                if (call.isReady()) {
                    onReady();
                }
            } else {
                call.close(Status.INTERNAL.withDescription("Half-closed without a request"), new Metadata());
            }
        }

        @Override
        public void onCancel() {
            responseObserver.cancelled = true;
            if (responseObserver.onCancelHandler != null) {
                responseObserver.onCancelHandler.run();
            }
        }

        @Override
        public void onReady() {
            if (responseObserver.onReadyHandler != null) {
                responseObserver.onReadyHandler.run();
            }
        }
    };
}
Also used : Response(com.weibo.api.motan.rpc.Response) Listener(io.grpc.ServerCall.Listener) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Metadata(io.grpc.Metadata) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 4 with Listener

use of io.grpc.ServerCall.Listener in project motan by weibocom.

the class MotanServerCallHandler method streamCall.

@SuppressWarnings("unchecked")
private <ReqT, RespT> Listener<ReqT> streamCall(final ServerCall<ReqT, RespT> call, Metadata headers) {
    final ServerCallStreamObserverImpl<ReqT, RespT> responseObserver = new ServerCallStreamObserverImpl<ReqT, RespT>(call);
    DefaultRequest request = getBaseMotanRequest(headers);
    request.setArguments(new Object[] { responseObserver });
    Response response = provider.call(request);
    final StreamObserver<ReqT> requestObserver = (StreamObserver<ReqT>) response.getValue();
    responseObserver.freeze();
    if (responseObserver.autoFlowControlEnabled) {
        call.request(1);
    }
    return new ServerCall.Listener<ReqT>() {

        boolean halfClosed = false;

        @Override
        public void onMessage(ReqT request) {
            requestObserver.onNext(request);
            if (responseObserver.autoFlowControlEnabled) {
                call.request(1);
            }
        }

        @Override
        public void onHalfClose() {
            halfClosed = true;
            requestObserver.onCompleted();
        }

        @Override
        public void onCancel() {
            responseObserver.cancelled = true;
            if (responseObserver.onCancelHandler != null) {
                responseObserver.onCancelHandler.run();
            }
            if (!halfClosed) {
                requestObserver.onError(Status.CANCELLED.asException());
            }
        }

        @Override
        public void onReady() {
            if (responseObserver.onReadyHandler != null) {
                responseObserver.onReadyHandler.run();
            }
        }
    };
}
Also used : Response(com.weibo.api.motan.rpc.Response) StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Listener(io.grpc.ServerCall.Listener) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Example 5 with Listener

use of io.grpc.ServerCall.Listener in project grpc-java by grpc.

the class TransportCompressionTest method getServerBuilder.

@Override
protected ServerBuilder<?> getServerBuilder() {
    NettyServerBuilder builder = NettyServerBuilder.forPort(0, InsecureServerCredentials.create()).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).compressorRegistry(compressors).decompressorRegistry(decompressors).intercept(new ServerInterceptor() {

        @Override
        public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            Listener<ReqT> listener = next.startCall(call, headers);
            // TODO(carl-mastrangelo): check that encoding was set.
            call.setMessageCompression(true);
            return listener;
        }
    });
    // Disable the default census stats tracer, use testing tracer instead.
    InternalNettyServerBuilder.setStatsEnabled(builder, false);
    return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
}
Also used : NettyServerBuilder(io.grpc.netty.NettyServerBuilder) InternalNettyServerBuilder(io.grpc.netty.InternalNettyServerBuilder) ForwardingClientCallListener(io.grpc.ForwardingClientCallListener) Listener(io.grpc.ServerCall.Listener) ServerInterceptor(io.grpc.ServerInterceptor) Metadata(io.grpc.Metadata)

Aggregations

Listener (io.grpc.ServerCall.Listener)10 Metadata (io.grpc.Metadata)8 ForwardingClientCallListener (io.grpc.ForwardingClientCallListener)4 ServerInterceptor (io.grpc.ServerInterceptor)3 ByteString (com.google.protobuf.ByteString)2 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)2 Response (com.weibo.api.motan.rpc.Response)2 CallOptions (io.grpc.CallOptions)2 Channel (io.grpc.Channel)2 ClientCall (io.grpc.ClientCall)2 ClientInterceptor (io.grpc.ClientInterceptor)2 ForwardingClientCall (io.grpc.ForwardingClientCall)2 MethodDescriptor (io.grpc.MethodDescriptor)2 ServiceDescriptor (io.grpc.ServiceDescriptor)2 NettyChannelBuilder (io.grpc.netty.NettyChannelBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Test (org.junit.Test)2 MotanBizException (com.weibo.api.motan.exception.MotanBizException)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 Context (io.grpc.Context)1