Beispiel #1
0
bool Graph::printDOT(const std::string& fileName) {
	
    FILE *file = fopen(fileName.c_str(), "w");
    if (file == NULL) {
        return false;
    }
    fprintf(file, "digraph G {\n");

    NodeSet visited;
    std::queue<Node::Ptr> worklist;

    NodeIterator entryBegin, entryEnd;
    entryNodes(entryBegin, entryEnd);

     // Initialize visitor worklist
    for (NodeIterator iter = entryBegin; iter != entryEnd; ++iter) {
        worklist.push(*iter);
    }

    // Put the entry nodes on their own (minimum) rank
    fprintf(file, "  { rank = min;");
    for (NodeIterator iter = entryBegin; iter != entryEnd; ++iter) {
      fprintf(file, "\"%p\"; ", (*iter).get());
    }
    fprintf(file, "}\n");

    NodeIterator exitBegin, exitEnd;
    exitNodes(exitBegin, exitEnd);

    // Put the entry nodes on their own (minimum) rank
    fprintf(file, "  { rank = max;");
    for (NodeIterator iter = exitBegin; iter != exitEnd; ++iter) {
      fprintf(file, "\"%p\"; ", (*iter).get());
    }
    fprintf(file, "}\n");
    

    while (!worklist.empty()) {
        Node::Ptr source = worklist.front();

        worklist.pop();

         //fprintf(stderr, "Considering node %s\n", source->format().c_str());

         // We may have already treated this node...
        if (visited.find(source) != visited.end()) {
             //fprintf(stderr, "\t skipping previously visited node\n");
            continue;
        }
         //fprintf(stderr, "\t inserting %s into visited set, %d elements pre-insert\n", source->format().c_str(), visited.size());
        visited.insert(source);

        fprintf(file, "\t%s\n", source->DOTshape().c_str());
        fprintf(file, "\t%s\n", source->DOTrank().c_str());
	fprintf(file, "\t\"%p\" [label=\"%s\"];\n", 
		source.get(), source->DOTname().c_str());

        NodeIterator outBegin, outEnd;
        source->outs(outBegin, outEnd);

        for (; outBegin != outEnd; ++outBegin) {
            Node::Ptr target = *outBegin;
            if (!target->DOTinclude()) continue;
            //fprintf(file, "\t %s -> %s;\n", source->DOTname().c_str(), target->DOTname().c_str());
	    fprintf(file, "\t \"%p\" -> \"%p\";\n", source.get(), target.get());
            if (visited.find(target) == visited.end()) {
                 //fprintf(stderr, "\t\t adding child %s\n", target->format().c_str());
                worklist.push(target);
            }
            else {
                 //fprintf(stderr, "\t\t skipping previously visited child %s\n", 
                 //target->format().c_str());
            }
        }
    }
    fprintf(file, "}\n\n\n");
    fclose(file);

    return true;
}
Beispiel #2
0
	/*
	 * Creates new (or update existing) node which is NOT added to our routing table
	 */
	Node::Ptr KBucket::createNode(const UserPtr& u, const string& ip, uint16_t port, bool update, bool isUdpKeyValid)
	{
		if(u->isSet(User::DHT)) // is this user already known in DHT?
		{
			Node::Ptr node = NULL;

			// no online node found, try get from routing table
			for(NodeList::iterator it = nodes.begin(); it != nodes.end(); ++it)
			{
				if(u->getCID() == (*it)->getUser()->getCID())
				{
					node = *it;

					// put node at the end of the list
					nodes.erase(it);
					nodes.push_back(node);
					break;
				}
			}

			if(node == NULL && u->isOnline())
			{
				// try to get node from ClientManager (user can be online but not in our routing table)
				// this fixes the bug with DHT node online twice
				node = (Node*)ClientManager::getInstance()->findDHTNode(u->getCID());
				node = node.get();
			}

			if(node != NULL)
			{
				// fine, node found, update it and return it
				if(update)
				{
					string oldIp	= node->getIdentity().getIp();
					string oldPort	= node->getIdentity().getUdpPort();
					if(ip != oldIp || static_cast<uint16_t>(Util::toInt(oldPort)) != port)
					{
						node->setIpVerified(false);

						 // TODO: don't allow update when new IP already exists for different node

						// erase old IP and remember new one
						ipMap.erase(oldIp + ":" + oldPort);
						ipMap.insert(ip + ":" + Util::toString(port));
					}

					if(!node->isIpVerified())
						node->setIpVerified(isUdpKeyValid);

					node->setAlive();
					node->getIdentity().setIp(ip);
					node->getIdentity().setUdpPort(Util::toString(port));

					DHT::getInstance()->setDirty();
				}

				return node;
			}
		}

		u->setFlag(User::DHT);

		Node::Ptr node(new Node(u));
		node->getIdentity().setIp(ip);
		node->getIdentity().setUdpPort(Util::toString(port));
		node->setIpVerified(isUdpKeyValid);
		return node;
	}