Hi all,
I'm working with the exercise 5 and I'm trying to define the provided and required services in two classes which implement the interfaces Service and ServiceRequester.
I also defined two components that implement the my own classes.
Now the problem is that when I launch the application I can not see the services in the task browser. Both, the hello and the world components have no services. Neither provided nor requested.
Where is my error? Maybe I have misunderstood the assignment?
Find below the code.
#include <rtt/os/main.h> #include <rtt/Logger.hpp> #include <rtt/TaskContext.hpp> #include <rtt/Activity.hpp> #include <rtt/ServiceRequester.hpp> #include <rtt/OperationCaller.hpp> #include <ocl/OCL.hpp> #include <ocl/TaskBrowser.hpp> using namespace std; using namespace RTT; using namespace Orocos; /** * Exercise 5: Read Orocos Component Builder's Manual, Chap 2 sect 5 * * In this exercise, we want to put our operations in a provided service * and our methods in a required service with the name 'Robot'. * * We will do this explicitly by creating a provides and a requires service * class. * - Create a RobotService class which inherits from ServiceProvider and * add the mymethod operation (cut & paste from Hello). * - Create a Robot class which inherits from ServiceRequester and * add the Method with the correct signature and name (cut & paste from World). * * In both cases, take care of proper constructor inintialisations. * Next inherit in Hello from the RobotService and in World from the Robot class. * * Extend World::configureHook() such that it connects its required service to * the provided service of Hello. * * Use sayIt and mymethod in the updateHook() of World and log the results. * * Finally test this all in the TaskBrowser, how did the interface of Hello and * World change compared to previous exercises ? * * Optional: rewrite this exercise, with the exact same functionality, * but with the least amount of code and classes. * What effects has this on re-usability ? */ namespace Example { class RobotService : public Service{ protected: string mymethod() { return "Hello World"; } bool sayIt(string sentence, string& answer) { log(Info) <<" Saying: Hello " << sentence << "!" <<endlog(); if (sentence == "Orocos") { answer="Hello Friend!"; return true; } return false; } public: RobotService(const string& name) : Service(name){ this->provides("robot")->addOperation("mymethod", &RobotService::mymethod, this).doc("Returns a friendly word."); this->provides("robot")->addOperation("sayIt", &RobotService::sayIt, this).doc("Returns a friendly answer.") .arg("sentence", "That's what I'll say.") .arg("answer", "That's the answer you'll get if you let me say the right thing."); } }; class Hello : public TaskContext, public RobotService { public: Hello(std::string name) : TaskContext(name), RobotService(name) { } }; class Robot : public ServiceRequester{ protected: OperationCaller< string(void) > mymethod; OperationCaller< bool(string, string&) > sayIt; public: Robot(const string& name) : ServiceRequester(name){ this->requires("robot")->addOperationCaller(mymethod); this->requires("robot")->addOperationCaller(sayIt); } }; class World : public TaskContext , public Robot { public: World(std::string name) : TaskContext(name, PreOperational), Robot(name) { } bool configureHook() { return this->TaskContext::requires("robot")->connectTo(getPeer("Hello")->provides("Robot")); //return requires("robot")->connectTo( getPeer("Hello")->provides("robot")); } void updateHook() { log(Info) << "Receiving from 'Hello': " << mymethod() <<endlog(); } }; } using namespace Example; int ORO_main(int argc, char** argv) { Logger::In in("main()"); // Set log level more verbose than default, // such that we can see output : if ( log().getLogLevel() < Logger::Info ) { log().setLogLevel( Logger::Info ); log(Info) << argv[0] << " manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog(); } log(Info) << "**** Creating the 'Hello' component ****" <<endlog(); Hello hello("Hello"); hello.setActivity( new Activity(1, 0.5 ) ); log(Info) << "**** Starting the 'hello' component ****" <<endlog(); hello.start(); log(Info) << "**** Creating the 'World' component ****" <<endlog(); World world("World"); world.setActivity( new Activity(1, 0.5 ) ); log(Info) << "**** Creating the 'Peer' connection ****" <<endlog(); connectPeers(&world, &hello ); log(Info) << "**** Starting the TaskBrowser ****" <<endlog(); TaskBrowser browser( &hello ); browser.loop(); return 0; }