Debian Codegraph @

protolib/examples/eventExample.cpp

raw
1 #include "protokit.h"
2
3 #ifdef UNIX
4 #include <unistd.h> // for sleep() function
5 #endif // UNIX
6
7 /**
8 * @class SimpleThread
9 *
10 * @brief This example illustrates the use of the ProtoEvent class threaded ProtoDispatcher.
11 *
12 * A "SimpleThread" class that uses a threaded ProtoDispatcher is defined that contains
13 * a ProtoTimer that can be started that fires with a one-second interval and also
14 * contains a ProtoEvent that can be signaled. The main() routine also has a
15 * ProtEvent and uses its "Wait()" method to be signaled by the ProtoDispatcher. So,
16 * this illustrates "procedural, "stand-alone" use of the ProtoEvent along with the
17 * asynchronous I/O, notification-based use within the SimpleThread()
18 *
19 * When using a threaded ProtoDispatcher, make sure to call SuspendThread()
20 * before manipulating dispatcher objects. (See SimpleThread::StartTimer() and
21 * SimpleThread::StopTimer() as an example)
22 */
23 class SimpleThread
24 {
25 public:
26 SimpleThread();
27 ~SimpleThread();
28
29 bool Start(ProtoEvent* parentEvent);
30 void Stop();
31
32 bool StartTimer()
33 {
34 bool result = dispatcher.SuspendThread();
35 if (result)
36 {
37 if (!timer.IsActive()) dispatcher.ActivateTimer(timer);
38 dispatcher.ResumeThread();
39 }
40 return result;
41 }
42 bool StopTimer()
43 {
44 bool result = dispatcher.SuspendThread();
45 {
46 if (timer.IsActive()) timer.Deactivate();
47 dispatcher.ResumeThread();
48 }
49 return result;
50 }
51 bool TimerIsActive()
52 {
53 bool result = dispatcher.SuspendThread();
54 if (result)
55 {
56 result = timer.IsActive();
57 dispatcher.ResumeThread();
58 }
59 return result;
60 }
61
62 bool SetEvent()
63 {
64 bool result = dispatcher.SuspendThread();
65 if (result)
66 {
67 result = event.Set();
68 dispatcher.ResumeThread();
69 }
70 return result;
71 }
72
73 private:
74
75 void OnTimeout(ProtoTimer& theTimer);
76 void OnEvent(ProtoEvent& theEvent);
77
78 ProtoDispatcher dispatcher;
79 ProtoTimer timer;
80 ProtoEvent event;
81 ProtoEvent* parent_event;
82 unsigned int timeout_count; // how many timeouts before we signal parent_event
83 }; // end class SimpleThread
84
85 SimpleThread::SimpleThread()
86 : event(true), parent_event(NULL), timeout_count(15)
87 {
88 timer.SetListener(this, &SimpleThread::OnTimeout);
89 timer.SetInterval(1.0);
90 timer.SetRepeat(-1);
91
92 event.SetNotifier(&dispatcher);
93 event.SetListener(this, &SimpleThread::OnEvent);
94 }
95
96 SimpleThread::~SimpleThread()
97 {
98 Stop();
99 }
100
101 bool SimpleThread::Start(ProtoEvent* parentEvent)
102 {
103 if (!event.Open())
104 {
105 PLOG(PL_ERROR, "eventExample: SimpleThread::Start() event open failure ...\n");
106 return false;
107 }
108 dispatcher.ActivateTimer(timer);
109 if (dispatcher.StartThread())
110 {
111 parent_event = parentEvent;
112 return true;
113 }
114 else
115 {
116 PLOG(PL_ERROR, "eventExample: SimpleThread::Start() thread start failure ...\n");
117 timer.Deactivate();
118 event.Close();
119 return false;
120 }
121 } // end SimpleThread::Start()
122
123 void SimpleThread::Stop()
124 {
125 dispatcher.Stop();
126 if (timer.IsActive()) timer.Deactivate();
127 event.Close();
128 } // end SimpleThread::Stop()
129
130 /**
131 * IMPORTANT NOTE @note Remember this timeout handler
132 * is being called by the dispatcher child thread, _not_
133 * the main thread since we're using ProtoDispatcher::StartThread()
134 * to run the dispatcher
135 */
136 void SimpleThread::OnTimeout(ProtoTimer& theTimer)
137 {
138 TRACE("eventExample: child thread SimpleThread::OnTimeout() timeout_count:%d ...\n", timeout_count);
139 if ((1 == timeout_count) && (NULL != parent_event))
140 {
141 TRACE("eventExample: child thread signaling main thread ...\n");
142 parent_event->Set();
143 }
144 if (0 != timeout_count)
145 timeout_count--;
146 } // end SimpleThread::OnTimeout()
147
148 void SimpleThread::OnEvent(ProtoEvent& theEvent)
149 {
150 TRACE("eventExample: child thread SimpleThread::OnEvent() ...\n");
151 } // end SimpleThread::OnTimeout()
152
153 int main(int argc, char* argv[])
154 {
155 SimpleThread simpleThread;
156 ProtoEvent myEvent(false);
157
158 if (!myEvent.Open())
159 {
160 PLOG(PL_ERROR, "eventExample:myEvent.Open() error:%s\n", GetErrorString());
161 return -1;
162 }
163
164 // This "starts" the simpleThread thread
165 if (!simpleThread.Start(&myEvent))
166 {
167 PLOG(PL_ERROR, "eventExample: SimpleThread::Start() error\n");
168 return -1;
169 }
170
171 int count = 2;
172 while (count--)
173 {
174 #ifdef WIN32
175 Sleep(3000);
176 #else
177 sleep(3);
178 #endif // if/else WIN32
179 TRACE("eventExample: main thread countdown = %d\n",count);
180 if (0 == count)
181 {
182 TRACE("eventExample: main thread waiting for event signal from child ...\n");
183 myEvent.Wait();
184 TRACE("eventExample: main thread got event signal from child ...\n");
185 simpleThread.Stop();
186 }
187 if (1 == count)
188 {
189 TRACE("eventExample: main thread signaling child thread ...\n");
190 simpleThread.SetEvent();
191 }
192 }
193 TRACE("eventExample: main thread simpleThread stopped.\n");
194 TRACE("eventExample: main thread Done.\n");
195 return 0;
196
197 } // end main()