Ejemplo n.º 1
0
Bottle NameServer::ncmdList(int argc, char *argv[]) {
    Bottle response;

    ConstString prefix = "";

    if (argc==1) {
        prefix = STR(argv[0]);
    }

    response.addString("ports");
    for (PLATFORM_MAP(ConstString,NameRecord)::iterator it = nameMap.begin(); it!=nameMap.end(); it++) {
        NameRecord& rec = PLATFORM_MAP_ITERATOR_SECOND(it);
        ConstString iname = rec.getAddress().getRegName();
        if (iname.find(prefix)==0) {
            if (iname==prefix || iname[prefix.length()]=='/' ||
                prefix[prefix.length()-1]=='/') {
                if (rec.getAddress().isValid()) {
                    response.addList() = botify(rec.getAddress());
                }
            }
        }
    }

    return response;
}
Ejemplo n.º 2
0
static void generateTypeMap(RosType& t, ConstString& txt) {
    txt = "";
    generateTypeMap1(t,txt);
    if (txt.length()>0) {
        txt = txt.substr(1,txt.length());
    }
    if (!t.reply) return;
    txt += " ---";
    generateTypeMap1(*(t.reply),txt);    
}
Ejemplo n.º 3
0
static void appendResourceType(ConstString& path,
                               const ConstString& resourceType) {
    if (resourceType=="") return;
    ConstString slash = NetworkBase::getDirectorySeparator();
    if (path.length()>0) {
        if (path[path.length()-1] != slash[0]) {
            path += NetworkBase::getDirectorySeparator();
        }
    }
    path += resourceType;
}
Ejemplo n.º 4
0
 ConstString context2path(Property& config, const ConstString& context ) {
     ConstString cap =
         config.check("capability_directory",Value("app")).asString();
     ConstString path = getPath(root,cap,context,"");
     if (path.length()>1) {
         if (path[path.length()-1]=='/') {
             path = path.substr(0,path.length()-1);
         }
     }
     return path;
 }
Ejemplo n.º 5
0
bool AbstractCarrier::sendSenderSpecifier(ConnectionState& proto)
{
    NetInt32 numberSrc;
    Bytes number((char*)&numberSrc, sizeof(NetInt32));
    const ConstString senderName = proto.getSenderSpecifier();
    //const ConstString& senderName = getRoute().getFromName();
    NetType::netInt((int)senderName.length()+1, number);
    OutputStream& os = proto.os();
    os.write(number);
    Bytes b((char*)senderName.c_str(), senderName.length()+1);
    os.write(b);
    os.flush();
    return os.isOk();
}
Ejemplo n.º 6
0
void Class::SetName(ConstString name,bool badd2map)
{
	if(name.length())
	{
		mClassName = AddPooledString(name.c_str());

		if( badd2map )
		{
			ClassMapType::iterator it = mClassMap.find(mClassName);
			if(it != mClassMap.end())
			{
				if(it->second != this)
				{
					Class *previous = it->second;

					orkprintf("ERROR: Duplicate class name %s! previous class %p\n", mClassName.c_str(), previous);
					OrkAssert(false);
				}
			}
			else
			{
				mClassMap.AddSorted(mClassName, this);
			}
		}
	}
}
Ejemplo n.º 7
0
    virtual void report(const SearchReport& report, const char *context) {
        ConstString ctx = context;
        ConstString key = report.key.c_str();
        ConstString prefix = "";

        prefix = ctx;
        prefix += ".";

        key = prefix + key;
        if (key.substr(0,1)==".") {
            key = key.substr(1,key.length());
        }

        if (!present.check(key.c_str())) {
            present.put(key.c_str(),"present");
            order.addString(key.c_str());
        }

        if (report.isFound) {
            actual.put(key.c_str(),report.value);
            return;
        }

        if (report.isComment==true) {
            comment.put(key.c_str(),report.value);
            return;
        }

        if (report.isDefault==true) {
            fallback.put(key.c_str(),report.value);
            return;
        }
    }
