From dde98be41a69b7b32101039996439e5719cfb558 Mon Sep 17 00:00:00 2001 From: PioDer Date: Tue, 17 Jan 2017 05:06:36 +0100 Subject: [PATCH] poprawki + tryb debugowania (bez ncurses) --- Log.cpp | 4 ++++ Main.cpp | 12 ++---------- Makefile | 2 +- NATRouter.cpp | 15 ++++++++++++--- Node.cpp | 19 ++++++++++++------- Simulation.cpp | 43 +++++++++++++++++++++++++++++++------------ Simulation.h | 2 +- common.h | 2 ++ 8 files changed, 65 insertions(+), 34 deletions(-) diff --git a/Log.cpp b/Log.cpp index ed241f9..af6e1eb 100644 --- a/Log.cpp +++ b/Log.cpp @@ -57,6 +57,7 @@ void Log::setObjectName(string* objectName) void Log::print(string msg) { +#ifndef DEBUG if (this->getObjectName() == NULL) return; @@ -72,6 +73,9 @@ void Log::print(string msg) attroff(COLOR_PAIR(this->getColor())); move(0, 0); +#else + cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl; +#endif } void Log::setLogParams(int lineNumber, LOG_COLOR color, string delimiter) diff --git a/Main.cpp b/Main.cpp index 55bb83a..52549cb 100644 --- a/Main.cpp +++ b/Main.cpp @@ -38,20 +38,12 @@ int main(int argc, char *argv[]) */ Simulation sim; - Node pc1("PC1", "10.0.0.2", "255.0.0.0", "10.0.0.1"); - pc1.setDelimiter("\t"); - pc1.setColor(CYAN); - sim.print("initalizing thread"); - //sim.createThread("timer", &Simulation::timerHelper, &sim); - //sim.createThread("nowa", &Simulation::threadWrapper, &sim.timer); - //sim.createThread("test", dupa); sim.createThread("timer", SIM_TIMER, &sim); - //sleep(5); sim.p2pSimulation(); - while(1); + while(1); exit(0); +/* - /* cout << "Obiekt PC1" << endl; Node pc1("PC1", "10.0.0.2", "255.0.0.0", "10.0.0.1"); diff --git a/Makefile b/Makefile index d2f9c08..c2798ce 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CXX = g++ CXXFLAGS = -Wall -std=c++11 LDFLAGS = -pthread -lncurses -OBJ = Log.o Packet.o Node.o NATItem.o NATRouter.o Simulation.o +OBJ = Log.o Packet.o Node.o NATItem.o NATRouter.o Peer.o Simulation.o all: +@make simulation diff --git a/NATRouter.cpp b/NATRouter.cpp index 661636a..47a008d 100644 --- a/NATRouter.cpp +++ b/NATRouter.cpp @@ -60,6 +60,7 @@ void NATRouter::onRecv() while (true) { Packet p = this->recv(); + stringstream s; if (p.getSrcPort() != 0) { @@ -75,17 +76,21 @@ void NATRouter::onRecv() else if (p.getDstIp() == this->getWanIp() && !this->natTable[p.getDstPort()].isFree()) { this->dNAT(&p); + } else { // TUTAJ ew. implementacja obslugi pakietu adresowanego do routera } + //usleep(500000); + sleep(1); } else { this->print("onRecv() sleeping..."); - sleep(1); } + sleep(1); + } } @@ -135,7 +140,7 @@ void NATRouter::sNAT(Packet *packet) while ((port = this->getFreePort()) == -1) { //TESTOWO - //cout << "Brak wolnych portow, czekam 1s" << endl; + this->print("NAT table full, waiting 1s"); sleep(1); } @@ -145,11 +150,15 @@ void NATRouter::sNAT(Packet *packet) natItem->setPort(packet->getSrcPort()); natItem->setTimeout(5); + stringstream ss; + ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port; + packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT - this->send(*packet, true); // wysylam dalej + this->print(ss.str()); + this->send(*packet, true); // wysylam dalej natItem = NULL; } diff --git a/Node.cpp b/Node.cpp index e0a3f8a..7443b59 100644 --- a/Node.cpp +++ b/Node.cpp @@ -156,6 +156,7 @@ bool Node::send(Packet packet, bool isRouter) { Node *node; node = this->findConnection(packet.getDstIp()); // znajdz bezposrednia trase + if (!node) node = this->findConnection(this->getGatewayIp()); @@ -164,7 +165,6 @@ bool Node::send(Packet packet, bool isRouter) if (packet.getDstPort() == 0) return false; // wypada zdefiniowac nadawce oraz docelowy port... - // jezeli wezel nie jest routerem to ma ustawic pakietowi swoj srcIp oraz losowy port if (!isRouter) { @@ -240,13 +240,18 @@ string Node::calculateNetwork(string ip, string mask) void Node::onRecv() { - // TESTOWO - Packet p = this->recv(); - if (p.getSrcPort() != 0) + while(true) { - stringstream s; - s << "onRecv() received \"" << p.getMsg() << "\" from " << p.getSrcIp() << ":" << p.getSrcPort(); - this->print(s.str()); + // TESTOWO + Packet p = this->recv(); + if (p.getSrcPort() != 0) + { + stringstream s; + s << "onRecv() received \"" << p.getMsg() << "\" from " << p.getSrcIp() << ":" << p.getSrcPort(); + + this->print(s.str()); + } + sleep(1); } } diff --git a/Simulation.cpp b/Simulation.cpp index a72ba62..8794f32 100644 --- a/Simulation.cpp +++ b/Simulation.cpp @@ -8,9 +8,13 @@ #include "Simulation.h" #include "Node.h" #include "NATRouter.h" +#include "Peer.h" Simulation::Simulation() { + this->rows = 0; + this->cols = 0; +#ifndef DEBUG // inicjalizacja ncurses initscr(); getmaxyx(stdscr, this->rows, this->cols); @@ -27,7 +31,7 @@ Simulation::Simulation() init_pair(6, COLOR_CYAN, COLOR_BLACK); init_pair(7, COLOR_WHITE, COLOR_BLACK); } - +#endif this->name = "Simulation"; this->setLineNumber(rows -1); this->setDelimiter("\t"); @@ -35,8 +39,10 @@ Simulation::Simulation() this->setColor(WHITE); this->startTime = time(NULL); - //this->createThread("resizeWnd", &Simulation::resizeWndHelper, this); +#ifndef DEBUG this->createThread("resizeWnd", SIM_RESIZE, this); +#endif + } Simulation::~Simulation() @@ -62,7 +68,7 @@ void Simulation::createThread(string name, THREAD_TYPE type, void* context) } this->threads.insert(pair(name, params)); - usleep(2000); + usleep(3000); // 3ms } /* @@ -113,6 +119,9 @@ void * Simulation::threadWrapper(void * context) { case NODE_RECV: ((Node *)params->context)->onRecv(); + // Node* c; + // c = static_cast(params->context); + // c->onRecv(); break; case SIM_TIMER: ((Simulation *)params->context)->timer(); @@ -128,8 +137,12 @@ void Simulation::resizeWnd() { while(true) { +#ifndef DEBUG getmaxyx(stdscr, this->rows, this->cols); + if (this->getLineNumber() != this->rows-1) + this->setLineNumber(rows -1); //refresh(); +#endif } } @@ -157,10 +170,10 @@ void Simulation::timer() void Simulation::p2pSimulation() { this->print("Creating P2P peers"); - Node peer1("Peer 1", "192.168.1.2", "255.255.255.0", "192.168.1.1"); + Peer peer1("Peer 1", "192.168.1.2", "255.255.255.0", "192.168.1.1"); peer1.setLogParams(0, GREEN, "\t\t"); - Node peer2("Peer 1", "10.0.0.2", "255.255.255.0", "10.0.0.1"); - peer2.setLogParams(1, CYAN, "\t\t"); + Peer peer2("Peer 2", "10.0.0.2", "255.255.255.0", "10.0.0.1"); + peer2.setLogParams(4, CYAN, "\t\t"); peer1.print("IP 192.168.1.2/24, Gateway: 192.168.1.1"); peer2.print("IP 10.0.0.2/24, Gateway: 10.0.0.1"); sleep(1); @@ -168,8 +181,8 @@ void Simulation::p2pSimulation() this->print("Creating NAT devices"); NATRouter r1("Router 1", "192.168.1.1", "255.255.255.0"); NATRouter r2("Router 2", "10.0.0.1", "255.255.255.0"); - r1.setLogParams(2, RED, "\t"); - r2.setLogParams(4, MAGENTA, "\t"); + r1.setLogParams(1, RED, "\t"); + r2.setLogParams(3, MAGENTA, "\t"); r1.print("IP 192.168.1.1/24"); r2.print("IP 10.0.0.1/24"); sleep(1); @@ -185,15 +198,15 @@ void Simulation::p2pSimulation() this->print("Creating P2P server"); Node server("Server", "80.80.90.91", "255.255.255.255"); - server.setLogParams(3, YELLOW, "\t\t"); + server.setLogParams(2, YELLOW, "\t\t"); server.print("IP 80.80.90.91/32"); sleep(1); this->print("Plugging Peer1 <---> Router 1"); - peer1.connectNode(&r1); + r1.connectNode(&peer1); sleep(1); this->print("Plugging Peer2 <---> Router 2"); - peer2.connectNode(&r2); + r2.connectNode(&peer2); sleep(1); this->print("Plugging Router 1 <---> Router 2"); r1.connectNode(&r2, true); @@ -205,8 +218,14 @@ void Simulation::p2pSimulation() server.connectNode(&r2); sleep(1); - sleep(3); + sleep(1); + this->print("Starting simulation..."); this->createThread("r1", NODE_RECV, &r1); this->createThread("r2", NODE_RECV, &r2); + this->createThread("s1", NODE_RECV, &server); + + peer1.connectToServer("80.80.90.91", 6565); + + while(1); } diff --git a/Simulation.h b/Simulation.h index 50dffc0..4add2c1 100644 --- a/Simulation.h +++ b/Simulation.h @@ -15,7 +15,7 @@ enum THREAD_TYPE { NODE_RECV, SIM_TIMER, - SIM_RESIZE + SIM_RESIZE, }; struct threadParams diff --git a/common.h b/common.h index f1f3335..5e1bb78 100644 --- a/common.h +++ b/common.h @@ -23,4 +23,6 @@ using namespace std; +//#define DEBUG 1 + #endif /* COMMON_H_ */