ActiveObject.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 ActiveObject.cpp
00024  * @Description ActiveObject base implementation
00025  * @Status Work in Progress
00026  * @Version $Id: ActiveObject.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Marco Barbosa
00028  */
00029 
00030 
00031 #include "config.h"
00032 
00033 #include "ActiveObject.hpp"
00034 
00035 #include <CommunicationGateway.hpp>
00036 #include <Exception.hpp>
00037 
00038 #include <iostream>
00039 
00040 using namespace mermaid::support::activeobject;
00041 using mermaid::support::errorhandling::Exception;
00042 using boost::shared_ptr;
00043 
00044 ActiveObject::ActiveObject (ActiveObjectManager * aom)
00045 {
00046   state_ = INITIALIZING; // not using set state to avoid manager notifications during construction
00047   
00048   aoManager_ = aom;
00049   taskScheduler_ = shared_ptr<TaskScheduler> (new TaskScheduler());
00050   
00051   commGateway_ = shared_ptr<CommunicationGateway> (new CommunicationGateway());
00052   
00053   state_ = INITIALIZED;
00054 }; // ActiveObject()
00055 
00056 
00057 ActiveObject::~ActiveObject()
00058 {
00059 
00060 };
00061 
00062 ActiveObjectState ActiveObject::getState()
00063 {
00064   return state_;
00065 };
00066 
00067 std::string ActiveObject::getStateString()
00068 {
00069   return getStateString (state_);
00070 }; // getStateString
00071 
00072 std::string ActiveObject::getStateString (ActiveObjectState state)
00073 {
00074 
00075   if (state == INITIALIZING) {
00076     return "INITIALIZING";
00077   }
00078   else if (state == INITIALIZED) {
00079     return "INITIALIZED";
00080   }
00081   else if (state == STOPPING) {
00082     return "STOPPING";
00083   }
00084   else if (state == STOPPED) {
00085     return "STOPPED";
00086   }
00087   else if (state == PAUSING) {
00088     return "PAUSING";
00089   }
00090   else if (state == PAUSED) {
00091     return "PAUSED";
00092   }
00093   else if (state == RUNNING) {
00094     return "RUNNING";
00095   }
00096   else {
00097     return "unknown";
00098   }
00099   
00100 };
00101 
00102 void ActiveObject::setState (ActiveObjectState s)
00103 {
00104   if (state_ != s) {
00105     ActiveObjectState previousState = state_;
00106     state_ = s;
00107     aoManager_->notifyActiveObjectStateChange (this, previousState);
00108   }
00109 }; // setState()
00110 
00111 
00112 void ActiveObject::addTask (shared_ptr<Task> t)
00113 {
00114   //std::cerr << "ActiveObject::addTask : task=" << t.get() << std::endl;
00115   if (!taskScheduler_) {
00116     throw new Exception ("ActiveObject::addTask : no available task scheduler");
00117   }
00118   
00119   taskScheduler_->addTask (t);
00120 }; // addTask()
00121 
00122 
00123 shared_ptr<CommunicationGateway> ActiveObject::getCommunicationGateway()
00124 {
00125   return commGateway_;
00126 }; // getCommunicationGateway()
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3