Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "SlaveActivity.hpp"
00040 #include "../os/MainThread.hpp"
00041 #include "Logger.hpp"
00042
00043 namespace RTT {
00044 using namespace extras;
00045 using namespace base;
00046 SlaveActivity::SlaveActivity( ActivityInterface* master, RunnableInterface* run )
00047 :ActivityInterface(run), mmaster(master), mperiod( master->getPeriod() ), running(false), active(false)
00048 {
00049 }
00050
00051 SlaveActivity::SlaveActivity( double period, RunnableInterface* run )
00052 :ActivityInterface(run), mmaster(0), mperiod(period), running(false), active(false)
00053 {
00054 }
00055
00056 SlaveActivity::SlaveActivity( RunnableInterface* run )
00057 :ActivityInterface(run), mmaster(0), mperiod(0.0), running(false), active(false)
00058 {
00059 }
00060
00061 SlaveActivity::~SlaveActivity()
00062 {
00063 stop();
00064 }
00065
00066 Seconds SlaveActivity::getPeriod() const
00067 {
00068 if (mmaster)
00069 return mmaster->getPeriod();
00070 return mperiod;
00071 }
00072
00073 bool SlaveActivity::setPeriod(Seconds s) {
00074 if (mmaster)
00075 return false;
00076 mperiod = s;
00077 return true;
00078 }
00079
00080 unsigned SlaveActivity::getCpuAffinity() const
00081 {
00082 if (mmaster)
00083 return mmaster->getCpuAffinity();
00084 return ~0;
00085 }
00086
00087 bool SlaveActivity::setCpuAffinity(unsigned cpu)
00088 {
00089 return false;
00090 }
00091
00092 os::ThreadInterface* SlaveActivity::thread()
00093 {
00094 return mmaster ? mmaster->thread() : os::MainThread::Instance();
00095 }
00096
00097 bool SlaveActivity::initialize()
00098 {
00099 return true;
00100 }
00101
00102 void SlaveActivity::step()
00103 {
00104 }
00105
00106 void SlaveActivity::loop()
00107 {
00108 this->step();
00109 }
00110
00111 bool SlaveActivity::breakLoop()
00112 {
00113 return false;
00114 }
00115
00116
00117 void SlaveActivity::finalize()
00118 {
00119 }
00120
00121 bool SlaveActivity::start()
00122 {
00123 if (mmaster && !mmaster->isActive())
00124 {
00125 Logger::log() << Logger::Error << "Unable to start slave as master activity is not running" << Logger::endl;
00126 return false;
00127 }
00128 if ( active == true )
00129 {
00130 Logger::log() << Logger::Error << "Unable to start slave as it is already started" << Logger::endl;
00131 return false;
00132 }
00133
00134 active = true;
00135
00136 if ( runner ? runner->initialize() : this->initialize() ) {
00137 running = this->isPeriodic();
00138 } else {
00139 active = false;
00140 }
00141 return active;
00142 }
00143
00144
00145 bool SlaveActivity::stop()
00146 {
00147 if ( !active )
00148 return false;
00149
00150
00151 if ( this->isPeriodic() == false) {
00152 if ( running && (runner ? (runner->breakLoop() == false): (this->breakLoop() == false) ) )
00153 return false;
00154 }
00155
00156 running = false;
00157 if (runner)
00158 runner->finalize();
00159 else
00160 this->finalize();
00161 active = false;
00162 return true;
00163 }
00164
00165 bool SlaveActivity::isRunning() const
00166 {
00167 return running;
00168 }
00169
00170 bool SlaveActivity::isPeriodic() const
00171 {
00172 return mperiod != 0.0;
00173 }
00174 bool SlaveActivity::isActive() const
00175 {
00176 return active;
00177 }
00178
00179 bool SlaveActivity::trigger()
00180 {
00181 if (mmaster)
00182 return mmaster->trigger();
00183 return false;
00184 }
00185
00186 bool SlaveActivity::execute()
00187 {
00188
00189 if ( mperiod == 0.0 ) {
00190 if ( !active || running )
00191 return false;
00192 running = true;
00193 if (runner)
00194 runner->loop();
00195 else
00196 this->loop();
00197 running = false;
00198 return true;
00199 }
00200
00201 if ( running ) {
00202 if (runner) runner->step(); else this->step();
00203 }
00204 return running;
00205 }
00206
00207
00208 }