poprawki + tryb debugowania (bez ncurses)
This commit is contained in:
4
Log.cpp
4
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)
|
||||
|
||||
12
Main.cpp
12
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");
|
||||
|
||||
|
||||
2
Makefile
2
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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
19
Node.cpp
19
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ enum THREAD_TYPE
|
||||
{
|
||||
NODE_RECV,
|
||||
SIM_TIMER,
|
||||
SIM_RESIZE
|
||||
SIM_RESIZE,
|
||||
};
|
||||
|
||||
struct threadParams
|
||||
|
||||
Reference in New Issue
Block a user