CCA
RTPeriodicBeat.cpp
Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of CCA project
00004  *
00005  * Copyright (C) 2011 by Arne Nordmann <anordman at cor-lab dot uni-bielefeld dot de>
00006  *
00007  * This file may be licensed under the terms of the
00008  * GNU Lesser General Public License Version 3 (the ``LGPL''),
00009  * or (at your option) any later version.
00010  *
00011  * Software distributed under the License is distributed
00012  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
00013  * express or implied. See the LGPL for the specific language
00014  * governing rights and limitations.
00015  *
00016  * You should have received a copy of the LGPL along with this
00017  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
00018  * or write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The development of this software was supported by:
00022  *   CoR-Lab, Research Institute for Cognition and Robotics
00023  *     Bielefeld University
00024  *
00025  * ============================================================ */
00026 
00027 #include "RTPeriodicBeat.h"
00028 
00029 #include <string>
00030 #include <sstream>
00031 #include <iostream>
00032 #include <exception>
00033 
00034 #include <boost/bind.hpp>
00035 #include <boost/signal.hpp>
00036 #include <boost/thread.hpp>
00037 #include <boost/date_time/posix_time/posix_time.hpp>
00038 
00039 using namespace std;
00040 using namespace boost;
00041 using namespace rsc;
00042 using namespace rsc::logging;
00043 using namespace rsc::threading;
00044 
00045 namespace cca {
00046 
00047 RTPeriodicBeat::RTPeriodicBeat(unsigned int milliseconds) :
00048         Beat(milliseconds), rsc::threading::PeriodicTask(milliseconds),
00049                 beatlogger(Logger::getLogger("cca.beat")) {
00050     beatlogger->debug("[RTPeriodicBeat] constructed");
00051 }
00052 
00053 void RTPeriodicBeat::execute() {
00054     beatlogger->trace("[RTPeriodicBeat] >beat<");
00055 
00056     // Send tick to registered receivers
00057     for (unsigned int index = 0; index < this->_numReceivers; ++index) {
00058         this->_receiver[index]->tick();
00059     }
00060 }
00061 
00062 RTPeriodicBeat::~RTPeriodicBeat() {
00063 }
00064 
00065 unsigned int RTPeriodicBeat::getCycleTime() {
00066     return this->_cycletime;
00067 }
00068 
00069 void RTPeriodicBeat::registerReceiver(NodePtr receiver) {
00070     beatlogger->debug(
00071             "[RTPeriodicBeat] registerReceiver() registering component");
00072     this->_receiver.push_back(receiver);
00073     this->_numReceivers = this->_receiver.size();
00074 }
00075 
00076 std::vector<NodePtr> RTPeriodicBeat::getReceivers() {
00077     return this->_receiver;
00078 }
00079 
00080 void RTPeriodicBeat::cancel() {
00081     beatlogger->debug("[RTPeriodicBeat] cancel() requested");
00082     PeriodicTask::cancel();
00083 }
00084 
00085 bool RTPeriodicBeat::isCancelRequested() {
00086     return PeriodicTask::isCancelRequested();
00087 }
00088 
00089 void RTPeriodicBeat::run() {
00090     beatlogger->debug("[RTPeriodicBeat] run() started");
00091     PeriodicTask::run();
00092 }
00093 
00094 NodePtr RTPeriodicBeat::getReceiver(unsigned int index) {
00095     if (index >= this->_numReceivers) {
00096         throw std::out_of_range("[RTPeriodicBeat] Index out of bound.");
00097     }
00098     return this->_receiver[index];
00099 }
00100 
00101 string RTPeriodicBeat::print() const {
00102     ostringstream outstream(ostringstream::out);
00103     outstream.precision(3); // Precision when printing double values
00104     outstream << "<RTPeriodicBeat (frequency=" << this->_cycletime << "ms, "
00105             << this->_numReceivers << " assigned receivers)>";
00106     return outstream.str();
00107 }
00108 
00109 BeatPtr RTPeriodicBeat::create(unsigned int milliseconds) {
00110     return BeatPtr(new RTPeriodicBeat(milliseconds));
00111 }
00112 
00113 ostream& operator<<(ostream& os, const RTPeriodicBeat& beat) {
00114     os.precision(3); // Precision when printing double values
00115     os << beat.print();
00116     return os;
00117 }
00118 
00119 }