Search in sources :

Example 11 with Request

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

the class HeaderExchangeHandlerTest method test_received_request_twoway.

@Test
public void test_received_request_twoway() throws RemotingException {
    final Person requestdata = new Person("charles");
    final Request request = new Request();
    request.setTwoWay(true);
    request.setData(requestdata);
    final AtomicInteger count = new AtomicInteger(0);
    final Channel mchannel = new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Response res = (Response) message;
            Assert.assertEquals(request.getId(), res.getId());
            Assert.assertEquals(request.getVersion(), res.getVersion());
            Assert.assertEquals(Response.OK, res.getStatus());
            Assert.assertEquals(requestdata, res.getResult());
            Assert.assertEquals(null, res.getErrorMessage());
            count.incrementAndGet();
        }
    };
    ExchangeHandler exhandler = new MockedExchangeHandler() {

        @Override
        public Object reply(ExchangeChannel channel, Object request) throws RemotingException {
            return request;
        }

        public void received(Channel channel, Object message) throws RemotingException {
            Assert.fail();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
    hexhandler.received(mchannel, request);
    Assert.assertEquals(1, count.get());
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Channel(com.alibaba.dubbo.remoting.Channel) HeaderExchangeHandler(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) Request(com.alibaba.dubbo.remoting.exchange.Request) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) HeaderExchangeHandler(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) ExchangeHandler(com.alibaba.dubbo.remoting.exchange.ExchangeHandler) Test(org.junit.Test)

Example 12 with Request

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

the class DeprecatedExchangeCodec method getRequestData.

protected Object getRequestData(long id) {
    DefaultFuture future = DefaultFuture.getFuture(id);
    if (future == null)
        return null;
    Request req = future.getRequest();
    if (req == null)
        return null;
    return req.getData();
}
Also used : Request(com.alibaba.dubbo.remoting.exchange.Request) DefaultFuture(com.alibaba.dubbo.remoting.exchange.support.DefaultFuture)

Example 13 with Request

use of com.alibaba.dubbo.remoting.exchange.Request 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);
    Serialization s = CodecSupport.getSerialization(channel.getUrl(), proto);
    ObjectInput in = s.deserialize(channel.getUrl(), is);
    // 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(Response.HEARTBEAT_EVENT);
        }
        // get status.
        byte status = header[3];
        res.setStatus(status);
        if (status == Response.OK) {
            try {
                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);
            } catch (Throwable t) {
                res.setStatus(Response.CLIENT_ERROR);
                res.setErrorMessage(StringUtils.toString(t));
            }
        } else {
            res.setErrorMessage(in.readUTF());
        }
        return res;
    } else {
        // decode request.
        Request req = new Request(id);
        req.setVersion("2.0.0");
        req.setTwoWay((flag & FLAG_TWOWAY) != 0);
        if ((flag & FLAG_EVENT) != 0) {
            req.setEvent(Request.HEARTBEAT_EVENT);
        }
        try {
            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 : Serialization(com.alibaba.dubbo.common.serialize.Serialization) Response(com.alibaba.dubbo.remoting.exchange.Response) Request(com.alibaba.dubbo.remoting.exchange.Request) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput)

Example 14 with Request

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

the class ExchangeCodecTest method testMessageLengthGreaterThanMessageActualLength.

// http://code.alibabatech.com/jira/browse/DUBBO-392
@Test
public void testMessageLengthGreaterThanMessageActualLength() throws Exception {
    Channel channel = getCliendSideChannel(url);
    Request request = new Request(1L);
    request.setVersion("2.0.0");
    Date date = new Date();
    request.setData(date);
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
    codec.encode(channel, encodeBuffer, request);
    byte[] bytes = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(bytes);
    int len = Bytes.bytes2int(bytes, 12);
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    out.write(bytes, 0, 12);
    /*
         * 填充长度不能低于256,hessian每次默认会从流中读取256个byte.
         * 参见 Hessian2Input.readBuffer
         */
    int padding = 512;
    out.write(Bytes.int2bytes(len + padding));
    out.write(bytes, 16, bytes.length - 16);
    for (int i = 0; i < padding; i++) {
        out.write(1);
    }
    out.write(bytes);
    /* request|1111...|request */
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(out.toByteArray());
    Request decodedRequest = (Request) codec.decode(channel, decodeBuffer);
    Assert.assertTrue(date.equals(decodedRequest.getData()));
    Assert.assertEquals(bytes.length + padding, decodeBuffer.readerIndex());
    decodedRequest = (Request) codec.decode(channel, decodeBuffer);
    Assert.assertTrue(date.equals(decodedRequest.getData()));
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) Request(com.alibaba.dubbo.remoting.exchange.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsafeByteArrayOutputStream(com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream) Date(java.util.Date) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 15 with Request

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

the class ConnectChannelHandlerTest method test_Received_Event_invoke_direct.

/**
     * 事件不通过线程池,直接在IO上执行
     */
@SuppressWarnings("deprecation")
@Ignore("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 {
            Assert.assertEquals("response.heartbeat", true, ((Response) message).isHeartbeat());
            count.incrementAndGet();
        }
    }, req);
    Assert.assertEquals("channel.send must be invoke", 1, count.get());
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RemotingException(com.alibaba.dubbo.remoting.RemotingException) Request(com.alibaba.dubbo.remoting.exchange.Request) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ConnectionOrderedChannelHandler(com.alibaba.dubbo.remoting.transport.dispatcher.connection.ConnectionOrderedChannelHandler) Ignore(org.junit.Ignore) 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