summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_5/ex04.html
blob: 95971be40b3bd560a1a267e335cabd60c5189bec (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
<HTML>
<!-- $Id$ -->
<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 4</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 4</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Reactor.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Event_Handler.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Synch_T.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Thread_Manager.h"</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">WAIT_TIME
1</FONT>
<BR><FONT COLOR="#000099">#define</FONT><FONT COLOR="#663366"> SLEEP_TIME
2</FONT>

<P>class My_Handler: public ACE_Event_Handler{
<BR>&nbsp;public:

<P><FONT COLOR="#FF0000">&nbsp;//Start the event handling process.</FONT>
<BR>&nbsp;My_Handler(){
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,"Event Handler created\n"));
<BR>&nbsp; ACE_Reactor::instance()->max_notify_iterations(5);
<BR>&nbsp; return 0;
<BR>&nbsp; }

<P><FONT COLOR="#FF0000">//Perform the notifications i.e., notify the reactor
10 times</FONT>
<BR>&nbsp;void perform_notifications(){
<BR>&nbsp; for(int i=0;i&lt;10;i++)
<BR>&nbsp;&nbsp; ACE_Reactor::instance()->
<BR>&nbsp;&nbsp;&nbsp;&nbsp; notify(this,ACE_Event_Handler::READ_MASK);
<BR>&nbsp; }
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//The actual handler which in this case
will handle the notifications</FONT>
<BR>&nbsp;int handle_input(int){
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,"Got notification # %d\n",no));
<BR>&nbsp; no++;
<BR>&nbsp; return 0;
<BR>&nbsp; }
<BR>private:
<BR>&nbsp;static int no;
<BR>};

<P><FONT COLOR="#FF0000">//Static members</FONT>
<BR>int My_Handler::no=1;
<BR>&nbsp;

<P>int main(int argc, char *argv[]){
<BR><FONT COLOR="#FF0000">&nbsp; //Instantiating the handler</FONT>
<BR>&nbsp;My_Handler handler;
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//The done flag is set to not done yet.</FONT>
<BR>&nbsp;int done=0;

<P>while(1){
<BR>&nbsp;<FONT COLOR="#FF0000"> //After WAIT_TIME the handle_events will
fall through if no events arrive.</FONT>
<BR>&nbsp; ACE_Reactor::instance()->handle_events(ACE_Time_Value(WAIT_TIME));
<BR>&nbsp; if(!done){
<BR>&nbsp;&nbsp; handler.perform_notifications();
<BR>&nbsp;&nbsp; done=1;
<BR>&nbsp;&nbsp; }
<BR>&nbsp; sleep(SLEEP_TIME);
<BR>&nbsp;}
<BR>}

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