General IRI utilities
test_bothservers.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 "threadserver.h"
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <iostream>
24 #include "log.h"
25 
29 const std::string thread1_id="thread1";
33 const std::string thread2_id="thread2";
34 
38 const std::string event1_id="event1";
42 const std::string event2_id="event2";
46 const std::string event3_id="event3";
47 
56 void *thread1_function(void *param)
57 {
58  std::list<std::string> event_list1;
59  CEventServer *event_server=CEventServer::instance();
60  CLog thread_log("thread1");
61 
62  event_list1.push_back(event1_id);
63  std::cout << "Thread1 - Waiting for event " << event1_id << std::endl;
64  thread_log.log((const std::string)"Thread1 - Waiting for event" + event1_id);
65  event_server->wait_first(event_list1);
66  std::cout << "Thread1 - Event " << event1_id << " received" << std::endl;
67 // thread_log.log((const std::string)"Thread1 - Event " + event1_id + " received");
68 // std::cout << "Thread1 - Sending event " << event2_id << std::endl;
69 // thread_log.log((const std::string)"Thread1 - Sending event" + event2_id);
70 // event_server->set_event(event2_id);
71 // sleep(2);
72 // std::cout << "Thread1 - Sending event " << event3_id << std::endl;
73 // thread_log.log((const std::string)"Thread1 - Sending event" + event3_id);
74 // event_server->set_event(event3_id);
75 // std::cout << "Thread1 - Ending" << std::endl;
76 // thread_log.log((const std::string)"Thread1 - Ending");
77 
78  pthread_exit(NULL);
79 }
80 
89 void *thread2_function(void *param)
90 {
91  std::list<std::string> event_list2;
92  CEventServer *event_server=CEventServer::instance();
93  CLog thread_log("thread2");
94 
95  event_list2.push_back(event1_id);
96  std::cout << "Thread2 - Waiting for event " << event1_id << std::endl;
97  thread_log.log((const std::string)"Thread2 - Waiting for event" + event1_id);
98  event_server->wait_first(event_list2);
99  std::cout << "Thread2 - Event " << event1_id << " received" << std::endl;
100 // event_list2.push_back(event2_id);
101 // event_list2.push_back(event3_id);
102 // std::cout << "Thread2 - Sending event " << event1_id << std::endl;
103 // thread_log.log((const std::string)"Thread2 - Sending event " + event1_id);
104 // event_server->set_event(event1_id);
105 // std::cout << "Thread2 - Waiting for event " << event2_id << std::endl;
106 // thread_log.log((const std::string)"Thread2 - Waiting for event " + event2_id);
107 // event_server->wait_all(event_list2);
108 // std::cout << "Thread2 - Event " << event2_id << " and " << event3_id << " received" << std::endl;
109 // thread_log.log((const std::string)"Thread2 - Event " + event2_id + " and " + event3_id + " received");
110 // std::cout << "Thread2 - Ending" << std::endl;
111 // thread_log.log((const std::string)"Thread2 - Ending");
112 
113  pthread_exit(NULL);
114 }
115 
149 int main(int argc,char *argv[])
150 {
151  CEventServer *event_server=CEventServer::instance();
152  CThreadServer *thread_server=CThreadServer::instance();
153  CLog main_log("main");
154 
155  //thread test
156  main_log.log("Creating events ...");
157  event_server->create_event(event1_id);
158  event_server->create_event(event2_id);
159  event_server->create_event(event3_id);
160 
161  main_log.log((const std::string)"Creating threads ...");
162  thread_server->create_thread(thread1_id);
163  thread_server->create_thread(thread2_id);
164  thread_server->attach_thread(thread1_id,thread1_function,NULL);
165  thread_server->attach_thread(thread2_id,thread2_function,NULL);
166  main_log.log((const std::string)"Starting thread 1 ...");
167  thread_server->start_thread(thread1_id);
168  sleep(3);
169  main_log.log((const std::string)"Starting thread 2 ...");
170  thread_server->start_thread(thread2_id);
171  sleep(3);
172  event_server->set_event(event1_id);
173  sleep(2);
174  event_server->set_event(event1_id);
175  sleep(2);
176  thread_server->end_thread(thread2_id);
177  thread_server->end_thread(thread1_id);
178  std::cout << "End of the program" << std::endl;
179  main_log.log((const std::string)"End of the program");
180 }
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 create_thread(const std::string &thread_id)
Function to create a new thread.
void start_thread(const std::string &thread_id)
Function to start a thread.
int wait_first(std::list< std::string > events, int timeout=-1)
Function to wait for the first event in a set.
void end_thread(const std::string &thread_id)
Function to request the termination of a thread.
Implementation of a log file.
Definition: log.h:50
void attach_thread(const std::string &thread_id, void *(*user_thread_function)(void *param), void *param)
Function to attach a function to a thread.
void set_event(const std::string &event_id)
Function to activate an event.
Global thread server.
Definition: threadserver.h:57
static CThreadServer * instance(void)
Function to get a reference to the unique instance.
static CEventServer * instance(void)
Function to get a reference to the unique instance.
Definition: eventserver.cpp:64