Browse Source

praktycznie pelna implementacja Node + testy dzialania

master
Piotr Dergun 8 years ago
parent
commit
c7b131025a
5 changed files with 54 additions and 24 deletions
  1. +11
    -1
      Main.cpp
  2. +19
    -1
      Node.cpp
  3. +16
    -17
      Packet.cpp
  4. +6
    -5
      Packet.h
  5. +2
    -0
      common.h

+ 11
- 1
Main.cpp View File

@ -23,7 +23,17 @@ int main(int argc, char *argv[])
cout << pc1.connectNode(&pc2) << endl; cout << pc1.connectNode(&pc2) << endl;
cout << "Tworze pakiet i adresuje go do 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; return 0;
} }

+ 19
- 1
Node.cpp View File

@ -108,7 +108,15 @@ bool Node::send(Packet packet)
node = this->findConnection(this->getGatewayIp()); node = this->findConnection(this->getGatewayIp());
if (!node) 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); node->putPacket(packet);
@ -166,5 +174,15 @@ string Node::getGatewayIp()
void Node::onRecv() 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;
}
} }

+ 16
- 17
Packet.cpp View File

@ -7,10 +7,9 @@
#include "Packet.h" #include "Packet.h"
Packet::Packet()
Packet::Packet(string msg) : Packet()
{ {
// TODO Auto-generated constructor stub
this->setMsg(msg);
} }
Packet::~Packet() Packet::~Packet()
@ -19,54 +18,54 @@ Packet::~Packet()
} }
//settery //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) void Packet::setMsg(string msg)
{ {
this->msg = msg;
} }
string Packet::getSrcIp() string Packet::getSrcIp()
{ {
return this->srcIp;
} }
string Packet::getDstIp() string Packet::getDstIp()
{ {
return this->dstIp;
} }
int Packet::Packet::getSrcPort() int Packet::Packet::getSrcPort()
{ {
return this->srcPort;
} }
int Packet::getDstPort() int Packet::getDstPort()
{ {
return this->dstPort;
} }
string Packet::getMsg() string Packet::getMsg()
{ {
return this->msg;
} }

+ 6
- 5
Packet.h View File

@ -18,14 +18,15 @@ class Packet
int dstPort; int dstPort;
string msg; string msg;
public: public:
Packet();
Packet() : srcIp("0.0.0.0"), dstIp("0.0.0.0"), srcPort(0), dstPort(0), msg("") {}
Packet(string msg);
virtual ~Packet(); virtual ~Packet();
// settery // 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); void setMsg(string msg);
// gettery // gettery

+ 2
- 0
common.h View File

@ -12,6 +12,8 @@
#include <queue> #include <queue>
#include <map> #include <map>
#include <functional> #include <functional>
#include <cstdlib>
#include <ctime>
using namespace std; using namespace std;

Loading…
Cancel
Save