Ejemplo n.º 8
0
void FallbackNameClient::run() {
    NameConfig nc;
    Contact call = FallbackNameServer::getAddress();
    DgramTwoWayStream send;
    send.join(call, true);
    listen.join(call, false);
    if (!listen.isOk()) {
        YARP_ERROR(Logger::get(), ConstString("Multicast not available"));
        return;
    }
    ConstString msg = ConstString("NAME_SERVER query ") + nc.getNamespace();
    send.beginPacket();
    send.writeLine(msg.c_str(), (int)msg.length());
    send.flush();
    send.endPacket();
    for (int i=0; i<5; i++) {
        listen.beginPacket();
        ConstString txt = listen.readLine();
        listen.endPacket();
        if (closed) return;
        YARP_DEBUG(Logger::get(), ConstString("Fallback name client got ") + txt);
        if (txt.find("registration ")==0) {
            address = NameClient::extractAddress(txt);
            YARP_INFO(Logger::get(), ConstString("Received address ") +
                      address.toURI());
            return;
        }
    }
}
Ejemplo n.º 9
0
bool BufferedConnectionWriter::applyConvertTextMode() {
    if (convertTextModePending) {
        convertTextModePending = false;

        Bottle b;
        StringOutputStream sos;
        for (size_t i=0; i<lst_used; i++) {
            yarp::os::ManagedBytes& m = *(lst[i]);
            sos.write(m.usedBytes());
        }
        const String& str = sos.str();
        b.fromBinary(str.c_str(),str.length());
        ConstString replacement = b.toString() + "\n";
        for (size_t i=0; i<lst.size(); i++) {
            delete lst[i];
        }
        lst_used = 0;
        target = &lst;
        lst.clear();
        stopPool();
        Bytes data((char*)replacement.c_str(),replacement.length());
        appendBlockCopy(data);
    }
    return true;
}
Ejemplo n.º 10
0
    void checkFormat() {
        report(0,"check matrix format conforms to network standard...");

        Matrix m;
        unsigned int rr = 10;
        unsigned int cc = 5;
        makeTestMatrix(m,rr,cc);

        BufferedConnectionWriter writer;
        m.write(writer);
        ConstString s = writer.toString();
        Bottle bot;
        bot.fromBinary(s.c_str(),(int)s.length());
        checkEqual(bot.get(0).asInt(),rr,"row count matches");
        checkEqual(bot.get(1).asInt(),cc,"column count matches");
        Bottle *lst = bot.get(2).asList();
        checkTrue(lst!=NULL,"have data");
        if (!lst) return;
        checkEqual(lst->size(),(int)(rr*cc),"data length matches");
        if (lst->size()!=(int)(rr*cc)) return;
        bool ok = true;
        for (int i=0; i<(int)(rr*cc); i++) {
            double v = lst->get(i).asDouble();
            if (fabs(v-i)>0.01) {
                ok = false;
                checkEqualish(v,i,"cell matches");
                break;
            }
        }
        checkTrue(ok,"data matches");
    }
Ejemplo n.º 11
0
static bool matchCarrier(const Bytes *header, Bottle& code) {
    int at = 0;
    bool success = true;
    bool done = false;
    for (int i=0; i<code.size() && !done; i++) {
        Value& v = code.get(i);
        if (v.isString()) {
            ConstString str = v.asString();
            for (int j=0; j<(int)str.length(); j++) {
                if ((int)header->length()<=at) {
                    success = false;
                    done = true;
                    break;
                }
                if (str[j] != header->get()[at]) {
                    success = false;
                    done = true;
                    break;
                }
                at++;
            }
        } else {
            at++;
        }
    }
    return success;
}
Ejemplo n.º 12
0
ConstString Protocol::getSenderSpecifier() {
    Route r = getRoute();
    // We pull the sender name from the route.
    ConstString from = r.getFromName();
    // But we need to add any qualifiers looking in the carrier
    // name.  Ideally, we wouldn't need to bundle that in with
    // the sender name, but we do it for now in the name of
    // backwards compatibility.
    ConstString carrier = r.getCarrierName();
    size_t start = carrier.find("+");
    if (start!=String::npos) {
        from += " (";
        for (size_t i=start+1; i<(size_t)carrier.length(); i++) {
            char ch = carrier[i];
            if (ch=='+') {
                from += ") (";
            } else if (ch=='.') {
                from += " ";
            } else {
                from += ch;
            }
        }
        from += ")";
    }
    return from;
}
Ejemplo n.º 13
0
NetInt32 Vocab::encode(const ConstString& str) {
    char a = '\0';
    char b = '\0';
    char c = '\0';
    char d = '\0';
    if (str.length()>=1) {
        a = str[0];
        if (str.length()>=2) {
            b = str[1];
            if (str.length()>=3) {
                c = str[2];
                if (str.length()>=4) {
                    d = str[3];
                }
            }
        }
    }
    return VOCAB(a,b,c,d);
}
Ejemplo n.º 14
0
 void testNulls() {
     report(0,"testing null insertion");
     ConstString s;
     s += 'h';
     s += '\0';
     s += 'd';
     checkEqual((int)s.length(),3,"length with internal null");
     checkEqual(s[1],'\0',"null is there");
     checkEqual(s[2],'d',"after null");
 }
