iCub-main
DispatcherManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2010 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * author: Arjan Gijsberts
4  * email: arjan.gijsberts@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #include <stdexcept>
20 #include <sstream>
21 
22 #include <yarp/os/Vocab.h>
23 
26 
27 namespace iCub {
28 namespace learningmachine {
29 
30 
32  // cache pointer to event dispatcher
33  this->dispatcher = &(EventDispatcher::instance());
34  // cache pointer to event listener factory
35  this->factory = &(EventListenerFactory::instance());
36 }
37 
38 bool DispatcherManager::respond(const yarp::os::Bottle& cmd, yarp::os::Bottle& reply) {
39  bool success = false;
40 
41  try {
42  switch(cmd.get(0).asVocab32()) {
43  case yarp::os::createVocab32('h','e','l','p'): // print help information
44  reply.add(yarp::os::Value::makeVocab32("help"));
45 
46  reply.addString("Event Manager configuration options");
47  reply.addString(" help Displays this message");
48  reply.addString(" list Print a list of available event listeners");
49  reply.addString(" add type [type2 ...] Adds one or more event listeners");
50  reply.addString(" remove [all|idx] Removes event listener at an index or all");
51  reply.addString(" set [all|idx] Configures a listener");
52  reply.addString(" stats Prints information");
53  success = true;
54  break;
55 
56  case yarp::os::createVocab32('l','i','s','t'): // print list of available event listeners
57  {
58  reply.add(yarp::os::Value::makeVocab32("help"));
59  std::vector<std::string> keys = FactoryT<std::string, IEventListener>::instance().getKeys();
60  for(unsigned int i = 0; i < keys.size(); i++) {
61  reply.addString((std::string(" ") + keys[i]).c_str());
62  }
63  success = true;
64  break;
65  }
66 
67  case yarp::os::createVocab32('a','d','d'): // add
68  { // prevent identifier initialization to cross borders of case
69  yarp::os::Bottle list = cmd.tail();
70  for(int i = 0; i < list.size(); i++) {
71  IEventListener* listener = this->factory->create(list.get(i).asString().c_str());
72  listener->start();
73  this->dispatcher->addListener(listener);
74  }
75  reply.addString("Successfully added listener(s)");
76  success = true;
77  break;
78  }
79 
80  case yarp::os::createVocab32('r','e','m','o'): // remove
81  case yarp::os::createVocab32('d','e','l'): // del(ete)
82  { // prevent identifier initialization to cross borders of case
83  if(cmd.get(1).isInt32() && cmd.get(1).asInt32() >= 1 && cmd.get(1).asInt32() <= this->dispatcher->countListeners()) {
84  this->dispatcher->removeListener(cmd.get(1).asInt32()-1);
85  reply.addString("Successfully removed listener.");
86  success = true;
87  } else if(cmd.get(1).asString() == "all") {
88  this->dispatcher->clear();
89  reply.addString("Successfully removed all listeners.");
90  success = true;
91  } else {
92  throw std::runtime_error("Illegal index!");
93  }
94  break;
95  }
96 
97  case yarp::os::createVocab32('s','e','t'): // set
98  { // prevent identifier initialization to cross borders of case
99  yarp::os::Bottle property;
100  property.addList() = cmd.tail().tail(); // see comment in TrainModule
101 
102  std::string replymsg = "Setting configuration option ";
103  if(cmd.get(1).isInt32() && cmd.get(1).asInt32() >= 1 &&
104  cmd.get(1).asInt32() <= this->dispatcher->countListeners()) {
105 
106  bool ok = this->dispatcher->getAt(cmd.get(1).asInt32()-1).configure(property);
107  replymsg += ok ? "succeeded" :
108  "failed; please check key and value type.";
109  reply.addString(replymsg.c_str());
110  success = true;
111  } else if(cmd.get(1).asString() == "all") {
112  for(int i = 0; i < this->dispatcher->countListeners(); i++) {
113  if(i > 0) {
114  replymsg += ", ";
115  }
116  bool ok = this->dispatcher->getAt(i).configure(property);
117  replymsg += ok ? "succeeded" :
118  "failed; please check key and value type.";
119  }
120  replymsg += ".";
121  reply.addString(replymsg.c_str());
122  success = true;
123  } else {
124  throw std::runtime_error("Illegal index!");
125  }
126  break;
127  }
128 
129  case yarp::os::createVocab32('i','n','f','o'): // information
130  case yarp::os::createVocab32('s','t','a','t'): // statistics
131  { // prevent identifier initialization to cross borders of case
132  reply.add(yarp::os::Value::makeVocab32("help"));
133  std::ostringstream buffer;
134  buffer << "Event Manager Information (" << this->dispatcher->countListeners() << " listeners)";
135  reply.addString(buffer.str().c_str());
136  for(int i = 0; i < this->dispatcher->countListeners(); i++) {
137  buffer.str(""); // why isn't there a proper reset method?
138  buffer << " [" << (i + 1) << "] " << this->dispatcher->getAt(i).getInfo();
139  reply.addString(buffer.str().c_str());
140  }
141 
142  success = true;
143  break;
144  }
145 
146  default:
147  break;
148 
149  }
150  } catch(const std::exception& e) {
151  std::string msg = std::string("Error: ") + e.what();
152  reply.addString(msg.c_str());
153  success = true;
154  }
155 
156  return success;
157 }
158 
159 
160 
161 
162 } // learningmachine
163 } // iCub
164 
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &reply)
Respond to a command or configuration message.
virtual void clear()
Clears all the IEventListeners from the EventDispatcher.
virtual IEventListener & getAt(int idx)
Returns the IEventListener at a specified index.
static EventDispatcher & instance()
An instance retrieval method that follows the Singleton pattern.
virtual void addListener(IEventListener *listener)
Adds an IEventListener to the list.
virtual int countListeners() const
Counts the number of registered listeners.
virtual void removeListener(int idx)
Removes an IEventListener from the list.
T * create(const K &key)
Creates a new object given a specific type of key.
Definition: FactoryT.h:127
static FactoryT< K, T > & instance()
An instance retrieval method that follows the Singleton pattern.
Definition: FactoryT.h:86
virtual std::string getInfo()
Asks the event listener to return a string containing information on its configuration so far.
virtual void start()
Starts the IEventListener, such that it can do perform initialization (e.g.
virtual bool configure(yarp::os::Searchable &config)
cmd
Definition: dataTypes.h:30
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.