Browse Source

poprawki + tryb debugowania (bez ncurses)

master
Piotr Dergun 7 years ago
parent
commit
dde98be41a
8 changed files with 65 additions and 34 deletions
  1. +4
    -0
      Log.cpp
  2. +2
    -10
      Main.cpp
  3. +1
    -1
      Makefile
  4. +12
    -3
      NATRouter.cpp
  5. +12
    -7
      Node.cpp
  6. +31
    -12
      Simulation.cpp
  7. +1
    -1
      Simulation.h
  8. +2
    -0
      common.h

+ 4
- 0
Log.cpp View File

@ -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)

+ 2
- 10
Main.cpp View File

@ -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");

+ 1
- 1
Makefile View File

@ -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

+ 12
- 3
NATRouter.cpp View File

@ -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;
}

+ 12
- 7
Node.cpp View File

@ -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);
}
}

+ 31
- 12
Simulation.cpp View File

@ -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);
}

+ 1
- 1
Simulation.h View File

@ -15,7 +15,7 @@ enum THREAD_TYPE
{
NODE_RECV,
SIM_TIMER,
SIM_RESIZE
SIM_RESIZE,
};
struct threadParams

+ 2
- 0
common.h View File

@ -23,4 +23,6 @@
using namespace std;
//#define DEBUG 1
#endif /* COMMON_H_ */

Loading…
Cancel
Save