20 #include <sys/select.h>
22 #include "eventserver.h"
23 #include "eventexceptions.h"
38 return *this->pinstance;
44 std::list<CEvent>::iterator it;
46 if(event_id.size()==0)
53 for(it=this->event_list.begin();it!=this->event_list.end();it++)
59 return (std::list<CEvent>::iterator)NULL;
61 return (std::list<CEvent>::iterator)NULL;
66 if (CEventServer::pinstance == NULL)
70 return CEventServer::pinstance;
76 std::list<CEvent>::iterator old_event;
78 this->access_events.
enter();
79 if((old_event=this->
search_event(event_id))==(std::list<CEvent>::iterator)NULL)
81 new_event=
new CEvent(event_id);
82 this->event_list.push_back(*new_event);
83 this->access_events.
exit();
87 this->access_events.
exit();
95 std::list<CEvent>::iterator old_event;
97 this->access_events.
enter();
98 if((old_event=this->
search_event(event_id))==(std::list<CEvent>::iterator)NULL)
100 this->access_events.
exit();
106 this->event_list.erase(old_event);
107 this->access_events.
exit();
113 std::list<CEvent>::iterator event;
115 this->access_events.
enter();
116 if((event=this->
search_event(event_id))==(std::list<CEvent>::iterator)NULL)
118 this->access_events.
exit();
124 this->access_events.
exit();
131 std::list<CEvent>::iterator event;
133 this->access_events.
enter();
134 if((event=this->
search_event(event_id))==(std::list<CEvent>::iterator)NULL)
136 this->access_events.
exit();
142 this->access_events.
exit();
149 std::list<CEvent>::iterator event;
151 this->access_events.
enter();
152 if((event=this->
search_event(event_id))==(std::list<CEvent>::iterator)NULL)
154 this->access_events.
exit();
160 this->access_events.
exit();
161 return event->is_set();
169 this->access_events.
enter();
170 num=this->event_list.size();
171 this->access_events.
exit();
178 std::list<CEvent>::iterator event;
180 this->access_events.
enter();
181 if((event=this->
search_event(event_id))==(std::list<CEvent>::iterator)NULL)
183 this->access_events.
exit();
189 this->access_events.
exit();
190 return event->get_num_activations();
196 std::list<std::string>::iterator it;
197 std::list<CEvent>::iterator event;
198 std::string event_list=
"";
199 int error,max_fd=0,pos=-1;
207 time_out.
set(timeout);
210 this->access_events.
enter();
214 for(it=events.begin();it!=events.end();it++)
216 if ((event=this->
search_event(*it)) == (std::list<CEvent>::iterator)NULL)
218 this->access_events.
exit();
224 FD_SET(event->get_fd(),&wait_set);
225 if(event->get_fd()>max_fd)
226 max_fd=event->get_fd();
227 if(it!=events.begin())
233 this->access_events.
exit();
235 error=select(max_fd+1,&wait_set,NULL,NULL,&time);
237 error=select(max_fd+1,&wait_set,NULL,NULL,NULL);
243 throw CEventServerException(_HERE_,
"Unexpected error while waiting the activation of one the events",event_list);
253 this->access_events.
enter();
254 for(it=events.begin();it!=events.end();it++)
257 if((event=this->
search_event(*it))==(std::list<CEvent>::iterator)NULL)
259 this->access_events.
exit();
265 if(FD_ISSET(event->get_fd(),&wait_set))
270 this->access_events.
exit();
278 this->access_events.
exit();
285 std::list<std::string>::iterator it;
286 std::list<CEvent>::iterator event;
287 std::string event_list;
296 time_out.
set(timeout);
299 this->access_events.
enter();
304 for(it=events.begin();it!=events.end();it++)
306 if((event=this->
search_event(*it))==(std::list<CEvent>::iterator)NULL)
308 this->access_events.
exit();
314 FD_SET(event->get_fd(),&wait_set);
315 if(event->get_fd()>max_fd)
316 max_fd=event->get_fd();
317 if(it!=events.begin())
322 this->access_events.
exit();
324 error=select(max_fd+1,&wait_set,NULL,NULL,&time);
326 error=select(max_fd+1,&wait_set,NULL,NULL,NULL);
332 throw CEventServerException(_HERE_,
"Unexpected error while waiting the activation of one the events",event_list);
342 this->access_events.
enter();
343 for(it=events.begin();it!=events.end();it++)
345 if((event=this->
search_event(*it))==(std::list<CEvent>::iterator)NULL)
347 this->access_events.
exit();
353 if(FD_ISSET(event->get_fd(),&wait_set))
372 this->access_events.
exit();
Implementation of a logical event.
void create_event(const std::string &event_id)
Function to create a new event.
int get_num_activations(const std::string &event_id)
Function to get the number of activations of a single event.
void wait_all(std::list< std::string > events, int timeout=-1)
Function to wait for all the events is a set.
bool event_is_set(const std::string &event_id)
Function to query if the event is active or not.
Event timeout exception class.
CEventServer & operator=(const CEventServer &object)
assign operator overloading
std::list< CEvent >::iterator search_event(const std::string &event_id)
Function to search for an specific event.
int get_num_events(void)
function to get the number of event
Event server exception class.
int wait_first(std::list< std::string > events, int timeout=-1)
Function to wait for the first event in a set.
timeval getTimeInTimeval(void)
Get time in a timeval strcuture.
Implementation of a time class and operations.
void delete_event(const std::string &event_id)
Function to delete an existing event.
void exit(void)
function to release the critical section
void enter(void)
function to request access to the critical section
void set_event(const std::string &event_id)
Function to activate an event.
void set(double milliseconds=-1.0)
Sets the internal time.
CEventServer()
Default constructor.
static CEventServer * instance(void)
Function to get a reference to the unique instance.
void reset_event(const std::string &event_id)
Function to deactivate an event.