testActiveObject.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename testActiveObject.cpp
00024  * @Description Tests the DataSemantics framework implementation
00025  * @Status unknown
00026  * @Version $Id: testActiveObject.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Marco Barbosa + Nelson Ramos
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 }; // stopAo
00072 
00073 int main (int argc, char * const argv[])
00074 {
00075   ACE_Trace::stop_tracing();
00076   
00077   // insert code here...
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     // do nothing
00095     cout << "ActiveObject::stop() failed as expected." << endl;
00096   }
00097   
00098   
00099   // test if starting and stopping works, no tasks
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   // test if starting and stopping works, periodic task
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 }; // main
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3