Search in sources :

Example 6 with ServerID

use of io.vertx.core.net.impl.ServerID in project vert.x by eclipse.

the class AsyncMultiMapTest method testMapAddGet.

@Test
public void testMapAddGet() {
    ServerID serverID1 = new ServerID(1234, "foo.com");
    map.add("some-sub", serverID1, onSuccess(res -> {
        assertNull(res);
        ServerID serverID2 = new ServerID(4321, "blah.com");
        map.add("some-sub", serverID2, onSuccess(res2 -> {
            assertNull(res2);
            ServerID serverID3 = new ServerID(5432, "quux.com");
            map.add("some-sub2", serverID3, onSuccess(res3 -> {
                assertNull(res3);
                map.get("some-sub", onSuccess(res4 -> {
                    Set<ServerID> set = new HashSet<>();
                    for (ServerID sid : res4) {
                        set.add(sid);
                    }
                    assertEquals(2, set.size());
                    assertTrue(set.contains(serverID1));
                    assertTrue(set.contains(serverID2));
                    map.get("some-sub2", onSuccess(res5 -> {
                        Set<ServerID> set2 = new HashSet<>();
                        for (ServerID sid : res5) {
                            set2.add(sid);
                        }
                        assertEquals(1, set2.size());
                        assertTrue(set2.contains(serverID3));
                        testComplete();
                    }));
                }));
            }));
        }));
    }));
    await();
}
Also used : HashSet(java.util.HashSet) CountDownLatch(java.util.concurrent.CountDownLatch) VertxInternal(io.vertx.core.impl.VertxInternal) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) Set(java.util.Set) AsyncMultiMap(io.vertx.core.spi.cluster.AsyncMultiMap) Test(org.junit.Test) ServerID(io.vertx.core.net.impl.ServerID) ServerID(io.vertx.core.net.impl.ServerID) HashSet(java.util.HashSet) Set(java.util.Set) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with ServerID

use of io.vertx.core.net.impl.ServerID in project vert.x by eclipse.

the class AsyncMultiMapTest method testMapRemove.

@Test
public void testMapRemove() {
    ServerID serverID1 = new ServerID(1234, "foo.com");
    map.add("some-sub", serverID1, onSuccess(res -> {
        assertNull(res);
        ServerID serverID2 = new ServerID(4321, "blah.com");
        map.add("some-sub", serverID2, onSuccess(res2 -> {
            assertNull(res2);
            ServerID serverID3 = new ServerID(5432, "quux.com");
            map.add("some-sub2", serverID3, onSuccess(res3 -> {
                assertNull(res3);
                map.get("some-sub", onSuccess(res4 -> {
                    Set<ServerID> set = new HashSet<>();
                    for (ServerID sid : res4) {
                        set.add(sid);
                    }
                    assertEquals(2, set.size());
                    assertTrue(set.contains(serverID1));
                    assertTrue(set.contains(serverID2));
                    map.get("some-sub2", onSuccess(res5 -> {
                        Set<ServerID> set2 = new HashSet<>();
                        for (ServerID sid : res5) {
                            set2.add(sid);
                        }
                        assertEquals(1, set2.size());
                        assertTrue(set2.contains(serverID3));
                        map.remove("some-sub2", serverID1, onSuccess(res6 -> {
                            assertFalse(res6);
                            map.remove("some-sub2", serverID3, onSuccess(res7 -> {
                                map.get("some-sub2", onSuccess(res8 -> {
                                    Set<ServerID> set3 = new HashSet<>();
                                    for (ServerID sid : res8) {
                                        set3.add(sid);
                                    }
                                    assertEquals(0, set3.size());
                                    testComplete();
                                }));
                            }));
                        }));
                    }));
                }));
            }));
        }));
    }));
    await();
}
Also used : HashSet(java.util.HashSet) CountDownLatch(java.util.concurrent.CountDownLatch) VertxInternal(io.vertx.core.impl.VertxInternal) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) Set(java.util.Set) AsyncMultiMap(io.vertx.core.spi.cluster.AsyncMultiMap) Test(org.junit.Test) ServerID(io.vertx.core.net.impl.ServerID) ServerID(io.vertx.core.net.impl.ServerID) HashSet(java.util.HashSet) Set(java.util.Set) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with ServerID

