Search in sources :

Example 11 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class AbstractClient method removeAttribute.

public void removeAttribute(String key) {
    Channel channel = getChannel();
    if (channel == null)
        return;
    channel.removeAttribute(key);
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel)

Example 12 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class ServerPeer method getChannel.

@Override
public Channel getChannel(InetSocketAddress remoteAddress) {
    String host = remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : remoteAddress.getHostName();
    int port = remoteAddress.getPort();
    Channel channel = super.getChannel(remoteAddress);
    if (channel == null) {
        for (Map.Entry<URL, Client> entry : clients.entrySet()) {
            URL url = entry.getKey();
            if (url.getIp().equals(host) && url.getPort() == port) {
                return entry.getValue();
            }
        }
    }
    return channel;
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) Client(com.alibaba.dubbo.remoting.Client) Map(java.util.Map) URL(com.alibaba.dubbo.common.URL)

Example 13 with Channel

use of com.alibaba.dubbo.remoting.Channel 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 14 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class TelnetCodecTest method testEecode_assertEquals.

protected void testEecode_assertEquals(Object request, byte[] ret, boolean isServerside) throws IOException {
    //init channel
    Channel channel = isServerside ? getServerSideChannel(url) : getCliendSideChannel(url);
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024);
    codec.encode(channel, buffer, request);
    byte[] data = new byte[buffer.readableBytes()];
    buffer.readBytes(data);
    Assert.assertEquals(ret.length, data.length);
    for (int i = 0; i < ret.length; i++) {
        if (ret[i] != data[i]) {
            Assert.fail();
        }
    }
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer)

Example 15 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class ExchangeCodecTest method test_Encode_Error_Response.

@Test
public void test_Encode_Error_Response() throws IOException {
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
    Channel channel = getCliendSideChannel(url);
    Response response = new Response();
    response.setHeartbeat(true);
    response.setId(1001l);
    response.setStatus((byte) 10);
    response.setVersion("11");
    String badString = "bad";
    response.setErrorMessage(badString);
    Person person = new Person();
    response.setResult(person);
    codec.encode(channel, encodeBuffer, response);
    byte[] data = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(data);
    //encode resault check need decode 
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
    Response obj = (Response) codec.decode(channel, decodeBuffer);
    Assert.assertEquals(response.getId(), obj.getId());
    Assert.assertEquals(response.getStatus(), obj.getStatus());
    Assert.assertEquals(response.isHeartbeat(), obj.isHeartbeat());
    Assert.assertEquals(badString, obj.getErrorMessage());
    Assert.assertEquals(null, obj.getResult());
//        Assert.assertEquals(response.getVersion(), obj.getVersion());
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Aggregations

Channel (com.alibaba.dubbo.remoting.Channel)34 Test (org.junit.Test)16 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)15 Request (com.alibaba.dubbo.remoting.exchange.Request)14 Response (com.alibaba.dubbo.remoting.exchange.Response)10 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)8 HeaderExchangeHandler (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 URL (com.alibaba.dubbo.common.URL)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 RemotingException (com.alibaba.dubbo.remoting.RemotingException)4 RpcResult (com.alibaba.dubbo.rpc.RpcResult)4 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TMessage (org.apache.thrift.protocol.TMessage)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 ExchangeHandler (com.alibaba.dubbo.remoting.exchange.ExchangeHandler)3 Client (com.alibaba.dubbo.remoting.Client)2 DefaultFuture (com.alibaba.dubbo.remoting.exchange.support.DefaultFuture)2 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2