General IRI utilities
test_events.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 "eventexceptions.h"
20 #include "event.h"
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <iostream>
24 
58 int main(int argc, char *argv[])
59 {
60  std::string event_id="";
61  int num=0;
62  CEvent Event("test_event1");
63 
64  event_id=Event.get_id();
65  std::cout << "Event identifier: " << event_id << std::endl;
66  // activate the event once
67  Event.set();
68  num=Event.get_num_activations();
69  // the expected value is 1
70  std::cout << "Number of instances of the event: " << num << std::endl;
71  // wait for it to be signaled (this function returns immediatelly)
72  Event.wait(1000000);
73  // activate the event again
74  Event.set();
75  num=Event.get_num_activations();
76  std::cout << "Number of instances of the event: " << num << std::endl;
77  // reset the event
78  Event.reset();
79  // wait for the event to be signales. This function will get blocked until
80  // the timeout expires.
81  try {
82  Event.wait(1000000);
83  }catch(CEventTimeoutException& e){
84  std::cout << e.what() << std::endl;
85  }catch(CException& e){
86  std::cout << e.what() << std::endl;
87  return 0;
88  }
89  // activate the event several times
90  Event.set();
91  Event.set();
92  Event.set();
93  num=Event.get_num_activations();
94  // the expected value is 3
95  std::cout << "Number of instances of the event: " << num << std::endl;
96  Event.reset();
97  num=Event.get_num_activations();
98  // the expected value is 2
99  std::cout << "Number of instances of the event: " << num << std::endl;
100  Event.wait(-1);
101  num=Event.get_num_activations();
102  // the expected value is 1
103  std::cout << "Number of instances of the event: " << num << std::endl;
104  if(Event.is_set())
105  {
106  Event.wait(-1);
107  }
108  num=Event.get_num_activations();
109  // the expected value is 0
110  std::cout << "Number of instances if the event: " << num << std::endl;
111  if(Event.is_set())
112  {
113  Event.wait(-1);
114  }
115 
116  return 0;
117 }
Implementation of a logical event.
Definition: event.h:65
virtual const std::string & what(void)
Function to get the error message.
Definition: exceptions.cpp:33
Event timeout exception class.
Generic exception.
Definition: exceptions.h:76