praktycznie pelna implementacja Node + testy dzialania
This commit is contained in:
12
Main.cpp
12
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;
|
||||
}
|
||||
|
||||
20
Node.cpp
20
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
33
Packet.cpp
33
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
11
Packet.h
11
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
|
||||
|
||||
Reference in New Issue
Block a user