#include #include #include "MKNode.h" #include "MKCommModel.h" #include "MKLib.h" using namespace std; MKNode::MKNeighbor::MKNeighbor(const MKNode *nb) : node(nb) { } MKNode::MKNeighbor::~MKNeighbor() { } MKNode::MKRoute::MKRoute(const MKNode *target) : node(target), latency(0) { } MKNode::MKRoute::~MKRoute() { hops.clear(); } void MKNode::MKRoute::clearHops() { hops.clear(); } void MKNode::MKRoute::addNextHop(MKNode *hop) { hops.push_back(hop); } throughput_t MKNode::MKRoute::throughput() { MKNode *current, *last = NULL; MKNeighbor *hopInfo = NULL; throughput_t min = INT_MAX; vector::const_iterator hItr = hops.begin(); while(hItr != hops.end()) { current = *hItr; if(last != NULL) { hopInfo = last->nbr(current->mac); if(hopInfo == NULL) { // neighbor info not available min = 0; } else { min = MIN(min, hopInfo->throughput); } } last = current; hItr++; } return min; } /* std::vector &MKNode::MKRoute::getHops() { return hops; // copy construct that shit //return vector(hops); } */ MKNode::MKNode(mac_t macAddr, ip4_t ipAddr) : mac(macAddr), ip4(ipAddr) { } MKNode::~MKNode() { clearNeighbors(); clearRoutes(); } MKNode::operator string() const { string str("[MKNode "); str.append(MKLib::mac2string(mac)).append(" @ "); str.append(MKLib::ip2string(ip4)).append("]"); return str; } node *MKNode::asCommNode() const { node *n = new node; n->ip_addr = ntohl(ip4); //n->ip_addr = ntohl(MKLib::parseIP4Addr("10.0.0.0")) | (mac.addr[4] << 8) | mac.addr[5]; n->mac_addr = mac; return n; } void MKNode::addNeighbor(const MKNode *node, NeighborInfo *pkt) { MKNeighbor *nb = new MKNeighbor(node); // set neighbor properties nb->inactive = ntohl(pkt->inactive); // nb->weight = pkt->metric; weight is deprecated nb->signal = static_cast(ntohs(pkt->signal)); //nb->throughput = (throughput_t) 1024 * 1024 * (float) pkt->throughput; uint32_t tInt = ntohl(pkt->throughput); float *tFloat = (float *) (&tInt); //make it an int bps nb->throughput = (*tFloat) * 1024 * 1024; /* //uint8_t no need to switch to network byte order nb->modRate = pkt->tx_rate; */ neighbors.insert(pair(node->mac, nb)); } MKNode::MKNeighbor *MKNode::nbr(mac_t target) { map::const_iterator itr = neighbors.find(target); MKNeighbor *nb = NULL; if(itr != neighbors.end()) { nb = itr->second; } return nb; } MKNode::MKRoute *MKNode::route(mac_t target) { map::const_iterator itr = routes.find(target); MKRoute *rt = NULL; if(itr != routes.end()) { rt = itr->second; } return rt; } MKNode::MKRoute *MKNode::addRoute(MKNode *node) { MKRoute *rt = new MKRoute(node); routes.insert(pair(node->mac, rt)); return rt; } void MKNode::clearRoutes() { map::iterator itr; for(itr = routes.begin(); itr != routes.end(); itr++) { delete (*itr).second; } routes.clear(); } void MKNode::clearNeighbors() { map::iterator itr; for(itr = neighbors.begin(); itr != neighbors.end(); itr++) { delete (*itr).second; } neighbors.clear(); } vector MKNode::getNeighbors() const { vector nbs; MKNeighbor* nbr; map::const_iterator itr; /* TODO wilson: Set inactivity time here */ for(itr = neighbors.begin(); itr != neighbors.end(); itr++) { nbr = (*itr).second; nbs.push_back(nbr->node); } return nbs; }