コード例 #1
0
ファイル: NetworkProfiler.cpp プロジェクト: jgvictores/yarp
bool NetworkProfiler::creatNetworkGraph(ports_detail_set details, yarp::profiler::graph::Graph& graph) {

    // adding the ports and processor nodes
    if(NetworkProfiler::progCallback)
        NetworkProfiler::progCallback->onProgress(0);

    ports_detail_iterator itr;
    unsigned int itr_count = 0;
    for(itr = details.begin(); itr!=details.end(); itr++) {
        PortDetails info = (*itr);

        // port node
        PortVertex* port = new PortVertex(info.name);
        if(!info.inputs.size() && !info.outputs.size())
            port->property.put("orphan", true);
        graph.insert(*port);

        //process node (owner)
        ProcessVertex* process = new ProcessVertex(info.owner.pid, info.owner.hostname);
        //prop.clear();
        process->property.put("name", info.owner.name);
        process->property.put("arguments", info.owner.arguments);
        process->property.put("hostname", info.owner.hostname);
        process->property.put("priority", info.owner.priority);
        process->property.put("policy", info.owner.policy);
        process->property.put("os", info.owner.os);
        process->property.put("hidden", false);
        pvertex_iterator itrVert=graph.insert(*process);
        // create connection between ports and its process
        if(dynamic_cast<ProcessVertex*> (*itrVert))
            port->setOwner((ProcessVertex*)(*itrVert));



        //machine node (owner of the process)
        MachineVertex* machine = new MachineVertex(info.owner.os, info.owner.hostname);
        graph.insert(*machine);
        //todo do the same done for the process.
        process->setOwner(machine);

        if(!info.inputs.size() && !info.outputs.size())
            graph.insertEdge(*process, *port, Property("(type ownership) (dir unknown)"));

        // calculate progress
        if(NetworkProfiler::progCallback) {
            NetworkProfiler::progCallback->onProgress((unsigned int) (++itr_count/((float)(details.size()*2)) * 100.0) );
        }
    }


    // create connection between ports
    for(itr = details.begin(); itr!=details.end(); itr++) {
        PortDetails info = (*itr);
        // find the current port vertex in the graph
        pvertex_iterator vi1 = graph.find(PortVertex(info.name));
        yAssert(vi1 != graph.vertices().end());
        for(size_t i=0; i<info.outputs.size(); i++) {
            ConnectionInfo cnn = info.outputs[i];
            pvertex_iterator vi2 = graph.find(PortVertex(cnn.name));
            if(vi2 != graph.vertices().end()) {
                //yInfo()<<"connecting "<<(*vi1)->property.find("name").asString()<<"->"<<(*vi2)->property.find("name").asString();
                Property edge_prop("(type connection)");
                edge_prop.put("carrier", cnn.carrier);
                graph.insertEdge(vi1, vi2, edge_prop);
            }
            else
                yWarning()<<"Found a nonexistent port ("<<cnn.name<<")"<<"in the output list of"<<(*vi1)->property.find("name").asString();
        }
        // calculate progress
        if(NetworkProfiler::progCallback) {
            NetworkProfiler::progCallback->onProgress((unsigned int) (++itr_count/((float)(details.size()*2)) * 100.0) );
        }
    }
    if(NetworkProfiler::progCallback)
        NetworkProfiler::progCallback->onProgress(100); // is it really needed? :p
    return true;
}