Search in sources :

Example 26 with IpAddress

use of org.jgroups.stack.IpAddress in project JGroups by belaban.

the class AUTHTest method testFixedMembershipTokenIPv6.

public void testFixedMembershipTokenIPv6() throws Exception {
    FixedMembershipToken tok = new FixedMembershipToken();
    tok.setMemberList("fe80::aa20:66ff:fe11:d346,2a02:120b:2c45:1b70:aa20:66ff:fe11:d346/7500,2a02:120b:2c45:1b70:f474:e6ca:3038:6b5f/7500");
    assert tok.isInMembersList(new IpAddress("2a02:120b:2c45:1b70:f474:e6ca:3038:6b5f", 7500));
}
Also used : FixedMembershipToken(org.jgroups.auth.FixedMembershipToken) IpAddress(org.jgroups.stack.IpAddress)

Example 27 with IpAddress

use of org.jgroups.stack.IpAddress in project JGroups by belaban.

the class Util method parse.

public static void parse(InputStream input, BiConsumer<Short, Message> msg_consumer, BiConsumer<Short, MessageBatch> batch_consumer, boolean tcp) {
    if (msg_consumer == null && batch_consumer == null)
        return;
    byte[] tmp = new byte[Global.INT_SIZE];
    MessageFactory mf = new DefaultMessageFactory();
    try (DataInputStream dis = new DataInputStream(input)) {
        for (; ; ) {
            // for TCP, we send the length first; this needs to be skipped as it is not part of the JGroups payload
            if (tcp) {
                // TCP / TCP_NIO2
                dis.readFully(tmp);
                if (Arrays.equals(Connection.cookie, tmp)) {
                    // connection establishment; parse version (short) and IpAddress
                    // version
                    dis.readShort();
                    // address length (only needed by TCP_NIO2)
                    dis.readShort();
                    IpAddress peer = new IpAddress();
                    peer.readFrom(dis);
                    continue;
                } else {
                // do nothing - the 4 bytes were the length
                // int len=Bits.readInt(tmp, 0);
                }
            }
            short version = dis.readShort();
            byte flags = dis.readByte();
            boolean is_message_list = (flags & LIST) == LIST;
            boolean multicast = (flags & MULTICAST) == MULTICAST;
            if (is_message_list) {
                // used if message bundling is enabled
                final MessageBatch[] batches = Util.readMessageBatch(dis, multicast, mf);
                for (MessageBatch batch : batches) {
                    if (batch == null)
                        continue;
                    if (batch_consumer != null)
                        batch_consumer.accept(version, batch);
                    else {
                        for (Message msg : batch) msg_consumer.accept(version, msg);
                    }
                }
            } else {
                Message msg = Util.readMessage(dis, mf);
                if (msg_consumer != null)
                    msg_consumer.accept(version, msg);
            }
        }
    } catch (EOFException ignored) {
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : IpAddress(org.jgroups.stack.IpAddress)

Example 28 with IpAddress

use of org.jgroups.stack.IpAddress in project JGroups by belaban.

the class FD_SOCK method fetchPingAddress.

/**
 *     Attempts to obtain the ping_addr first from the cache, then by unicasting q request to {@code mbr},
 *     then by multicasting a request to all members.
 */
protected IpAddress fetchPingAddress(final Address mbr) {
    IpAddress ret;
    if (mbr == null)
        return null;
    // 1. Try to get the server socket address from the cache
    if ((ret = cache.get(mbr)) != null)
        return ret;
    if (!isPingerThreadRunning())
        return null;
    // 2. Try to get the server socket address from mbr (or all, as fallback)
    ping_addr_promise.reset();
    for (Address dest : Arrays.asList(mbr, null)) {
        Message msg = new EmptyMessage(dest).setFlag(Message.TransientFlag.DONT_LOOPBACK).putHeader(this.id, new FdHeader(FdHeader.WHO_HAS_SOCK, mbr));
        down_prot.down(msg);
        if ((ret = ping_addr_promise.getResult(500)) != null)
            return ret;
        if (!isPingerThreadRunning())
            return null;
    }
    return null;
}
Also used : IpAddress(org.jgroups.stack.IpAddress) IpAddress(org.jgroups.stack.IpAddress)

Example 29 with IpAddress

use of org.jgroups.stack.IpAddress in project JGroups by belaban.

the class FD_SOCK method marshal.

public static ByteArray marshal(LazyRemovalCache<Address, IpAddress> addrs) {
    final ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(512);
    try {
        int size = addrs != null ? addrs.size() : 0;
        out.writeInt(size);
        if (size > 0) {
            for (Map.Entry<Address, LazyRemovalCache.Entry<IpAddress>> entry : addrs.entrySet()) {
                Address key = entry.getKey();
                IpAddress val = entry.getValue().getVal();
                Util.writeAddress(key, out);
                Util.writeStreamable(val, out);
            }
        }
        return out.getBuffer();
    } catch (Exception ex) {
        return null;
    }
}
Also used : IpAddress(org.jgroups.stack.IpAddress) IpAddress(org.jgroups.stack.IpAddress) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 30 with IpAddress

use of org.jgroups.stack.IpAddress in project JGroups by belaban.

the class FD_SOCK method up.

public Object up(Message msg) {
    FdHeader hdr = msg.getHeader(this.id);
    if (hdr == null)
        // message did not originate from FD_SOCK layer, just pass up
        return up_prot.up(msg);
    switch(hdr.type) {
        case FdHeader.SUSPECT:
            if (hdr.mbrs != null) {
                log.trace("%s: received SUSPECT message from %s: suspects=%s", local_addr, msg.getSrc(), hdr.mbrs);
                suspect(hdr.mbrs);
            }
            break;
        case FdHeader.UNSUSPECT:
            if (hdr.mbrs != null) {
                log.trace("%s: received UNSUSPECT message from %s: mbrs=%s", local_addr, msg.getSrc(), hdr.mbrs);
                hdr.mbrs.forEach(this::unsuspect);
            }
            break;
        // If I have the sock for 'hdr.mbr', return it. Otherwise look it up in my cache and return it
        case FdHeader.WHO_HAS_SOCK:
            if (Objects.equals(local_addr, msg.getSrc()))
                // don't reply to WHO_HAS bcasts sent by me !
                return null;
            if (hdr.mbr == null)
                return null;
            log.trace("%s: who-has-sock %s", local_addr, hdr.mbr);
            // 1. Try my own address, maybe it's me whose socket is wanted
            if (local_addr != null && local_addr.equals(hdr.mbr) && srv_sock_addr != null) {
                // unicast message to msg.getSrc()
                sendIHaveSockMessage(msg.getSrc(), local_addr, srv_sock_addr);
                return null;
            }
            // 2. If I don't have it, maybe it is in the cache
            IpAddress addr = cache.get(hdr.mbr);
            if (addr != null)
                // ucast msg
                sendIHaveSockMessage(msg.getSrc(), hdr.mbr, addr);
            break;
        // Update the cache with the addr:sock_addr entry (if on the same host)
        case FdHeader.I_HAVE_SOCK:
            if (hdr.mbr == null || hdr.sock_addr == null)
                return null;
            // update the cache
            cache.add(hdr.mbr, hdr.sock_addr);
            log.trace("%s: i-have-sock: %s --> %s (cache is %s)", local_addr, hdr.mbr, hdr.sock_addr, cache);
            if (hdr.mbr.equals(ping_dest))
                ping_addr_promise.setResult(hdr.sock_addr);
            break;
        // Return the cache to the sender of this message
        case FdHeader.GET_CACHE:
            msg = new BytesMessage(msg.getSrc()).putHeader(this.id, new FdHeader(FdHeader.GET_CACHE_RSP)).setArray(marshal(cache));
            down_prot.down(msg);
            break;
        case FdHeader.GET_CACHE_RSP:
            Map<Address, IpAddress> cachedAddrs = unmarshal(msg.getArray(), msg.getOffset(), msg.getLength());
            if (cachedAddrs != null)
                get_cache_promise.setResult(cachedAddrs);
            break;
    }
    return null;
}
Also used : IpAddress(org.jgroups.stack.IpAddress) IpAddress(org.jgroups.stack.IpAddress)

Aggregations

IpAddress (org.jgroups.stack.IpAddress)87 InetSocketAddress (java.net.InetSocketAddress)15 Address (org.jgroups.Address)13 InetAddress (java.net.InetAddress)7 Test (org.testng.annotations.Test)7 Event (org.jgroups.Event)6 PhysicalAddress (org.jgroups.PhysicalAddress)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 IOException (java.io.IOException)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 JChannel (org.jgroups.JChannel)3 ManagedOperation (org.jgroups.annotations.ManagedOperation)3 DefaultSocketFactory (org.jgroups.util.DefaultSocketFactory)3 Container (io.fabric8.kubernetes.api.model.Container)2 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 ServerSocket (java.net.ServerSocket)2