Search in sources :

Example 1 with Peer

use of org.apache.dubbo.remoting.p2p.Peer in project dubbo by alibaba.

the class FileGroup method join.

@Override
public Peer join(URL url, ChannelHandler handler) throws RemotingException {
    Peer peer = super.join(url, handler);
    try {
        String full = url.toFullString();
        String[] lines = IOUtils.readLines(file);
        for (String line : lines) {
            if (full.equals(line)) {
                return peer;
            }
        }
        IOUtils.appendLines(file, new String[] { full });
    } catch (IOException e) {
        throw new RemotingException(new InetSocketAddress(NetUtils.getLocalHost(), 0), getUrl().toInetSocketAddress(), e.getMessage(), e);
    }
    return peer;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Peer(org.apache.dubbo.remoting.p2p.Peer) RemotingException(org.apache.dubbo.remoting.RemotingException) IOException(java.io.IOException)

Example 2 with Peer

use of org.apache.dubbo.remoting.p2p.Peer 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)

Example 3 with Peer

use of org.apache.dubbo.remoting.p2p.Peer in project dubbo by alibaba.

the class MulticastNetworkerTest method testJoin.

@Test
public void testJoin() throws RemotingException, InterruptedException {
    final String groupURL = "multicast://224.5.6.7:1234";
    final String peerURL = "exchange://0.0.0.0:" + NetUtils.getAvailablePort() + "?exchanger=header";
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    Peer peer1 = Networkers.join(groupURL, peerURL, new ChannelHandlerAdapter() {

        @Override
        public void received(Channel channel, Object message) {
            countDownLatch.countDown();
        }
    });
    Peer peer2 = Networkers.join(groupURL, "exchange://0.0.0.0:" + NetUtils.getAvailablePort() + "?exchanger=header", mock(ChannelHandlerAdapter.class));
    while (true) {
        long count = countDownLatch.getCount();
        if (count > 0) {
            break;
        }
        for (Channel channel : peer1.getChannels()) {
            channel.send("hello world!");
        }
        TimeUnit.MILLISECONDS.sleep(50);
    }
    Group lookup = Networkers.lookup(groupURL);
    assertThat(lookup, not(nullValue()));
    peer2.close();
    peer1.close();
}
Also used : ChannelHandlerAdapter(org.apache.dubbo.remoting.transport.ChannelHandlerAdapter) Group(org.apache.dubbo.remoting.p2p.Group) Peer(org.apache.dubbo.remoting.p2p.Peer) Channel(org.apache.dubbo.remoting.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with Peer

use of org.apache.dubbo.remoting.p2p.Peer in project dubbo by alibaba.

the class MulticastGroup method join.

@Override
public Peer join(URL url, ChannelHandler handler) throws RemotingException {
    Peer peer = super.join(url, handler);
    send(JOIN + " " + url.toFullString());
    return peer;
}
Also used : Peer(org.apache.dubbo.remoting.p2p.Peer)

Example 5 with Peer

use of org.apache.dubbo.remoting.p2p.Peer in project dubbo by alibaba.

the class FileNetworkerTest method testJoin.

@Test
public void testJoin(@TempDir Path folder) throws RemotingException, InterruptedException {
    final String groupURL = "file:///" + folder.getFileName().toAbsolutePath();
    FileNetworker networker = new FileNetworker();
    Group group = networker.lookup(URL.valueOf(groupURL));
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    Peer peer1 = group.join(URL.valueOf("exchange://0.0.0.0:" + NetUtils.getAvailablePort() + "?exchanger=header"), new ChannelHandlerAdapter() {

        @Override
        public void received(Channel channel, Object message) {
            countDownLatch.countDown();
        }
    });
    Peer peer2 = group.join(URL.valueOf("exchange://0.0.0.0:" + NetUtils.getAvailablePort() + "?exchanger=header"), mock(ChannelHandlerAdapter.class));
    while (true) {
        long count = countDownLatch.getCount();
        if (count > 0) {
            break;
        }
        for (Channel channel : peer1.getChannels()) {
            channel.send(0, false);
            channel.send("hello world!");
        }
        TimeUnit.MILLISECONDS.sleep(50);
    }
    peer2.close();
    peer1.close();
}
Also used : ChannelHandlerAdapter(org.apache.dubbo.remoting.transport.ChannelHandlerAdapter) Group(org.apache.dubbo.remoting.p2p.Group) Peer(org.apache.dubbo.remoting.p2p.Peer) Channel(org.apache.dubbo.remoting.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

Peer (org.apache.dubbo.remoting.p2p.Peer)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 Channel (org.apache.dubbo.remoting.Channel)3 Group (org.apache.dubbo.remoting.p2p.Group)3 Test (org.junit.jupiter.api.Test)3 RemotingException (org.apache.dubbo.remoting.RemotingException)2 ChannelHandlerAdapter (org.apache.dubbo.remoting.transport.ChannelHandlerAdapter)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)1 ExchangeHandler (org.apache.dubbo.remoting.exchange.ExchangeHandler)1 ExchangeHandlerAdapter (org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter)1