SessionCounter.cpp

00001 #include "SessionCounter.hpp" 00002 #include <assert.h> 00003 00004 SessionCounter::SessionCounter(const char* filenam) : VThread() 00005 { 00006 assert(filenam); 00007 strcpy(this->filename,filenam); 00008 this->counter=0; 00009 this->incr=0; 00010 this->decr=0; 00011 this->stopped=false; 00012 imutex.initialize("SessionCounterImutex"); 00013 dmutex.initialize("SessionCounterDmutex"); 00014 } 00015 00016 SessionCounter::~SessionCounter() 00017 { 00018 imutex.destroy(); 00019 dmutex.destroy(); 00020 } 00021 void SessionCounter::increment() 00022 { 00023 imutex.lock(); 00024 incr+=1; 00025 imutex.release(); 00026 } 00027 00028 void SessionCounter::decrement() 00029 { 00030 dmutex.lock(); 00031 decr+=1; 00032 dmutex.release(); 00033 } 00034 00035 void SessionCounter::stop() 00036 { 00037 this->stopped = true; 00038 } 00039 void SessionCounter::run() 00040 { 00041 timeval currentTime,startTime; 00042 if(filename==NULL) { 00043 this->exit(); 00044 return; 00045 } 00046 FILE* file = fopen(this->filename,"w+"); 00047 if(file) { 00048 gettimeofday(&startTime,NULL); 00049 for (unsigned int i = 0 ; !stopped ; i++) 00050 { 00051 if(gettimeofday(&currentTime,NULL) == 0) 00052 { 00053 fprintf(file,"%u %ld %u %u %u\n",i,currentTime.tv_sec,this->incr-this->decr,this->incr,this->decr); 00054 fflush(file); 00055 sleep(1); 00056 } 00057 } 00058 fclose(file); 00059 } 00060 this->exit(); 00061 }