CCA
HeartBeat.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 "HeartBeat.h"
00028 
00029 #include <string>
00030 #include <sstream>
00031 #include <iostream>
00032 
00033 #include "cca/config.h"
00034 
00035 using namespace std;
00036 using namespace rsc::logging;
00037 
00038 namespace cca {
00039 namespace timing {
00040 
00041 HeartBeat::HeartBeat(unsigned int milliseconds,
00042         PortConfiguration::Transport transport) :
00043         BeatProvider(), rsc::threading::PeriodicTask(milliseconds),
00044                 _cycletime(milliseconds), _sequenceNumber(0),
00045                 logger(Logger::getLogger("cca.heartbeat")) {
00046 
00047     RSCDEBUG(logger, "[HeartBeat] constructed");
00048 
00049     if (transport == PortConfiguration::CCALocalRemote) {
00050         RSCWARN(logger, "HeartBeat configured locally and remote. This can lead"
00051         " to nodes receiving ticks twice (locally and remotely) if not"
00052         " configured properly.")
00053     }
00054 
00055     this->outputBeat->configure(
00056             PortConfiguration::create(transport, CCA_DEFAULTSCOPE_HEARTBEAT));
00057 }
00058 
00059 HeartBeat::~HeartBeat() {
00060 }
00061 
00062 void HeartBeat::execute() {
00063     RSCTRACE(logger, "[HeartBeat] >beat<");
00064 
00065     OutputPort<Tick>::DataPtr tick(new Tick(_sequenceNumber++));
00066     this->outputBeat->publish(tick);
00067 }
00068 
00069 unsigned int HeartBeat::getCycleTime() {
00070     return this->_cycletime;
00071 }
00072 
00073 void HeartBeat::cancel() {
00074     RSCDEBUG(logger, "[HeartBeat] cancel() requested");
00075     PeriodicTask::cancel();
00076 }
00077 
00078 bool HeartBeat::isCancelRequested() {
00079     return PeriodicTask::isCancelRequested();
00080 }
00081 
00082 void HeartBeat::run() {
00083     RSCDEBUG(logger, "[HeartBeat] run() started");
00084     PeriodicTask::run();
00085 }
00086 
00087 void HeartBeat::run(unsigned int cycles) {
00088     // TODO
00089     RSCDEBUG(logger, "[HeartBeat] run() started");
00090     PeriodicTask::run();
00091 }
00092 
00093 string HeartBeat::print() const {
00094     ostringstream outstream(ostringstream::out);
00095     outstream.precision(3); // Precision when printing double values
00096     outstream << "<HeartBeat (frequency=" << this->_cycletime << "ms)>";
00097     return outstream.str();
00098 }
00099 
00100 HeartBeatPtr HeartBeat::LOCAL(unsigned int milliseconds) {
00101     return HeartBeatPtr(
00102             new HeartBeat(milliseconds, PortConfiguration::CCALocal));
00103 }
00104 
00105 HeartBeatPtr HeartBeat::REMOTE(unsigned int milliseconds) {
00106     return HeartBeatPtr(
00107             new HeartBeat(milliseconds, PortConfiguration::CCARemote));
00108 }
00109 
00110 HeartBeatPtr HeartBeat::LOCALREMOTE(unsigned int milliseconds) {
00111     return HeartBeatPtr(
00112             new HeartBeat(milliseconds, PortConfiguration::CCALocalRemote));
00113 }
00114 
00115 ostream& operator<<(ostream& os, const HeartBeat& beat) {
00116     os.precision(3); // Precision when printing double values
00117     os << beat.print();
00118     return os;
00119 }
00120 
00121 }
00122 }