Search in sources :

Example 16 with ExchangeChannel

use of org.apache.dubbo.remoting.exchange.ExchangeChannel 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());
}
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 17 with ExchangeChannel

use of org.apache.dubbo.remoting.exchange.ExchangeChannel 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());
}
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 18 with ExchangeChannel

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

the class HeaderExchangeHandler method received.

@Override
public void received(Channel channel, Object message) throws RemotingException {
    final ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
    if (message instanceof Request) {
        // handle request.
        Request request = (Request) message;
        if (request.isEvent()) {
            handlerEvent(channel, request);
        } else {
            if (request.isTwoWay()) {
                handleRequest(exchangeChannel, request);
            } else {
                handler.received(exchangeChannel, request.getData());
            }
        }
    } else if (message instanceof Response) {
        handleResponse(channel, (Response) message);
    } else if (message instanceof String) {
        if (isClientSide(channel)) {
            Exception e = new Exception("Dubbo client can not supported string message: " + message + " in channel: " + channel + ", url: " + channel.getUrl());
            logger.error(e.getMessage(), e);
        } else {
            String echo = handler.telnet(channel, (String) message);
            if (echo != null && echo.length() > 0) {
                channel.send(echo);
            }
        }
    } else {
        handler.received(exchangeChannel, message);
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) RemotingException(org.apache.dubbo.remoting.RemotingException) ExecutionException(org.apache.dubbo.remoting.ExecutionException)

Example 19 with ExchangeChannel

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

the class HeaderExchangeHandler method caught.

@Override
public void caught(Channel channel, Throwable exception) throws RemotingException {
    if (exception instanceof ExecutionException) {
        ExecutionException e = (ExecutionException) exception;
        Object msg = e.getRequest();
        if (msg instanceof Request) {
            Request req = (Request) msg;
            if (req.isTwoWay() && !req.isHeartbeat()) {
                Response res = new Response(req.getId(), req.getVersion());
                res.setStatus(Response.SERVER_ERROR);
                res.setErrorMessage(StringUtils.toString(e));
                channel.send(res);
                return;
            }
        }
    }
    ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
    try {
        handler.caught(exchangeChannel, exception);
    } finally {
        HeaderExchangeChannel.removeChannelIfDisconnected(channel);
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) ExecutionException(org.apache.dubbo.remoting.ExecutionException)

Example 20 with ExchangeChannel

use of org.apache.dubbo.remoting.exchange.ExchangeChannel 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();
}
Also used : ExchangeHandlerAdapter(org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter) Group(org.apache.dubbo.remoting.p2p.Group) Peer(org.apache.dubbo.remoting.p2p.Peer) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) CountDownLatch(java.util.concurrent.CountDownLatch) ExchangeHandler(org.apache.dubbo.remoting.exchange.ExchangeHandler) CompletableFuture(java.util.concurrent.CompletableFuture) RemotingException(org.apache.dubbo.remoting.RemotingException) Test(org.junit.jupiter.api.Test)

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