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 base::ActivityInterface *SlaveActivity::getMaster() const
00098 {
00099 return mmaster;
00100 }
00101
00102 bool SlaveActivity::initialize()
00103 {
00104 return true;
00105 }
00106
00107 void SlaveActivity::step()
00108 {
00109 }
00110
00111 void SlaveActivity::loop()
00112 {
00113 this->step();
00114 }
00115
00116 bool SlaveActivity::breakLoop()
00117 {
00118 return false;
00119 }
00120
00121
00122 void SlaveActivity::finalize()
00123 {
00124 }
00125
00126 bool SlaveActivity::start()
00127 {
00128 if (mmaster && !mmaster->isActive())
00129 {
00130 Logger::log() << Logger::Error << "Unable to start slave as master activity is not running" << Logger::endl;
00131 return false;
00132 }
00133 if ( active == true )
00134 {
00135 Logger::log() << Logger::Error << "Unable to start slave as it is already started" << Logger::endl;
00136 return false;
00137 }
00138
00139 active = true;
00140
00141 if ( runner ? runner->initialize() : this->initialize() ) {
00142 running = this->isPeriodic();
00143 } else {
00144 active = false;
00145 }
00146 return active;
00147 }
00148
00149
00150 bool SlaveActivity::stop()
00151 {
00152 if ( !active )
00153 return false;
00154
00155
00156 if ( this->isPeriodic() == false) {
00157 if ( running && (runner ? (runner->breakLoop() == false): (this->breakLoop() == false) ) )
00158 return false;
00159 }
00160
00161 running = false;
00162 if (runner)
00163 runner->finalize();
00164 else
00165 this->finalize();
00166 active = false;
00167 return true;
00168 }
00169
00170 bool SlaveActivity::isRunning() const
00171 {
00172 return running;
00173 }
00174
00175 bool SlaveActivity::isPeriodic() const
00176 {
00177 return mperiod != 0.0;
00178 }
00179 bool SlaveActivity::isActive() const
00180 {
00181 return active;
00182 }
00183
00184 bool SlaveActivity::trigger()
00185 {
00186 if (mmaster)
00187 return mmaster->trigger();
00188 return false;
00189 }
00190
00191 bool SlaveActivity::execute()
00192 {
00193
00194 if ( mperiod == 0.0 ) {
00195 if ( !active || running )
00196 return false;
00197 running = true;
00198 if (runner)
00199 runner->loop();
00200 else
00201 this->loop();
00202 running = false;
00203 return true;
00204 }
00205
00206 if ( running ) {
00207 if (runner) runner->step(); else this->step();
00208 }
00209 return running;
00210 }
00211
00212
00213 }