testActiveObject.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 <iostream>
00033 #include <string>
00034
00035 #include <ace/Trace.h>
00036
00037 #include <ActiveObject.hpp>
00038 #include <ActiveObjectAce.hpp>
00039 #include <ActiveObjectManager.hpp>
00040 #include <ActiveObjectManagerAce.hpp>
00041 #include <Task.hpp>
00042
00043
00044 #include <Exception.hpp>
00045 #include <Service.hpp>
00046 #include <Time.hpp>
00047
00048 #include <pthread.h>
00049
00050 using namespace std;
00051 using mermaid::support::activeobject::ActiveObject;
00052 using mermaid::support::activeobject::ActiveObjectAce;
00053 using mermaid::support::activeobject::ActiveObjectManager;
00054 using mermaid::support::activeobject::ActiveObjectManagerAce;
00055 using mermaid::support::activeobject::ActiveObjectState;
00056 using mermaid::support::activeobject::Task;
00057 using mermaid::support::errorhandling::Exception;
00058 using boost::shared_ptr;
00059 using mermaid::support::service::Service;
00060 using mermaid::support::system::Time;
00061
00062 shared_ptr<ActiveObject> ao;
00063
00064 void * stopAo (void * arg)
00065 {
00066 shared_ptr<ActiveObject> a = ao;
00067 sleep (2);
00068 std::cerr << "Stopping ActiveObject" << std::endl;
00069 a->stop();
00070 return 0;
00071 };
00072
00073 int main (int argc, char * const argv[])
00074 {
00075 ACE_Trace::stop_tracing();
00076
00077
00078
00079 std::cerr << "ActiveObject framework test" << endl;
00080
00081
00082 ao = ActiveObjectManagerAce::getInstance()->createActiveObject();
00083
00084
00085 std::cerr << "# of ActiveObjects: " << ActiveObjectManagerAce::getInstance()->getNumberOfActiveObjects() << endl;
00086
00087
00088 try {
00089 ao->stop();
00090 cout << "It is supposed to return an error because the ActiveObject wasn't in the right state to do a 'stop'" << endl;
00091 return 1;
00092 }
00093 catch (Exception &e) {
00094
00095 cout << "ActiveObject::stop() failed as expected." << endl;
00096 }
00097
00098
00099
00100 try {
00101 ao->start();
00102 pthread_t stopThread;
00103 pthread_create (&stopThread, 0, &stopAo, 0);
00104
00105 cout << "Waiting for ActiveObjects to stop..." << endl;
00106 ActiveObjectManagerAce::getInstance()->waitForActiveObjectsToStop();
00107 cout << "All ActiveObjects stopped." << endl;
00108
00109 }
00110 catch (Exception &e) {
00111 cout << e.what() << endl;
00112 cout << "Test failed." << endl;
00113 return (1);
00114 }
00115 catch (...) {
00116 cout << "Test failed." << endl;
00117 return (1);
00118 }
00119
00120
00121
00122
00123 try {
00124 ao = ActiveObjectManagerAce::getInstance()->createActiveObject();
00125 ao->start();
00126 shared_ptr<Task> periodicTask (new Task (Time::getCurrentTime(), true, 2));
00127 ao->addTask (periodicTask);
00128
00129 pthread_t stopThread;
00130 pthread_create (&stopThread, 0, &stopAo, 0);
00131
00132 cout << "Waiting for ActiveObjects to stop..." << endl;
00133 ActiveObjectManagerAce::getInstance()->waitForActiveObjectsToStop();
00134 cout << "All ActiveObjects stopped." << endl;
00135 }
00136 catch (Exception &e) {
00137 cout << e.what() << endl;
00138 cout << "Test failed." << endl;
00139 return (1);
00140 }
00141 catch (...) {
00142 cout << "Test failed." << endl;
00143 return (1);
00144 }
00145
00146 cout << "Test succeeded." << endl;
00147 return (0);
00148
00149 };