Search in sources :

Example 26 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeaderExchangeHandler method received.

public void received(Channel channel, Object message) throws RemotingException {
    channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis());
    ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
    try {
        if (message instanceof Request) {
            // handle request.
            Request request = (Request) message;
            if (request.isEvent()) {
                handlerEvent(channel, request);
            } else {
                if (request.isTwoWay()) {
                    Response response = handleRequest(exchangeChannel, request);
                    channel.send(response);
                } else {
                    handler.received(exchangeChannel, request.getData());
                }
            }
        } else if (message instanceof Response) {
            handleResponse(channel, (Response) message);
        } else if (message instanceof String) {
            if (isClientSide(channel)) {
                Exception e = new Exception("Dubbo client can not supported string message: " + message + " in channel: " + channel + ", url: " + channel.getUrl());
                logger.error(e.getMessage(), e);
            } else {
                String echo = handler.telnet(channel, (String) message);
                if (echo != null && echo.length() > 0) {
                    channel.send(echo);
                }
            }
        } else {
            handler.received(exchangeChannel, message);
        }
    } finally {
        HeaderExchangeChannel.removeChannelIfDisconnected(channel);
    }
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) Request(com.alibaba.dubbo.remoting.exchange.Request) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) ExecutionException(com.alibaba.dubbo.remoting.ExecutionException) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Example 27 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_Return_Request_Event_String.

@Test
public void test_Decode_Return_Request_Event_String() throws IOException {
    //|10011111|20-stats=ok|id=0|length=0
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0xff, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    String event = Request.READONLY_EVENT;
    byte[] request = getRequestBytes(event, header);
    Request obj = (Request) decode(request);
    Assert.assertEquals(event, obj.getData());
    Assert.assertEquals(true, obj.isTwoWay());
    Assert.assertEquals(true, obj.isEvent());
    Assert.assertEquals("2.0.0", obj.getVersion());
    System.out.println(obj);
}
Also used : Request(com.alibaba.dubbo.remoting.exchange.Request) Test(org.junit.Test)

Example 28 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class ExchangeCodecTest method test_Encode_Request.

@Test
public void test_Encode_Request() throws IOException {
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(2014);
    Channel channel = getCliendSideChannel(url);
    Request request = new Request();
    Person person = new Person();
    request.setData(person);
    codec.encode(channel, encodeBuffer, request);
    //encode resault check need decode
    byte[] data = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(data);
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
    Request obj = (Request) codec.decode(channel, decodeBuffer);
    Assert.assertEquals(request.isBroken(), obj.isBroken());
    Assert.assertEquals(request.isHeartbeat(), obj.isHeartbeat());
    Assert.assertEquals(request.isTwoWay(), obj.isTwoWay());
    Assert.assertEquals(person, obj.getData());
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) Request(com.alibaba.dubbo.remoting.exchange.Request) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 29 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_Error_Request_Object.

@Test
public void test_Decode_Error_Request_Object() throws IOException {
    //00000010-response/oneway/hearbeat=true |20-stats=ok|id=0|length=0
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0xdf, 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);
    Request obj = (Request) decode(request);
    Assert.assertEquals(true, obj.isBroken());
    Assert.assertEquals(true, obj.getData() instanceof Throwable);
}
Also used : Request(com.alibaba.dubbo.remoting.exchange.Request) Test(org.junit.Test)

Example 30 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class ExchangeCodecTest method testMessageLengthExceedPayloadLimitWhenEncode.

@Test
public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception {
    Request request = new Request(1L);
    request.setData("hello");
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    try {
        codec.encode(channel, encodeBuffer, request);
        Assert.fail();
    } catch (IOException e) {
        Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6));
    }
    Response response = new Response(1L);
    response.setResult("hello");
    encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    codec.encode(channel, encodeBuffer, response);
    Assert.assertTrue(channel.getReceivedMessage() instanceof Response);
    Response receiveMessage = (Response) channel.getReceivedMessage();
    Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
    Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) Request(com.alibaba.dubbo.remoting.exchange.Request) IOException(java.io.IOException) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Aggregations

Request (com.alibaba.dubbo.remoting.exchange.Request)38 Test (org.junit.Test)22 Response (com.alibaba.dubbo.remoting.exchange.Response)18 Channel (com.alibaba.dubbo.remoting.Channel)14 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)10 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)8 RemotingException (com.alibaba.dubbo.remoting.RemotingException)7 TMessage (org.apache.thrift.protocol.TMessage)7 HeaderExchangeHandler (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)6 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)6 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)6 DefaultFuture (com.alibaba.dubbo.remoting.exchange.support.DefaultFuture)5 RpcResult (com.alibaba.dubbo.rpc.RpcResult)5 URL (com.alibaba.dubbo.common.URL)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Serialization (com.alibaba.dubbo.common.serialize.Serialization)3 ExchangeHandler (com.alibaba.dubbo.remoting.exchange.ExchangeHandler)3 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)3 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)3