Menu

#8 Return name list of registered ports

eurobot
closed
None
5
2017-11-02
2012-02-06
CarlosF
No

Hi all,

First of all, i'm fairly new to YARP so my doubt might be really dumb, the fact is i couldn't figure t out it, which is the reason behind this message. It's very simple actually, i want to access the list of all registered ports. I've looked throughout the Network class in os namespace, but i couldn't find any method to solve my problem. I can access this list by command line interface but, i want to use it internally in my program. So i checked your code, and the supposed variable that holds this record (i guess) has private access (that would be nameMap in yarp/os/impl/NameServer.h).
Thanks in advance for any feedback.

Best regards,
Carlos Faria

Discussion

  • Paul Fitzpatrick

    • assigned_to: nobody --> eshuy
     
  • Paul Fitzpatrick

    Here's some sample code that should do it. In summary, you send the name server the command "bot list" and it sends you back a list of port information:

    #include <stdio.h>
    #include <yarp/os/all.h>
    using namespace yarp::os;
    int main() {
    Network yarp;
    BufferedPort<Bottle> server;
    server.open("/info");
    Bottle query, response;
    query.addString("bot");
    query.addString("list");
    yarp.write(yarp.getNameServerName().c_str(),query,response);
    if (!(response.get(0).asString()=="ports")) {
    printf("Unknown response from name server\n");
    }
    for (int i=1; i<response.size(); i++) {
    Bottle *port = response.get(i).asList();
    if (port!=NULL) {
    if (port->check("name")) {
    ConstString portName = port->find("name").asString();
    printf("Port: %s\n", portName.c_str());
    }
    }
    }
    return 0;
    }

     
  • CarlosF

    CarlosF - 2012-02-07

    Thank you, that solved the problem.

     
  • CarlosF

    CarlosF - 2012-02-07

    Thank you, that solved the problem.

     
  • CarlosF

    CarlosF - 2012-02-07

    Well, soon another problem arised.

    I wanted to implement this functionality in MatLab, so i am using the Calling yarp from Matlab approach (http://eris.liralab.it/wiki/Calling_yarp_from_Matlab). The installation went smoothly so did the code tests i made so far, but when i tried to translate this sample of code you provided to MatLab language, the method write from Network somehow didn't work with the standard parameters.

    From C++

    Network yarp;
    BufferedPort<Bottle> server;
    server.open("/info");
    Bottle query, response;
    query.addString("bot");
    query.addString("list");

    yarp.write(std::string("/root").c_str(),query,response);

    Into MatLab

    Net = yarp.Network();
    Port = yarp.Port();
    Port.open('/info');
    Query = yarp.Bottle();
    Response = yarp.Bottle();

    Query.addString('bot');
    Query.addString('list');

    % works as intended until this point
    Net.write(Net.getNameServerName(),Query,Response);

    In this last line, i get the
    "??? No method 'write' with matching signature
    found for class 'yarp.Network'."
    error, as it says it didn't find any suitable method for this parameters.

    Network. class from java extends NetworkBase (where i found this)

    public static boolean write(String port_name, PortWriter cmd, PortReader reply) {
    return yarpJNI.NetworkBase_write__SWIG_5(port_name, PortWriter.getCPtr(cmd), cmd, PortReader.getCPtr(reply), reply);
    }

    which i think is suitable for the purpose.

    I don't know if you are familiar with this kind of problems, nor if i am submitting it to the correct place. But since you we're so helpful the first time, i thought maybe you knew something about it.
    Thanks again !

     
  • Daniele E. Domenichelli

    • status: open --> closed
    • Group: --> eurobot