Search in sources :

Example 1 with ExchangeChannel

use of org.apache.dubbo.remoting.exchange.ExchangeChannel 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;
            Assertions.assertEquals(request.getId(), res.getId());
            Assertions.assertEquals(request.getVersion(), res.getVersion());
            Assertions.assertEquals(Response.OK, res.getStatus());
            Assertions.assertEquals(requestdata, res.getResult());
            Assertions.assertNull(res.getErrorMessage());
            count.incrementAndGet();
        }
    };
    ExchangeHandler exhandler = new MockedExchangeHandler() {

        @Override
        public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
            return CompletableFuture.completedFuture(request);
        }

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

Example 2 with ExchangeChannel

use of org.apache.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.

the class DubboTelnetDecodeTest method testDubboDubboDecoded.

/**
 * dubbo and dubbo request
 *
 * <p>
 * First ByteBuf (firstDubboByteBuf):
 * ++-------------------------------------------------+
 * ||               dubbo(incomplete)                 |
 * ++-------------------------------------------------+
 * ||
 * Magic Code
 * <p>
 *
 * <p>
 * Second ByteBuf (secondDubboByteBuf):
 * +-------------------------++-----------------------+
 * |  dubbo(the remaining)   ||    dubbo(complete)    |
 * +-------------------------++-----------------------+
 *                           ||
 *                       Magic Code
 *
 * @throws InterruptedException
 */
@Test
public void testDubboDubboDecoded() throws InterruptedException, IOException {
    ByteBuf dubboByteBuf = createDubboByteBuf();
    ByteBuf firstDubboByteBuf = dubboByteBuf.copy(0, 50);
    ByteBuf secondLeftDubboByteBuf = dubboByteBuf.copy(50, dubboByteBuf.readableBytes() - 50);
    ByteBuf secondDubboByteBuf = Unpooled.wrappedBuffer(secondLeftDubboByteBuf, dubboByteBuf);
    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)) {
                    dubboDubbo.incrementAndGet();
                }
                return getDefaultFuture();
            }
        }))));
        ch = new LocalEmbeddedChannel();
        ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
        ch.writeInbound(firstDubboByteBuf);
        ch.writeInbound(secondDubboByteBuf);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ch != null) {
            ch.close().await(200, TimeUnit.MILLISECONDS);
        }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    Assertions.assertEquals(2, dubboDubbo.get());
}
Also used : ExchangeHandlerAdapter(org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter) NettyCodecAdapter(org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter) Codec2(org.apache.dubbo.remoting.Codec2) MultiMessageHandler(org.apache.dubbo.remoting.transport.MultiMessageHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) ByteBuf(io.netty.buffer.ByteBuf) URL(org.apache.dubbo.common.URL) IOException(java.io.IOException) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) DecodeHandler(org.apache.dubbo.remoting.transport.DecodeHandler) Test(org.junit.jupiter.api.Test)

Example 3 with ExchangeChannel

use of org.apache.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.

the class DubboTelnetDecodeTest method testTelnetDecode.

/**
 * just telnet request
 *
 * @throws InterruptedException
 */
@Test
public void testTelnetDecode() throws InterruptedException {
    ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("test\r\n".getBytes());
    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)) {
                telnet.incrementAndGet();
            }
        }, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {

            @Override
            public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
                return getDefaultFuture();
            }
        }))));
        ch = new LocalEmbeddedChannel();
        ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
        ch.writeInbound(telnetByteBuf);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ch != null) {
            ch.close().await(200, TimeUnit.MILLISECONDS);
        }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    Assertions.assertEquals(1, telnet.get());
}
Also used : ExchangeHandlerAdapter(org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter) NettyCodecAdapter(org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter) Codec2(org.apache.dubbo.remoting.Codec2) MultiMessageHandler(org.apache.dubbo.remoting.transport.MultiMessageHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) ByteBuf(io.netty.buffer.ByteBuf) URL(org.apache.dubbo.common.URL) IOException(java.io.IOException) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) DecodeHandler(org.apache.dubbo.remoting.transport.DecodeHandler) Test(org.junit.jupiter.api.Test)

