Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends
CAReplayDatabase Class Reference

#include <CAReplayDatabase.hpp>

Collaboration diagram for CAReplayDatabase:
[legend]

List of all members.

Public Member Functions

 CAReplayDatabase ()
 ~CAReplayDatabase ()
SINT32 insert (UINT8 key[16])
 Inserts this key in the replay DB.
SINT32 start ()
SINT32 stop ()
SINT32 getCurrentReplayTimestamp (tReplayTimestamp &replayTimestamp) const
 Returns the current Replay timestamp for this database.
UINT32 getRefTime () const
 Returns the local time in seconds since epoch for replay timestamp='0' for this database.

Static Public Member Functions

static SINT32 getReplayTimestampForTime (tReplayTimestamp &replayTimestamp, UINT32 aktTime, UINT32 refTime)
 Returns the replay timestamp for this reference time (seconds since epoch) and time.
static SINT32 getTimeForReplayTimestamp (UINT32 &refTime, tReplayTimestamp replayTimestamp)
 Returns the refernce time (seconds since epoch) for the given replay timestamp.
static SINT32 test ()
static SINT32 measurePerformance (UINT8 *strLogFile, UINT32 lowerBoundEntries, UINT32 upperBoundEntries, UINT32 stepBy, UINT32 meassuresPerStep, UINT32 insertsPerMeasure)
 This mehtod can be used to measure the performance of the Replay database.

Private Member Functions

LP_replay_databaseEntry getNewDBEntry (t_replay_databaseInfo *pDB)
t_replay_databaseInfocreateDBInfo ()
 Creates and initialises a dbinfo struct.
SINT32 clearDB (t_replay_databaseInfo *pDB)
 clears the whole database pDB - but does not delete the hashtable pDB
SINT32 deleteDB (t_replay_databaseInfo *&pDB)
 Deletes the whole database pDB.
SINT32 nextClock ()
SINT32 fill (UINT32 nrOfEntries)
 Pre fills the database with nrOfEntries random entries.
SINT32 simulateInsert (UINT8 key[16])
 This is a modified copy of insert() which simulates the insert() function as close as possible without actually changing the replay database.

Private Attributes

t_replay_databaseInfom_currDatabase
t_replay_databaseInfom_nextDatabase
t_replay_databaseInfom_prevDatabase
volatile bool m_bRun
UINT32 m_refTime
volatile SINT32 m_currentClock
CAMutexm_pMutex
CAThreadm_pThread

Friends

THREAD_RETURN replaydb_loopMaintenance (void *param)

Detailed Description

Definition at line 64 of file CAReplayDatabase.hpp.


Constructor & Destructor Documentation

Definition at line 34 of file CAReplayDatabase.cpp.

References createDBInfo(), m_currDatabase, m_currentClock, m_nextDatabase, m_pMutex, m_prevDatabase, m_pThread, and m_refTime.

Referenced by measurePerformance().

Here is the call graph for this function:

Definition at line 52 of file CAReplayDatabase.cpp.

References deleteDB(), CAMutex::lock(), m_currDatabase, m_nextDatabase, m_pMutex, m_prevDatabase, stop(), and CAMutex::unlock().

Here is the call graph for this function:


Member Function Documentation

clears the whole database pDB - but does not delete the hashtable pDB

Parameters:
pDBdatabase to delete

Definition at line 63 of file CAReplayDatabase.cpp.

References E_SUCCESS, __t_database_info::m_pHeap, __t_database_info::m_pLastHeap, and __t_replay_database_heap::next.

Referenced by deleteDB(), and nextClock().

  {
    while(pDBInfo->m_pHeap!=NULL)
      {
        pDBInfo->m_pLastHeap=pDBInfo->m_pHeap;
        pDBInfo->m_pHeap=pDBInfo->m_pHeap->next;
        delete pDBInfo->m_pLastHeap;
      }
    memset(pDBInfo,0,sizeof(t_replay_databaseInfo));
    return E_SUCCESS;
  }

