From 07b06c3670885248f986a8514e7c364ea5afcae9 Mon Sep 17 00:00:00 2001 From: PioDer Date: Thu, 19 Jan 2017 14:08:42 +0100 Subject: [PATCH] nat overflow working 100% --- Log.cpp | 41 ++++++++++++++++++++++++++++++++++++++--- Log.h | 9 +++++++-- Main.cpp | 4 ++-- Simulation.cpp | 21 +++++++++++++-------- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/Log.cpp b/Log.cpp index b1d1436..cc9b02c 100644 --- a/Log.cpp +++ b/Log.cpp @@ -13,7 +13,8 @@ Log::Log() this->delimiter = ""; this->lineNumber = 0; this->objectName = NULL; - + this->currentLine = 0; + this->firstLine = 0; this->writeMutex = Log::getMutex(); this->setDelay(1); } @@ -120,13 +121,44 @@ void Log::printProgressBar(int lineNumber, int offset, string msg, cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl; #endif } +/* + * metoda wypisuje log w wielu liniach od firstLineNumber do rows-2 + */ +void Log::printLine(string msg, bool force) +{ + int cols, rows; + getmaxyx(stdscr, rows, cols); + + if (this->currentLine > rows-3) + this->currentLine = this->firstLine; + + this->print(msg, force, this->currentLine); + + pthread_mutex_lock(this->writeMutex); + move(this->currentLine+1, 0); + clrtoeol(); + move(0,0); + pthread_mutex_unlock(this->writeMutex); + ++this->currentLine; +} + +int Log::getFirstLine() const +{ + return firstLine; +} + +void Log::setFirstLine(int firstLine) +{ + this->firstLine = firstLine; + this->currentLine = firstLine; +} void Log::setObjectName(string* objectName) { this->objectName = objectName; } -void Log::print(string msg, bool force) +void Log::print(string msg, bool force, int customLine) { #ifndef DEBUG if (this->getObjectName() == NULL) @@ -136,7 +168,10 @@ void Log::print(string msg, bool force) return; pthread_mutex_lock(this->writeMutex); - move(this->getLineNumber(), 0); + if (customLine > -1) + move(customLine, 0); + else + move(this->getLineNumber(), 0); clrtoeol(); if(has_colors()) diff --git a/Log.h b/Log.h index 5b9f85a..fc0fc9c 100644 --- a/Log.h +++ b/Log.h @@ -29,10 +29,13 @@ class Log string *objectName; pthread_mutex_t *writeMutex; int delayVal; - + /* do obslugi wielolinijkowosci (na dole) */ + int firstLine; + int currentLine; public: Log(); - void print(string msg, bool force=false); + void print(string msg, bool force=false, int customLine = -1); + void printLine(string msg, bool force=false); void printProgressBar(int lineNumber, int offset, string msg, float percent); LOG_COLOR getColor() const; @@ -48,6 +51,8 @@ public: void setDelay(int delay); string* getObjectName() const; void setObjectName(string* objectName); + int getFirstLine() const; + void setFirstLine(int firstLine); }; #endif /* LOG_H_ */ diff --git a/Main.cpp b/Main.cpp index 4e85361..55363bf 100644 --- a/Main.cpp +++ b/Main.cpp @@ -40,9 +40,9 @@ int main(int argc, char *argv[]) usage(argv[0]); nodesCount = strtol(argv[2], &endptr, 10); - if (nodesCount < 1 || nodesCount > 255) + if (nodesCount < 1 || nodesCount > 253) { - cerr << "Invalid number of nodes format [1,255]" << endl; + cerr << "Invalid number of nodes format [1,253]" << endl; usage(argv[0]); } diff --git a/Simulation.cpp b/Simulation.cpp index e711c49..e286226 100644 --- a/Simulation.cpp +++ b/Simulation.cpp @@ -251,8 +251,9 @@ void Simulation::p2pSimulation() void Simulation::natOverflowSimulation(int nNodes) { - int i = 0;//, srcPort; + int i = 0, srcPort; stringstream ss; + string hostname; this->print("Creating server"); Node server("Server", "93.92.91.90", "255.255.255.255"); @@ -275,10 +276,8 @@ void Simulation::natOverflowSimulation(int nNodes) this->print("Creating and setting clients"); Node *client = new Node[nNodes]; - int *currentSrcPort = new int[nNodes]; for (i = 0; i < nNodes; ++i) { - currentSrcPort[i] = 1; client[i].setLogParams(2, CYAN, "\t"); ss.str(""); ss << "Client " << (i + 1); @@ -318,16 +317,22 @@ void Simulation::natOverflowSimulation(int nNodes) p.setDstPort(80); i=0; + Log clientLog; + clientLog.setLogParams(5, MAGENTA, "\t"); + clientLog.setFirstLine(6); + clientLog.setObjectName(&hostname); + while(true) { i = (nNodes>1) ? rand()%nNodes : 0; - if (currentSrcPort[i] == 65535) - currentSrcPort[i] = 1; + ss.str(""); - //p.setSrcPort(currentSrcPort[i]++); - p.setSrcPort(0); - client[i].send(p); + //p.setSrcPort(0); + srcPort = client[i].send(p); + hostname = client[i].getHostname(); + ss << "Sent " << client[i].getIp() << ":" << srcPort << " to 93.92.91.90:80"; + clientLog.printLine(ss.str()); this->delay(150); //usleep(1); }