diff --git a/Main.cpp b/Main.cpp index f4fb644..0d8ffb5 100644 --- a/Main.cpp +++ b/Main.cpp @@ -11,15 +11,19 @@ int main(int argc, char *argv[]) { cout << "Obiekt PC1" << endl; - Node pc1; + Node pc1("PC1", "10.0.0.2", "255.0.0.0"); cout << "Obiekt PC2" << endl; - Node pc2; + Node pc2("PC2", "10.0.0.3", "255.0.0.0"); cout << "Lacze PC1 z PC2" << endl; cout << pc1.connectNode(&pc2) << endl; cout << "Lacze PC1 z PC2 (ponownie)" << endl; cout << pc1.connectNode(&pc2) << endl; + + cout << "Tworze pakiet i adresuje go do PC2" << endl; + Packet p; + return 0; } diff --git a/Node.cpp b/Node.cpp index 893d592..bb09d1f 100644 --- a/Node.cpp +++ b/Node.cpp @@ -7,27 +7,81 @@ #include "Node.h" -Node::Node() +Node * Node::findConnection(string ip) { - // TODO Auto-generated constructor stub + if (ip == "0.0.0.0") + return NULL; + + vector::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() +{ + this->setHostname(""); + this->setIp("0.0.0.0"); + this->setMask("0.0.0.0"); + this->setGatewayIp("0.0.0.0"); } 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 * 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) { + if (node->getIp() == "0.0.0.0") // jezeli wezel nie ma skonfigurowanej sieci, nie mozna go przylaczyc + return false; + if (!firstConnected) { vector::iterator it = this->connectedNodes.begin(); - for (; it != this->connectedNodes.end(); ++it) //sprawdzamy, czy połączenia już przypadkiem nie ma + for (; it != this->connectedNodes.end(); ++it) // sprawdzamy, czy połączenia już przypadkiem nie ma { if (*it == node) return false; @@ -41,43 +95,73 @@ bool Node::connectNode(Node *node, bool firstConnected) 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) { - + this->hostname = hostname; } void Node::setIp(string ip) { - + this->netConf.ip = ip; } void Node::setMask(string mask) { + this->netConf.mask = mask; +} +void Node::setGatewayIp(string gatewayIp) +{ + this->netConf.gatewayIp = gatewayIp; } string Node::getHostname() { - + return this->hostname; } string Node::getIp() { - + return this->netConf.ip; } string Node::getMask() { + return this->netConf.mask; +} +string Node::getGatewayIp() +{ + return this->netConf.gatewayIp; } void Node::onRecv() diff --git a/Node.h b/Node.h index 0d56618..d5dd04f 100644 --- a/Node.h +++ b/Node.h @@ -14,31 +14,40 @@ typedef struct { string ip; string mask; + string gatewayIp; } NetConf; class Node { - queuercvPackets; // kolejka z pakietami do przetworzenia +protected: + queuercvBuffer; // kolejka z pakietami do przetworzenia vector connectedNodes; // referencje do węzłów, z którymi jest podłączony NetConf netConf; // konfiguracja sieciowa string hostname; // nazwa węzła + Node * findConnection(string ip); + Packet recv(); public: Node(); + Node(string hostname); + Node(string hostname, string ip, string mask); + Node(string hostname, string ip, string mask, string gatewayIp); virtual ~Node(); bool connectNode(Node *node, bool firstConnected = false); - void send(Packet packet); - Packet recv(); + bool send(Packet packet); + void putPacket(Packet packet); // settery void setHostname(string hostname); void setIp(string ip); void setMask(string mask); + void setGatewayIp(string gatewayIp); // gettery string getHostname(); string getIp(); string getMask(); + string getGatewayIp(); virtual void onRecv(); // wirtualna metoda na odbiór i dalszą akcję };