Creates and initialises a dbinfo struct.

Definition at line 45 of file CAReplayDatabase.cpp.

Referenced by CAReplayDatabase().

  {
    t_replay_databaseInfo* pInfo=new t_replay_databaseInfo;
    memset(pInfo,0,sizeof(t_replay_databaseInfo));
    return pInfo;
  }

Deletes the whole database pDB.

Parameters:
pDBdatabase to delete

Definition at line 75 of file CAReplayDatabase.cpp.

References clearDB(), and E_SUCCESS.

Referenced by ~CAReplayDatabase().

  {
    clearDB(pDBInfo);
    delete pDBInfo;
    pDBInfo=NULL;
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 CAReplayDatabase::fill ( UINT32  nrOfEntries) [private]

Pre fills the database with nrOfEntries random entries.

Parameters:
nrOfEntriesnumber of entries to put in the database

Definition at line 321 of file CAReplayDatabase.cpp.

References E_SUCCESS, getRandom(), and insert().

Referenced by measurePerformance().

  {
    UINT32 i=0;
    UINT8 key[16];
    key[14]=0;
    key[15]=0;
    while(i<nrOfEntries)
      {
        getRandom(key,14);
        if(insert(key)==E_SUCCESS)
          i++;
      }
    return E_SUCCESS;
  }

Here is the call graph for this function:

Returns the current Replay timestamp for this database.

Returns the current replay timestamp for this database.

Parameters:
replayTimestampstores the current replay timestamp

Definition at line 249 of file CAReplayDatabase.cpp.

References getReplayTimestampForTime(), and m_refTime.

Referenced by nextClock(), and replaydb_loopMaintenance().

  {
    return getReplayTimestampForTime(replayTimestamp,time(NULL),m_refTime);
  }

Here is the call graph for this function:

Returns the local time in seconds since epoch for replay timestamp='0' for this database.

Definition at line 76 of file CAReplayDatabase.hpp.

References m_refTime.

        {
          return m_refTime;
        }
SINT32 CAReplayDatabase::getReplayTimestampForTime ( tReplayTimestamp replayTimestamp,
UINT32  aktTime,
UINT32  refTime 
) [static]

Returns the replay timestamp for this reference time (seconds since epoch) and time.

Definition at line 255 of file CAReplayDatabase.cpp.

References E_SUCCESS, t_replay_timestamp::interval, t_replay_timestamp::offset, and SECONDS_PER_INTERVALL.

Referenced by getCurrentReplayTimestamp().

  {
    UINT32 timeDiff=aktTime-refTime;
    replayTimestamp.interval=timeDiff/SECONDS_PER_INTERVALL;
    replayTimestamp.offset=timeDiff%SECONDS_PER_INTERVALL;
    return E_SUCCESS;
  }
static SINT32 CAReplayDatabase::getTimeForReplayTimestamp ( UINT32 refTime,
tReplayTimestamp  replayTimestamp 
) [inline, static]

Returns the refernce time (seconds since epoch) for the given replay timestamp.

Definition at line 85 of file CAReplayDatabase.hpp.

References E_SUCCESS, t_replay_timestamp::interval, t_replay_timestamp::offset, and SECONDS_PER_INTERVALL.

        {
          time_t now=time(NULL);
          refTime=(UINT32)(now-replayTimestamp.interval*SECONDS_PER_INTERVALL-replayTimestamp.offset);
          return E_SUCCESS;
        }

Inserts this key in the replay DB.

The last two bytes are the timestamp

Definition at line 84 of file CAReplayDatabase.cpp.

References E_SUCCESS, E_UNKNOWN, getNewDBEntry(), __t_replay_database_entry::key, __t_replay_database_entry::left, CAMutex::lock(), m_currDatabase, m_currentClock, m_nextDatabase, __t_database_info::m_pHashTable, m_pMutex, m_prevDatabase, __t_database_info::m_u32Size, __t_replay_database_entry::right, and CAMutex::unlock().

Referenced by fill(), and test().

  {
    m_pMutex->lock();
    UINT16 timestamp=(key[14]<<8)|key[15];
    if(timestamp<m_currentClock-1||timestamp>m_currentClock+1)
      {
        m_pMutex->unlock();
        return E_UNKNOWN;
      }
    t_replay_databaseInfo* aktDB=m_currDatabase;
    if(timestamp>m_currentClock)
      {
        aktDB=m_nextDatabase;
      }
    else if(timestamp<m_currentClock)
      {
        aktDB=m_prevDatabase;
      }
    UINT16 hashKey=(key[8]<<8)|key[9];
    LP_replay_databaseEntry hashList=aktDB->m_pHashTable[hashKey];
    if(hashList==NULL)
      {
        LP_replay_databaseEntry newEntry=getNewDBEntry(aktDB);
        newEntry->left=NULL;
        newEntry->right=NULL;
        newEntry->key=key[0]<<24|key[1]<<16|key[2]<<8|key[3];
        aktDB->m_pHashTable[hashKey]=newEntry;
        aktDB->m_u32Size++;
        m_pMutex->unlock();
        return E_SUCCESS;
      }
    else
      {
        UINT32 ret=key[0]<<24|key[1]<<16|key[2]<<8|key[3];;
        LP_replay_databaseEntry before=NULL;
        do
          {
            //newEntry->keymemcmp(key,hashList->key,6);
            if(ret==hashList->key)
              {
                m_pMutex->unlock();
                return E_UNKNOWN;
              }
            before=hashList;  
            if(hashList->key<ret)
              {
                hashList=hashList->right;
              }
            else
              {
                hashList=hashList->left;
              }
          } while(hashList!=NULL);
        LP_replay_databaseEntry newEntry=getNewDBEntry(aktDB);
        newEntry->left=newEntry->right=NULL;
        //memcpy(newEntry->key,key,6);        
        newEntry->key=ret;
        if(before->key<ret)
          {
            before->right=newEntry;
          }
        else
            before->left=newEntry;

      }
    aktDB->m_u32Size++; 
    m_pMutex->unlock();
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 CAReplayDatabase::measurePerformance ( UINT8 strLogFile,
UINT32  lowerBoundEntries,
UINT32  upperBoundEntries,
UINT32  stepBy,
UINT32  meassuresPerStep,
UINT32  insertsPerMeasure 
) [static]

This mehtod can be used to measure the performance of the Replay database.

The results are stored in a file in csv format. Ths method will do several measures with different numbers of elements in the database. These number could be specified using owerBoundEntries,upperBoundEntries and stepBy.

Parameters:
strLogFilethe log file name
lowerBoundEntriesthe number of entries in the database (at beginn)
upperBoundEntriesthe number of entries in the database (at end)
stepByhow many entries should be added for each new measurement
meassuresPerStephow many measure values should be generate per step. That means that the experiement is repeated this many times.
insertsPerMeasureone measure value will be the time: (Total Insertion Time)/insertsPerMeasure

Definition at line 264 of file CAReplayDatabase.cpp.

References CAReplayDatabase(), diff64(), E_SUCCESS, fill(), getcurrentTimeMicros(), getRandom(), initRandom(), m_currentClock, CAMsg::printMsg(), and simulateInsert().

Referenced by main().

  {
    initRandom();
    CAReplayDatabase* pDatabase=NULL;
    UINT32 aktNrOfEntries=lowerBoundEntries;
    UINT8* key=new UINT8[insertsPerMeasure*16];
    UINT8* aktKey;
    SINT32 file=open((char*)strLogFile,O_CREAT|O_WRONLY|O_LARGEFILE|O_TRUNC,S_IREAD|S_IWRITE);
    char buff[255];
    const char* atemplate="%u,%u,%u\n";
    const char* header="#The format is as follows: Number of Entries in DB, Number of Inserts done, Total time for Inserts (in micro seconds)\n";
    write(file,header,strlen(header));
    while(aktNrOfEntries<=upperBoundEntries)
      {
        CAMsg::printMsg(LOG_DEBUG,"Starting measurement with %u entries in the replay database\n",aktNrOfEntries);
        for(UINT32 i=0;i<meassuresPerStep;i++)
          {
            pDatabase=new CAReplayDatabase();
            pDatabase->m_currentClock=0;
            pDatabase->fill(aktNrOfEntries);
            UINT64 startTime,endTime;
            getRandom(key,insertsPerMeasure*16);
            aktKey=key+14;
            for(UINT32 j=0;j<insertsPerMeasure;j++)
              {
                *aktKey=0;
                aktKey++;
                *aktKey=0;
                aktKey+=15;
              }
            aktKey=key;
            getcurrentTimeMicros(startTime);
            for(UINT32 j=0;j<insertsPerMeasure;j++)
              {
                pDatabase->simulateInsert(aktKey);
                aktKey+=16;
              }
            getcurrentTimeMicros(endTime);
            sprintf(buff,atemplate,aktNrOfEntries,insertsPerMeasure,diff64(endTime,startTime));
            write(file,buff,strlen(buff));
            printf("Start delete \n");
            getcurrentTimeMicros(startTime);
            delete pDatabase;
            getcurrentTimeMicros(endTime);
            printf("delete takes %u microsecs\n",diff64(endTime,startTime));
          }
        aktNrOfEntries+=stepBy;
      }
    delete[] key;
    return E_SUCCESS;
  }

Here is the call graph for this function:

This is a modified copy of insert() which simulates the insert() function as close as possible without actually changing the replay database.

Definition at line 336 of file CAReplayDatabase.cpp.

References E_SUCCESS, E_UNKNOWN, getNewDBEntry(), __t_replay_database_entry::key, __t_replay_database_entry::left, CAMutex::lock(), m_currDatabase, m_currentClock, m_nextDatabase, __t_database_info::m_pHashTable, m_pMutex, m_prevDatabase, __t_database_info::m_u32Size, __t_replay_database_entry::right, and CAMutex::unlock().

Referenced by measurePerformance().

  {
    m_pMutex->lock();
    UINT16 timestamp=(key[14]<<8)|key[15];
    if(timestamp<m_currentClock-1||timestamp>m_currentClock+1)
      {
        m_pMutex->unlock();
        return E_UNKNOWN;
      }
    t_replay_databaseInfo* aktDB=m_currDatabase;
    if(timestamp>m_currentClock)
      {
        aktDB=m_nextDatabase;
      }
    else if(timestamp<m_currentClock)
      {
        aktDB=m_prevDatabase;
      }
    UINT16 hashKey=(key[8]<<8)|key[9];
    LP_replay_databaseEntry hashList=aktDB->m_pHashTable[hashKey];
    if(hashList==NULL)
      {
        LP_replay_databaseEntry newEntry=getNewDBEntry(aktDB);
        newEntry->left=NULL;
        newEntry->right=NULL;
        newEntry->key=key[0]<<24|key[1]<<16|key[2]<<8|key[3];
        m_nextDatabase->m_pHashTable[hashKey]=newEntry; //we simply use the 'next' Database to simulate insert while not inserting anythign in the current db
        aktDB->m_u32Size++;
        m_pMutex->unlock();
        return E_SUCCESS;
      }
    else
      {
        UINT32 ret=key[0]<<24|key[1]<<16|key[2]<<8|key[3];
        LP_replay_databaseEntry before=NULL;
        do
          {
            //newEntry->keymemcmp(key,hashList->key,6);
            if(ret==hashList->key)
              {
                m_pMutex->unlock();
                return E_UNKNOWN;
              }
            before=hashList;  
            if(hashList->key<ret)
              {
                hashList=hashList->right;
              }
            else
              {
                hashList=hashList->left;
              }
          } while(hashList!=NULL);
        LP_replay_databaseEntry newEntry=getNewDBEntry(aktDB);
        newEntry->left=newEntry->right=NULL;
        //memcpy(newEntry->key,key,6);        
        newEntry->key=ret;
        if(before->key<ret)
          {
            newEntry->right=newEntry; //do the pointer operation without actually changing the DB
          }
        else
          {
            newEntry->left=newEntry; //do the pointer operation without actually changing the DB
          }
      }
    aktDB->m_u32Size++; 
    m_pMutex->unlock();
    return E_SUCCESS; 
  }

Here is the call graph for this function:

Definition at line 154 of file CAReplayDatabase.cpp.

References m_bRun, m_pThread, replaydb_loopMaintenance, CAThread::setMainLoop(), and CAThread::start().

Referenced by test().

  {
    m_pThread=new CAThread((UINT8*)"DB Maintenance Thread");
    m_bRun=true;
    m_pThread->setMainLoop(replaydb_loopMaintenance);
    return m_pThread->start(this);
  }

Here is the call graph for this function:

Definition at line 162 of file CAReplayDatabase.cpp.

References E_SUCCESS, CAThread::join(), m_bRun, and m_pThread.

Referenced by test(), and ~CAReplayDatabase().

  {
    m_bRun=false;
    SINT32 ret=E_SUCCESS;
    if(m_pThread!=NULL)
      {
        ret=m_pThread->join();
        delete m_pThread;
        m_pThread=NULL;
      }
    return ret;
  }

Here is the call graph for this function:

TODO WRONG - fixme

Definition at line 213 of file CAReplayDatabase.cpp.

References E_SUCCESS, getRandom(), insert(), start(), and stop().

  {
    CAReplayDatabase oDatabase;
    oDatabase.start();
    UINT8 key[16];
    memset(key,0,16);
    UINT32 i;
    for(i=0;i<20;i++)
      {
        getRandom(key,1);
        oDatabase.insert(key);
      }
    for(i=0;i<200000;i++)
      {
        getRandom(key,16);
        oDatabase.insert(key);//TODO WRONG - Fixme
      }
    oDatabase.stop();
//check it
//TODO fixme!
/*    for(i=0;i<0x10000;i++)
      {
        LP_databaseEntry tmp=oDatabase.m_currDatabase[i];
        while(tmp!=NULL&&tmp->next!=NULL)
          {
            if(memcmp(tmp->key,tmp->next->key,14)>=0)
              return E_UNKNOWN;
            tmp=tmp->next;
          }
      }*/
    return E_SUCCESS;
  }

Here is the call graph for this function:


Friends And Related Function Documentation

THREAD_RETURN replaydb_loopMaintenance ( void *  param) [friend]

Definition at line 175 of file CAReplayDatabase.cpp.

Referenced by start().

  {
    INIT_STACK;
    BEGIN_STACK("CADatabase::db_loopMaintenance");
    
    CAReplayDatabase* pDatabase=(CAReplayDatabase*)param;
    tReplayTimestamp rt;
    pDatabase->getCurrentReplayTimestamp(rt);
    pDatabase->m_currentClock=rt.interval;
    while(pDatabase->m_bRun)
      {
        sSleep(10);
        SINT32 secondsTilNextClock=((pDatabase->m_currentClock+1)*SECONDS_PER_INTERVALL)+pDatabase->m_refTime-time(NULL);
        if(secondsTilNextClock<=0&&pDatabase->m_bRun)
          pDatabase->nextClock();
      }
      
    FINISH_STACK("CADatabase::db_loopMaintenance");
      
    THREAD_RETURN_SUCCESS;
  }

Member Data Documentation

volatile bool CAReplayDatabase::m_bRun [private]

Definition at line 158 of file CAReplayDatabase.hpp.

Referenced by replaydb_loopMaintenance(), start(), and stop().

Definition at line 162 of file CAReplayDatabase.hpp.

Referenced by CAReplayDatabase(), start(), and stop().


The documentation for this class was generated from the following files: