iCub-main
IPortEventListener.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/Network.h>
23 
25 
26 namespace iCub {
27 namespace learningmachine {
28 
30  if(this == &other) return *this; // handle self initialization
31 
32  this->IEventListener::operator=(other);
33  this->portPrefix = other.portPrefix;
34  return *this;
35 }
36 
37 void IPortEventListener::resetPort(std::string portName) {
38  // if empty portName, find first available standardly prefixed port
39  std::ostringstream buffer;
40  if(portName.empty()) {
41  int i = 1;
42  do {
43  // standard prefix + i
44  buffer.str(""); // clear buffer
45  buffer << this->portPrefix << i++ << ":o";
46  } while(yarp::os::Network::queryName(buffer.str().c_str()).isValid());
47  portName = buffer.str();
48  }
49 
50  // check availability of new port
51  if(yarp::os::Network::queryName(portName.c_str()).isValid()) {
52  throw std::runtime_error(std::string("port already registered"));
53  } else {
54  this->port.interrupt();
55  this->port.close();
56  if(port.open(portName.c_str()) != true) {
57  throw std::runtime_error(std::string("could not register port"));
58  }
59  }
60 }
61 
62 bool IPortEventListener::configure(yarp::os::Searchable& config) {
63  bool success = this->IEventListener::configure(config);
64 
65  // enable verbose output
66  if(!config.findGroup("verbose").isNull()) {
67  this->verbose = true;
68  success = true;
69  }
70 
71  // enable silent output
72  if(!config.findGroup("silent").isNull()) {
73  this->verbose = false;
74  success = true;
75  }
76 
77  // change port
78  if(!config.findGroup("port").isNull()) {
79  this->resetPort(config.findGroup("port").get(1).asString().c_str());
80  success = true;
81  }
82 
83  return success;
84 }
85 
86 
87 } // learningmachine
88 } // iCub
virtual bool configure(yarp::os::Searchable &config)
The abstract base class for EventListeners that output to a port.
yarp::os::Port port
The outgoing port for the events.
bool verbose
Verbosity controls whether events are printed to standard output.
std::string portPrefix
A prefix path for the ports that will be registered.
virtual IPortEventListener & operator=(const IPortEventListener &other)
Assignment operator.
virtual bool configure(yarp::os::Searchable &config)
void resetPort(std::string portName)
Resets the port and opens it at the specified name.
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.