Search code examples
jgroups

JGroups fetch IPAddess/bind_addr from a received Message


I have a 3 node JGroups cluster working fine. Is there any way to retrieve bind_addr property from the other nodes via communication protocols? Essentially, I am trying to fetch IP address of the sender (on the receiver), or the IP address of the dead node in the changed view. Is there such a way?

Also, the class Address, contains hostname, can we configure this class to contain IP address (bind_addr) so that I can make use of?

address instanceof IpAddress this guy returns me false (as it is an instance of UUID).

On the current node: GMS gms = (GMS) stack.findProtocol(GMS.class); logger.info("GMS.local address : "+gms.getLocalAddress()); It returns me the hostname :(

I know working with hostname is recommended, but I would also like to retrieve the bind_addr in the message requests/cluster join/leave view changes.

Any help?

ProtocolStack stack = channel.getProtocolStack();
final TCP tcp = (TCP) stack.findProtocol(TCP.class);
channel.addAddressGenerator(new AddressGenerator() {
     public Address generateAddress() {
        ExtendedUUID retval = ExtendedUUID.randomUUID();
        retval.put("ip", tcp.getBindAddress().getAddress());
        return retval;
    }
});
channel.connect(clusterName);

byte[] ipAsByteArray =((ExtendedUUID)sourceAddress).get("ip");
String str=Util.bytesToString(ipAsByteArray);

This prints junk character :|


Solution

  • Regarding retrieving the IP, you should get hold of the transport protocol (UDP or TCP) and call getBindAddress().

    You can send this information via a custom address. For that you need to use AddressGenerator:

    JChannel ch=new JChannel();
    ch.addAddressGenerator(new AddressGenerator() {
        public Address generateAddress() {
            ExtendedUUID extendedAddress=ExtendedUUID.randomUUID();
                retval.put("ip", Util.stringToBytes(transportProtocol.getBindAddress().getAddress()));
                return extendedAddress;
            }
        });
    ch.connect("my-cluster");
    

    ExtendedUUID is basically a way to add additional payload to the address, but be aware that this payload is sent with all messages (keep it small!)

    To retrieve the payload, you need to get the address from the message and do something like this:

    Address sourceAddress = message.getSrc();
    byte[] ipAsByteArray =((ExtendedUUID)sourceAddress).get("ip");
    String str=Util.bytesToString(ipAsByteArray);