Node - wysylanie/odbieranie, konstruktory
This commit is contained in:
8
Main.cpp
8
Main.cpp
@@ -11,15 +11,19 @@
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
cout << "Obiekt PC1" << endl;
|
cout << "Obiekt PC1" << endl;
|
||||||
Node pc1;
|
Node pc1("PC1", "10.0.0.2", "255.0.0.0");
|
||||||
|
|
||||||
cout << "Obiekt PC2" << endl;
|
cout << "Obiekt PC2" << endl;
|
||||||
Node pc2;
|
Node pc2("PC2", "10.0.0.3", "255.0.0.0");
|
||||||
|
|
||||||
cout << "Lacze PC1 z PC2" << endl;
|
cout << "Lacze PC1 z PC2" << endl;
|
||||||
cout << pc1.connectNode(&pc2) << endl;
|
cout << pc1.connectNode(&pc2) << endl;
|
||||||
|
|
||||||
cout << "Lacze PC1 z PC2 (ponownie)" << endl;
|
cout << "Lacze PC1 z PC2 (ponownie)" << endl;
|
||||||
cout << pc1.connectNode(&pc2) << endl;
|
cout << pc1.connectNode(&pc2) << endl;
|
||||||
|
|
||||||
|
cout << "Tworze pakiet i adresuje go do PC2" << endl;
|
||||||
|
Packet p;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
102
Node.cpp
102
Node.cpp
@@ -7,23 +7,77 @@
|
|||||||
|
|
||||||
#include "Node.h"
|
#include "Node.h"
|
||||||
|
|
||||||
|
Node * Node::findConnection(string ip)
|
||||||
|
{
|
||||||
|
if (ip == "0.0.0.0")
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
vector<Node*>::iterator it = this->connectedNodes.begin();
|
||||||
|
for (; it != this->connectedNodes.end(); ++it)
|
||||||
|
{
|
||||||
|
if ((*it)->getIp() == ip)
|
||||||
|
return *it;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* funkcja pobiera i usuwa pakiet z bufora "karty sieciowej" */
|
||||||
|
Packet Node::recv()
|
||||||
|
{
|
||||||
|
Packet packet;
|
||||||
|
if (!this->rcvBuffer.empty())
|
||||||
|
{
|
||||||
|
packet = this->rcvBuffer.front();
|
||||||
|
this->rcvBuffer.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
|
||||||
Node::Node()
|
Node::Node()
|
||||||
{
|
{
|
||||||
// TODO Auto-generated constructor stub
|
this->setHostname("");
|
||||||
|
this->setIp("0.0.0.0");
|
||||||
|
this->setMask("0.0.0.0");
|
||||||
|
this->setGatewayIp("0.0.0.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
Node::~Node()
|
Node::~Node()
|
||||||
{
|
{
|
||||||
// TODO Auto-generated destructor stub
|
this->connectedNodes.clear();
|
||||||
|
while(!this->rcvBuffer.empty())
|
||||||
|
this->rcvBuffer.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
Node::Node(string hostname) : Node()
|
||||||
|
{
|
||||||
|
this->setHostname(hostname);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node::Node(string hostname, string ip, string mask) : Node(hostname)
|
||||||
|
{
|
||||||
|
this->setIp(ip);
|
||||||
|
this->setMask(mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node::Node(string hostname, string ip, string mask, string gatewayIp) : Node(hostname, ip, mask)
|
||||||
|
{
|
||||||
|
this->setGatewayIp(gatewayIp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* funkcja symuluje typową sytuację: mamy kartę sieciową, a funkcja connectNode jest podłączeniem wtyczki
|
* funkcja symuluje typową sytuację: mamy kartę sieciową, a funkcja connectNode jest podłączeniem wtyczki
|
||||||
* z jednej strony
|
* z jednej strony
|
||||||
|
* @param firstConnected - oznacza, czy pierwsza strona jest podlaczona (jezeli nie to sprawdzamy
|
||||||
|
* czy polaczenie juz gdzies zostalo przypadkowo nawiazane, nawiazujemy polaczenie, a nastepnie
|
||||||
|
* przekazujemy sterowanie drugiemu wezlowi zeby zrobil to samo)
|
||||||
*/
|
*/
|
||||||
bool Node::connectNode(Node *node, bool firstConnected)
|
bool Node::connectNode(Node *node, bool firstConnected)
|
||||||
{
|
{
|
||||||
|
if (node->getIp() == "0.0.0.0") // jezeli wezel nie ma skonfigurowanej sieci, nie mozna go przylaczyc
|
||||||
|
return false;
|
||||||
|
|
||||||
if (!firstConnected)
|
if (!firstConnected)
|
||||||
{
|
{
|
||||||
vector<Node*>::iterator it = this->connectedNodes.begin();
|
vector<Node*>::iterator it = this->connectedNodes.begin();
|
||||||
@@ -41,43 +95,73 @@ bool Node::connectNode(Node *node, bool firstConnected)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::send(Packet packet)
|
/*
|
||||||
|
* funkcja wysyla, czyli przekazuje kopie pakietu kolejnemu wezlowi - docelowemu lub bramie domyslnej
|
||||||
|
* o ile jest skonfigurowana i istnieje do niej sciezka
|
||||||
|
* TODO zaimplementowac porownywanie z maska - jezeli dstIp z maska nie gra, wysylac na ruter
|
||||||
|
*/
|
||||||
|
bool Node::send(Packet packet)
|
||||||
{
|
{
|
||||||
|
Node *node;
|
||||||
|
node = this->findConnection(packet.getDstIp()); // znajdz bezposrednia trase
|
||||||
|
if (!node)
|
||||||
|
node = this->findConnection(this->getGatewayIp());
|
||||||
|
|
||||||
|
if (!node)
|
||||||
|
return false; //nie ma zadnej trasy do wezla
|
||||||
|
|
||||||
|
node->putPacket(packet);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Packet Node::recv()
|
/*
|
||||||
|
* funkcja jest modelem karty sieciowej - odbiera dane z sieci i umieszcza je
|
||||||
|
* w swoim buforze (czy tam systemie)
|
||||||
|
*/
|
||||||
|
void Node::putPacket(Packet packet)
|
||||||
{
|
{
|
||||||
|
this->rcvBuffer.push(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::setHostname(string hostname)
|
void Node::setHostname(string hostname)
|
||||||
{
|
{
|
||||||
|
this->hostname = hostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::setIp(string ip)
|
void Node::setIp(string ip)
|
||||||
{
|
{
|
||||||
|
this->netConf.ip = ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::setMask(string mask)
|
void Node::setMask(string mask)
|
||||||
{
|
{
|
||||||
|
this->netConf.mask = mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Node::setGatewayIp(string gatewayIp)
|
||||||
|
{
|
||||||
|
this->netConf.gatewayIp = gatewayIp;
|
||||||
}
|
}
|
||||||
|
|
||||||
string Node::getHostname()
|
string Node::getHostname()
|
||||||
{
|
{
|
||||||
|
return this->hostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
string Node::getIp()
|
string Node::getIp()
|
||||||
{
|
{
|
||||||
|
return this->netConf.ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
string Node::getMask()
|
string Node::getMask()
|
||||||
{
|
{
|
||||||
|
return this->netConf.mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
string Node::getGatewayIp()
|
||||||
|
{
|
||||||
|
return this->netConf.gatewayIp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::onRecv()
|
void Node::onRecv()
|
||||||
|
|||||||
15
Node.h
15
Node.h
@@ -14,31 +14,40 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
string ip;
|
string ip;
|
||||||
string mask;
|
string mask;
|
||||||
|
string gatewayIp;
|
||||||
} NetConf;
|
} NetConf;
|
||||||
|
|
||||||
class Node
|
class Node
|
||||||
{
|
{
|
||||||
queue<Packet>rcvPackets; // kolejka z pakietami do przetworzenia
|
protected:
|
||||||
|
queue<Packet>rcvBuffer; // kolejka z pakietami do przetworzenia
|
||||||
vector<Node*> connectedNodes; // referencje do węzłów, z którymi jest podłączony
|
vector<Node*> connectedNodes; // referencje do węzłów, z którymi jest podłączony
|
||||||
NetConf netConf; // konfiguracja sieciowa
|
NetConf netConf; // konfiguracja sieciowa
|
||||||
string hostname; // nazwa węzła
|
string hostname; // nazwa węzła
|
||||||
|
Node * findConnection(string ip);
|
||||||
|
Packet recv();
|
||||||
public:
|
public:
|
||||||
Node();
|
Node();
|
||||||
|
Node(string hostname);
|
||||||
|
Node(string hostname, string ip, string mask);
|
||||||
|
Node(string hostname, string ip, string mask, string gatewayIp);
|
||||||
virtual ~Node();
|
virtual ~Node();
|
||||||
bool connectNode(Node *node, bool firstConnected = false);
|
bool connectNode(Node *node, bool firstConnected = false);
|
||||||
|
|
||||||
void send(Packet packet);
|
bool send(Packet packet);
|
||||||
Packet recv();
|
void putPacket(Packet packet);
|
||||||
|
|
||||||
// settery
|
// settery
|
||||||
void setHostname(string hostname);
|
void setHostname(string hostname);
|
||||||
void setIp(string ip);
|
void setIp(string ip);
|
||||||
void setMask(string mask);
|
void setMask(string mask);
|
||||||
|
void setGatewayIp(string gatewayIp);
|
||||||
|
|
||||||
// gettery
|
// gettery
|
||||||
string getHostname();
|
string getHostname();
|
||||||
string getIp();
|
string getIp();
|
||||||
string getMask();
|
string getMask();
|
||||||
|
string getGatewayIp();
|
||||||
|
|
||||||
virtual void onRecv(); // wirtualna metoda na odbiór i dalszą akcję
|
virtual void onRecv(); // wirtualna metoda na odbiór i dalszą akcję
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user