RTXI 1.3
src/event.cpp
Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
00003 
00004  This program is free software: you can redistribute it and/or modify
00005  it under the terms of the GNU General Public License as published by
00006  the Free Software Foundation, either version 3 of the License, or
00007  (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  GNU General Public License for more details.
00013 
00014  You should have received a copy of the GNU General Public License
00015  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017  */
00018 
00019 #include <debug.h>
00020 #include <event.h>
00021 #include <string.h>
00022 
00023 const char *Event::RT_PERIOD_EVENT = "SYSTEM : period";
00024 const char *Event::RT_PREPERIOD_EVENT = "SYSTEM : pre period";
00025 const char *Event::RT_POSTPERIOD_EVENT = "SYSTEM : post period";
00026 const char *Event::RT_THREAD_INSERT_EVENT = "SYSTEM : thread insert";
00027 const char *Event::RT_THREAD_REMOVE_EVENT = "SYSTEM : thread remove";
00028 const char *Event::RT_DEVICE_INSERT_EVENT = "SYSTEM : device insert";
00029 const char *Event::RT_DEVICE_REMOVE_EVENT = "SYSTEM : device remove";
00030 const char *Event::IO_BLOCK_INSERT_EVENT = "SYSTEM : block insert";
00031 const char *Event::IO_BLOCK_REMOVE_EVENT = "SYSTEM : block remove";
00032 const char *Event::IO_LINK_INSERT_EVENT = "SYSTEM : link insert";
00033 const char *Event::IO_LINK_REMOVE_EVENT = "SYSTEM : link remove";
00034 const char *Event::WORKSPACE_PARAMETER_CHANGE_EVENT = "SYSTEM : parameter change";
00035 const char *Event::PLUGIN_INSERT_EVENT = "SYSTEM : plugin insert";
00036 const char *Event::PLUGIN_REMOVE_EVENT = "SYSTEM : plugin remove";
00037 const char *Event::SETTINGS_OBJECT_INSERT_EVENT = "SYSTEM : settings object insert";
00038 const char *Event::SETTINGS_OBJECT_REMOVE_EVENT = "SYSTEM : settings object remove";
00039 const char *Event::OPEN_FILE_EVENT = "SYSTEM : open file";
00040 const char *Event::START_RECORDING_EVENT = "SYSTEM : start recording";
00041 const char *Event::STOP_RECORDING_EVENT = "SYSTEM : stop recording";
00042 const char *Event::ASYNC_DATA_EVENT = "SYSTEM : async data";
00043 const char *Event::THRESHOLD_CROSSING_EVENT = "SYSTEM : threshold crossing event";
00044 
00045 Event::Handler::Handler(void) {
00046     Event::Manager::getInstance()->registerHandler(this);
00047 }
00048 
00049 Event::Handler::~Handler(void) {
00050     Event::Manager::getInstance()->unregisterHandler(this);
00051 }
00052 
00053 void Event::Handler::receiveEvent(const Event::Object *) {}
00054 
00055 Event::RTHandler::RTHandler(void) {
00056     Event::Manager::getInstance()->registerRTHandler(this);
00057 }
00058 
00059 Event::RTHandler::~RTHandler(void) {
00060     Event::Manager::getInstance()->unregisterRTHandler(this);
00061 }
00062 
00063 void Event::RTHandler::receiveEventRT(const Event::Object *) {}
00064 
00065 Event::Object::Object(const char *nam) : name(nam), nparams(0) {
00066     memset(params,0,sizeof(params));
00067 }
00068 
00069 Event::Object::~Object(void) {}
00070 
00071 void *Event::Object::getParam(const char *nam) const {
00072     for(size_t i=0;i<nparams;++i)
00073         if(!strcmp(params[i].name,nam))
00074             return params[i].value;
00075     return 0;
00076 }
00077 
00078 void Event::Object::setParam(const char *nam,void *val) {
00079     for(size_t i=0;i<nparams;++i)
00080         if(!strcmp(params[i].name,nam)) {
00081             params[i].value = val;
00082             return;
00083         }
00084 
00085     if(nparams >= MAX_PARAMS)
00086         return;
00087 
00088     params[nparams].name = nam;
00089     params[nparams].value = val;
00090     ++nparams;
00091 }
00092 
00093 Event::Manager::Manager(void){}
00094 
00095 Event::Manager::~Manager(void) {}
00096 
00097 void Event::Manager::postEvent(const Object *event) {
00098     Mutex::Locker lock(&mutex);
00099 
00100     for(std::list<Handler *>::iterator i = handlerList.begin(),end = handlerList.end();i != end;++i)
00101         (*i)->receiveEvent(event);
00102 }
00103 
00104 void Event::Manager::postEventRT(const Object *event) {
00105     for(RT::List<RTHandler>::iterator i = rthandlerList.begin(),end = rthandlerList.end();i != end;++i)
00106         i->receiveEventRT(event);
00107 }
00108 
00109 void Event::Manager::registerHandler(Handler *handler) {
00110     Mutex::Locker lock(&mutex);
00111     handlerList.insert(handlerList.end(),handler);
00112 }
00113 
00114 void Event::Manager::unregisterHandler(Handler *handler) {
00115     Mutex::Locker lock(&mutex);
00116     handlerList.remove(handler);
00117 }
00118 
00119 void Event::Manager::registerRTHandler(RTHandler *handler) {
00120     rthandlerList.insert(rthandlerList.end(),*handler);
00121 }
00122 
00123 void Event::Manager::unregisterRTHandler(RTHandler *handler) {
00124     rthandlerList.remove(*handler);
00125 }
00126 
00127 static Mutex mutex;
00128 Event::Manager *Event::Manager::instance = 0;
00129 
00130 Event::Manager *Event::Manager::getInstance(void) {
00131     if(instance)
00132         return instance;
00133 
00134     /*************************************************************************
00135      * Seems like alot of hoops to jump through, but static allocation isn't *
00136      *   thread-safe. So effort must be taken to ensure mutual exclusion.    *
00137      *************************************************************************/
00138 
00139     Mutex::Locker lock(&::mutex);
00140     if(!instance) {
00141         static Manager manager;
00142         instance = &manager;
00143     }
00144 
00145     return instance;
00146 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines