bool NodeStore::update(NodeRef &node, NodeRefList *nl) { Mutex::AutoLocker l(mutex); bool found = false; if (!node) return false; // There may be undefined nodes in the node store that should // be removed/merged with a 'defined' node that we create from a // node description. We loop through all nodes in the store and // compare their interface lists with the one in the 'defined' node. // If any interfaces match, we remove the matching nodes in the store // and eventually replace them with the new one. for (NodeStore::iterator it = begin(); it != end();) { NodeRecord *nr = *it; bool found_now = false; nr->node.lock(); const InterfaceRefList *ifaces = nr->node->getInterfaces(); for (InterfaceRefList::const_iterator it2 = ifaces->begin(); it2 != ifaces->end(); it2++) { InterfaceRef iface = *it2; if (node->hasInterface(iface)) { // Transfer all the "up" interface states to the updated node if (iface->isUp()) node->setInterfaceUp(iface); found_now = true; } } nr->node.unlock(); if (found_now) { if (nl) nl->push_back(nr->node); nr->node->setStored(false); node->setExchangedNodeDescription(nr->node->hasExchangedNodeDescription()); it = erase(it); delete nr; found = true; } else { it++; } } if (found) { node->setStored(true); push_back(new NodeRecord(node)); } return found; }
bool NodeStore::add(NodeRef &node) { Mutex::AutoLocker l(mutex); if (!node) return false;; if (_stored(node)) { HAGGLE_DBG("Node %s is already in node store\n", node->getIdStr()); return false; } HAGGLE_DBG("Adding new node to node store %s\n", node->getIdStr()); node->setStored(); push_back(new NodeRecord(node)); return true; }
// Remove neighbor with a specified interface NodeRef NodeStore::remove(const InterfaceRef &iface) { Mutex::AutoLocker l(mutex); if (!iface) return NULL; for (NodeStore::iterator it = begin(); it != end(); it++) { NodeRecord *nr = *it; if (nr->node->hasInterface(iface)) { NodeRef node = nr->node; erase(it); node->setStored(false); delete nr; return node; } } return NULL; }