Search in sources :

Example 16 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class DefaultRpcCodecTest method testOriginalTypeArrayRequest.

@Test
public void testOriginalTypeArrayRequest() throws Exception {
    DefaultRequest request = getRequest("int[]", new Object[] { new int[] { 1, 2 } });
    testCodecRequest(request);
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 17 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class NettyHttpRequestHandler method buildRpcRequest.

protected DefaultRequest buildRpcRequest(FullHttpRequest httpRequest) throws UnsupportedEncodingException {
    String uri = httpRequest.uri();
    String[] uriInfo = uri.split("\\?");
    String[] serviceInfo = uriInfo[0].split("/");
    if (serviceInfo.length != 4) {
        throw new MotanServiceException("invalid request uri! uri like '/${group}/${service}/${method}'");
    }
    DefaultRequest rpcRequest = new DefaultRequest();
    rpcRequest.setAttachment(URLParamType.group.getName(), serviceInfo[1]);
    rpcRequest.setInterfaceName(serviceInfo[2]);
    rpcRequest.setMethodName(serviceInfo[3]);
    HashMap<String, String> params = new HashMap<String, String>();
    if (uriInfo.length == 2) {
        addParams(params, uriInfo[1]);
    }
    ByteBuf buf = httpRequest.content();
    final byte[] contentBytes = new byte[buf.readableBytes()];
    buf.getBytes(0, contentBytes);
    String body = new String(contentBytes, "UTF-8");
    addParams(params, body);
    MethodInfo mi = methodDescMap.get(rpcRequest.getMethodName());
    if (mi != null && mi.isDuplicate()) {
        mi = null;
        String paramDesc = params.get("paramDesc");
        if (StringUtils.isBlank(paramDesc)) {
            throw new MotanServiceException("request method name conflict! paramDesc is required!" + rpcRequest.getMethodName());
        }
        mi = methodDescMap.get(rpcRequest.getMethodName() + paramDesc);
    }
    if (mi == null) {
        throw new MotanServiceException("request method name not found" + rpcRequest.getMethodName());
    }
    rpcRequest.setParamtersDesc(mi.getMethodDesc());
    // TODO other info
    addAttachment(rpcRequest, httpRequest.headers());
    rpcRequest.setArguments(parseArguments(params.get("params"), mi));
    return rpcRequest;
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ByteBuf(io.netty.buffer.ByteBuf) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 18 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest 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 19 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest 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 20 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class ProtobufCodec method decodeRequest.

private Object decodeRequest(CodedInputStream input, long requestId, Serialization serialization) throws IOException, ClassNotFoundException {
    String interfaceName = input.readString();
    String methodName = input.readString();
    String paramtersDesc = input.readString();
    DefaultRequest rpcRequest = new DefaultRequest();
    rpcRequest.setRequestId(requestId);
    rpcRequest.setInterfaceName(interfaceName);
    rpcRequest.setMethodName(methodName);
    rpcRequest.setParamtersDesc(paramtersDesc);
    rpcRequest.setArguments(decodeRequestParameter(input, paramtersDesc, serialization));
    rpcRequest.setAttachments(decodeRequestAttachments(input));
    return rpcRequest;
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Aggregations

DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)45 Test (org.junit.Test)26 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)8 Request (com.weibo.api.motan.rpc.Request)7 Model (com.weibo.api.motan.protocol.example.Model)5 Response (com.weibo.api.motan.rpc.Response)5 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)4 Channel (com.weibo.api.motan.transport.Channel)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 IHello (com.weibo.api.motan.protocol.example.IHello)3 URL (com.weibo.api.motan.rpc.URL)3 MessageHandler (com.weibo.api.motan.transport.MessageHandler)3 HashMap (java.util.HashMap)3 MockChannel (com.weibo.api.motan.mock.MockChannel)2 MockReferer (com.weibo.api.motan.mock.MockReferer)2 Referer (com.weibo.api.motan.rpc.Referer)2 Listener (io.grpc.ServerCall.Listener)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Codec (com.weibo.api.motan.codec.Codec)1 Serialization (com.weibo.api.motan.codec.Serialization)1