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; }
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); }
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; }
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; }
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(); }
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); } } } }
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; } }
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; } } }
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; }
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"); }
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; }
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; }
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); }
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"); }
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; }
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; }
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; }
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"); }
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; }
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; }
/** * 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; }
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()); }
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; }
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; }
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; }
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(); }
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; }
/** * 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; }
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; }
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; }