use of io.vertx.core.net.impl.ServerID in project vert.x by eclipse.

the class ClusteredEventBus method sendToSubs.

private <T> void sendToSubs(ChoosableIterable<ClusterNodeInfo> subs, SendContextImpl<T> sendContext) {
    String address = sendContext.message.address();
    if (sendContext.message.isSend()) {
        // Choose one
        ClusterNodeInfo ci = subs.choose();
        ServerID sid = ci == null ? null : ci.serverID;
        if (sid != null && !sid.equals(serverID)) {
            //We don't send to this node
            metrics.messageSent(address, false, false, true);
            sendRemote(sid, sendContext.message);
        } else {
            metrics.messageSent(address, false, true, false);
            deliverMessageLocally(sendContext);
        }
    } else {
        // Publish
        boolean local = false;
        boolean remote = false;
        for (ClusterNodeInfo ci : subs) {
            if (!ci.serverID.equals(serverID)) {
                //We don't send to this node
                remote = true;
                sendRemote(ci.serverID, sendContext.message);
            } else {
                local = true;
            }
        }
        metrics.messageSent(address, true, local, remote);
        if (local) {
            deliverMessageLocally(sendContext);
        }
    }
}
Also used : ServerID(io.vertx.core.net.impl.ServerID)

Example 9 with ServerID

use of io.vertx.core.net.impl.ServerID in project vert.x by eclipse.

the class ClusteredMessage method readFromWire.

public void readFromWire(Buffer buffer, CodecManager codecManager) {
    int pos = 0;
    // Overall Length already read when passed in here
    byte protocolVersion = buffer.getByte(pos);
    if (protocolVersion > WIRE_PROTOCOL_VERSION) {
        throw new IllegalStateException("Invalid wire protocol version " + protocolVersion + " should be <= " + WIRE_PROTOCOL_VERSION);
    }
    pos++;
    byte systemCodecCode = buffer.getByte(pos);
    pos++;
    if (systemCodecCode == -1) {
        // User codec
        int length = buffer.getInt(pos);
        pos += 4;
        byte[] bytes = buffer.getBytes(pos, pos + length);
        String codecName = new String(bytes, CharsetUtil.UTF_8);
        messageCodec = codecManager.getCodec(codecName);
        if (messageCodec == null) {
            throw new IllegalStateException("No message codec registered with name " + codecName);
        }
        pos += length;
    } else {
        messageCodec = codecManager.systemCodecs()[systemCodecCode];
    }
    byte bsend = buffer.getByte(pos);
    send = bsend == 0;
    pos++;
    int length = buffer.getInt(pos);
    pos += 4;
    byte[] bytes = buffer.getBytes(pos, pos + length);
    address = new String(bytes, CharsetUtil.UTF_8);
    pos += length;
    length = buffer.getInt(pos);
    pos += 4;
    if (length != 0) {
        bytes = buffer.getBytes(pos, pos + length);
        replyAddress = new String(bytes, CharsetUtil.UTF_8);
        pos += length;
    }
    int senderPort = buffer.getInt(pos);
    pos += 4;
    length = buffer.getInt(pos);
    pos += 4;
    bytes = buffer.getBytes(pos, pos + length);
    String senderHost = new String(bytes, CharsetUtil.UTF_8);
    pos += length;
    headersPos = pos;
    int headersLength = buffer.getInt(pos);
    pos += headersLength;
    bodyPos = pos;
    sender = new ServerID(senderPort, senderHost);
    wireBuffer = buffer;
    fromWire = true;
}
Also used : ServerID(io.vertx.core.net.impl.ServerID)

Aggregations

ServerID (io.vertx.core.net.impl.ServerID)9 VertxInternal (io.vertx.core.impl.VertxInternal)5 AsyncMultiMap (io.vertx.core.spi.cluster.AsyncMultiMap)3 ClusterManager (io.vertx.core.spi.cluster.ClusterManager)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 Buffer (io.vertx.core.buffer.Buffer)2 Logger (io.vertx.core.logging.Logger)2 LoggerFactory (io.vertx.core.logging.LoggerFactory)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 Channel (io.netty.channel.Channel)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1