Search in sources :

Example 26 with TMessage

use of org.apache.thrift.protocol.TMessage in project dubbo by alibaba.

the class ThriftCodecTest method testEncodeReplyResponse.

@Test
public void testEncodeReplyResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.Iface.class.getName());
    Channel channel = new MockedChannel(url);
    Request request = createRequest();
    AppResponse appResponse = new AppResponse();
    appResponse.setValue("Hello, World!");
    Response response = new Response();
    response.setResult(appResponse);
    response.setId(request.getId());
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
    ThriftCodec.CACHED_REQUEST.putIfAbsent(request.getId(), rd);
    codec.encode(channel, bos, response);
    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    if (bis.markSupported()) {
        bis.mark(0);
    }
    TIOStreamTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    Assertions.assertEquals(protocol.readI32() + 4, bos.writerIndex());
    int headerLength = protocol.readI16();
    Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    Assertions.assertEquals(request.getId(), protocol.readI64());
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }
    TMessage message = protocol.readMessageBegin();
    Assertions.assertEquals("echoString", message.name);
    Assertions.assertEquals(TMessageType.REPLY, message.type);
    // Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    Demo.echoString_result result = new Demo.echoString_result();
    result.read(protocol);
    protocol.readMessageEnd();
    Assertions.assertEquals(appResponse.getValue(), result.getSuccess());
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) Test(org.junit.jupiter.api.Test)

Example 27 with TMessage

use of org.apache.thrift.protocol.TMessage in project dubbo by alibaba.

the class ThriftCodecTest method testDecodeRequest.

@Test
public void testDecodeRequest() throws Exception {
    Request request = createRequest();
    // encode
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024);
    TIOStreamTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    int messageLength, headerLength;
    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(((RpcInvocation) request.getData()).getAttachment(INTERFACE_KEY));
    protocol.writeString(((RpcInvocation) request.getData()).getAttachment(PATH_KEY));
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();
    Demo.echoString_args args = new Demo.echoString_args();
    args.setArg("Hell, World!");
    TMessage message = new TMessage("echoString", TMessageType.CALL, ThriftCodec.getSeqId());
    protocol.writeMessageBegin(message);
    args.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();
    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
    } finally {
        bos.setWriteIndex(oldIndex);
    }
    Object obj = codec.decode((Channel) null, ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray())));
    Assertions.assertTrue(obj instanceof Request);
    obj = ((Request) obj).getData();
    Assertions.assertTrue(obj instanceof RpcInvocation);
    RpcInvocation invocation = (RpcInvocation) obj;
    Assertions.assertEquals("echoString", invocation.getMethodName());
    Assertions.assertArrayEquals(new Class[] { String.class }, invocation.getParameterTypes());
    Assertions.assertArrayEquals(new Object[] { args.getArg() }, invocation.getArguments());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Demo(org.apache.dubbo.rpc.gen.thrift.Demo) RandomAccessByteArrayOutputStream(org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TMessage(org.apache.thrift.protocol.TMessage) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) Test(org.junit.jupiter.api.Test)

Example 28 with TMessage

use of org.apache.thrift.protocol.TMessage in project dubbo by alibaba.

the class ThriftCodecTest method testEncodeExceptionResponse.

@Test
public void testEncodeExceptionResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.Iface.class.getName());
    Channel channel = new MockedChannel(url);
    Request request = createRequest();
    AppResponse appResponse = new AppResponse();
    String exceptionMessage = "failed";
    appResponse.setException(new RuntimeException(exceptionMessage));
    Response response = new Response();
    response.setResult(appResponse);
    response.setId(request.getId());
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
    ThriftCodec.CACHED_REQUEST.put(request.getId(), rd);
    codec.encode(channel, bos, response);
    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    if (bis.markSupported()) {
        bis.mark(0);
    }
    TIOStreamTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    Assertions.assertEquals(protocol.readI32() + 4, bos.writerIndex());
    int headerLength = protocol.readI16();
    Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    Assertions.assertEquals(request.getId(), protocol.readI64());
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }
    TMessage message = protocol.readMessageBegin();
    Assertions.assertEquals("echoString", message.name);
    Assertions.assertEquals(TMessageType.EXCEPTION, message.type);
    Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    TApplicationException exception = TApplicationException.readFrom(protocol);
    protocol.readMessageEnd();
    Assertions.assertEquals(exceptionMessage, exception.getMessage());
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) TApplicationException(org.apache.thrift.TApplicationException) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) Test(org.junit.jupiter.api.Test)

Example 29 with TMessage

use of org.apache.thrift.protocol.TMessage in project dubbo by alibaba.

the class ThriftNativeCodec method encodeRequest.

protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException {
    Invocation invocation = (Invocation) request.getData();
    TProtocol protocol = newProtocol(channel.getUrl(), buffer);
    try {
        protocol.writeMessageBegin(new TMessage(invocation.getMethodName(), TMessageType.CALL, thriftSeq.getAndIncrement()));
        protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args"));
        for (int i = 0; i < invocation.getParameterTypes().length; i++) {
            Class<?> type = invocation.getParameterTypes()[i];
        }
    } catch (TException e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : TException(org.apache.thrift.TException) Invocation(org.apache.dubbo.rpc.Invocation) TProtocol(org.apache.thrift.protocol.TProtocol) TMessage(org.apache.thrift.protocol.TMessage) IOException(java.io.IOException) TStruct(org.apache.thrift.protocol.TStruct)

Aggregations

TMessage (org.apache.thrift.protocol.TMessage)29 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)20 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)16 TException (org.apache.thrift.TException)15 TApplicationException (org.apache.thrift.TApplicationException)12 TTransport (org.apache.thrift.transport.TTransport)10 Request (com.alibaba.dubbo.remoting.exchange.Request)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 Request (org.apache.dubbo.remoting.exchange.Request)7 RpcResult (com.alibaba.dubbo.rpc.RpcResult)6 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Method (java.lang.reflect.Method)6 AppResponse (org.apache.dubbo.rpc.AppResponse)6 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)6 TBase (org.apache.thrift.TBase)6 TFieldIdEnum (org.apache.thrift.TFieldIdEnum)6 Test (org.junit.jupiter.api.Test)6 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)5 Response (com.alibaba.dubbo.remoting.exchange.Response)5