use of org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter in project dubbo by alibaba.
the class DubboTelnetDecodeTest method testDubboDecode.
/**
* just dubbo request
*
* @throws InterruptedException
*/
@Test
public void testDubboDecode() throws InterruptedException, IOException {
ByteBuf dubboByteBuf = createDubboByteBuf();
EmbeddedChannel ch = null;
try {
Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo");
URL url = new URL("dubbo", "localhost", 22226);
NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler());
MockHandler mockHandler = new MockHandler(null, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
if (checkDubboDecoded(msg)) {
dubbo.incrementAndGet();
}
return getDefaultFuture();
}
}))));
ch = new LocalEmbeddedChannel();
ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
ch.writeInbound(dubboByteBuf);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ch != null) {
ch.close().await(200, TimeUnit.MILLISECONDS);
}
}
TimeUnit.MILLISECONDS.sleep(100);
Assertions.assertEquals(1, dubbo.get());
}
use of org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter in project dubbo by alibaba.
the class DubboTelnetDecodeTest method testDubboTelnetDecoded.
/**
* dubbo and telnet request
*
* <p>
* First ByteBuf:
* ++-------------------------------------------------+
* || dubbo(incomplete) |
* ++-------------------------------------------------+
* ||
* Magic Code
*
* <p>
* Second ByteBuf:
* +--------------------------------------------------+
* | dubbo(the remaining) | telnet(complete) |
* +--------------------------------------------------+
*
* @throws InterruptedException
*/
@Test
public void testDubboTelnetDecoded() throws InterruptedException, IOException {
ByteBuf dubboByteBuf = createDubboByteBuf();
ByteBuf firstDubboByteBuf = dubboByteBuf.copy(0, 50);
ByteBuf secondLeftDubboByteBuf = dubboByteBuf.copy(50, dubboByteBuf.readableBytes());
ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("\r\n".getBytes());
ByteBuf secondByteBuf = Unpooled.wrappedBuffer(secondLeftDubboByteBuf, telnetByteBuf);
EmbeddedChannel ch = null;
try {
Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo");
URL url = new URL("dubbo", "localhost", 22226);
NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler());
MockHandler mockHandler = new MockHandler((msg) -> {
if (checkTelnetDecoded(msg)) {
dubboTelnet.incrementAndGet();
}
}, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
if (checkDubboDecoded(msg)) {
dubboTelnet.incrementAndGet();
}
return getDefaultFuture();
}
}))));
ch = new LocalEmbeddedChannel();
ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
ch.writeInbound(firstDubboByteBuf);
ch.writeInbound(secondByteBuf);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ch != null) {
ch.close().await(200, TimeUnit.MILLISECONDS);
}
}
TimeUnit.MILLISECONDS.sleep(100);
Assertions.assertEquals(2, dubboTelnet.get());
}
use of org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter in project dubbo by alibaba.
the class PerformanceServerTest method statTelnetServer.
private static ExchangeServer statTelnetServer(int port) throws Exception {
// Start server
ExchangeServer telnetserver = Exchangers.bind("exchange://0.0.0.0:" + port, new ExchangeHandlerAdapter() {
public String telnet(Channel channel, String message) throws RemotingException {
if (message.equals("help")) {
return "support cmd: \r\n\tstart \r\n\tstop \r\n\tshutdown \r\n\trestart times [alive] [sleep] \r\ntelnet>";
} else if (message.equals("stop")) {
logger.info("server closed:" + server);
server.close();
return "stop server\r\ntelnet>";
} else if (message.startsWith("start")) {
try {
restartServer(0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
return "start server\r\ntelnet>";
} else if (message.startsWith("shutdown")) {
System.exit(0);
return "start server\r\ntelnet>";
} else if (message.startsWith("channels")) {
return "server.getExchangeChannels():" + server.getExchangeChannels().size() + "\r\ntelnet>";
} else if (message.startsWith("restart ")) {
// r times [sleep] r 10 or r 10 100
String[] args = message.split(" ");
int times = Integer.parseInt(args[1]);
int alive = args.length > 2 ? Integer.parseInt(args[2]) : 0;
int sleep = args.length > 3 ? Integer.parseInt(args[3]) : 100;
try {
restartServer(times, alive, sleep);
} catch (Exception e) {
e.printStackTrace();
}
return "restart server,times:" + times + " stop alive time: " + alive + ",sleep time: " + sleep + " usage:r times [alive] [sleep] \r\ntelnet>";
} else {
return "echo: " + message + "\r\ntelnet> ";
}
}
});
return telnetserver;
}
use of org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter in project dubbo by alibaba.
the class MulticastExchangeNetworkerTest method testJoin.
@Test
public void testJoin() throws RemotingException, InterruptedException {
final String groupURL = "multicast://224.5.6.7:1234";
MulticastExchangeNetworker multicastExchangeNetworker = new MulticastExchangeNetworker();
final CountDownLatch countDownLatch = new CountDownLatch(1);
Peer peer1 = multicastExchangeNetworker.lookup(URL.valueOf(groupURL)).join(URL.valueOf("exchange://0.0.0.0:" + NetUtils.getAvailablePort() + "?exchanger=header"), new ExchangeHandlerAdapter() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) throws RemotingException {
countDownLatch.countDown();
return super.reply(channel, msg);
}
});
Peer peer2 = multicastExchangeNetworker.lookup(URL.valueOf(groupURL)).join(URL.valueOf("exchange://0.0.0.0:" + NetUtils.getAvailablePort() + "?exchanger=header"), mock(ExchangeHandler.class));
while (true) {
for (Channel channel : peer1.getChannels()) {
channel.send("hello multicast exchange network!");
}
TimeUnit.MILLISECONDS.sleep(50);
long count = countDownLatch.getCount();
if (count > 0) {
break;
}
}
Group lookup = Networkers.lookup(groupURL);
assertThat(lookup, not(nullValue()));
assertThat(peer1, instanceOf(ExchangeServerPeer.class));
peer1.close();
peer2.close();
}
Aggregations