Example 4 with ExchangeChannel

use of org.apache.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.

the class PerformanceServerTest method statServer.

private static ExchangeServer statServer() throws Exception {
    final int port = PerformanceUtils.getIntProperty("port", 9911);
    final String transporter = PerformanceUtils.getProperty(Constants.TRANSPORTER_KEY, Constants.DEFAULT_TRANSPORTER);
    final String serialization = PerformanceUtils.getProperty(Constants.SERIALIZATION_KEY, Constants.DEFAULT_REMOTING_SERIALIZATION);
    final String threadpool = PerformanceUtils.getProperty(THREADPOOL_KEY, DEFAULT_THREADPOOL);
    final int threads = PerformanceUtils.getIntProperty(THREADS_KEY, DEFAULT_THREADS);
    final int iothreads = PerformanceUtils.getIntProperty(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS);
    final int buffer = PerformanceUtils.getIntProperty(BUFFER_KEY, DEFAULT_BUFFER_SIZE);
    final String channelHandler = PerformanceUtils.getProperty(Constants.DISPATCHER_KEY, ExecutionDispatcher.NAME);
    // Start server
    ExchangeServer server = Exchangers.bind("exchange://0.0.0.0:" + port + "?transporter=" + transporter + "&serialization=" + serialization + "&threadpool=" + threadpool + "&threads=" + threads + "&iothreads=" + iothreads + "&buffer=" + buffer + "&channel.handler=" + channelHandler, new ExchangeHandlerAdapter() {

        public String telnet(Channel channel, String message) throws RemotingException {
            return "echo: " + message + "\r\ntelnet> ";
        }

        public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
            if ("environment".equals(request)) {
                return CompletableFuture.completedFuture(PerformanceUtils.getEnvironment());
            }
            if ("scene".equals(request)) {
                List<String> scene = new ArrayList<String>();
                scene.add("Transporter: " + transporter);
                scene.add("Service Threads: " + threads);
                return CompletableFuture.completedFuture(scene);
            }
            return CompletableFuture.completedFuture(request);
        }
    });
    return server;
}
Also used : ExchangeHandlerAdapter(org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter) CompletableFuture(java.util.concurrent.CompletableFuture) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) ExchangeServer(org.apache.dubbo.remoting.exchange.ExchangeServer) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with ExchangeChannel

use of org.apache.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.

the class HeaderExchangeServer method getExchangeChannels.

@Override
public Collection<ExchangeChannel> getExchangeChannels() {
    Collection<ExchangeChannel> exchangeChannels = new ArrayList<ExchangeChannel>();
    Collection<Channel> channels = server.getChannels();
    if (CollectionUtils.isNotEmpty(channels)) {
        for (Channel channel : channels) {
            exchangeChannels.add(HeaderExchangeChannel.getOrAddChannel(channel));
        }
    }
    return exchangeChannels;
}
Also used : ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) ArrayList(java.util.ArrayList) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel)

Aggregations

ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)23 Test (org.junit.jupiter.api.Test)12 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)9 ExchangeHandlerAdapter (org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter)8 URL (org.apache.dubbo.common.URL)7 ByteBuf (io.netty.buffer.ByteBuf)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 IOException (java.io.IOException)6 Codec2 (org.apache.dubbo.remoting.Codec2)6 Request (org.apache.dubbo.remoting.exchange.Request)6 DecodeHandler (org.apache.dubbo.remoting.transport.DecodeHandler)6 MultiMessageHandler (org.apache.dubbo.remoting.transport.MultiMessageHandler)6 NettyCodecAdapter (org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter)6 Channel (org.apache.dubbo.remoting.Channel)5 RemotingException (org.apache.dubbo.remoting.RemotingException)4 Response (org.apache.dubbo.remoting.exchange.Response)4 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExchangeHandler (org.apache.dubbo.remoting.exchange.ExchangeHandler)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2