ActiveObjectAce.cpp
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 #include "config.h"
00031
00032 #include <ActiveObjectAce.hpp>
00033 #include <ActiveObjectAceAddTaskMethod.hpp>
00034 #include <Exception.hpp>
00035 #include <Time.hpp>
00036
00037 #include <iostream>
00038
00039 #include <ace/Date_Time.h>
00040 #include <ace/Future.h>
00041 #include <ace/Method_Object.h>
00042
00043 #include <stdexcept>
00044
00045 using namespace mermaid::support::activeobject;
00046 using mermaid::support::errorhandling::Exception;
00047 using mermaid::support::system::Time;
00048
00049
00050
00051 ActiveObjectAce::ActiveObjectAce (ActiveObjectManager * aom) : ActiveObject (aom), ACE_Task<ACE_MT_SYNCH>()
00052 {
00053
00054 }
00055
00056 ActiveObjectAce::~ActiveObjectAce()
00057 {
00058 std::cerr << "ActiveObjectAce::~ActiveObjectAce()" << std::endl;
00059 }
00060
00061
00062
00063 void ActiveObjectAce::start()
00064 {
00065
00066
00067 if (getState() != INITIALIZED) {
00068 throw Exception ("ActiveObjectAce::doShutdown() : To run start(), the ActiveObject must have state = INITIALIZED, and the actual state is " + getStateString());
00069 }
00070
00071 setState (RUNNING);
00072 this->activate();
00073 }
00074
00075
00076
00077 void ActiveObjectAce::stop()
00078 {
00079
00080
00081 if (getState() != RUNNING) {
00082 throw Exception ("ActiveObjectAce::doShutdown() : To run stop(), the ActiveObject must have state = RUNNING, and the actual state is " + getStateString());
00083 }
00084
00085 setState (STOPPING);
00086 activationQueue_.queue()->close();
00087 this->wait();
00088 };
00089
00090
00091
00092
00093
00094 int ActiveObjectAce::svc (void)
00095 {
00096 Time lastTime = Time::getCurrentTime();
00097
00098 try {
00099 while (1) {
00100
00101
00102 shared_ptr<Task> task = taskScheduler_->peakNextTask();
00103
00104 if (task == false) {
00105
00106
00107 shared_ptr<ACE_Method_Object> mo = shared_ptr<ACE_Method_Object> (activationQueue_.dequeue());
00108
00109 if (mo) {
00110
00111 {
00112 Time delta = Time::getCurrentTime() - lastTime;
00113
00114 lastTime = Time::getCurrentTime();
00115 }
00116 if (mo->call() == -1) {
00117 break;
00118 }
00119
00120 }
00121 else {
00122
00123
00124 setState (STOPPED);
00125 return 0;
00126 }
00127 }
00128 else {
00129
00130 Time scheduleTime = task->getScheduledTime();
00131
00132 shared_ptr<ACE_Method_Object> mo = shared_ptr<ACE_Method_Object> (activationQueue_.dequeue ( (ACE_Time_Value*) & scheduleTime));
00133
00134
00135 if (mo == false) {
00136
00137 if (getState() != RUNNING) {
00138
00139
00140 setState (STOPPED);
00141 return 0;
00142 }
00143
00144
00145 shared_ptr<Task> t = taskScheduler_->getNextTask();
00146
00147 {
00148 Time delta = Time::getCurrentTime() - lastTime;
00149
00150 lastTime = Time::getCurrentTime();
00151 }
00152
00153 if (t->isOkToRun() == true) {
00154 try {
00155 t->run();
00156 }
00157 catch (Exception &e) {
00158 std::cerr << "ActiveObjectAce::svc : caught an Exception from a Task : " << e.what() << std::endl;
00159 }
00160 }
00161 else {
00162 std::cerr << "ActiveObjectAce::svc : task not ok to run" << std::endl;
00163 }
00164
00165
00166
00167 if (t->hasRepeatScheduling()) {
00168
00169 t->incrementScheduledTimeForRepeat();
00170 this->addTaskInternal (t);
00171 }
00172 else {
00173
00174 }
00175 }
00176 else {
00177
00178
00179 {
00180 Time delta = Time::getCurrentTime() - lastTime;
00181
00182 lastTime = Time::getCurrentTime();
00183 }
00184 if (mo->call() == -1) {
00185 break;
00186 }
00187
00188 }
00189
00190 }
00191
00192 }
00193 }
00194 catch (Exception &e) {
00195 std::cerr << "ActiveObjectAce::svc() : caught an Exception: " << e.what() << std::endl;
00196
00197 setState (STOPPED);
00198
00199 if (getState() != STOPPED) {
00200 std::cerr << "ActiveObjectAce::svc() : failed to set state to STOPPED" << std::endl;
00201 }
00202 }
00203 catch (std::runtime_error &e) {
00204 std::cerr << "ActiveObjectAce::svc() : caught a runtime_error exception: " << e.what() << std::endl;
00205
00206 setState (STOPPED);
00207
00208 if (getState() != STOPPED) {
00209 std::cerr << "ActiveObjectAce::svc() : failed to set state to STOPPED" << std::endl;
00210 }
00211 }
00212 catch (...) {
00213
00214
00215 std::cerr << "ActiveObjectAce::svc() : something nasty happened" << std::endl;
00216
00217 setState (STOPPED);
00218
00219 if (getState() != STOPPED) {
00220 std::cerr << "ActiveObjectAce::svc() : failed to set state to STOPPED" << std::endl;
00221 }
00222
00223
00224 }
00225 return 0;
00226 };
00227
00228
00229
00230 void ActiveObjectAce::addTask (shared_ptr<Task> t)
00231 {
00232
00233
00234 ACE_Future<shared_ptr<Exception> > resultantFuture;
00235
00236 this->activationQueue_.enqueue (new ActiveObjectAceAddTaskMethod (this, t, resultantFuture));
00237
00238 shared_ptr<Exception> e;
00239
00240
00241
00242
00243 if (e) {
00244 throw Exception ("ActiveObjectAce::addTask : task added UNSUCCESSFULY");
00245 }
00246 else {
00247
00248 }
00249
00250 };
00251
00252
00253
00254 void ActiveObjectAce::addTaskInternal (shared_ptr<Task> t)
00255 {
00256
00257 addTaskImplementation (t);
00258 };
00259
00260
00261
00262 void ActiveObjectAce::addTaskImplementation (shared_ptr<Task> t)
00263 {
00264
00265 ActiveObject::addTask (t);
00266 };