From c7b131025a03f41504914c9d418aaafd3f6591d0 Mon Sep 17 00:00:00 2001 From: PioDer Date: Wed, 11 Jan 2017 00:47:25 +0100 Subject: [PATCH] praktycznie pelna implementacja Node + testy dzialania --- Main.cpp | 12 +++++++++++- Node.cpp | 20 +++++++++++++++++++- Packet.cpp | 33 ++++++++++++++++----------------- Packet.h | 11 ++++++----- common.h | 2 ++ 5 files changed, 54 insertions(+), 24 deletions(-) diff --git a/Main.cpp b/Main.cpp index 0d8ffb5..30c9625 100644 --- a/Main.cpp +++ b/Main.cpp @@ -23,7 +23,17 @@ int main(int argc, char *argv[]) cout << pc1.connectNode(&pc2) << endl; cout << "Tworze pakiet i adresuje go do PC2" << endl; - Packet p; + Packet p("piekna wiadomosc"); + + p.setDstIp("10.0.0.3"); + p.setDstPort(80); + + cout << "Wysylam wiadomosc: "; + pc1.send(p); + cout << endl; + + cout << "Wywoluje onRecv() na PC2" << endl; + pc2.onRecv(); return 0; } diff --git a/Node.cpp b/Node.cpp index bb09d1f..152172e 100644 --- a/Node.cpp +++ b/Node.cpp @@ -108,7 +108,15 @@ bool Node::send(Packet packet) node = this->findConnection(this->getGatewayIp()); if (!node) - return false; //nie ma zadnej trasy do wezla + return false; // nie ma zadnej trasy do wezla + + if (packet.getDstPort() == 0) + return false; // wypada zdefiniowac nadawce oraz docelowy port... + + packet.setSrcIp(this->getIp()); + // ustaw port zrodlowy na port losowy z zakresu [32768,61000] - zob. empheral port (port emferyczny) + srand(time(NULL)); + packet.setSrcPort(rand() % 32768 + 28233); node->putPacket(packet); @@ -166,5 +174,15 @@ string Node::getGatewayIp() void Node::onRecv() { + // TESTOWO + Packet p = this->recv(); + if (p.getSrcPort() != 0) + { + cout << endl << "<<<<<< " << this->getHostname() << " >>>>>>" << endl; + cout << "Odebrano wiadomosc!" << endl; + cout << "Src: " << p.getSrcIp() << ":" << p.getSrcPort() << endl; + cout << "Dst: " << p.getDstIp() << ":" << p.getDstPort() << endl; + cout << "---" << endl << p.getMsg() << endl << endl; + } } diff --git a/Packet.cpp b/Packet.cpp index 2efb34a..5efe920 100644 --- a/Packet.cpp +++ b/Packet.cpp @@ -7,10 +7,9 @@ #include "Packet.h" -Packet::Packet() +Packet::Packet(string msg) : Packet() { - // TODO Auto-generated constructor stub - + this->setMsg(msg); } Packet::~Packet() @@ -19,54 +18,54 @@ Packet::~Packet() } //settery -void Packet::setSrcIp(string ip) +void Packet::setSrcIp(string srcIp) { - + this->srcIp = srcIp; } -void Packet::setDstIp(string ip) +void Packet::setDstIp(string dstIp) { - + this->dstIp = dstIp; } -void Packet::setSrcPort(int port) +void Packet::setSrcPort(int srcPort) { - + this->srcPort = srcPort; } -void Packet::setDstPort(int port) +void Packet::setDstPort(int dstPort) { - + this->dstPort = dstPort; } void Packet::setMsg(string msg) { - + this->msg = msg; } string Packet::getSrcIp() { - + return this->srcIp; } string Packet::getDstIp() { - + return this->dstIp; } int Packet::Packet::getSrcPort() { - + return this->srcPort; } int Packet::getDstPort() { - + return this->dstPort; } string Packet::getMsg() { - + return this->msg; } diff --git a/Packet.h b/Packet.h index bed6fa6..81ca839 100644 --- a/Packet.h +++ b/Packet.h @@ -18,14 +18,15 @@ class Packet int dstPort; string msg; public: - Packet(); + Packet() : srcIp("0.0.0.0"), dstIp("0.0.0.0"), srcPort(0), dstPort(0), msg("") {} + Packet(string msg); virtual ~Packet(); // settery - void setSrcIp(string ip); - void setDstIp(string ip); - void setSrcPort(int port); - void setDstPort(int port); + void setSrcIp(string srcIp); + void setDstIp(string dstIp); + void setSrcPort(int srcPort); + void setDstPort(int dstPort); void setMsg(string msg); // gettery diff --git a/common.h b/common.h index c5ccefc..2e0b0d9 100644 --- a/common.h +++ b/common.h @@ -12,6 +12,8 @@ #include #include #include +#include +#include using namespace std;