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(); if (n!="..." && n!="" && n[0]!='/') { 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!="") { if (fakeName==NULL) { ConstString prefix = NetworkBase::getEnvironment("YARP_PORT_PREFIX"); if (prefix!="") { n = prefix + n; contact2 = contact2.addName(n); } } } // Allow for open() to be called safely many times on the same Port PortCoreAdapter *currentCore = &(HELPER(implementation)); 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(); bool local = false; if (NetworkBase::localNetworkAllocation()&&contact2.getPort()<=0) { YARP_DEBUG(Logger::get(),"local network allocation needed"); local = true; } bool success = true; Address caddress(contact2.getHost().c_str(), contact2.getPort(), contact2.getCarrier().c_str(), contact2.getName().c_str()); Address address = caddress; core.setReadHandler(core); if (contact2.getPort()>0 && contact2.getHost()!="") { registerName = false; } if (registerName&&!local) { Contact contactFull = NetworkBase::registerContact(contact2); address = Address::fromContact(contactFull); } core.setControlRegistration(registerName); success = (address.isValid()||local)&&(fakeName==NULL); 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.getCarrierName().c_str(), address.getName().c_str(), address.getPort()); contact2 = contact2.addName(address.getRegName().c_str()); Contact newName = NetworkBase::registerContact(contact2); core.resetPortName(newName.getName()); address = core.getAddress(); } if (core.getVerbosity()>=1) { YARP_INFO(Logger::get(), String("Port ") + address.getRegName() + " active at " + address.toString()); } } 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.toString():String("")) + " (" + blame.c_str() + ")"); } return success; }
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; }