Search in sources :

Example 6 with RpcResponseCommand

use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project dingo by dingodb.

the class ProtobufSerializer method serializeContent.

@Override
public <T extends ResponseCommand> boolean serializeContent(T response) throws SerializationException {
    final RpcResponseCommand cmd = (RpcResponseCommand) response;
    final Message msg = (Message) cmd.getResponseObject();
    cmd.setContent(msg.toByteArray());
    return true;
}
Also used : Message(com.google.protobuf.Message) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand)

Example 7 with RpcResponseCommand

use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project sofa-jraft by sofastack.

the class ProtobufSerializer method deserializeContent.

@Override
public <T extends ResponseCommand> boolean deserializeContent(T response, InvokeContext invokeContext) throws DeserializationException {
    final RpcResponseCommand cmd = (RpcResponseCommand) response;
    final String className = cmd.getResponseClass();
    cmd.setResponseObject(ProtobufMsgFactory.newMessageByJavaClassName(className, response.getContent()));
    return true;
}
Also used : RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand)

Example 8 with RpcResponseCommand

use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project sofa-jraft by sofastack.

the class ProtobufSerializer method serializeContent.

@Override
public <T extends ResponseCommand> boolean serializeContent(T response) throws SerializationException {
    final RpcResponseCommand cmd = (RpcResponseCommand) response;
    final Message msg = (Message) cmd.getResponseObject();
    cmd.setContent(msg.toByteArray());
    return true;
}
Also used : Message(com.google.protobuf.Message) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand)

Example 9 with RpcResponseCommand

use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project sofa-rpc by sofastack.

the class SofaRpcSerializationTest method serializeResponseContent.

@Test
public void serializeResponseContent() {
    String traceId = "traceId";
    String rpcId = "rpcId";
    RpcInternalContext.getContext().setAttachment("_trace_id", traceId);
    RpcInternalContext.getContext().setAttachment("_span_id", rpcId);
    RpcResponseCommand command = new RpcResponseCommand();
    SofaRpcSerialization sofaRpcSerialization = new SofaRpcSerialization();
    boolean exp = false;
    try {
        sofaRpcSerialization.serializeContent(command);
    } catch (SerializationException e) {
        exp = true;
        Assert.assertTrue(e.getMessage().contains("traceId=" + traceId + ", rpcId=" + rpcId));
    }
    Assert.assertTrue(exp);
}
Also used : SerializationException(com.alipay.remoting.exception.SerializationException) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) Test(org.junit.Test)

Example 10 with RpcResponseCommand

use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project sofa-rpc by sofastack.

the class SofaRpcSerialization method deserializeContent.

@Override
public <Response extends ResponseCommand> boolean deserializeContent(Response response, InvokeContext invokeContext) throws DeserializationException {
    if (response instanceof RpcResponseCommand) {
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte serializer = response.getSerializer();
        byte[] content = responseCommand.getContent();
        if (content == null || content.length == 0) {
            return false;
        }
        long deserializeStartTime = System.nanoTime();
        try {
            Object sofaResponse = ClassUtils.forName(responseCommand.getResponseClass()).newInstance();
            Map<String, String> header = (Map<String, String>) responseCommand.getResponseHeader();
            if (header == null) {
                header = new HashMap<String, String>();
            }
            putKV(header, RemotingConstants.HEAD_TARGET_SERVICE, (String) invokeContext.get(RemotingConstants.HEAD_TARGET_SERVICE));
            putKV(header, RemotingConstants.HEAD_METHOD_NAME, (String) invokeContext.get(RemotingConstants.HEAD_METHOD_NAME));
            putKV(header, RemotingConstants.HEAD_GENERIC_TYPE, (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));
            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializer);
            rpcSerializer.decode(new ByteArrayWrapperByteBuf(responseCommand.getContent()), sofaResponse, header);
            if (sofaResponse instanceof SofaResponse) {
                parseResponseHeader(header, (SofaResponse) sofaResponse);
            }
            responseCommand.setResponseObject(sofaResponse);
            return true;
        } catch (Exception ex) {
            throw new DeserializationException(ex.getMessage(), ex);
        } finally {
            // R5:Record response deserialization time
            recordDeserializeResponse(responseCommand, invokeContext, deserializeStartTime);
        }
    }
    return false;
}
Also used : ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException) DeserializationException(com.alipay.remoting.exception.DeserializationException) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) HashMap(java.util.HashMap) Map(java.util.Map) Serializer(com.alipay.sofa.rpc.codec.Serializer) DefaultCustomSerializer(com.alipay.remoting.DefaultCustomSerializer)

Aggregations

RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)10 SerializationException (com.alipay.remoting.exception.SerializationException)4 DeserializationException (com.alipay.remoting.exception.DeserializationException)3 DefaultCustomSerializer (com.alipay.remoting.DefaultCustomSerializer)2 Serializer (com.alipay.sofa.rpc.codec.Serializer)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 Message (com.google.protobuf.Message)2 Test (org.junit.Test)2 RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)1 Status (com.alipay.sofa.jraft.Status)1 ErrorResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)1 PingRequest (com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest)1 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)1 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1