summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_5/ex05.html
blob: 80422683cd6b57280dc6dce3f75a9d013669ea66 (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
87
88
<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 5</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="#CC0000">//Example 5</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>

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

<P><FONT COLOR="#FF0000">//Spawn a separate thread so that it notifies
the reactor</FONT>
<BR>&nbsp;void activate_threads(){
<BR>&nbsp; ACE_Thread_Manager::instance()
<BR>&nbsp;&nbsp;&nbsp;&nbsp; ->spawn((ACE_THR_FUNC)svc_start,(void*)this);
<BR>&nbsp; }
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//Notify the Reactor 10 times.</FONT>
<BR>&nbsp;void svc(){
<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;&nbsp; }

<P><FONT COLOR="#FF0000">//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; }

<P>&nbsp;<FONT COLOR="#FF0000">//The entry point for the new thread that
is to be created.</FONT>
<BR>&nbsp;static int svc_start(void* arg);
<BR>private:
<BR>&nbsp;static int no;
<BR>};
<BR>&nbsp;

<P><FONT COLOR="#FF0000">//Static members</FONT>
<BR>int My_Handler::no=1;
<BR>int My_Handler::svc_start(void* arg){
<BR>&nbsp;My_Handler *eh= (My_Handler*)arg;
<BR>&nbsp;eh->svc();
<BR>&nbsp;return -1; <FONT COLOR="#FF0000">//de-register from the reactor</FONT>
<BR>&nbsp;}

<P>int main(int argc, char *argv[]){
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Starting test \n"));
<BR>&nbsp;My_Handler handler;

<P>&nbsp;while(1){
<BR>&nbsp; ACE_Reactor::instance()->handle_events();
<BR>&nbsp; sleep(3);
<BR>&nbsp; }
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp; <A HREF="../Chap_6/ex01.html">Next Example</A>
</BODY>
</HTML>