Ejemplo n.º 15
0
ConstString RosNameSpace::toRosName(const ConstString& name) {
    if (name.find(':')==ConstString::npos) return name;
    ConstString result;
    for (size_t i=0; i<name.length(); i++) {
        if (name[i]!=':') {
            result += name[i];
        } else {
            result += "__";
        }
    }
    return result;
}
Ejemplo n.º 16
0
bool yarp::os::impl::HttpCarrier::checkHeader(const Bytes& header, const char *prefix) {
    if (header.length()==8) {
        ConstString target = prefix;
        for (unsigned int i=0; i<target.length(); i++) {
            if (!(target[i]==header.get()[i])) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Ejemplo n.º 17
0
bool NameServiceOnTriples::cmdList(NameTripleState& act) {
    if (!act.bottleMode) {
        act.reply.addString("old");
    } else {
        act.reply.addString("ports");
    }
    lock();
    Triple t;
    t.setNameValue("port","*");
    ConstString prefix = "";
    if (act.cmd.size()>1) {
        prefix = act.cmd.get(1).asString();
    }
    list<Triple> lst = act.mem.query(t, YARP_NULLPTR);
    act.nestedMode = true;
    for (list<Triple>::iterator it=lst.begin(); it!=lst.end(); it++) {
        if (prefix=="") {
            act.cmd.clear();
            act.cmd.addString("query");
            act.cmd.addString(it->value.c_str());
            act.mem.reset();
            cmdQuery(act,true);
        } else {
            ConstString iname = it->value.c_str();
            if (iname.find(prefix)==0) {
                if (iname==prefix || iname[prefix.length()]=='/' ||
                    prefix[prefix.length()-1]=='/') {
                    act.cmd.clear();
                    act.cmd.addString("query");
                    act.cmd.addString(iname);
                    act.mem.reset();
                    cmdQuery(act,true);
                }
            }
        }
    }
    unlock();
    return true;
}
Ejemplo n.º 18
0
 void checkStandard() {
     PortablePair<Bottle,Bottle> pp;
     pp.head.fromString("1 2 3");
     pp.body.fromString("yes no");
     BufferedConnectionWriter writer;
     pp.write(writer);
     ConstString s = writer.toString();
     Bottle bot;
     bot.fromBinary(s.c_str(),s.length());
     checkEqual(bot.size(),2,"it is a pair");  
     checkEqual(bot.get(0).asList()->size(),3,"head len is right");  
     checkEqual(bot.get(1).asList()->size(),2,"body len is right");  
 }
Ejemplo n.º 19
0
bool MjpegCarrier::sendHeader(ConnectionState& proto) {
    Name n(proto.getRoute().getCarrierName() + "://test");
    ConstString pathValue = n.getCarrierModifier("path");
    ConstString target = "GET /?action=stream\n\n";
    if (pathValue!="") {
        target = "GET /";
        target += pathValue;
    }
    target += " HTTP/1.1\n\n";
    Bytes b((char*)target.c_str(),target.length());
    proto.os().write(b);
    return true;
}
Ejemplo n.º 20
0
bool MpiComm::connect(ConstString port) {

    char* port_name = new char[port.length()+1];
    memcpy(port_name, port.c_str(), port.length());
    port_name[port.length()] = '\0';

    #ifdef MPI_DEBUG
    printf("[MpiComm @ %s] Waiting for accept\n", name.c_str());
    #endif

    MPI_Comm intercomm;
    MPI_Comm_set_errhandler(comm, MPI_ERRORS_RETURN);
    int err = MPI_Comm_connect( port_name, MPI_INFO_NULL, 0, comm,  &intercomm );
    MPI_Comm_set_errhandler(comm, MPI_ERRORS_ARE_FATAL);

    if (err != MPI_SUCCESS ) {
        yError("MpiCarrier: Couldn't create connection\n");
        return false;
    }


    #ifdef MPI_DEBUG
    printf("[MpiComm @ %s] Connection established\n", name.c_str());
    #endif

    bool high = true;
    MPI_Intercomm_merge(intercomm, high, &comm);
    MPI_Comm_disconnect(&intercomm);

    #ifdef MPI_DEBUG
    printf("[MpiComm @ %s] Comms merged \n", name.c_str());
    #endif

    delete[] port_name;

    return true;
}
Ejemplo n.º 21
0
/**
* Quick implementation, space for improvement.
*/
ConstString Matrix::toString(int precision, int width, const char* endRowStr) const
{
    ConstString ret;
    char tmp[350];
    int c, r;
    if(width>0) // if width is specified use a space as separator
    {
        for(r=0;r<nrows;r++)
        {        
            for(c=0;c<ncols;c++)
            {
                sprintf(tmp, "% *.*lf ", width, precision, (*this)[r][c]);
                ret+=tmp;
            }
            ret = ret.substr(0,ret.length()-1);     // remove the last character (space)
            if(r<nrows-1)                          // if it is not the last row
                ret+= endRowStr;
        }
    }
    else    // if width is not specified use tab as separator
    {
        for(r=0;r<nrows;r++)
        {
            for(c=0;c<ncols;c++)
            {
                sprintf(tmp, "% .*lf\t", precision, (*this)[r][c]);
                ret+=tmp;
            }
            ret = ret.substr(0,ret.length()-1);     // remove the last character (tab)
            if(r<nrows-1)                          // if it is not the last row
                ret+= endRowStr;
        }
    }

    return ret;
}
Ejemplo n.º 22
0
 void testStandard() {
     report(0,"checking standard compliance of description...");
     ImageOf<PixelRgb> img;
     img.resize(8,4);
     img.zero();
     BufferedConnectionWriter writer;
     img.write(writer);
     ConstString s = writer.toString();
     Bottle bot;
     bot.fromBinary(s.c_str(),s.length());
     checkEqual(bot.size(),4,"plausible bottle out");
     checkEqual(bot.get(0).toString().c_str(),"mat","good tag");
     YARP_DEBUG(Logger::get(),"an example image:");
     YARP_DEBUG(Logger::get(),bot.toString().c_str());
 }
Ejemplo n.º 23
0
ConstString UnitTest::humanize(const ConstString& txt) {
    ConstString result("");
    for (unsigned int i=0; i<txt.length(); i++) {
        char ch = txt[i];
        if (ch == '\n') {
            result += "\\n";
        } else if (ch == '\r') {
            result += "\\r";
        } else if (ch == '\0') {
            result += "\\0";
        } else {
            result += ch;
        }
    }
    return result;
}
Ejemplo n.º 24
0
bool XmlRpcCarrier::sendHeader(ConnectionState& proto) {
    shouldInterpretRosMessages(proto);
    ConstString target = "POST /RPC2";
    Name n(proto.getRoute().getCarrierName() + "://test");
    ConstString pathValue = n.getCarrierModifier("path");
    if (pathValue!="") {
        target = "POST /";
        target += pathValue;
        // on the wider web, we should provide real host names
        host = NetworkBase::queryName(proto.getRoute().getToName());
    }
    target += " HTTP/1.1\n";
    http = target;
    Bytes b((char*)target.c_str(),target.length());
    proto.os().write(b);
    return true;
}
Ejemplo n.º 25
0
bool yarp::os::impl::HttpCarrier::sendHeader(ConnectionState& proto) {
    ConstString target = "GET / HTTP/1.0\r\n";
    ConstString path = proto.getRoute().getToName();
    if (path.size()>=2) {
        target = "GET " + path + " HTTP/1.0\r\n";
    }
    Contact host = proto.getRoute().getToContact();
    if (host.getHost()!="") {
        target += "Host: ";
        target += host.getHost();
        target += "\r\n";
    }
    target += "\r\n";
    Bytes b((char*)target.c_str(),target.length());
    proto.os().write(b);
    return true;

}
Ejemplo n.º 26
0
bool HumanCarrier::sendHeader(ConnectionState& proto) {
    // Send the "magic number" for this carrier
    ManagedBytes header(8);
    getHeader(header.bytes());
    proto.os().write(header.bytes());
    if (!proto.os().isOk()) return false;

    // Now we can do whatever we want, as long as somehow
    // we also send the name of the originating port

    // let's just send the port name in plain text terminated with a
    // carriage-return / line-feed
    ConstString from = proto.getRoute().getFromName();
    Bytes b2((char*)from.c_str(),from.length());
    proto.os().write(b2);
    proto.os().write('\r');
    proto.os().write('\n');
    proto.os().flush();
    return proto.os().isOk();
}
Ejemplo n.º 27
0
ConstString RosNameSpace::fromRosName(const ConstString& name) {
    if (name.find("__")==ConstString::npos) return name;
    // length is at least 2
    ConstString result;
    int ct = 0;
    for (size_t i=0; i<name.length(); i++) {
        if (name[i]!='_') {
            if (ct) result += '_';
            result += name[i];
            ct = 0;
        } else {
            ct++;
            if (ct==2) {
                result += ':';
                ct = 0;
            }
        }
    }
    if (ct) result += '_';
    return result;
}
Ejemplo n.º 28
0
/**
* Quick implementation, space for improvement.
*/
ConstString Vector::toString(int precision, int width) const
{
    ConstString ret = "";
    size_t c;
    char tmp[350];
    if(width<0){
        for(c=0;c<length();c++){
            sprintf(tmp, "% .*lf\t", precision, (*this)[c]);
            ret+=tmp;
        }
    }else{
        for(c=0;c<length();c++){
            sprintf(tmp, "% *.*lf ", width, precision, (*this)[c]);
            ret+=tmp;
        }
    }

    if(length()>=1)
        return ret.substr(0, ret.length()-1);
    return ret;
}
Ejemplo n.º 29
0
    bool readDir(const ConstString& dirname, ACE_DIR *&dir, String& result) {
        bool ok = true;

        struct YARP_DIRENT **namelist;
        YARP_closedir(dir);
        dir = NULL;
        int n = YARP_scandir(dirname.c_str(),&namelist,NULL,YARP_alphasort);
        if (n<0) {
            return false;
        }
        for (int i=0; i<n; i++) {
            ConstString name = namelist[i]->d_name;
            free(namelist[i]);
            int len = (int)name.length();
            if (len<4) continue;
            if (name.substr(len-4)!=".ini") continue;
            ConstString fname = ConstString(dirname) + "/" + name;
            ok = ok && readFile(fname,result,false);
            result += "\n[]\n";  // reset any nested sections
        }
        free(namelist);

        /*
        struct YARP_DIRENT *ent = YARP_readdir(dir);
        while (ent) {
            ConstString name = ent->d_name;
            ent = ACE_OS::readdir(dir);
            int len = (int)name.length();
            if (len<4) continue;
            if (name.substr(len-4)!=".ini") continue;
            ConstString fname = dirname + "/" + name;
            ok = ok && readFile(fname,result,false);
            result += "\n[]\n";  // reset any nested sections
        }
        YARP_closedir(dir);
        dir = NULL;
        */
        return ok;
    }
Ejemplo n.º 30
0
bool WireWriter::writeTag(const char *tag, int split, int len) {
    if (!split) {
        return writeString(tag);
    }
    ConstString bit = "";
    char ch = 'x';
    while (ch!='\0') {
        ch = *tag;
        tag++;
        if (ch=='\0'||ch=='_') {
            if (bit.length()<=4) {
                writer.appendInt(BOTTLE_TAG_VOCAB);
                writer.appendInt(Vocab::encode(bit));
            } else {
                writeString(bit.c_str());
            }
            bit = "";
        } else {
            bit += ch;
        }
    }
    return true;
}