Adds method to generate Reservation-Token and overrides toString method in ReservationTokenAttribute. Patch by Aakash Garg.

This commit is contained in:
paweldomas 2014-06-15 19:20:52 +00:00
parent d6b41e2251
commit 8949f3f318

View File

@ -11,10 +11,11 @@ import java.util.*;
import org.ice4j.*; import org.ice4j.*;
/** /**
* The RESERVATION-TOKEN attribute contains a token that identifie a reservation port * The RESERVATION-TOKEN attribute contains a token that identifies a
* on a TURN server. The value is on 64 bits (8 bytes). * reservation port on a TURN server. The value is on 64 bits (8 bytes).
* *
* @author Sebastien Vincent * @author Sebastien Vincent
* @author Aakash Garg
*/ */
public class ReservationTokenAttribute public class ReservationTokenAttribute
extends Attribute extends Attribute
@ -29,12 +30,24 @@ public class ReservationTokenAttribute
*/ */
private byte reservationToken[] = null; 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. * Constructor.
*/ */
protected ReservationTokenAttribute () protected ReservationTokenAttribute ()
{ {
super(RESERVATION_TOKEN); super(RESERVATION_TOKEN);
this.reservationToken = new byte[8];
} }
/** /**
@ -47,6 +60,7 @@ public class ReservationTokenAttribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attributeValue contains invalid reservationToken. * @throws StunException if attributeValue contains invalid reservationToken.
*/ */
@Override
void decodeAttributeBody(byte[] attributeValue, char offset, char length) void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException throws StunException
{ {
@ -63,6 +77,7 @@ public class ReservationTokenAttribute
* Returns a binary representation of this attribute. * Returns a binary representation of this attribute.
* @return a binary representation of this attribute. * @return a binary representation of this attribute.
*/ */
@Override
public byte[] encode() public byte[] encode()
{ {
char type = getAttributeType(); char type = getAttributeType();
@ -87,6 +102,7 @@ public class ReservationTokenAttribute
* *
* @return this attribute's name. * @return this attribute's name.
*/ */
@Override
public String getName() public String getName()
{ {
return NAME; return NAME;
@ -129,17 +145,64 @@ public class ReservationTokenAttribute
* Returns the length of this attribute's body. * Returns the length of this attribute's body.
* @return the length of this attribute's value. * @return the length of this attribute's value.
*/ */
@Override
public char getDataLength() public char getDataLength()
{ {
return (char)reservationToken.length; 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
* <tt>System.currentTimeMillis()</tt>. Putting the right most bits first so
* that we get a more optimized equals() method.
*
* @return A <tt>Reservation Token </tt>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 * Compares two STUN Attributes. Two attributes are considered equal when
* they have the same type length and value. * they have the same type length and value.
* @param obj the object to compare this attribute with. * @param obj the object to compare this attribute with.
* @return true if the attributes are equal and false otherwise. * @return true if the attributes are equal and false otherwise.
*/ */
@Override
public boolean equals(Object obj) public boolean equals(Object obj)
{ {
if (! (obj instanceof ReservationTokenAttribute) if (! (obj instanceof ReservationTokenAttribute)
@ -157,4 +220,50 @@ public class ReservationTokenAttribute
return true; 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
* <tt>String</tt>.
*
* @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;
}
} }