summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_5/ex03.html
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">////&nbsp; Chapter:&nbsp; "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">////&nbsp; 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>&nbsp;<FONT COLOR="#FF0000">//Method which is called back by the Reactor
when timeout occurs.</FONT>
<BR>&nbsp;virtual int handle_timeout (const ACE_Time_Value &amp;tv,
<BR>&nbsp;const void *arg){
<BR>&nbsp; long current_count = long (arg);
<BR>&nbsp; ACE_ASSERT (current_count == count);
<BR>&nbsp; ACE_DEBUG ((LM_DEBUG, "%d: Timer #%d timed out at %d!\n",
<BR>&nbsp;&nbsp;&nbsp;&nbsp; count, current_count, tv.sec()));
<BR>&nbsp;

<P><FONT COLOR="#FF0000">//Increment count</FONT>
<BR>&nbsp; count ++;

<P><FONT COLOR="#FF0000">//Make sure assertion doesnt fail for missing
5th timer.</FONT>
<BR>&nbsp; if (count ==5)
<BR>&nbsp;&nbsp; count++;
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp; //If all timers done then set done flag</FONT>
<BR>&nbsp; if (current_count == NUMBER_TIMERS - 1)
<BR>&nbsp;&nbsp; done = 1;
<BR><FONT COLOR="#FF0000">&nbsp; //Keep yourself registered with the Reactor.</FONT>
<BR>&nbsp; return 0;
<BR>&nbsp; }
<BR>};

<P>int
<BR>main (int, char *[])
<BR>{
<BR>&nbsp;ACE_Reactor reactor;
<BR>&nbsp;Time_Handler *th=new Time_Handler;
<BR>&nbsp;int timer_id[NUMBER_TIMERS];
<BR>&nbsp;int i;

<P>&nbsp;for (i = 0; i &lt; NUMBER_TIMERS; i++)
<BR>&nbsp; timer_id[i] = reactor.schedule_timer (th,
<BR>&nbsp;&nbsp; (const void *) i, <FONT COLOR="#FF0000">// argument sent
to handle_timeout()</FONT>
<BR>&nbsp;&nbsp; ACE_Time_Value (2 * i + 1));<FONT COLOR="#FF0000"> //set
timer to go off with delay</FONT>

<P><FONT COLOR="#CC0000">&nbsp;//Cancel the fifth timer before it goes
off</FONT>
<BR>&nbsp;reactor.cancel_timer(timer_id[5]);<FONT COLOR="#FF0000">//Timer
ID of timer to be removed</FONT>

<P>&nbsp;while (!done)
<BR>&nbsp; reactor.handle_events ();

<P>&nbsp;return 0;
<BR>}

<P>&nbsp;<A HREF="ex04.html">Next Example</A>
</BODY>
</HTML>