General IRI utilities
test_eventserver.cpp
1 // Copyright (C) 2009-2010 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
2 // Author Sergi Hernandez (shernand@iri.upc.edu)
3 // All rights reserved.
4 //
5 // This file is part of iriutils
6 // iriutils is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #include "eventserver.h"
20 #include "eventexceptions.h"
21 #include <iostream>
22 
26 const std::string event1="event_1";
27 
31 const std::string event2="event_2";
32 
36 const std::string event3="invalid_event";
37 
85 int main(int argc, char *argv[])
86 {
87  CEventServer *event_server;
88  std::list<std::string> events;
89  int event_id;
90 
91  event_server=CEventServer::instance();
92  event_server->create_event(event1);
93  event_server->create_event(event2);
94 
95  events.push_back(event1);
96  events.push_back(event2);
97 
98  std::cout << "Activate event 1" << std::endl;
99  event_server->set_event(event1);
100  std::cout << "Activate event 2" << std::endl;
101  event_server->set_event(event2);
102  if (event_server->event_is_set(event1))
103  {
104  std::cout << "Reset event 1" << std::endl;
105  event_server->reset_event(event1);
106  }
107  std::cout << "Wait for the first event to get active ... " << std::endl;
108  event_id=event_server->wait_first(events);
109  std::cout << event_id << std::endl;
110  if (event_server->event_is_set(event1))
111  std::cout << "event 1 is set" << std::endl;
112  else
113  std::cout << "event 1 is not set" << std::endl;
114  if (event_server->event_is_set(event2))
115  std::cout << "event 2 is set" << std::endl;
116  else
117  std::cout << "event 2 is not set" << std::endl;
118  std::cout << "Activate event 1" << std::endl;
119  event_server->set_event(event1);
120  std::cout << "Activate event 2" << std::endl;
121  event_server->set_event(event2);
122  event_server->wait_all(events);
123  std::cout << "All events were active" << std::endl;
124  if (event_server->event_is_set(event1))
125  std::cout << "event 1 is set" << std::endl;
126  else
127  std::cout << "event 1 is not set" << std::endl;
128  if (event_server->event_is_set(event2))
129  std::cout << "event 2 is set" << std::endl;
130  else
131  std::cout << "event 2 is not set" << std::endl;
132  try{
133  event_server->wait_all(events,1000);
134  }catch(CEventTimeoutException &e){
135  std::cout << e.what() << std::endl;
136  }catch(CException& e){
137  std::cout << e.what() << std::endl;
138  return 0;
139  }
140  event_server->delete_event(event1);
141  event_server->delete_event(event2);
142  try{
143  event_server->delete_event(event3);
144  }catch(CEventServerException &e){
145  std::cout << e.what() << std::endl;
146  }
147  delete event_server;
148 }
void create_event(const std::string &event_id)
Function to create a new event.
Definition: eventserver.cpp:73
Global event server.
Definition: eventserver.h:57
void wait_all(std::list< std::string > events, int timeout=-1)
Function to wait for all the events is a set.
virtual const std::string & what(void)
Function to get the error message.
Definition: exceptions.cpp:33
bool event_is_set(const std::string &event_id)
Function to query if the event is active or not.
Event timeout exception class.
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.
void delete_event(const std::string &event_id)
Function to delete an existing event.
Definition: eventserver.cpp:93
Generic exception.
Definition: exceptions.h:76
void set_event(const std::string &event_id)
Function to activate an event.
static CEventServer * instance(void)
Function to get a reference to the unique instance.
Definition: eventserver.cpp:64
void reset_event(const std::string &event_id)
Function to deactivate an event.