1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
// $Id$
#include "ace/Get_Opt.h"
#include "ace/Task.h"
#include "ace/Array.h"
#include "ace/Reactor.h"
#include "ace/INet/HTTP_URL.h"
#include "ace/INet/HTTP_ClientRequestHandler.h"
#include "ace/INet/String_IOStream.h"
#include "ace/Truncate.h"
#include <iostream>
int n_threads = 3;
#if defined (ACE_HAS_THREADS)
class Get_Task : public ACE_Task<ACE_MT_SYNCH>
{
public:
Get_Task (ACE_Thread_Manager *thr_mgr,
int n_threads);
virtual int svc (void);
const ACE_Array<ACE_CString>& results ();
private:
void shutdown ();
int n_threads_;
ACE_Array<ACE_CString> results_;
ACE_SYNCH_MUTEX lock_;
};
Get_Task::Get_Task (ACE_Thread_Manager *thr_mgr,
int n_threads)
: ACE_Task<ACE_MT_SYNCH> (thr_mgr),
n_threads_ (n_threads)
{
// Create worker threads.
if (this->activate (THR_NEW_LWP, n_threads_) == -1)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("activate failed")));
else
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) started %d threads\n"), n_threads_));
}
void Get_Task::shutdown ()
{
ACE_GUARD (ACE_SYNCH_MUTEX,
guard_,
lock_);
--n_threads_;
if (n_threads_ <= 0)
{
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) ending event loop\n")));
ACE_Reactor::instance ()->end_event_loop ();
}
}
int Get_Task::svc ()
{
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) task started\n")));
ACE::HTTP::URL http_url;
if (http_url.parse ("http://www.theaceorb.nl"))
{
ACE::HTTP::ClientRequestHandler rh;
ACE::INet::URLStream urlin = http_url.open (rh);
if (urlin)
{
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) opened URL %C\n"), http_url.to_string ().c_str ()));
ACE::IOS::CString_OStream sos;
sos << urlin->rdbuf ();
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) result downloaded\n")));
if (!sos.bad ())
{
ACE_GUARD_RETURN (ACE_SYNCH_MUTEX,
guard_,
lock_,
0);
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) storing result\n")));
results_.size (results_.size () + 1);
results_[results_.size ()-1] = sos.str ();
}
}
else
ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) failed to open URL %C\n"), http_url.to_string ().c_str ()));
shutdown ();
}
else
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) failed to parse URL : result = %C\n"),
http_url.to_string ().c_str ()));
}
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) task finished\n")));
return 0;
}
const ACE_Array<ACE_CString>& Get_Task::results ()
{
return results_;
}
class Get_MultiTask : public ACE_Task<ACE_MT_SYNCH>
{
public:
Get_MultiTask (ACE_Thread_Manager *thr_mgr);
virtual int svc (void);
int failures ();
private:
const char* get_url ();
void wait_for_all ();
void shutdown ();
int n_threads_;
int n_thread_starts_;
int n_open_urls_;
int n_fails_;
ACE_SYNCH_MUTEX lock_;
ACE_SYNCH_CONDITION signal_;
};
const char* urls_[] = {
"http://www.theaceorb.nl",
"http://www.remedy.nl",
"http://www.google.com"
};
Get_MultiTask::Get_MultiTask (ACE_Thread_Manager *thr_mgr)
: ACE_Task<ACE_MT_SYNCH> (thr_mgr),
n_threads_ (sizeof (urls_)/sizeof (const char*)),
n_thread_starts_ (0),
n_open_urls_ (0),
n_fails_ (n_threads_),
signal_ (lock_)
{
// Create worker threads.
if (this->activate (THR_NEW_LWP, n_threads_) == -1)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("activate failed")));
}
const char* Get_MultiTask::get_url ()
{
ACE_GUARD_RETURN (ACE_SYNCH_MUTEX,
guard_,
lock_,
0);
const char* url = urls_[n_thread_starts_];
++n_thread_starts_;
return url;
}
void Get_MultiTask::wait_for_all ()
{
ACE_GUARD (ACE_SYNCH_MUTEX,
guard_,
lock_);
++n_open_urls_;
if (n_open_urls_ == n_threads_)
signal_.broadcast (); // signal all threads that all urls are opened
else
signal_.wait (); // wait for all urls to be opened
}
void Get_MultiTask::shutdown ()
{
ACE_GUARD (ACE_SYNCH_MUTEX,
guard_,
lock_);
--n_threads_;
if (n_threads_ <= 0)
{
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) ending event loop\n")));
ACE_Reactor::instance ()->end_event_loop ();
}
}
int Get_MultiTask::svc ()
{
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) multitask started\n")));
ACE::HTTP::URL http_url;
const char* url = get_url ();
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) parsing URL %C\n"), url));
if (http_url.parse (url))
{
ACE::HTTP::ClientRequestHandler rh;
http_url.open (rh);
if (rh.response_stream ())
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) opened URL %C\n"), http_url.to_string ().c_str ()));
else
ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) failed to open URL %C\n"), http_url.to_string ().c_str ()));
wait_for_all ();
if (rh.response_stream ())
{
ACE::IOS::CString_OStream sos;
sos << rh.response_stream ().rdbuf ();
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) result downloaded\n")));
if (!sos.bad ())
{
ACE_GUARD_RETURN (ACE_SYNCH_MUTEX,
guard_,
lock_,
0);
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) marking result\n")));
--n_fails_;
}
}
shutdown ();
}
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) task finished\n")));
return 0;
}
int Get_MultiTask::failures ()
{
return n_fails_;
}
#endif
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
#if defined (ACE_HAS_THREADS)
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) MGet_Test started\n")));
ACE_Reactor* reactor = ACE_Reactor::instance ();
#if 1
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) starting serialized test\n")));
Get_Task get_task (ACE_Thread_Manager::instance (),
n_threads);
// run event loop until ended by last task thread
reactor->run_event_loop ();
// all threads really ended?
ACE_Thread_Manager::instance ()->wait ();
// All threads should have resulted in the same data...
if (get_task.results ().size () != ACE_Utils::truncate_cast<size_t> (n_threads))
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%d results found; ")
ACE_TEXT ("should be %d\n"),
get_task.results ().size (),
n_threads));
}
else
{
const ACE_Array<ACE_CString>& results = get_task.results ();
for (int i=1;
i < n_threads;
++i)
{
if (results[0] != results[i])
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("result %d does not match other results\n"),
i));
}
}
}
#endif
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) starting parallelized test\n")));
reactor->reset_event_loop ();
Get_MultiTask get_multi_task (ACE_Thread_Manager::instance ());
// run event loop until ended by last task thread
reactor->run_event_loop ();
// all threads really ended?
ACE_Thread_Manager::instance ()->wait ();
// All threads should have resulted in data...
if (get_multi_task.failures () > 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%d failures found"),
get_multi_task.failures ()));
}
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) MGet_Test finished\n")));
#else
ACE_ERROR ((LM_INFO,
ACE_TEXT ("threads not supported on this platform\n")));
#endif /* ACE_HAS_THREADS */
return 0;
}
|