RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
event.cpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #include <debug.h>
21 #include <event.h>
22 #include <string.h>
23 
24 const char *Event::RT_PERIOD_EVENT = "SYSTEM : period";
25 const char *Event::RT_PREPERIOD_EVENT = "SYSTEM : pre period";
26 const char *Event::RT_POSTPERIOD_EVENT = "SYSTEM : post period";
27 const char *Event::RT_THREAD_INSERT_EVENT = "SYSTEM : thread insert";
28 const char *Event::RT_THREAD_REMOVE_EVENT = "SYSTEM : thread remove";
29 const char *Event::RT_DEVICE_INSERT_EVENT = "SYSTEM : device insert";
30 const char *Event::RT_DEVICE_REMOVE_EVENT = "SYSTEM : device remove";
31 const char *Event::IO_BLOCK_INSERT_EVENT = "SYSTEM : block insert";
32 const char *Event::IO_BLOCK_REMOVE_EVENT = "SYSTEM : block remove";
33 const char *Event::IO_LINK_INSERT_EVENT = "SYSTEM : link insert";
34 const char *Event::IO_LINK_REMOVE_EVENT = "SYSTEM : link remove";
35 const char *Event::WORKSPACE_PARAMETER_CHANGE_EVENT = "SYSTEM : parameter change";
36 const char *Event::PLUGIN_INSERT_EVENT = "SYSTEM : plugin insert";
37 const char *Event::PLUGIN_REMOVE_EVENT = "SYSTEM : plugin remove";
38 const char *Event::SETTINGS_OBJECT_INSERT_EVENT = "SYSTEM : settings object insert";
39 const char *Event::SETTINGS_OBJECT_REMOVE_EVENT = "SYSTEM : settings object remove";
40 const char *Event::OPEN_FILE_EVENT = "SYSTEM : open file";
41 const char *Event::START_RECORDING_EVENT = "SYSTEM : start recording";
42 const char *Event::STOP_RECORDING_EVENT = "SYSTEM : stop recording";
43 const char *Event::ASYNC_DATA_EVENT = "SYSTEM : async data";
44 const char *Event::THRESHOLD_CROSSING_EVENT = "SYSTEM : threshold crossing event";
45 const char *Event::START_GENICAM_RECORDING_EVENT = "SYSTEM : start genicam recording";
46 const char *Event::PAUSE_GENICAM_RECORDING_EVENT = "SYSTEM : pause genicam recording";
47 const char *Event::STOP_GENICAM_RECORDING_EVENT = "SYSTEM : stop genicam recording";
48 const char *Event::GENICAM_SNAPSHOT_EVENT = "SYSTEM : genicam snap";
49 
51 {
53 }
54 
56 {
58 }
59 
61 
63 {
65 }
66 
68 {
70 }
71 
73 
74 Event::Object::Object(const char *nam) : name(nam), nparams(0)
75 {
76  memset(params,0,sizeof(params));
77 }
78 
80 
81 void *Event::Object::getParam(const char *nam) const
82 {
83  for (size_t i=0; i<nparams; ++i)
84  if (!strcmp(params[i].name,nam))
85  return params[i].value;
86  return 0;
87 }
88 
89 void Event::Object::setParam(const char *nam,void *val)
90 {
91  for (size_t i=0; i<nparams; ++i)
92  if (!strcmp(params[i].name,nam))
93  {
94  params[i].value = val;
95  return;
96  }
97 
98  if (nparams >= MAX_PARAMS)
99  return;
100 
101  params[nparams].name = nam;
102  params[nparams].value = val;
103  ++nparams;
104 }
105 
107 
109 
111 {
112  Mutex::Locker lock(&mutex);
113 
114  for (std::list<Handler *>::iterator i = handlerList.begin(),end = handlerList.end(); i != end; ++i)
115  (*i)->receiveEvent(event);
116 }
117 
119 {
120  for (RT::List<RTHandler>::iterator i = rthandlerList.begin(),end = rthandlerList.end(); i != end; ++i)
121  i->receiveEventRT(event);
122 }
123 
125 {
126  Mutex::Locker lock(&mutex);
127  handlerList.insert(handlerList.end(),handler);
128 }
129 
131 {
132  Mutex::Locker lock(&mutex);
133  handlerList.remove(handler);
134 }
135 
137 {
138  rthandlerList.insert(rthandlerList.end(),*handler);
139 }
140 
142 {
143  rthandlerList.remove(*handler);
144 }
145 
146 static Mutex mutex;
148 
150 {
151  if (instance)
152  return instance;
153 
154  /*************************************************************************
155  * Seems like alot of hoops to jump through, but static allocation isn't *
156  * thread-safe. So effort must be taken to ensure mutual exclusion. *
157  *************************************************************************/
158 
159  Mutex::Locker lock(&::mutex);
160  if (!instance)
161  {
162  static Manager manager;
163  instance = &manager;
164  }
165 
166  return instance;
167 }
void registerHandler(Handler *)
Definition: event.cpp:124
const char * RT_PERIOD_EVENT
Definition: event.cpp:24
Manager(void)
Definition: event.cpp:106
const char * OPEN_FILE_EVENT
Definition: event.cpp:40
void setParam(const char *, void *)
Definition: event.cpp:89
const char * IO_LINK_REMOVE_EVENT
Definition: event.cpp:34
void * getParam(const char *) const
Definition: event.cpp:81
~Manager(void)
Definition: event.cpp:108
const char * STOP_GENICAM_RECORDING_EVENT
Definition: event.cpp:47
const char * SETTINGS_OBJECT_INSERT_EVENT
Definition: event.cpp:38
void postEvent(const Object *event)
Definition: event.cpp:110
const char * START_GENICAM_RECORDING_EVENT
Definition: event.cpp:45
virtual void receiveEventRT(const Object *event)
Definition: event.cpp:72
~Object(void)
Definition: event.cpp:79
const char * GENICAM_SNAPSHOT_EVENT
Definition: event.cpp:48
const char * RT_PREPERIOD_EVENT
Definition: event.cpp:25
Definition: mutex.h:28
virtual void receiveEvent(const Object *event)
Definition: event.cpp:60
const char * IO_BLOCK_REMOVE_EVENT
Definition: event.cpp:32
const char * START_RECORDING_EVENT
Definition: event.cpp:41
const char * THRESHOLD_CROSSING_EVENT
Definition: event.cpp:44
void unregisterRTHandler(RTHandler *)
Definition: event.cpp:141
static Manager * getInstance(void)
Definition: event.cpp:149
virtual ~Handler(void)
Definition: event.cpp:55
const char * STOP_RECORDING_EVENT
Definition: event.cpp:42
const char * PLUGIN_INSERT_EVENT
Definition: event.cpp:36
const char * IO_LINK_INSERT_EVENT
Definition: event.cpp:33
const char * PLUGIN_REMOVE_EVENT
Definition: event.cpp:37
const char * RT_THREAD_REMOVE_EVENT
Definition: event.cpp:28
static Manager * instance
Definition: event.h:214
const char * WORKSPACE_PARAMETER_CHANGE_EVENT
Definition: event.cpp:35
const char * RT_DEVICE_INSERT_EVENT
Definition: event.cpp:29
RTHandler(void)
Definition: event.cpp:62
const char * ASYNC_DATA_EVENT
Definition: event.cpp:43
const char * SETTINGS_OBJECT_REMOVE_EVENT
Definition: event.cpp:39
struct Event::Object::@3 params[MAX_PARAMS]
const char * RT_POSTPERIOD_EVENT
Definition: event.cpp:26
const char * IO_BLOCK_INSERT_EVENT
Definition: event.cpp:31
virtual ~RTHandler(void)
Definition: event.cpp:67
const char * RT_DEVICE_REMOVE_EVENT
Definition: event.cpp:30
void registerRTHandler(RTHandler *)
Definition: event.cpp:136
void unregisterHandler(Handler *)
Definition: event.cpp:130
void postEventRT(const Object *event)
Definition: event.cpp:118
Handler(void)
Definition: event.cpp:50
const char * PAUSE_GENICAM_RECORDING_EVENT
Definition: event.cpp:46
const char * RT_THREAD_INSERT_EVENT
Definition: event.cpp:27
Object(const char *)
Definition: event.cpp:74