I3Identifier Class Reference

#include <I3Identifier.h>

List of all members.

Public Member Functions

 I3Identifier ()
 Constructor.
 I3Identifier (unsigned char b)
 Constructs an identifier filled with a byte.
 I3Identifier (int prefixL, int keyL)
 Constructor for variable prefix length, key length.
 I3Identifier (const I3Identifier &id)
 Copy constructor.
 I3Identifier (std::string s)
 Constructs an identifier from the hash of a string.
int getKeyLength () const
 Returns the key length (total length) in bits.
int getPrefixLength () const
 Returns the prefix length in bits.
void clear ()
 Sets all bits to 0.
int compareTo (const I3Identifier &) const
 Comparation function.
bool operator< (const I3Identifier &) const
 "Less than" comparation function
bool operator> (const I3Identifier &) const
 "Greater than" comparation function
bool operator== (const I3Identifier &) const
 "Equals" comparation function
I3Identifieroperator= (const I3Identifier &)
 Copy operator.
bool isClear ()
 Checks if this identifier has been cleared.
bool isMatch (const I3Identifier &id) const
 Checks if this identifier's first prefixLength bits equal those of id.
int distanceTo (const I3Identifier &id) const
 Returns the "distance to" a identifier.
void createFromHash (const std::string &s, const std::string &o="")
 Creates an identifier from the hash of a string.
void createRandomKey ()
void createRandomPrefix ()
void createRandomSuffix ()
int length () const
OverlayKey asOverlayKey () const
 Creates an OverlayKey from an identifier, to be used in the overlay underneath.
void setName (std::string s)
std::string getName ()
 ~I3Identifier ()
 Destructor.

Protected Member Functions

void initKey (int prefixL, int keyL)
 Inits the key with a given prefix length and key length.

Protected Attributes

unsigned char * key
 Identifier bits.
unsigned short prefixLength
 Size of prefix in bits.
unsigned short keyLength
 Size of identifier in bits.
std::string name

Friends

std::ostream & operator<< (std::ostream &os, const I3Identifier &id)
 String stream output operator.

Detailed Description

Definition at line 40 of file I3Identifier.h.


Constructor & Destructor Documentation

I3Identifier::I3Identifier (  ) 

Constructor.

Definition at line 36 of file I3Identifier.cc.

I3Identifier::I3Identifier ( unsigned char  b  ) 

Constructs an identifier filled with a byte.

Parameters:
b Byte to fill with
I3Identifier::I3Identifier ( int  prefixL,
int  keyL 
)

Constructor for variable prefix length, key length.

Parameters:
prefixL Prefix length
keyL Key length

Definition at line 41 of file I3Identifier.cc.

00042 {
00043     initKey(prefixL, keyL);
00044 }

I3Identifier::I3Identifier ( const I3Identifier id  ) 

Copy constructor.

Parameters:
id Identifier to copy

Definition at line 46 of file I3Identifier.cc.

00047 {
00048     initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH);
00049     *this = id;
00050 }

I3Identifier::I3Identifier ( std::string  s  ) 

Constructs an identifier from the hash of a string.

Parameters:
s String to be hashed

Definition at line 52 of file I3Identifier.cc.

I3Identifier::~I3Identifier (  ) 

Destructor.

Definition at line 230 of file I3Identifier.cc.

00231 {
00232     if (key == 0) {
00233         cout << "Warning: key already deleted." << endl;
00234     }
00235     delete[] key;
00236     key = 0;
00237 }


Member Function Documentation

OverlayKey I3Identifier::asOverlayKey (  )  const

Creates an OverlayKey from an identifier, to be used in the overlay underneath.

No hashing is done, the first min(OverlayKey::keySize, keyLength) bits are copied directly.

Returns:
An overlay key

Definition at line 124 of file I3Identifier.cc.

Referenced by I3::handleUDPMessage(), and I3::sendPacket().

00125 {
00126     return OverlayKey(key, prefixLength / 8);
00127 }

void I3Identifier::clear (  ) 

Sets all bits to 0.

Definition at line 58 of file I3Identifier.cc.

Referenced by I3Trigger::clear(), createFromHash(), and I3SubIdentifier::setIPAddress().

