bool NetworkBase::write(const Contact& contact, PortWriter& cmd, PortReader& reply, const ContactStyle& style) { if (!getNameSpace().serverAllocatesPortNumbers()) { // switch to more up-to-date method Port port; port.setAdminMode(style.admin); port.openFake("network_write"); Contact ec = contact; if (style.carrier!="") { ec = ec.addCarrier(style.carrier); } if (!port.addOutput(ec)) { if (!style.quiet) { ACE_OS::fprintf(stderr, "Cannot make connection to '%s'\n", ec.toString().c_str()); } return false; } bool ok = port.write(cmd,reply); return ok; } const char *connectionName = "admin"; ConstString name = contact.getName(); const char *targetName = name.c_str(); // use carefully! Contact address = contact; if (!address.isValid()) { address = getNameSpace().queryName(targetName); } if (!address.isValid()) { if (!style.quiet) { YARP_SPRINTF1(Logger::get(),error, "cannot find port %s", targetName); } return false; } if (style.timeout>0) { address.setTimeout((float)style.timeout); } OutputProtocol *out = Carriers::connect(address); if (out==NULL) { if (!style.quiet) { YARP_SPRINTF1(Logger::get(),error, "Cannot connect to port %s", targetName); } return false; } if (style.timeout>0) { out->setTimeout(style.timeout); } Route r(connectionName,targetName, (style.carrier!="")?style.carrier.c_str():"text_ack"); out->open(r); PortCommand pc(0,style.admin?"a":"d"); BufferedConnectionWriter bw(out->getConnection().isTextMode(), out->getConnection().isBareMode()); bool ok = true; if (out->getConnection().canEscape()) { ok = pc.write(bw); } if (!ok) { if (!style.quiet) { YARP_ERROR(Logger::get(),"could not write to connection"); } if (out!=NULL) delete out; return false; } ok = cmd.write(bw); if (!ok) { if (!style.quiet) { YARP_ERROR(Logger::get(),"could not write to connection"); } if (out!=NULL) delete out; return false; } if (style.expectReply) { bw.setReplyHandler(reply); } out->write(bw); if (out!=NULL) { delete out; out = NULL; } return true; }
bool RosNameSpace::writeToNameServer(PortWriter& cmd, PortReader& reply, const ContactStyle& style) { DummyConnector con0; cmd.write(con0.getWriter()); Bottle in; in.read(con0.getReader()); ConstString key = in.get(0).asString(); ConstString arg1 = in.get(1).asString(); Bottle cmd2, cache; bool use_cache = false; if (key=="query") { Contact c = queryName(arg1.c_str()).addName(""); Bottle reply2; reply2.addString(arg1.c_str()); reply2.addString(c.toString().c_str()); DummyConnector con; reply2.write(con.getWriter()); reply.read(con.getReader()); return true; } else if (key=="list") { cmd2.addString("getSystemState"); cmd2.addString("dummy_id"); use_cache = true; } else { return false; } bool ok = NetworkBase::write(getNameServerContact(), cmd2, *(use_cache?&cache:&reply), style); if (!ok) { fprintf(stderr,"Failed to contact ROS server\n"); return false; } if (key=="list") { Bottle out; out.addVocab(Vocab::encode("many")); Bottle *parts = cache.get(2).asList(); Property nodes; Property topics; Property services; if (parts) { for (int i=0; i<3; i++) { Bottle *part = parts->get(i).asList(); if (!part) continue; for (int j=0; j<part->size(); j++) { Bottle *unit = part->get(j).asList(); if (!unit) continue; ConstString stem = unit->get(0).asString(); Bottle *links = unit->get(1).asList(); if (!links) continue; if (i<2) { topics.put(stem,1); } else { services.put(stem,1); } for (int j=0; j<links->size(); j++) { nodes.put(links->get(j).asString(),1); } } } Property *props[3] = {&nodes, &topics, &services}; const char *title[3] = {"node", "topic", "service"}; for (int p=0; p<3; p++) { Bottle blist; blist.read(*props[p]); for (int i=0; i<blist.size(); i++) { ConstString name = blist.get(i).asList()->get(0).asString(); Bottle& info = out.addList(); info.addString(title[p]); info.addString(name); } } } out.write(reply); } return ok; }
virtual bool read(ConnectionReader& reader) { if (permanentReadDelegate!=NULL) { bool result = permanentReadDelegate->read(reader); return result; } // called by comms code readBlock.wait(); if (!reader.isValid()) { // interrupt stateMutex.wait(); if (readDelegate!=NULL) { readResult = readDelegate->read(reader); } stateMutex.post(); produce.post(); readBlock.post(); return false; } if (closed) { YARP_DEBUG(Logger::get(),"Port::read shutting down"); readBlock.post(); return false; } // wait for happy consumer - don't want to miss a packet if (!readBackground) { consume.wait(); } stateMutex.wait(); readResult = false; if (readDelegate!=NULL) { readResult = readDelegate->read(reader); } else { // read and ignore YARP_DEBUG(Logger::get(),"data received in Port, no reader for it"); Bottle b; b.read(reader); } if (!readBackground) { readDelegate = NULL; writeDelegate = NULL; } bool result = readResult; stateMutex.post(); if (!readBackground) { produce.post(); } if (result&&willReply) { consume.wait(); if (closed) { YARP_DEBUG(Logger::get(),"Port::read shutting down"); readBlock.post(); return false; } if (writeDelegate!=NULL) { stateMutex.wait(); ConnectionWriter *writer = reader.getWriter(); if (writer!=NULL) { result = readResult = writeDelegate->write(*writer); } stateMutex.post(); } if (dropDue) { reader.requestDrop(); } produce.post(); } readBlock.post(); return result; }
bool ConnectionWriter::writeToStream(PortWriter& portable, OutputStream& os) { BufferedConnectionWriter writer; if (!portable.write(writer)) return false; writer.write(os); return os.isOk(); }