From d10c6de7eb349d9dcbc8bcd553d568506e34ab8f Mon Sep 17 00:00:00 2001 From: PioDer Date: Thu, 19 Jan 2017 01:33:10 +0100 Subject: [PATCH] delay'e --- Log.cpp | 24 ++++++++++++++++++++++++ Log.h | 4 ++++ NATRouter.cpp | 11 +++++------ Node.cpp | 1 + 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Log.cpp b/Log.cpp index 4cc8c6f..eb5267f 100644 --- a/Log.cpp +++ b/Log.cpp @@ -15,6 +15,7 @@ Log::Log() this->objectName = NULL; this->writeMutex = Log::getMutex(); + this->setDelay(1); } LOG_COLOR Log::getColor() const @@ -93,6 +94,16 @@ void Log::setLogParams(int lineNumber, LOG_COLOR color, string delimiter) this->setDelimiter(delimiter); } +int Log::getDelay() const +{ + return delayVal; +} + +void Log::setDelay(int delay) +{ + this->delayVal = delay; +} + pthread_mutex_t* Log::getMutex() { static pthread_mutex_t m; @@ -104,3 +115,16 @@ pthread_mutex_t* Log::getMutex() return &m; } + +void Log::delay(int optional) +{ + int t = (optional > -1) ? optional : this->delayVal; + + if (t == 0) + return; + + if (t >= 10) // ns + usleep(t); + else + sleep(t); // s +} diff --git a/Log.h b/Log.h index f9a1cc9..fa4c160 100644 --- a/Log.h +++ b/Log.h @@ -28,6 +28,7 @@ class Log string delimiter; string *objectName; pthread_mutex_t *writeMutex; + int delayVal; protected: string* getObjectName() const; void setObjectName(string* objectName); @@ -43,6 +44,9 @@ public: void setLineNumber(int lineNumber); void setLogParams(int lineNumber, LOG_COLOR color, string delimiter); static pthread_mutex_t * getMutex(); + void delay(int optional=-1); + int getDelay() const; + void setDelay(int delay); }; #endif /* LOG_H_ */ diff --git a/NATRouter.cpp b/NATRouter.cpp index a069661..2eb5927 100644 --- a/NATRouter.cpp +++ b/NATRouter.cpp @@ -86,8 +86,7 @@ void NATRouter::onRecv() { // TUTAJ ew. implementacja obslugi pakietu adresowanego do routera } - //usleep(500000); - sleep(1); + this->delay(); } else { @@ -96,7 +95,7 @@ void NATRouter::onRecv() this->print("onRecv() sleeping..."); #endif } - sleep(1); + this->delay(); } } @@ -148,7 +147,7 @@ void NATRouter::sNAT(Packet *packet) { //TESTOWO this->print("NAT table full, waiting 1s"); - sleep(1); + this->delay(1); } // wstawiam do tablicy NAT info ze port zarezerowany na odbior @@ -164,7 +163,7 @@ void NATRouter::sNAT(Packet *packet) packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT this->print(ss.str()); - sleep(1); + this->delay(); this->send(*packet, true); // wysylam dalej natItem = NULL; } @@ -188,7 +187,7 @@ void NATRouter::dNAT(Packet* packet) natItem->increaseTimeout(5); // podbijam o kolejne 5 sekund skoro transmisja trwa this->print(ss.str()); - sleep(1); + this->delay(); this->send(*packet, true); // wysylam dalej natItem = NULL; diff --git a/Node.cpp b/Node.cpp index 4bb6e05..3508ac6 100644 --- a/Node.cpp +++ b/Node.cpp @@ -94,6 +94,7 @@ Node::Node() this->setGatewayIp("0.0.0.0"); this->setObjectName(&this->hostname); + this->setDelay(1); } Node::~Node()