virtual bool expectIndex(yarp::os::ConnectionState& proto) { yarp::os::ConstString prefix = "human says "; yarp::os::ConstString compare = prefix; yarp::os::Bytes b2((char*)prefix.c_str(),prefix.length()); proto.is().read(b2); bool ok = proto.is().isOk() && (prefix==compare); if (!ok) yInfo() << "YOU DID NOT SAY 'human says '"; return ok; }
virtual bool expectAck(yarp::os::ConnectionState& proto) { std::string prefix = "computers rule!\r\n"; std::string compare = prefix; yarp::os::Bytes b2((char*)prefix.c_str(),prefix.length()); proto.is().read(b2); bool ok = proto.is().isOk() && (prefix==compare); if (!ok) yInfo() << "YOU DID NOT SAY 'computers rule!'"; return ok; }
virtual bool expectReplyToHeader(yarp::os::ConnectionState& proto) { // SWITCH TO NEW STREAM TYPE HumanStream *stream = new HumanStream(); if (stream==NULL) { return false; } proto.takeStreams(stream); return true; }
// Read connection settings. bool PortMonitor::configure(yarp::os::ConnectionState& proto) { portName = proto.getRoute().getToName(); sourceName = proto.getRoute().getFromName(); group = getPeers().add(portName,this); if (!group) return false; Property options; options.fromString(proto.getSenderSpecifier().c_str()); if(binder) delete binder; binder = NULL; ConstString script = options.check("type", Value("lua")).asString(); ConstString filename = options.check("file", Value("modifier")).asString(); ConstString constraint = options.check("constraint", Value("")).asString(); // context is used to find the script files ConstString context = options.check("context", Value("")).asString(); // check which monitor should be used if((binder = MonitorBinding::create(script.c_str())) == NULL) { yError("Currently only \'lua\' script and \'dll\' object is supported by portmonitor"); return false; } // set the acceptance constraint binder->setAcceptConstraint(constraint.c_str()); ConstString strFile = filename; if(script != "dll") { yarp::os::ResourceFinder rf; rf.setDefaultContext(context.c_str()); rf.configure(0, NULL); strFile = rf.findFile(filename.c_str()); if(strFile == "") strFile = rf.findFile(filename+".lua"); } // provide some useful information for the monitor object // which can be accessed in the create() callback. Property info; info.clear(); info.put("filename", strFile); info.put("type", script); info.put("sender_side", (proto.getContactable()->getName() == sourceName)); info.put("receiver_side", (proto.getContactable()->getName() == portName)); info.put("source", sourceName); info.put("destination", portName); info.put("carrier", proto.getRoute().getCarrierName()); PortMonitor::lock(); bReady = binder->load(info); PortMonitor::unlock(); return bReady; }
virtual bool sendHeader(yarp::os::ConnectionState& proto) { // Send the "magic number" for this carrier yarp::os::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 std::string from = proto.getRoute().getFromName(); yarp::os::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(); }
// Read connection settings. bool PortMonitor::configure(yarp::os::ConnectionState& proto) { portName = proto.getRoute().getToName(); sourceName = proto.getRoute().getFromName(); group = getPeers().add(portName,this); if (!group) return false; Property options; options.fromString(proto.getSenderSpecifier().c_str()); options.put("source", sourceName); options.put("destination", portName); options.put("sender_side", (proto.getContactable()->getName() == sourceName) ? 1 : 0); options.put("receiver_side", (proto.getContactable()->getName() == portName) ? 1 : 0); options.put("carrier", proto.getRoute().getCarrierName()); return configureFromProperty(options); }
// Read connection settings. bool PriorityCarrier::configure(yarp::os::ConnectionState& proto) { portName = proto.getRoute().getToName(); sourceName = proto.getRoute().getFromName(); group = getPeers().add(portName,this); if (!group) return false; Property options; options.fromString(proto.getSenderSpecifier().c_str()); timeConstant = fabs(options.check("tc",Value(1.0)).asFloat64()); timeResting = fabs(options.check("tr",Value(0.0)).asFloat64()); stimulation = fabs(options.check("st",Value(STIMUL_THRESHOLD*10)).asFloat64()); // Zero stimulation is undefined and will be interpreted as S=Thresould. if(stimulation == 0) stimulation = STIMUL_THRESHOLD*10; stimulation /= 10.0; baias = options.check("bs",Value(STIMUL_THRESHOLD*10)).asFloat64(); baias /= 10.0; excitation = options.findGroup("ex"); isVirtual = options.check("virtual"); #ifdef WITH_PRIORITY_DEBUG if(options.check("debug")) { std::string msg; char dummy[1024]; safe_printf(dummy, 1024, "\n%s:\n", sourceName.c_str()); msg+= dummy; safe_printf(dummy, 1024, " stimulation: %.2f\n", stimulation); msg+= dummy; safe_printf(dummy, 1024, " bias: %.2f\n", baias); msg+= dummy; safe_printf(dummy, 1024, " tc: %.2fs\n", timeConstant); msg+= dummy; safe_printf(dummy, 1024, " tr: %.2fs\n", timeResting); msg+= dummy; safe_printf(dummy, 1024, " ex: "); msg+= dummy; for(size_t i=0; i<excitation.size(); i++) { Value v = excitation.get(i); if(v.isList() && (v.asList()->size()>=2)) { Bottle* b = v.asList(); safe_printf(dummy, 1024, "(%s, %.2f) ", b->get(0).asString().c_str(), b->get(1).asFloat64()/10.0 ); msg+= dummy; } } //safe_printf(dummy, 1024, "\n"); msg+= "\n"; safe_printf(dummy, 1024, " virtual: %s\n", (isVirtual)?"yes":"no"); msg+= dummy; double rate = options.check("rate", Value(10)).asInt32() / 1000.0; safe_printf(dummy, 1024, " db.rate: %fs\n", rate); msg+= dummy; yInfo("%s", msg.c_str()); debugger.stop(); debugger.setPeriod(rate); debugger.start(); } #endif return true; }
virtual bool sendAck(yarp::os::ConnectionState& proto) { std::string prefix = "computers rule!\r\n"; yarp::os::Bytes b2((char*)prefix.c_str(),prefix.length()); proto.os().write(b2); return true; }
virtual bool sendIndex(yarp::os::ConnectionState& proto) { std::string prefix = "human says "; yarp::os::Bytes b2((char*)prefix.c_str(),prefix.length()); proto.os().write(b2); return true; }
virtual bool write(yarp::os::ConnectionState& proto, yarp::os::SizedWriter& writer) { bool ok = sendIndex(proto); if (!ok) return false; writer.write(proto.os()); return proto.os().isOk(); }
virtual bool expectSenderSpecifier(yarp::os::ConnectionState& proto) { // interpret everything that sendHeader wrote proto.setRoute(proto.getRoute().addFromName(proto.is().readLine())); return proto.is().isOk(); }