Search in sources :

Example 21 with Channel

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

the class PeerMain method main.

public static void main(String[] args) throws Throwable {
    // Group address, supporting two groups of multicast and file, extensible
    String groupURL = "multicast://224.5.6.7:9911";
    // The native server address for cross networking
    final String peerURL = "dubbo://0.0.0.0:" + (((int) (Math.random() * 10000)) + 20000);
    // Join the group and get the peer reference
    Peer peer = Networkers.join(groupURL, peerURL, new ChannelHandlerAdapter() {

        @Override
        public void received(Channel channel, Object message) throws RemotingException {
            System.out.println("Received: " + message + " in " + peerURL);
        }
    });
    // Sending messages to other peers in the network
    for (int i = 0; i < Integer.MAX_VALUE; i++) {
        // Access to channels with all other peers, this list changes dynamically
        Collection<Channel> channels = peer.getChannels();
        if (channels != null && channels.size() > 0) {
            for (Channel channel : channels) {
                // Sending messages to a specified peer
                channel.send("(" + i + ") " + peerURL);
            }
        }
        Thread.sleep(1000);
    }
    // leave the network
    peer.leave();
}
Also used : ChannelHandlerAdapter(com.alibaba.dubbo.remoting.transport.ChannelHandlerAdapter) Channel(com.alibaba.dubbo.remoting.Channel) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Example 22 with Channel

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

the class AbstractClient method setAttribute.

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

Example 23 with Channel

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

the class ExchangeCodecTest method test_Encode_Response.

@Test
public void test_Encode_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) 20);
    response.setVersion("11");
    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(person, obj.getResult());
// encode response verson ??
// 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)

Example 24 with Channel

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

the class ExchangeCodecTest method test_Decode_MigicCodec_Contain_ExchangeHeader.

@Test
public void test_Decode_MigicCodec_Contain_ExchangeHeader() throws IOException {
    byte[] header = new byte[] { 0, 0, MAGIC_HIGH, MAGIC_LOW, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    Channel channel = getServerSideChannel(url);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(header);
    Object obj = codec.decode(channel, buffer);
    Assert.assertEquals(TelnetCodec.DecodeResult.NEED_MORE_INPUT, obj);
    // If the telnet data and request data are in the same data packet, we should guarantee that the receipt of request data won't be affected by the factor that telnet does not have an end characters.
    Assert.assertEquals(2, buffer.readerIndex());
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 25 with Channel

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

the class ExchangeCodecTest method test_Decode_Error_Length.

@Test
public void test_Decode_Error_Length() throws IOException {
    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);
    Channel channel = getServerSideChannel(url);
    byte[] baddata = new byte[] { 1, 2 };
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(join(request, baddata));
    Response obj = (Response) codec.decode(channel, buffer);
    Assert.assertEquals(person, obj.getResult());
    // only decode necessary bytes
    Assert.assertEquals(request.length, buffer.readerIndex());
}
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)35 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