Bottle NameServer::botify(const Contact& address) { Bottle result; if (address.isValid()) { Bottle bname; bname.addString("name"); bname.addString(address.getRegName().c_str()); Bottle bip; bip.addString("ip"); bip.addString(address.getHost().c_str()); Bottle bnum; bnum.addString("port_number"); bnum.addInt(address.getPort()); Bottle bcarrier; bcarrier.addString("carrier"); bcarrier.addString(address.getCarrier().c_str()); result.addString("port"); result.addList() = bname; result.addList() = bip; result.addList() = bnum; result.addList() = bcarrier; } else { Bottle bstate; bstate.addString("error"); bstate.addInt(-2); bstate.addString("port not known"); result.addString("port"); result.addList() = bstate; } return result; }
Contact YarpNameSpace::registerContact(const Contact& contact) { NameClient& nic = HELPER(this); Contact address = nic.registerName(contact.getName().c_str(), contact); if (address.isValid()) { NestedContact nc; nc.fromString(address.getRegName().c_str()); std::string cat = nc.getCategory(); if (nc.getNestedName()!="") { //bool service = (cat.find("1") != std::string::npos); bool publish = (cat.find('+') != std::string::npos); bool subscribe = (cat.find('-') != std::string::npos); ContactStyle style; Contact c1(nc.getFullName()); Contact c2(std::string("topic:/") + nc.getNestedName()); if (subscribe) { style.persistenceType = ContactStyle::END_WITH_TO_PORT; connectPortToTopic(c2, c1, style); } if (publish) { style.persistenceType = ContactStyle::END_WITH_FROM_PORT; connectPortToTopic(c1, c2, style); } } } return address; }
Contact NameServer::unregisterName(const ConstString& name) { Contact prev = queryName(name); if (prev.isValid()) { if (prev.getPort()!=-1) { NameRecord& rec = getNameRecord(prev.getRegName()); if (rec.isReusablePort()) { HostRecord& host = getHostRecord(prev.getHost()); host.release(prev.getPort()); } if (rec.isReusableIp()) { if (rec.getAddress().getCarrier()=="mcast") { mcastRecord.releaseAddress(rec.getAddress().getHost().c_str()); } } rec.clear(); tmpNames.release(name); Bottle event; event.addVocab(Vocab::encode("del")); event.addString(name.c_str()); onEvent(event); } } return queryName(name); }
ConstString NameServer::textify(const Contact& address) { ConstString result = ""; if (address.isValid()) { if (address.getPort()>=0) { result = "registration name "; result = result + address.getRegName() + " ip " + address.getHost() + " port " + NetType::toString(address.getPort()) + " type " + address.getCarrier() + "\n"; } else { result = "registration name "; result = result + address.getRegName() + " ip " + "none" + " port " + "none" + " type " + address.getCarrier() + "\n"; } } return result; }
bool yarp::os::impl::McastCarrier::sendHeader(ConnectionState& proto) { // need to do more than the default bool ok = defaultSendHeader(proto); if (!ok) return false; YARP_DEBUG(Logger::get(),"Adding extra mcast header"); Contact addr; Contact alt = proto.getStreams().getLocalAddress(); ConstString altKey = proto.getRoute().getFromName() + "/net=" + alt.getHost(); McastCarrier *elect = getCaster().getElect(altKey); if (elect!=YARP_NULLPTR) { YARP_DEBUG(Logger::get(),"picking up peer mcast name"); addr = elect->mcastAddress; mcastName = elect->mcastName; } else { // fetch an mcast address Contact target("...", "mcast", "...", 0); addr = NetworkBase::registerContact(target); mcastName = addr.getRegName(); if (addr.isValid()) { // mark owner of mcast address NetworkBase::setProperty(proto.getRoute().getFromName().c_str(), "owns", Value(mcastName.c_str())); } } int ip[] = { 224, 3, 1, 1 }; int port = 11000; if (addr.isValid()) { SplitString ss(addr.getHost().c_str(),'.'); if (ss.size()!=4) { addr = Contact(); } else { yAssert(ss.size()==4); for (int i=0; i<4; i++) { ip[i] = NetType::toInt(ss.get(i)); } port = addr.getPort(); } } if (!addr.isValid()) { YARP_ERROR(Logger::get(), "Name server not responding helpfully, setting mcast name arbitrarily."); YARP_ERROR(Logger::get(), "Only a single mcast address supported in this mode."); addr = Contact("/tmp/mcast", "mcast", "224.3.1.1", 11000); } ManagedBytes block(6); for (int i=0; i<4; i++) { ((unsigned char*)block.get())[i] = (unsigned char)ip[i]; } block.get()[5] = (char)(port%256); block.get()[4] = (char)(port/256); proto.os().write(block.bytes()); mcastAddress = addr; return true; }
bool BootstrapServer::configFileBootstrap(yarp::os::Contact& contact, bool configFileRequired, bool mayEditConfigFile) { Contact suggest = contact; // see what address is lying around Contact prev; NameConfig conf; if (conf.fromFile()) { prev = conf.getAddress(); } else if (configFileRequired) { fprintf(stderr,"Could not read configuration file %s\n", conf.getConfigFileName().c_str()); return false; } // merge if (prev.isValid()) { if (suggest.getHost() == "...") { suggest.setHost(prev.getHost()); } if (suggest.getCarrier() == "...") { suggest.setCarrier(prev.getCarrier()); } if (suggest.getPort() == 0) { suggest.setPort(prev.getPort()); } } if (suggest.getRegName() == "...") { suggest.setName(conf.getNamespace()); } // still something not set? if (suggest.getPort() == 0) { suggest.setPort(10000); } if (suggest.getHost() == "...") { // should get my IP suggest.setHost(conf.getHostName()); } if (!configFileRequired) { // finally, should make sure IP is local, and if not, correct it if (!conf.isLocalName(suggest.getHost())) { fprintf(stderr,"Overriding non-local address for name server\n"); suggest.setHost(conf.getHostName()); } else { // Let's just check we're not a loopback ConstString betterHost = conf.getHostName(false,suggest.getHost()); if (betterHost!=suggest.getHost()) { fprintf(stderr,"Overriding loopback address for name server\n"); suggest.setHost(betterHost); } } } else { if (!conf.isLocalName(conf.getHostName())) { fprintf(stderr,"The address written in config file doesn't belong any interface \n"); return false; } suggest.setHost(conf.getHostName()); } bool changed = false; if (prev.isValid()) { changed = (prev.getHost() != suggest.getHost()) || (prev.getPort() != suggest.getPort()) || (conf.getMode() != "yarp" && conf.getMode() != ""); } if (changed && !mayEditConfigFile) { fprintf(stderr,"PROBLEM: need to change settings in %s\n", conf.getConfigFileName().c_str()); fprintf(stderr," Current settings: host %s port %d family %s\n", prev.getHost().c_str(), prev.getPort(), (conf.getMode()=="")?"yarp":conf.getMode().c_str()); fprintf(stderr," Desired settings: host %s port %d family %s\n", suggest.getHost().c_str(), suggest.getPort(), "yarp"); fprintf(stderr,"Please specify '--write' if it is ok to overwrite current settings, or\n"); if(!configFileRequired) fprintf(stderr,"Please specify '--read' to use the current settings, or\n"); else fprintf(stderr,"Please set an existing address in config file, or\n"); fprintf(stderr,"delete %s\n", conf.getConfigFileName().c_str()); return false; } bool shouldSave = changed || !prev.isValid(); if (shouldSave) { // and save conf.setAddress(suggest); if (!conf.toFile()) { fprintf(stderr,"Could not save configuration file %s\n", conf.getConfigFileName().c_str()); } } contact = suggest; return true; }
bool Port::open(const Contact& contact, bool registerName, const char *fakeName) { Contact contact2 = contact; if (!NetworkBase::initialized()) { YARP_ERROR(Logger::get(), "YARP not initialized; create a yarp::os::Network object before using ports"); return false; } ConstString n = contact2.getName(); bool local = false; if (n == "" && contact2.getPort()<=0) { local = true; registerName = false; n = "..."; } NestedContact nc(n); if (nc.getNestedName()!="") { if (nc.getNodeName() == "") { Nodes& nodes = NameClient::getNameClient().getNodes(); nodes.requireActiveName(); ConstString node_name = nodes.getActiveName(); if (node_name!="") { n = n + node_name; } } } if (n!="" && n[0]!='/' && n[0]!='=' && n!="..." && n.substr(0,3)!="...") { if (fakeName==NULL) { Nodes& nodes = NameClient::getNameClient().getNodes(); ConstString node_name = nodes.getActiveName(); if (node_name!="") { // n = node_name + "=/" + n; n = "/" + n + "@" + node_name; } } } if (n!="" && n[0]!='/' && n[0]!='=' && n!="..." && n.substr(0,3)!="...") { if (fakeName==NULL) { YARP_SPRINTF1(Logger::get(),error, "Port name '%s' needs to start with a '/' character", n.c_str()); return false; } } if (n!="" && n!="..." && n[0]!='=' && n.substr(0,3)!="...") { if (fakeName==NULL) { ConstString prefix = NetworkBase::getEnvironment("YARP_PORT_PREFIX"); if (prefix!="") { n = prefix + n; contact2 = contact2.addName(n); } } } PortCoreAdapter *currentCore = &(HELPER(implementation)); if (currentCore!=NULL) { NestedContact nc; nc.fromString(n); if (nc.getNestedName()!="") { if (nc.getCategory()=="") { // we need to add in a category ConstString cat = ""; if (currentCore->commitToRead) { cat = "-"; } else if (currentCore->commitToWrite) { cat = "+"; } if (cat!="") { if (currentCore->commitToRpc) { cat += "1"; } contact2 = contact2.addName(nc.getNestedName() + cat + "@" + nc.getNodeName()); } else { YARP_SPRINTF1(Logger::get(),error, "Error: Port '%s' is not committed to being either an input or output port.", n.c_str()); YARP_SPRINTF0(Logger::get(),error, "YARP does not mind, but we are trying to register with a name server that does."); YARP_SPRINTF0(Logger::get(),error, "You can call Port::setWriteOnly() or Port::setReadOnly(), OR rename the port."); NestedContact nc2 = nc; nc2.setCategoryWrite(); YARP_SPRINTF1(Logger::get(),error, "For an output port, call it: %s (+ adds data)", nc2.toString().c_str()); nc2.setCategoryRead(); YARP_SPRINTF1(Logger::get(),error, "For an input port, call it: %s (- takes data)", nc2.toString().c_str()); return false; } } } } // Allow for open() to be called safely many times on the same Port if (currentCore->isOpened()) { PortCoreAdapter *newCore = new PortCoreAdapter(*this); YARP_ASSERT(newCore!=NULL); // copy state that should survive in a new open() if (currentCore->checkPortReader()!=NULL) { newCore->configReader(*(currentCore->checkPortReader())); } if (currentCore->checkReadCreator()!=NULL) { newCore->configReadCreator(*(currentCore->checkReadCreator())); } if (currentCore->checkWaitAfterSend()>=0) { newCore->configWaitAfterSend(currentCore->checkWaitAfterSend()); } close(); delete ((PortCoreAdapter*)implementation); implementation = newCore; } PortCoreAdapter& core = HELPER(implementation); core.openable(); if (NetworkBase::localNetworkAllocation()&&contact2.getPort()<=0) { YARP_DEBUG(Logger::get(),"local network allocation needed"); local = true; } bool success = true; Contact caddress = Contact::bySocket(contact2.getCarrier(), contact2.getHost(), contact2.getPort()) .addName(contact2.getName()); caddress.setNested(contact2.getNested()); Contact address = caddress; core.setReadHandler(core); if (contact2.getPort()>0 && contact2.getHost()!="") { registerName = false; } ConstString typ = getType().getName(); if (typ!="") { NestedContact nc; nc.fromString(contact2.getName()); nc.setTypeName(typ); contact2.setNested(nc); } if (registerName&&!local) { address = NetworkBase::registerContact(contact2); } core.setControlRegistration(registerName); success = (address.isValid()||local)&&(fakeName==NULL); if (success) { // create a node if needed Nodes& nodes = NameClient::getNameClient().getNodes(); nodes.prepare(address.getRegName().c_str()); } // If we are a service client, go ahead and connect if (success) { NestedContact nc; nc.fromString(address.getName()); if (nc.getNestedName()!="") { if (nc.getCategory() == "+1") { addOutput(nc.getNestedName()); } } } ConstString blame = "invalid address"; if (success) { success = core.listen(address,registerName); blame = "address conflict"; if (success) { success = core.start(); blame = "manager did not start"; } } if (success) { address = core.getAddress(); if (registerName&&local) { contact2 = contact2.addSocket(address.getCarrier(), address.getHost(), address.getPort()); contact2 = contact2.addName(address.getRegName().c_str()); Contact newName = NetworkBase::registerContact(contact2); core.resetPortName(newName.getName()); address = core.getAddress(); } else if (core.getAddress().getRegName()=="" && !registerName) { core.resetPortName(core.getAddress().addCarrier("").toURI()); core.setName(core.getAddress().getRegName()); } if (core.getVerbosity()>=1) { if (address.getRegName()=="") { YARP_INFO(Logger::get(), String("Anonymous port active at ") + address.toURI()); } else { YARP_INFO(Logger::get(), String("Port ") + address.getRegName() + " active at " + address.toURI()); } } } if (fakeName!=NULL) { success = core.manualStart(fakeName); blame = "unmanaged port failed to start"; } if (!success) { YARP_ERROR(Logger::get(), String("Port ") + (address.isValid()?(address.getRegName().c_str()):(contact2.getName().c_str())) + " failed to activate" + (address.isValid()?" at ":"") + (address.isValid()?address.toURI():String("")) + " (" + blame.c_str() + ")"); } if (success) { // create a node if needed Nodes& nodes = NameClient::getNameClient().getNodes(); nodes.add(*this); } return success; }
void yarp::os::impl::HttpTwoWayStream::apply(char ch) { if (ch=='\r') { return; } if (ch == '\n') { proc = ""; Contact addr = yarp::os::impl::NameClient::extractAddress(part); if (addr.isValid()) { if (addr.getCarrier()=="tcp"&& (addr.getRegName().find("/quit")==ConstString::npos)) { proc += "<a href=\"http://"; proc += addr.getHost(); proc += ":"; proc += NetType::toString(addr.getPort()); proc += "\">"; proc += addr.getRegName(); proc += "</A> "; size_t len = addr.getRegName().length(); size_t target = 30; if (len<target) { for (size_t i=0; i<target-len; i++) { proc += " "; } } proc += "("; proc += addr.toString().c_str(); proc += ")"; proc += "\n"; } else { // Don't show non tcp connections //proc += part; //proc += "\n"; } } else { if ((part[0]=='\"'&&part[1]=='[')||(part[0]=='+')) { // translate this to a form if (part[0]=='+') { part[0] = ' '; } ConstString org = part; part = "<p><form method=post action='/form'>"; size_t i=0; for (i=0; i<org.length(); i++) { if (org[i]=='"') { org[i] = ' '; } } part += "<input type=hidden name=data value=\""; part += org; part += "\">"; part += org; org += " "; bool arg = false; ConstString var = ""; for (i=0; i<org.length(); i++) { char ch = org[i]; if (arg) { if ((ch>='A'&&ch<='Z')|| (ch>='a'&&ch<='z')|| (ch>='0'&&ch<='9')|| (ch=='_')) { var += ch; } else { arg = false; part += "\n "; part += var; part += " "; part += "<input type=text name="; part += var; part += " size=5 value=\"\">"; var = ""; } } if (ch=='$') { arg = true; } } part += "<input type=submit value=\"go\">"; part += "</form></p>"; } proc += part; proc += "\n"; } if (data||!filterData) { Bytes tmp((char*)proc.c_str(),proc.length()); delegate->getOutputStream().write(tmp); delegate->getOutputStream().flush(); } data = false; if (proc[0] == 'd' || proc[0] == 'D') { data = true; } part = ""; } else { part += ch; } }
Contact NameClient::registerName(const String& name, const Contact& suggest) { String np = getNamePart(name); Bottle cmd; cmd.addString("register"); if (np!="") { cmd.addString(np.c_str()); } else { cmd.addString("..."); } ConstString prefix = NetworkBase::getEnvironment("YARP_IP"); NestedContact nc = suggest.getNested(); ConstString typ = nc.getTypeNameStar(); if (suggest.isValid()||prefix!=""||typ!="*") { if (suggest.getCarrier()!="") { cmd.addString(suggest.getCarrier().c_str()); } else { cmd.addString("..."); } if (suggest.getHost()!="") { cmd.addString(suggest.getHost().c_str()); } else { if (prefix!="") { Bottle ips = NameConfig::getIpsAsBottle(); for (int i=0; i<ips.size(); i++) { String ip = ips.get(i).asString().c_str(); if (ip.find(prefix)==0) { prefix = ip.c_str(); break; } } } cmd.addString((prefix!="")?prefix:"..."); } if (suggest.getPort()!=0) { cmd.addInt(suggest.getPort()); } else { cmd.addString("..."); } if (typ!="*") { cmd.addString(typ); } } Bottle reply; send(cmd,reply); Contact address = extractAddress(reply); if (address.isValid()) { String reg = address.getRegName(); /* // this never really got used cmd.fromString("set /port offers tcp text text_ack udp mcast shmem name_ser"); cmd.get(1) = Value(reg.c_str()); send(cmd,reply); // accept the same set of carriers cmd.get(2) = Value("accepts"); send(cmd,reply); */ cmd.clear(); cmd.addString("set"); cmd.addString(reg.c_str()); cmd.addString("ips"); cmd.append(NameConfig::getIpsAsBottle()); send(cmd,reply); cmd.clear(); cmd.addString("set"); cmd.addString(reg.c_str()); cmd.addString("process"); cmd.addInt(ACE_OS::getpid()); send(cmd,reply); } return address; }
Contact NameServer::registerName(const ConstString& name, const Contact& address, const ConstString& remote) { bool reusablePort = false; bool reusableIp = false; //YARP_DEBUG(Logger::get(),"in registerName..."); if (name!="...") { unregisterName(name); } Contact suggestion = address; if (!suggestion.isValid()) { suggestion = Contact(name, "...", "...", 0); } ConstString portName = name; if (portName == "...") { portName = tmpNames.get(); } ConstString carrier = suggestion.getCarrier(); if (carrier == "...") { carrier = "tcp"; } ConstString machine = suggestion.getHost(); int overridePort = 0; if (machine == "...") { if (carrier!="mcast") { if (remote=="...") { YARP_ERROR(Logger::get(),"remote machine name was not found! can only guess it is local..."); machine = "127.0.0.1"; } else { machine = remote; } } else { machine = mcastRecord.get(); overridePort = mcastRecord.lastPortNumber(); reusableIp = true; } } int port = suggestion.getPort(); if (port == 0) { if (overridePort) { port = overridePort; } else { port = getHostRecord(machine).get(); reusablePort = true; } } suggestion = Contact(portName, carrier, machine, port); YARP_DEBUG(Logger::get(),ConstString("Registering ") + suggestion.toURI() + " for " + suggestion.getRegName()); NameRecord& nameRecord = getNameRecord(suggestion.getRegName()); nameRecord.setAddress(suggestion,reusablePort,reusableIp); Bottle event; event.addVocab(Vocab::encode("add")); event.addString(suggestion.getRegName().c_str()); onEvent(event); return nameRecord.getAddress(); }