Search in sources :

Example 1 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class DeprecatedExchangeCodec method decodeBody.

protected Object decodeBody(Channel channel, InputStream is, byte[] header) throws IOException {
    byte flag = header[2], proto = (byte) (flag & SERIALIZATION_MASK);
    // get request id.
    long id = Bytes.bytes2long(header, 4);
    if ((flag & FLAG_REQUEST) == 0) {
        // decode response.
        Response res = new Response(id);
        if ((flag & FLAG_EVENT) != 0) {
            res.setEvent(true);
        }
        // get status.
        byte status = header[3];
        res.setStatus(status);
        try {
            ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
            if (status == Response.OK) {
                Object data;
                if (res.isHeartbeat()) {
                    data = decodeHeartbeatData(channel, in);
                } else if (res.isEvent()) {
                    data = decodeEventData(channel, in);
                } else {
                    data = decodeResponseData(channel, in, getRequestData(id));
                }
                res.setResult(data);
            } else {
                res.setErrorMessage(in.readUTF());
            }
        } catch (Throwable t) {
            res.setStatus(Response.CLIENT_ERROR);
            res.setErrorMessage(StringUtils.toString(t));
        }
        return res;
    } else {
        // decode request.
        Request req = new Request(id);
        req.setVersion(Version.getProtocolVersion());
        req.setTwoWay((flag & FLAG_TWOWAY) != 0);
        if ((flag & FLAG_EVENT) != 0) {
            req.setEvent(true);
        }
        try {
            ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
            Object data;
            if (req.isHeartbeat()) {
                data = decodeHeartbeatData(channel, in);
            } else if (req.isEvent()) {
                data = decodeEventData(channel, in);
            } else {
                data = decodeRequestData(channel, in);
            }
            req.setData(data);
        } catch (Throwable t) {
            // bad request
            req.setBroken(true);
            req.setData(t);
        }
        return req;
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput)

Example 2 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class PayloadDropperTest method test.

@Test
public void test() {
    Request request = new Request(1);
    request.setData(new Object());
    Request requestWithoutData = (Request) PayloadDropper.getRequestWithoutData(request);
    Assertions.assertEquals(requestWithoutData.getId(), request.getId());
    Assertions.assertNull(requestWithoutData.getData());
    Response response = new Response(1);
    response.setResult(new Object());
    Response responseWithoutData = (Response) PayloadDropper.getRequestWithoutData(response);
    Assertions.assertEquals(responseWithoutData.getId(), response.getId());
    Assertions.assertNull(responseWithoutData.getResult());
    Object object = new Object();
    Assertions.assertEquals(object, PayloadDropper.getRequestWithoutData(object));
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) Test(org.junit.jupiter.api.Test)

Example 3 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class ConnectChannelHandlerTest method test_Received_Event_invoke_direct.

/**
 * Events do not pass through the thread pool and execute directly on the IO
 */
@SuppressWarnings("deprecation")
@Disabled("Heartbeat is processed in HeartbeatHandler not WrappedChannelHandler.")
@Test
public void test_Received_Event_invoke_direct() throws RemotingException {
    handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url);
    ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "SHARED_EXECUTOR", 1);
    executor.shutdown();
    executor = (ThreadPoolExecutor) getField(handler, "executor", 1);
    executor.shutdown();
    Request req = new Request();
    req.setHeartbeat(true);
    final AtomicInteger count = new AtomicInteger(0);
    handler.received(new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Assertions.assertTrue(((Response) message).isHeartbeat(), "response.heartbeat");
            count.incrementAndGet();
        }
    }, req);
    Assertions.assertEquals(1, count.get(), "channel.send must be invoke");
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RemotingException(org.apache.dubbo.remoting.RemotingException) Request(org.apache.dubbo.remoting.exchange.Request) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ConnectionOrderedChannelHandler(org.apache.dubbo.remoting.transport.dispatcher.connection.ConnectionOrderedChannelHandler) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_Error_Response_Object.

@Test
public void test_Decode_Error_Response_Object() throws IOException {
    // 00000010-response/oneway/hearbeat=true |20-stats=ok|id=0|length=0
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 0x02, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    Person person = new Person();
    byte[] request = getRequestBytes(person, header);
    // bad object
    byte[] badbytes = new byte[] { -1, -2, -3, -4, -3, -4, -3, -4, -3, -4, -3, -4 };
    System.arraycopy(badbytes, 0, request, 21, badbytes.length);
    Response obj = (Response) decode(request);
    Assertions.assertEquals(90, obj.getStatus());
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Test(org.junit.jupiter.api.Test)

Example 5 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class ExchangeCodecTest method testInvalidSerializaitonId.

@Test
public void testInvalidSerializaitonId() throws Exception {
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0x8F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    Object obj = decode(header);
    Assertions.assertTrue(obj instanceof Request);
    Request request = (Request) obj;
    Assertions.assertTrue(request.isBroken());
    Assertions.assertTrue(request.getData() instanceof IOException);
    header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0x1F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    obj = decode(header);
    Assertions.assertTrue(obj instanceof Response);
    Response response = (Response) obj;
    Assertions.assertEquals(response.getStatus(), Response.CLIENT_ERROR);
    Assertions.assertTrue(response.getErrorMessage().contains("IOException"));
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

Response (org.apache.dubbo.remoting.exchange.Response)39 Request (org.apache.dubbo.remoting.exchange.Request)21 Test (org.junit.jupiter.api.Test)20 Channel (org.apache.dubbo.remoting.Channel)12 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)9 IOException (java.io.IOException)6 RemotingException (org.apache.dubbo.remoting.RemotingException)6 AppResponse (org.apache.dubbo.rpc.AppResponse)6 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)5 TMessage (org.apache.thrift.protocol.TMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 URL (org.apache.dubbo.common.URL)4 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)3 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)3 TApplicationException (org.apache.thrift.TApplicationException)3 ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)2