Search in sources :

Example 1 with Head

use of srpc.Head in project MSEC by Tencent.

the class RequestDecoder method deserializeProtobufPackage.

private RpcRequest deserializeProtobufPackage(byte[] headBytes, byte[] bodyBytes) {
    Head.CRpcHead pbHead = null;
    RpcRequest rpcRequest = new RpcRequest();
    try {
        pbHead = Head.CRpcHead.parseFrom(headBytes);
        rpcRequest.setSeq(pbHead.getSequence());
    } catch (InvalidProtocolBufferException e) {
        log.error("Parse protobuf head failed.");
        rpcRequest.setException(new IllegalArgumentException("Parse protobuf head failed."));
        return rpcRequest;
    }
    String serviceMethodName = pbHead.getMethodName().toStringUtf8();
    int pos = serviceMethodName.lastIndexOf('.');
    if (pos == -1 || pos == 0 || pos == serviceMethodName.length() - 1) {
        log.error("Invalid serviceMethodName (" + serviceMethodName + "). Must be in format like *.*");
        rpcRequest.setException(new IllegalArgumentException("Invalid serviceMethodName (" + serviceMethodName + "). Must be in format like *.*"));
        return rpcRequest;
    }
    String serviceName = serviceMethodName.substring(0, pos);
    String methodName = serviceMethodName.substring(pos + 1);
    rpcRequest.setServiceName(serviceName);
    rpcRequest.setMethodName(methodName);
    rpcRequest.setFlowid(pbHead.getFlowId());
    if (pbHead.getCaller() != null && !pbHead.getCaller().isEmpty()) {
        rpcRequest.setFromModule(pbHead.getCaller().toStringUtf8());
    } else {
        rpcRequest.setFromModule("UnknownModule");
    }
    //If flowid == 0, we must generate one
    if (rpcRequest.getFlowid() == 0) {
        rpcRequest.setFlowid(rpcRequest.getSeq() ^ NettyCodecUtils.generateColorId(serviceMethodName));
    }
    AccessMonitor.add("frm.rpc request incoming: " + methodName);
    ServiceFactory.ServiceMethodEntry serviceMethodEntry = ServiceFactory.getServiceMethodEntry(serviceName, methodName);
    if (serviceMethodEntry == null) {
        log.error("No service method registered: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName());
        rpcRequest.setException(new IllegalArgumentException("No service method registered: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName()));
        return rpcRequest;
    }
    MessageLite param = null;
    try {
        param = (MessageLite) serviceMethodEntry.getParamTypeParser().parseFrom(bodyBytes);
    } catch (InvalidProtocolBufferException ex) {
        log.error("Parse protobuf body failed.");
        rpcRequest.setException(new IllegalArgumentException("RParse protobuf body failed." + ex.getMessage()));
        return rpcRequest;
    }
    rpcRequest.setParameter(param);
    log.info("RPC Request received. ServiceMethodName: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName() + "\tSeq: " + rpcRequest.getSeq() + "\tParameter: " + rpcRequest.getParameter());
    return rpcRequest;
}
Also used : Head(srpc.Head) ServiceFactory(org.msec.rpc.ServiceFactory) RpcRequest(org.msec.rpc.RpcRequest) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) MessageLite(com.google.protobuf.MessageLite)

Example 2 with Head

use of srpc.Head in project MSEC by Tencent.

the class RequestEncoder method encodeHead.

protected int encodeHead(RpcRequest rpcRequest, ChannelBufferOutputStream stream) throws IOException {
    //TODO: set coloring, color_id & caller..
    //System.out.println("Start encode request head: " + System.currentTimeMillis());
    String serviceMethodName = rpcRequest.getServiceName() + "." + rpcRequest.getMethodName();
    long colorId = NettyCodecUtils.generateColorId(serviceMethodName);
    Head.CRpcHead.Builder builder = Head.CRpcHead.newBuilder();
    builder.clear();
    builder.setSequence(rpcRequest.getSeq());
    builder.setColoring(0);
    builder.setColorId(colorId);
    if (rpcRequest.getFlowid() == 0) {
        builder.setFlowId(rpcRequest.getSeq() ^ colorId);
    } else {
        builder.setFlowId(rpcRequest.getFlowid());
    }
    builder.setErr(0);
    builder.setResult(0);
    builder.setErrMsg(ByteString.EMPTY);
    builder.setCaller(ByteString.copyFromUtf8(ServiceFactory.getModuleName()));
    builder.setMethodName(ByteString.copyFrom((serviceMethodName).getBytes()));
    builder.addCallerStack(ByteString.EMPTY);
    //System.out.println("Start encode request head3: " + System.currentTimeMillis());
    Head.CRpcHead rpchead = builder.build();
    stream.write(rpchead.toByteArray());
    //System.out.println("Start encode request head4: " + System.currentTimeMillis());
    AccessMonitor.add("frm.rpc call to " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName());
    return rpchead.getSerializedSize();
}
Also used : Head(srpc.Head) ByteString(com.google.protobuf.ByteString)

Aggregations

Head (srpc.Head)2 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 MessageLite (com.google.protobuf.MessageLite)1 RpcRequest (org.msec.rpc.RpcRequest)1 ServiceFactory (org.msec.rpc.ServiceFactory)1