Search in sources :

Example 1 with RpcRequestCommand

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

the class ProtobufSerializerTest method testEncodeDecodeResponseContent.

@Test
public void testEncodeDecodeResponseContent() throws Exception {
    final PingRequest reqObject = TestUtils.createPingRequest();
    final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject);
    final ErrorResponse respObject = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null, new Status(-1, "test"));
    final RpcResponseCommand response = cmdFactory.createResponse(respObject, request);
    response.setResponseClass(ErrorResponse.class.getName());
    assertTrue(serializer.serializeContent(response));
    response.setResponseObject(null);
    assertTrue(serializer.deserializeContent(response, null));
    assertNotNull(response.getResponseObject());
    assertEquals(respObject, response.getResponseObject());
    assertNotSame(respObject, response.getResponseObject());
}
Also used : Status(com.alipay.sofa.jraft.Status) PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 2 with RpcRequestCommand

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

the class ProtobufSerializerTest method testEncodeDecodeRequestContent.

@Test
public void testEncodeDecodeRequestContent() throws Exception {
    final PingRequest reqObject = TestUtils.createPingRequest();
    final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject);
    request.setRequestClass(PingRequest.class.getName());
    assertTrue(serializer.serializeContent(request, null));
    request.setRequestObject(null);
    assertTrue(serializer.deserializeContent(request));
    assertNotNull(request.getRequestObject());
    assertEquals(reqObject, request.getRequestObject());
    assertNotSame(reqObject, request.getRequestObject());
}
Also used : PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) Test(org.junit.Test)

Example 3 with RpcRequestCommand

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

the class ProtobufSerializer method serializeContent.

@Override
public <T extends RequestCommand> boolean serializeContent(T request, InvokeContext invokeContext) throws SerializationException {
    final RpcRequestCommand cmd = (RpcRequestCommand) request;
    final Message msg = (Message) cmd.getRequestObject();
    cmd.setContent(msg.toByteArray());
    return true;
}
Also used : Message(com.google.protobuf.Message) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand)

Example 4 with RpcRequestCommand

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

the class ProtobufSerializer method deserializeHeader.

@Override
public <T extends RequestCommand> boolean deserializeHeader(T request) throws DeserializationException {
    final RpcRequestCommand cmd = (RpcRequestCommand) request;
    final String className = cmd.getRequestClass();
    if (className.equals(RpcRequests.AppendEntriesRequest.class.getName())) {
        final byte[] header = cmd.getHeader();
        cmd.setRequestHeader(ProtobufMsgFactory.newMessageByJavaClassName(RpcRequests.AppendEntriesRequestHeader.class.getName(), header));
        return true;
    }
    return false;
}
Also used : RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand)

Example 5 with RpcRequestCommand

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

the class ProtobufSerializer method deserializeContent.

@Override
public <T extends RequestCommand> boolean deserializeContent(T request) throws DeserializationException {
    final RpcRequestCommand cmd = (RpcRequestCommand) request;
    final String className = cmd.getRequestClass();
    cmd.setRequestObject(ProtobufMsgFactory.newMessageByJavaClassName(className, request.getContent()));
    return true;
}
Also used : RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand)

Aggregations

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