blob: 07dabd17a974d54c02e23a90274a7fd4af9c9edc (
plain)
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
|
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Ambreen Ilyas">
<META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (X11; I; SunOS 5.5.1 sun4u) [Netscape]">
<TITLE>Example 3</TITLE>
</HEAD>
<BODY>
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers
Guide.</FONT>
<BR><FONT COLOR="#CC0000">//// Chapter: "The Reactor" (Event
Management)</FONT>
<BR><FONT COLOR="#CC0000">//// For details please see the guide at</FONT>
<BR><FONT COLOR="#CC0000">//// http://www.cs.wustl.edu/~schmidt/ACE.html</FONT>
<BR><FONT COLOR="#CC0000">//// AUTHOR: Umar Syyid (usyyid@hns.com)</FONT>
<BR><FONT COLOR="#CC0000">//// and Ambreen Ilyas (ambreen@bitsmart.com)</FONT>
<BR><FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
<P><FONT COLOR="#FF0000">//Example 3</FONT>
<BR><FONT COLOR="#000099">#include</FONT> "<FONT COLOR="#006600">ace/Timer_Queue.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> "<FONT COLOR="#006600">ace/Reactor.h"</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">NUMBER_TIMERS
10</FONT>
<P>static int done = 0;
<BR>static int count = 0;
<P>class Time_Handler : public ACE_Event_Handler
<BR>{
<BR>public:
<BR> <FONT COLOR="#FF0000">//Method which is called back by the Reactor
when timeout occurs.</FONT>
<BR> virtual int handle_timeout (const ACE_Time_Value &tv,
<BR> const void *arg){
<BR> long current_count = long (arg);
<BR> ACE_ASSERT (current_count == count);
<BR> ACE_DEBUG ((LM_DEBUG, "%d: Timer #%d timed out at %d!\n",
<BR> count, current_count, tv.sec()));
<BR>
<P><FONT COLOR="#FF0000">//Increment count</FONT>
<BR> count ++;
<P><FONT COLOR="#FF0000">//Make sure assertion doesnt fail for missing
5th timer.</FONT>
<BR> if (count ==5)
<BR> count++;
<BR>
<BR><FONT COLOR="#FF0000"> //If all timers done then set done flag</FONT>
<BR> if (current_count == NUMBER_TIMERS - 1)
<BR> done = 1;
<BR><FONT COLOR="#FF0000"> //Keep yourself registered with the Reactor.</FONT>
<BR> return 0;
<BR> }
<BR>};
<P>int
<BR>main (int, char *[])
<BR>{
<BR> ACE_Reactor reactor;
<BR> Time_Handler *th=new Time_Handler;
<BR> int timer_id[NUMBER_TIMERS];
<BR> int i;
<P> for (i = 0; i < NUMBER_TIMERS; i++)
<BR> timer_id[i] = reactor.schedule_timer (th,
<BR> (const void *) i, <FONT COLOR="#FF0000">// argument sent
to handle_timeout()</FONT>
<BR> ACE_Time_Value (2 * i + 1));<FONT COLOR="#FF0000"> //set
timer to go off with delay</FONT>
<P><FONT COLOR="#CC0000"> //Cancel the fifth timer before it goes
off</FONT>
<BR> reactor.cancel_timer(timer_id[5]);<FONT COLOR="#FF0000">//Timer
ID of timer to be removed</FONT>
<P> while (!done)
<BR> reactor.handle_events ();
<P> return 0;
<BR>}
<P> <A HREF="ex04.html">Next Example</A>
</BODY>
</HTML>
|