Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merged changed implementing the generation of Address (https://issues…

  • Loading branch information...
commit 4efed0f35ac4d044896126317a58b0a617099730 2 parents c0bec04 + 1604383
@belaban authored
View
1  conf/jg-magic-map.xml
@@ -50,4 +50,5 @@
<class id="79" name="org.jgroups.util.ProxyAddress"/>
<class id="80" name="org.jgroups.protocols.PrioHeader"/>
<class id="81" name="org.jgroups.protocols.Locking$LockingHeader"/>
+ <class id="82" name="org.jgroups.util.PayloadUUID"/>
</magic-number-class-mapping>
View
17 doc/manual/en/modules/api.xml
@@ -820,6 +820,23 @@
</section>
<section>
+ <title>Generating custom addresses</title>
+ <para>
+ Since 2.12 address generation is pluggable. This means that an application can determine what kind of
+ addresses it uses. The default address type is UUID, and since some protocols use UUID, it is recommended
+ to provide custom classes as <emphasis>subclasses of UUID</emphasis>.
+ </para>
+ <para>
+ This can be used to for example pass additional data around with an address, for example information about
+ the location of the node to which the address is assigned. Note that methods equals(), hashCode() and
+ compare() of the UUID super class should not be changed.
+ </para>
+ <para>
+ An example of a subclass is org.jgroups.util.PayloadUUID.
+ </para>
+ </section>
+
+ <section>
<title>Connecting to a channel</title>
<para>When a client wants to join a group, it
View
27 src/org/jgroups/JChannel.java
@@ -10,10 +10,7 @@
import org.jgroups.logging.Log;
import org.jgroups.logging.LogFactory;
import org.jgroups.protocols.TP;
-import org.jgroups.stack.Configurator;
-import org.jgroups.stack.Protocol;
-import org.jgroups.stack.ProtocolStack;
-import org.jgroups.stack.StateTransferInfo;
+import org.jgroups.stack.*;
import org.jgroups.util.*;
import org.w3c.dom.Element;
@@ -91,6 +88,8 @@
/*the address of this JChannel instance*/
protected Address local_addr=null;
+ protected AddressGenerator address_generator=null;
+
protected String name=null;
/*the channel (also know as group) name*/
@@ -912,6 +911,24 @@ public String getClusterName() {
return closed ? null : !connected ? null : cluster_name;
}
+ /**
+ * Returns the current {@link AddressGenerator}, or null if none is set
+ * @return
+ * @since 2.12
+ */
+ public AddressGenerator getAddressGenerator() {
+ return address_generator;
+ }
+
+ /**
+ * Sets the new {@link AddressGenerator}. New addresses will be generated using the new generator. This
+ * should <em>not</em> be done while a channel is connected, but before connecting.
+ * @param address_generator
+ * @since 2.12
+ */
+ public void setAddressGenerator(AddressGenerator address_generator) {
+ this.address_generator=address_generator;
+ }
/**
* Sets a channel option. The options can be one of the following:
@@ -1786,7 +1803,7 @@ private void startStack(String cluster_name) throws ChannelException {
*/
protected void setAddress() {
Address old_addr=local_addr;
- local_addr=UUID.randomUUID();
+ local_addr=address_generator != null? address_generator.generateAddress() : UUID.randomUUID();
byte[] buf=(byte[])additional_data.get("additional_data");
if(buf != null)
View
12 src/org/jgroups/stack/AddressGenerator.java
@@ -0,0 +1,12 @@
+package org.jgroups.stack;
+
+import org.jgroups.Address;
+
+/**
+ * Callback to provide custom addresses. Will be called by {@link org.jgroups.JChannel#connect(String)}.
+ * @author Bela Ban
+ * @since 2.12
+ */
+public interface AddressGenerator {
+ Address generateAddress();
+}
View
79 src/org/jgroups/util/PayloadUUID.java
@@ -0,0 +1,79 @@
+package org.jgroups.util;
+
+import org.jgroups.Global;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.security.SecureRandom;
+
+/**
+ * Subclass of {@link UUID} which adds a string as payload. An instance of this can be fed to
+ * {@link org.jgroups.JChannel#setAddressGenerator(org.jgroups.stack.AddressGenerator)}, with the address generator
+ * creating PayloadUUIDs.
+ * @author Bela Ban
+ */
+public class PayloadUUID extends UUID {
+ private static final long serialVersionUID=-7042544908572216601L;
+
+ // don't need this as we already added PayloadUUID to jg-magic-map.xml
+ // static {
+ // ClassConfigurator.add((short)2222, PayloadUUID.class);
+ // }
+
+ protected String payload;
+
+ public PayloadUUID() {
+ }
+
+ protected PayloadUUID(byte[] data, String payload) {
+ super(data);
+ this.payload=payload;
+ }
+
+ public static PayloadUUID randomUUID(String payload) {
+ return new PayloadUUID(generateRandomBytes(), payload);
+ }
+
+ public static PayloadUUID randomUUID(String logical_name, String payload) {
+ PayloadUUID retval=new PayloadUUID(generateRandomBytes(), payload);
+ UUID.add(retval, logical_name);
+ return retval;
+ }
+
+
+ protected static byte[] generateRandomBytes() {
+ SecureRandom ng=numberGenerator;
+ if(ng == null)
+ numberGenerator=ng=new SecureRandom();
+
+ byte[] randomBytes=new byte[16];
+ ng.nextBytes(randomBytes);
+ return randomBytes;
+ }
+
+ public int size() {
+ int retval=super.size() + Global.BYTE_SIZE;
+ if(payload != null)
+ retval+=payload.length() +2;
+ return retval;
+ }
+
+ public void writeTo(DataOutputStream out) throws IOException {
+ super.writeTo(out);
+ Util.writeString(payload, out);
+ }
+
+ public void readFrom(DataInputStream in) throws IOException, IllegalAccessException, InstantiationException {
+ super.readFrom(in);
+ payload=Util.readString(in);
+ }
+
+ public String toString() {
+ return super.toString() + "-" + payload;
+ }
+
+ public String toStringLong() {
+ return super.toStringLong() + "-" + payload;
+ }
+}
View
14 src/org/jgroups/util/UUID.java
@@ -21,18 +21,18 @@
protected byte[] additional_data;
/** The random number generator used by this class to create random based UUIDs */
- private static volatile SecureRandom numberGenerator=null;
+ protected static volatile SecureRandom numberGenerator=null;
/** Keeps track of associations between logical addresses (UUIDs) and logical names */
- private static LazyRemovalCache<Address,String> cache;
+ protected static LazyRemovalCache<Address,String> cache;
private static final long serialVersionUID=3972962439975931228L;
- private static boolean print_uuids=false;
+ protected static boolean print_uuids=false;
- private static final int SIZE=Global.LONG_SIZE * 2 + Global.BYTE_SIZE;
+ protected static final int SIZE=Global.LONG_SIZE * 2 + Global.BYTE_SIZE;
- private static final LazyRemovalCache.Printable<Address,String> print_function=new LazyRemovalCache.Printable<Address,String>() {
+ protected static final LazyRemovalCache.Printable<Address,String> print_function=new LazyRemovalCache.Printable<Address,String>() {
public java.lang.String print(Address key, String val) {
return val + ": " + (key instanceof UUID? ((UUID)key).toStringLong() : key) + "\n";
}
@@ -85,7 +85,7 @@ public UUID(long mostSigBits, long leastSigBits) {
}
/** Private constructor which uses a byte array to construct the new UUID */
- private UUID(byte[] data) {
+ protected UUID(byte[] data) {
long msb = 0;
long lsb = 0;
if(data.length != 16)
@@ -227,7 +227,7 @@ public String toStringLong() {
}
/** Returns val represented by the specified number of hex digits. */
- private static String digits(long val, int digits) {
+ protected static String digits(long val, int digits) {
long hi = 1L << (digits * 4);
return Long.toHexString(hi | (val & (hi - 1))).substring(1);
}
View
8 src/org/jgroups/util/Util.java
@@ -827,13 +827,15 @@ public static void writeAddress(Address addr, DataOutputStream out) throws IOExc
out.writeByte(flags);
return;
}
- if(addr instanceof UUID) {
+
+ Class clazz=addr.getClass();
+ if(clazz.equals(UUID.class)) {
flags=Util.setFlag(flags, Address.UUID_ADDR);
}
- else if(addr instanceof IpAddress) {
+ else if(clazz.equals(IpAddress.class)) {
flags=Util.setFlag(flags, Address.IP_ADDR);
}
- else if(addr instanceof ProxyAddress) {
+ else if(clazz.equals(ProxyAddress.class)) {
flags=Util.setFlag(flags, Address.PROXY_ADDR);
}
else {
Please sign in to comment.
Something went wrong with that request. Please try again.