00059 {
00060     memset(key, 0, keyLength / 8);
00061 }

int I3Identifier::compareTo ( const I3Identifier id  )  const

Comparation function.

Definition at line 81 of file I3Identifier.cc.

Referenced by I3Trigger::compareTo(), I3SubIdentifier::compareTo(), operator<(), operator==(), and operator>().

00082 {
00083     return memcmp(key, id.key, keyLength / 8);
00084 }

void I3Identifier::createFromHash ( const std::string &  s,
const std::string &  o = "" 
)

Creates an identifier from the hash of a string.

Parameters:
s String to be hashed to form the prefix
o String to be hashed to form the remaining bits

Definition at line 129 of file I3Identifier.cc.

Referenced by I3HostMobility::discoverPartners(), I3SessionClient::handleTimerEvent(), I3LatencyStretch::handleTimerEvent(), I3Identifier(), I3SessionClient::initializeApp(), I3Triggers::initializeI3(), I3SessionClient::initializeI3(), I3SessionServer::initializeI3(), I3LatencyStretch::initializeI3(), and I3HostMobility::initializeI3().

00130 {
00131     uint8_t temp[20];
00132     CSHA1 sha1;
00133     int size1, size2;
00134 
00135     sha1.Reset();
00136     sha1.Update((uint8_t*)p.c_str(), p.size());
00137     sha1.Final();
00138     sha1.GetHash(temp);
00139 
00140     clear();
00141     size1 = prefixLength / 8;
00142     if (size1 > 20) size1 = 20;
00143     memcpy(key, temp, size1);
00144 
00145     name = p + ":0";
00146 
00147     if (o.size() == 0) return;
00148 
00149     name = p + ":" + o;
00150 
00151     sha1.Reset();
00152     sha1.Update((uint8_t*)o.c_str(), o.size());
00153     sha1.Final();
00154     sha1.GetHash(temp);
00155 
00156     clear();
00157     size2 = (keyLength - prefixLength) / 8;
00158     if (size2 > 20) size2 = 20;
00159     memcpy(key + size1, temp, size2);
00160 }

void I3Identifier::createRandomKey (  ) 
void I3Identifier::createRandomPrefix (  ) 

Definition at line 168 of file I3Identifier.cc.

Referenced by createRandomKey().

00169 {
00170     for (int i = 0; i < prefixLength / 8; i++) {
00171         key[i] = intrand(256);
00172     }
00173 }

void I3Identifier::createRandomSuffix (  ) 
int I3Identifier::distanceTo ( const I3Identifier id  )  const

Returns the "distance to" a identifier.

This is used when many triggers match an identifier, to check which is the biggest prefix match. The distance is defined as the index of the byte (counting from the end) which is the first that is not equal, multiplied by 256, then added the XOR'ing of the differing byte. That way an earlier differing bits are considered "further" from later differing ones (notice that this distance is not a real metric!)

Parameters:
id Identifier to be compared to

Definition at line 113 of file I3Identifier.cc.

00114 {
00115     int index;
00116 
00117     for (index = 0; index < keyLength; index++) {
00118         if (key[index] != id.key[index]) break;
00119     }
00120 
00121     return (keyLength - index) * 256 + (key[index] ^ id.key[index]);
00122 }

int I3Identifier::getKeyLength (  )  const

Returns the key length (total length) in bits.

Definition at line 76 of file I3Identifier.cc.

00077 {
00078     return keyLength;
00079 }

std::string I3Identifier::getName (  ) 

Definition at line 190 of file I3Identifier.cc.

00190                                 {
00191     return name;
00192 }

int I3Identifier::getPrefixLength (  )  const

Returns the prefix length in bits.

Definition at line 71 of file I3Identifier.cc.

00072 {
00073     return prefixLength;
00074 }

void I3Identifier::initKey ( int  prefixL,
int  keyL 
) [protected]

Inits the key with a given prefix length and key length.

Parameters:
prefixL Prefix length
keyL Key (identifier) length

Definition at line 29 of file I3Identifier.cc.

Referenced by I3Identifier().

00030 {
00031     prefixLength = prefixL;
00032     keyLength = keyL;
00033     key = new unsigned char[keyLength / 8];
00034 }

