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.


Constructor & Destructor Documentation

I3Identifier::I3Identifier (  ) 

Constructor.

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
00039 {
00040     initKey(prefixL, keyL);
00041 }

I3Identifier::I3Identifier ( const I3Identifier id  ) 

Copy constructor.

Parameters:
id Identifier to copy
00044 {
00045     initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH);
00046     *this = id;
00047 }

I3Identifier::I3Identifier ( std::string  s  ) 

Constructs an identifier from the hash of a string.

Parameters:
s String to be hashed

I3Identifier::~I3Identifier (  ) 

Destructor.

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


Member Function Documentation

int I3Identifier::getKeyLength (  )  const

Returns the key length (total length) in bits.

00074 {
00075     return keyLength;
00076 }

int I3Identifier::getPrefixLength (  )  const

Returns the prefix length in bits.

00069 {
00070     return prefixLength;
00071 }

void I3Identifier::clear (  ) 

Sets all bits to 0.

00056 {
00057     memset(key, 0, keyLength / 8);
00058 }

int I3Identifier::compareTo ( const I3Identifier id  )  const

Comparation function.

00079 {
00080     return memcmp(key, id.key, keyLength / 8);
00081 }

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

"Less than" comparation function

00084 {
00085     return compareTo(id) < 0;
00086 }

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

"Greater than" comparation function

00089 {
00090     return compareTo(id) > 0;
00091 }

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

"Equals" comparation function

00094 {
00095     return compareTo(id) == 0;
00096 }

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

Copy operator.

00099 {
00100     memcpy(key, id.key, keyLength / 8);
00101     name = id.name;
00102     return *this;
00103 }

bool I3Identifier::isClear (  ) 

Checks if this identifier has been cleared.

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

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
00106 {
00107     return memcmp(key, id.key, prefixLength / 8) == 0;
00108 }

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
00111 {
00112     int index;
00113 
00114     for (index = 0; index < keyLength; index++) {
00115         if (key[index] != id.key[index]) break;
00116     }
00117 
00118     return (keyLength - index) * 256 + (key[index] ^ id.key[index]);
00119 }

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

void I3Identifier::createRandomKey (  ) 

00160 {
00161     createRandomPrefix();
00162     createRandomSuffix();
00163 }

void I3Identifier::createRandomPrefix (  ) 

00166 {
00167     for (int i = 0; i < prefixLength / 8; i++) {
00168         key[i] = intrand(256);
00169     }
00170 }

void I3Identifier::createRandomSuffix (  ) 

00173 {
00174     for (int i = prefixLength / 8; i < keyLength / 8; i++) {
00175         key[i] = intrand(256);
00176     }
00177 }

int I3Identifier::length (  )  const

00179                                {
00180     return 16 + keyLength;
00181 }

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
00122 {
00123     return OverlayKey(key, prefixLength / 8);
00124 }

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

00183                                       {
00184     name = s;
00185 }

std::string I3Identifier::getName (  ) 

00187                                 {
00188     return name;
00189 }

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
00027 {
00028     prefixLength = prefixL;
00029     keyLength = keyL;
00030     key = new unsigned char[keyLength / 8];
00031 }


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


Member Data Documentation

unsigned char* I3Identifier::key [protected]

Identifier bits.

unsigned short I3Identifier::prefixLength [protected]

Size of prefix in bits.

unsigned short I3Identifier::keyLength [protected]

Size of identifier in bits.

std::string I3Identifier::name [protected]


The documentation for this class was generated from the following files:
Generated on Thu Apr 17 13:19:29 2008 for ITM OverSim by  doxygen 1.5.3