/* --------------------------------------------------------------------------- */ void Router::connectTo(Host &h) { if ((this->routeMap).count(h.getNumber()) > 0) { if (h.getConnection() != NULL) { cout << "Sorry, but you must disconnect from your current router and " << "reconnect." << endl; return; } else { cout << "Sorry, but you are currently using a name already in the network," << " this must be reassigned." << endl; return; } } // Update neighborHosts table. (this->neighborHosts).insert(pair<int, Host*>(h.getNumber(), &h)); // Add entry to the routeMap. vector<int> route (1); *(route.begin()) = h.getNumber(); (this->routeMap).insert(pair<int, vector<int> >(h.getNumber(), route)); // Have the Host update its connection. h.connectTo(this); // Update the Router's neighbors of the new connection. route.insert(route.begin(), this->getNumber()); for (multimap<int, Router *>::iterator ri = (this->neighborRouters).begin(); ri != (this->neighborRouters).end(); ri++) { this->updateNeighbor(ri->second, route); } }
/* --------------------------------------------------------------------------- */ void Router::updateNeighborDisconnectHost(Router *r, Host &h) { // If the Host does not appear in its routeMap, end function call. if ((r->routeMap).count(h.getNumber()) == 0) { return; } (r->routeMap).erase(h.getNumber()); for (multimap<int, Router *>::iterator ri = (r->neighborRouters).begin(); ri != (r->neighborRouters).end(); ri++) { r->updateNeighborDisconnectHost(ri->second, h); } }
/* --------------------------------------------------------------------------- */ void Router::disconnect(Host &h) { // Check if the Host is actually connected to the Router, if not throw error. if ((this->neighborHosts).count(h.getNumber()) == 0) { cout << "Sorry, but Host " << h.getNumber() << " is not connected to Router " << this->getNumber() << ", so cannot disconnect from it." << endl; return; } // Remove the connection from neighborHosts, routeMap, and the Host. (this->neighborHosts).erase(h.getNumber()); (this->routeMap).erase(h.getNumber()); h.disconnectFrom(this); // Update the Router's neighbors of the deletion of the connection. for (multimap<int, Router *>::iterator ri = (this->neighborRouters).begin(); ri != (this->neighborRouters).end(); ri++) { this->updateNeighborDisconnectHost(ri->second, h); } }