bool I3Identifier::isClear (  ) 

Checks if this identifier has been cleared.

Definition at line 63 of file I3Identifier.cc.

00064 {
00065     for (int i = 0; i < keyLength / 8; i++) {
00066         if (key[i] != 0) return true;
00067     }
00068     return false;
00069 }

bool I3Identifier::isMatch ( const I3Identifier id  )  const

Checks if this identifier's first prefixLength bits equal those of id.

Parameters:
id Identifier to be matched against

Definition at line 108 of file I3Identifier.cc.

00109 {
00110     return memcmp(key, id.key, prefixLength / 8) == 0;
00111 }

int I3Identifier::length (  )  const

Definition at line 182 of file I3Identifier.cc.

Referenced by I3Trigger::length(), and I3SubIdentifier::length().

00182                                {
00183     return 16 + keyLength;
00184 }

bool I3Identifier::operator< ( const I3Identifier id  )  const

"Less than" comparation function

Definition at line 86 of file I3Identifier.cc.

00087 {
00088     return compareTo(id) < 0;
00089 }

I3Identifier & I3Identifier::operator= ( const I3Identifier id  ) 

Copy operator.

Definition at line 101 of file I3Identifier.cc.

00102 {
00103     memcpy(key, id.key, keyLength / 8);
00104     name = id.name;
00105     return *this;
00106 }

bool I3Identifier::operator== ( const I3Identifier id  )  const

"Equals" comparation function

Definition at line 96 of file I3Identifier.cc.

00097 {
00098     return compareTo(id) == 0;
00099 }

bool I3Identifier::operator> ( const I3Identifier id  )  const

"Greater than" comparation function

Definition at line 91 of file I3Identifier.cc.

00092 {
00093     return compareTo(id) > 0;
00094 }

void I3Identifier::setName ( std::string  s  ) 

Definition at line 186 of file I3Identifier.cc.

Referenced by I3LatencyStretch::initializeI3(), I3HostMobility::initializeI3(), and I3BaseApp::retrieveClosestIdentifier().

00186                                       {
00187     name = s;
00188 }


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const I3Identifier id 
) [friend]

String stream output operator.

Parameters:
os String stream
id I3Identifier to be output
Returns:
os parameter

Definition at line 195 of file I3Identifier.cc.

00196 {
00197     bool allzeros;
00198     const char hex[] = "0123456789abcdef";
00199     string s0, s1;
00200 
00201     if (id.name.length() != 0) {
00202         os << "(" << id.name << ") ";
00203     }
00204 
00205     for (int i = 0; i < id.prefixLength / 8; i++) {
00206         os << hex[id.key[i] >> 4];
00207         os << hex[id.key[i] & 0xf];
00208     }
00209     os << ':';
00210 
00211     allzeros = true;
00212     for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) {
00213         if (id.key[i] != 0) {
00214             allzeros = false;
00215             break;
00216         }
00217     }
00218     if (allzeros) {
00219         os << "0...";
00220     } else {
00221         for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) {
00222             os << hex[id.key[i] >> 4];
00223             os << hex[id.key[i] & 0xf];
00224         }
00225     }
00226     return os;
00227 }


Member Data Documentation

unsigned char* I3Identifier::key [protected]
unsigned short I3Identifier::keyLength [protected]

Size of identifier in bits.

Definition at line 148 of file I3Identifier.h.

Referenced by clear(), compareTo(), createFromHash(), createRandomSuffix(), distanceTo(), getKeyLength(), initKey(), isClear(), length(), and operator=().

std::string I3Identifier::name [protected]

Definition at line 150 of file I3Identifier.h.

Referenced by createFromHash(), getName(), operator=(), and setName().

unsigned short I3Identifier::prefixLength [protected]

Size of prefix in bits.

Definition at line 145 of file I3Identifier.h.

Referenced by asOverlayKey(), createFromHash(), createRandomPrefix(), createRandomSuffix(), getPrefixLength(), initKey(), and isMatch().


The documentation for this class was generated from the following files:
Generated on Wed May 26 16:21:17 2010 for OverSim by  doxygen 1.6.3