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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
//=============================================================================
/**
* @file Dev_Poll_Reactor_Test.cpp
*
* This test verifies that the Dev_Poll_Reactor is functioning
* properly, and demonstrates how "speculative reads" can be
* performed. "Speculative reads" are necessary when using event
* demultiplexing mechanisms that use a "state change" interface.
* Similarly, "speculative writes" may be necessary, i.e. keep
* writing until the connection is flow controlled. An example of a
* demuxing mechanism with such an interface is Linux's `/dev/epoll'
* character device. Mechansims with state change interfaces are
* also said to be "edge triggered," versus "level triggered"
* mechanisms such as select().
*
* @author Ossama Othman <ossama@dre.vanderbilt.edu>
*/
//=============================================================================
#include "test_config.h"
#if defined (ACE_HAS_DEV_POLL) || defined (ACE_HAS_EVENT_POLL)
#include "ace/OS_NS_signal.h"
#include "ace/Reactor.h"
#include "ace/Dev_Poll_Reactor.h"
#include "ace/Acceptor.h"
#include "ace/Connector.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Stream.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_netdb.h"
typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> SVC_HANDLER;
// ----------------------------------------------------
class Client : public SVC_HANDLER
{
public:
Client (void);
//FUZZ: disable check_for_lack_ACE_OS
virtual int open (void * = 0);
//FUZZ: enable check_for_lack_ACE_OS
virtual int handle_output (ACE_HANDLE handle);
virtual int handle_timeout (const ACE_Time_Value ¤t_time,
const void *act);
virtual int handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask mask);
private:
unsigned int call_count_;
};
class Server : public SVC_HANDLER
{
public:
Server (void);
virtual int handle_input (ACE_HANDLE handle);
virtual int handle_timeout (const ACE_Time_Value ¤t_time,
const void *act);
virtual int handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask mask);
private:
unsigned int call_count_;
};
// ----------------------------------------------------
Client::Client (void)
: call_count_ (0)
{
}
int
Client::open (void *)
{
// ACE_TEST_ASSERT (this->reactor () != 0);
if (this->reactor ()
&& this->reactor ()->register_handler (
this,
ACE_Event_Handler::WRITE_MASK) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("unable to register client handler")),
-1);
return 0;
}
int
Client::handle_output (ACE_HANDLE)
{
for (int i = 1; i <= 5; ++i)
{
char buffer[BUFSIZ] = { 0 };
ACE_OS::snprintf (buffer, BUFSIZ, "test message %d.\n", i);
ssize_t bytes_sent =
this->peer ().send (buffer, ACE_OS::strlen (buffer));
if (bytes_sent == -1)
{
if (errno == EWOULDBLOCK)
return 0; // Flow control kicked in.
else if (errno == EPIPE || errno == ECONNRESET)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client::handle_output; server ")
ACE_TEXT ("closed handle %d\n"),
this->peer ().get_handle ()));
return -1;
}
else
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Client::handle_output")),
-1);
}
else if (bytes_sent == 0)
return -1;
else
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%t) Sent %s"), buffer));
}
return 0;
}
int
Client::handle_timeout (const ACE_Time_Value &, const void *)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%t) Expected client timeout occurred at: %T\n")));
this->call_count_++;
int status = this->handle_output (this->get_handle ());
if (status == -1 || this->call_count_ > 10)
{
if (this->reactor ()->end_reactor_event_loop () == 0)
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%t) Successful client reactor shutdown.\n")));
else
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Failed client reactor shutdown")));
// Force this service handler to be closed in either case.
return -1;
}
return 0;
}
int
Client::handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask mask)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%t) Client Svc_Handler closed ")
ACE_TEXT ("handle <%d> with reactor mask <0x%x>.\n"),
handle,
mask));
// There is no point in running reactor after this client is closed.
if (this->reactor ()->end_reactor_event_loop () == 0)
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%t) Successful client reactor shutdown.\n")));
else
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Failed client reactor shutdown")));
return SVC_HANDLER::handle_close (handle, mask);
}
// ----------------------------------------------------
Server::Server (void)
: call_count_ (0)
{
}
int
Server::handle_input (ACE_HANDLE /* handle */)
{
char buffer[BUFSIZ+1] = { 0 }; // Insure a trailing nul
ssize_t bytes_read = 0;
char * const begin = buffer;
char * const end = buffer + BUFSIZ;
for (char * buf = begin; buf < end; buf += bytes_read)
{
// Keep reading until it is no longer possible to do so.
//
// This is done since the underlying event demultiplexing
// mechanism may have a "state change" interface (as opposed to
// "state monitoring"), in which case a "speculative" read is
// done.
bytes_read = this->peer ().recv (buf, end - buf);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("****** bytes_read = %d\n"),
bytes_read));
if (bytes_read == -1)
{
if (errno == EWOULDBLOCK)
{
// ACE_HEX_DUMP ((LM_DEBUG,
// buf,
// 80,
// "BUFFER CONTENTS"));
if (buf == buffer)
return 0;
else
break;
}
else
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Server::handle_input")),
-1);
}
else if (bytes_read == 0)
return -1;
}
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%t) Message received: %s\n"), buffer));
return 0;
}
int
Server::handle_timeout (const ACE_Time_Value &,
const void *)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%t) Expected server timeout occurred at: %T\n")));
// if (this->call_count_ == 0
// && this->handle_input (this->get_handle ()) != 0
// && errno != EWOULDBLOCK)
// return -1;
// ACE_DEBUG ((LM_INFO,
// "SERVER HANDLE = %d\n",
// this->get_handle ()));
this->call_count_++;
if (this->call_count_ > 10)
{
if (this->reactor ()->end_reactor_event_loop () == 0)
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%t) Successful server reactor shutdown.\n")));
else
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Failed server reactor shutdown")));
// Force this service handler to be closed in either case.
return -1;
}
return 0;
}
int
Server::handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask mask)
{
if (this->call_count_ > 4)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%t) Server Svc_Handler closing ")
ACE_TEXT ("handle <%d,%d> with reactor mask <0x%x>.\n"),
handle,
this->get_handle (),
mask));
}
return SVC_HANDLER::handle_close (handle, mask);
}
// ----------------------------------------------------
typedef ACE_Acceptor<Server, ACE_SOCK_ACCEPTOR> ACCEPTOR;
typedef ACE_Connector<Client, ACE_SOCK_CONNECTOR> CONNECTOR;
// ----------------------------------------------------
class TestAcceptor : public ACCEPTOR
{
public:
virtual int accept_svc_handler (Server * handler)
{
int result = this->ACCEPTOR::accept_svc_handler (handler);
if (result != 0)
{
if (errno != EWOULDBLOCK)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Unable to accept connection")));
return result;
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Accepted connection. ")
ACE_TEXT ("Stream handle: <%d>\n"),
handler->get_handle ()));
// if (handler->handle_input (handler->get_handle ()) == -1
// && errno != EWOULDBLOCK)
// return -1;
// #if 0
ACE_Time_Value delay (2, 0);
ACE_Time_Value restart (2, 0);
if (handler->reactor ()->schedule_timer (handler,
0,
delay,
restart) == -1)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Unable to schedule server side ")
ACE_TEXT ("timer in ACE_Dev_Poll_Reactor")),
-1);
}
// #endif /* 0 */
return result;
}
};
// ----------------------------------------------------
class TestConnector : public CONNECTOR
{
public:
virtual int connect_svc_handler (
CONNECTOR::handler_type *& handler,
const CONNECTOR::addr_type &remote_addr,
ACE_Time_Value *timeout,
const CONNECTOR::addr_type &local_addr,
int reuse_addr,
int flags,
int perms)
{
const int result = this->CONNECTOR::connect_svc_handler (handler,
remote_addr,
timeout,
local_addr,
reuse_addr,
flags,
perms);
if (result != 0)
return result;
ACE_TCHAR hostname[MAXHOSTNAMELEN];
if (remote_addr.get_host_name (hostname,
sizeof (hostname)) != 0)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Unable to retrieve hostname")),
-1);
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Connected to <%s:%d>.\n"),
hostname,
(int) remote_addr.get_port_number ()));
// #if 0
ACE_Time_Value delay (4, 0);
ACE_Time_Value restart (3, 0);
if (handler->reactor ()->schedule_timer (handler,
0,
delay,
restart) == -1)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Unable to schedule client side ")
ACE_TEXT ("timer in ACE_Dev_Poll_Reactor")),
-1);
}
// #endif /* 0 */
return result;
}
virtual int connect_svc_handler (
CONNECTOR::handler_type *& handler,
CONNECTOR::handler_type *& sh_copy,
const CONNECTOR::addr_type &remote_addr,
ACE_Time_Value *timeout,
const CONNECTOR::addr_type &local_addr,
int reuse_addr,
int flags,
int perms) {
sh_copy = handler;
return this->connect_svc_handler (handler, remote_addr, timeout,
local_addr, reuse_addr, flags,
perms);
}
};
// ----------------------------------------------------
static int
disable_signal (int sigmin, int sigmax)
{
#if !defined (ACE_LACKS_UNIX_SIGNALS)
sigset_t signal_set;
if (ACE_OS::sigemptyset (&signal_set) == - 1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Error: (%P|%t):%p\n"),
ACE_TEXT ("sigemptyset failed")));
for (int i = sigmin; i <= sigmax; i++)
ACE_OS::sigaddset (&signal_set, i);
// Put the <signal_set>.
# if defined (ACE_LACKS_PTHREAD_THR_SIGSETMASK)
// In multi-threaded application this is not POSIX compliant
// but let's leave it just in case.
if (ACE_OS::sigprocmask (SIG_BLOCK, &signal_set, 0) != 0)
# else
if (ACE_OS::thr_sigsetmask (SIG_BLOCK, &signal_set, 0) != 0)
# endif /* ACE_LACKS_PTHREAD_THR_SIGSETMASK */
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Error: (%P|%t): %p\n"),
ACE_TEXT ("SIG_BLOCK failed")),
-1);
#else
ACE_UNUSED_ARG (sigmin);
ACE_UNUSED_ARG (sigmax);
#endif /* ACE_LACKS_UNIX_SIGNALS */
return 0;
}
// ----------------------------------------------------
ACE_THR_FUNC_RETURN
server_worker (void *p)
{
disable_signal (SIGPIPE, SIGPIPE);
const unsigned short port = *(static_cast<unsigned short *> (p));
ACE_INET_Addr addr;
if (addr.set (port, INADDR_LOOPBACK) != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("server_worker - ACE_INET_Addr::set")));
return (void *) -1;
}
ACE_Dev_Poll_Reactor dp_reactor;
dp_reactor.restart (1); // Restart on EINTR
ACE_Reactor reactor (&dp_reactor);
TestAcceptor server;
int flags = 0;
ACE_SET_BITS (flags, ACE_NONBLOCK); // Enable non-blocking in the
// Svc_Handlers.
if (server.open (addr, &reactor, flags) != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Unable to open server service handler")));
return (void *) -1;
}
if (reactor.run_reactor_event_loop () != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Error when running server ")
ACE_TEXT ("reactor event loop")));
return (void *) -1;
}
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%t) Reactor event loop finished ")
ACE_TEXT ("successfully.\n")));
return 0;
}
// ----------------------------------------------------
// struct server_arg
// {
// unsigned short port;
// ACE_Condition<ACE_SYNCH_MUTEX> * cv;
// };
// ----------------------------------------------------
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Dev_Poll_Reactor_Test"));
// Make sure we ignore SIGPIPE
disable_signal (SIGPIPE, SIGPIPE);
ACE_Dev_Poll_Reactor dp_reactor;
dp_reactor.restart (1); // Restart on EINTR
ACE_Reactor reactor (&dp_reactor);
TestConnector client;
int flags = 0;
ACE_SET_BITS (flags, ACE_NONBLOCK); // Enable non-blocking in the
// Svc_Handlers.
if (client.open (&reactor, flags) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Unable to open client service handler")),
-1);
// ACE_SYNCH_MUTEX mutex;
// ACE_Condition<ACE_SYNCH_MUTEX> cv (mutex);
// server_arg arg;
// arg.port = 54678; // Port the server will listen on.
// arg.cv = &cv;
unsigned short port = 54678;
if (ACE_Thread_Manager::instance ()->spawn (server_worker, &port) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Unable to spawn server thread")),
-1);
ACE_OS::sleep (5); // Wait for the listening endpoint to be set up.
ACE_INET_Addr addr;
if (addr.set (port, INADDR_LOOPBACK) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("ACE_INET_Addr::set")),
-1);
Client *client_handler = 0;
if (client.connect (client_handler, addr) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Unable to connect to server")),
-1);
if (reactor.run_reactor_event_loop () != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Error when running client ")
ACE_TEXT ("reactor event loop")),
-1);
if (ACE_Thread_Manager::instance ()->wait () != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Error waiting for threads to complete")),
-1);
ACE_END_TEST;
return 0;
}
#else
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Dev_Poll_Reactor_Test"));
ACE_ERROR ((LM_INFO,
ACE_TEXT ("Dev Poll and Event Poll are not supported ")
ACE_TEXT ("on this platform\n")));
ACE_END_TEST;
return 0;
}
#endif /* ACE_HAS_DEV_POLL || ACE_HAS_EVENT_POLL */
|