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();
}
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);
}
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());
}
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());
}
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());
}
Aggregations