diff --git a/src/org/ice4j/attribute/ReservationTokenAttribute.java b/src/org/ice4j/attribute/ReservationTokenAttribute.java
index e8baa9c..628952b 100644
--- a/src/org/ice4j/attribute/ReservationTokenAttribute.java
+++ b/src/org/ice4j/attribute/ReservationTokenAttribute.java
@@ -11,10 +11,11 @@ import java.util.*;
import org.ice4j.*;
/**
- * The RESERVATION-TOKEN attribute contains a token that identifie a reservation port
- * on a TURN server. The value is on 64 bits (8 bytes).
- *
+ * The RESERVATION-TOKEN attribute contains a token that identifies a
+ * reservation port on a TURN server. The value is on 64 bits (8 bytes).
+ *
* @author Sebastien Vincent
+ * @author Aakash Garg
*/
public class ReservationTokenAttribute
extends Attribute
@@ -29,12 +30,24 @@ public class ReservationTokenAttribute
*/
private byte reservationToken[] = null;
+ /**
+ * A hashcode for hashtable storage.
+ */
+ private int hashCode = 0;
+
+ /**
+ * The object to use to generate the rightmost 8 bytes of the token.
+ */
+ private static final Random random
+ = new Random(System.currentTimeMillis());
+
/**
* Constructor.
*/
protected ReservationTokenAttribute ()
{
super(RESERVATION_TOKEN);
+ this.reservationToken = new byte[8];
}
/**
@@ -47,6 +60,7 @@ public class ReservationTokenAttribute
* @param length the length of the binary array.
* @throws StunException if attributeValue contains invalid reservationToken.
*/
+ @Override
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{
@@ -63,6 +77,7 @@ public class ReservationTokenAttribute
* Returns a binary representation of this attribute.
* @return a binary representation of this attribute.
*/
+ @Override
public byte[] encode()
{
char type = getAttributeType();
@@ -87,6 +102,7 @@ public class ReservationTokenAttribute
*
* @return this attribute's name.
*/
+ @Override
public String getName()
{
return NAME;
@@ -129,17 +145,64 @@ public class ReservationTokenAttribute
* Returns the length of this attribute's body.
* @return the length of this attribute's value.
*/
+ @Override
public char getDataLength()
{
return (char)reservationToken.length;
}
+ /**
+ * Creates a Reservation Token object.The Reservation Token itself is
+ * generated using the following algorithm:
+ *
+ * The first 6 bytes of the id are given the value of
+ * System.currentTimeMillis(). Putting the right most bits first so
+ * that we get a more optimized equals() method.
+ *
+ * @return A Reservation Token object with a unique token value.
+ */
+ public static ReservationTokenAttribute createNewReservationTokenAttribute()
+ {
+ ReservationTokenAttribute token = new ReservationTokenAttribute();
+
+ generateReservationTokenAttribute(token, 8);
+ return token;
+ }
+
+ /**
+ * Generates a random ReservationTokenAttribute
+ *
+ * @param token ReservationTokenAttribute
+ * @param nb number of bytes to generate
+ */
+ private static void generateReservationTokenAttribute(
+ ReservationTokenAttribute token, int nb)
+ {
+ long left = System.currentTimeMillis();// the first nb/2 bytes of the
+ // token
+ long right = random.nextLong();// the last nb/2 bytes of the token
+ int b = nb / 2;
+
+ for(int i = 0; i < b; i++)
+ {
+ token.reservationToken[i] = (byte)((left >> (i * 8)) & 0xFFl);
+ token.reservationToken[i + b] = (byte)((right >> (i * 8)) & 0xFFl);
+ }
+
+ //calculate hashcode for Hashtable storage.
+ token.hashCode = (token.reservationToken[3] << 24 & 0xFF000000)
+ | (token.reservationToken[2] << 16 & 0x00FF0000)
+ | (token.reservationToken[1] << 8 & 0x0000FF00)
+ | (token.reservationToken[0] & 0x000000FF);
+ }
+
/**
* Compares two STUN Attributes. Two attributes are considered equal when
* they have the same type length and value.
* @param obj the object to compare this attribute with.
* @return true if the attributes are equal and false otherwise.
*/
+ @Override
public boolean equals(Object obj)
{
if (! (obj instanceof ReservationTokenAttribute)
@@ -157,4 +220,50 @@ public class ReservationTokenAttribute
return true;
}
+
+
+ /**
+ * Returns a string representation of the token.
+ *
+ * @return a hex string representing the token.
+ */
+ @Override
+ public String toString()
+ {
+ return ReservationTokenAttribute.toString(this.reservationToken);
+ }
+
+ /**
+ * Returns a string representation of the token.
+ *
+ * @param reservationToken the Reservation Token to convert into
+ * String.
+ *
+ * @return a hex string representing the token.
+ */
+ public static String toString(byte[] reservationToken)
+ {
+ StringBuilder idStr = new StringBuilder();
+
+ idStr.append("0x");
+ for (int i = 0; i < reservationToken.length; i++)
+ {
+ if ((reservationToken[i] & 0xFF) <= 15)
+ idStr.append("0");
+
+ idStr.append(Integer.toHexString(
+ reservationToken[i] & 0xFF).toUpperCase());
+ }
+ return idStr.toString();
+ }
+
+ /**
+ * Returns the hash code of this Reservation-Token.
+ */
+ @Override
+ public int hashCode()
+ {
+ return this.hashCode;
+ }
+
}