|
|
@ -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<string, struct threadParams>(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<Node*>(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); |
|
|
|
} |