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">//// 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="#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"> //Start the event handling process.</FONT>
<BR> My_Handler(){
<BR> ACE_DEBUG((LM_DEBUG,"Got open\n"));
<BR> activate_threads();
<BR> ACE_Reactor::instance()->max_notify_iterations(5);
<BR> return 0;
<BR> }
<P><FONT COLOR="#FF0000">//Spawn a separate thread so that it notifies
the reactor</FONT>
<BR> void activate_threads(){
<BR> ACE_Thread_Manager::instance()
<BR> ->spawn((ACE_THR_FUNC)svc_start,(void*)this);
<BR> }
<BR>
<BR><FONT COLOR="#FF0000"> //Notify the Reactor 10 times.</FONT>
<BR> void svc(){
<BR> for(int i=0;i<10;i++)
<BR> ACE_Reactor::instance()
<BR> ->notify(this, ACE_Event_Handler::READ_MASK);
<BR> }
<P><FONT COLOR="#FF0000">//The actual handler which in this case will handle
the notifications</FONT>
<BR> int handle_input(int){
<BR> ACE_DEBUG((LM_DEBUG, ?Got notification # %d\n?, no));
<BR> no++;
<BR> return 0;
<BR> }
<P> <FONT COLOR="#FF0000">//The entry point for the new thread that
is to be created.</FONT>
<BR> static int svc_start(void* arg);
<BR>private:
<BR> static int no;
<BR>};
<BR>
<P><FONT COLOR="#FF0000">//Static members</FONT>
<BR>int My_Handler::no=1;
<BR>int My_Handler::svc_start(void* arg){
<BR> My_Handler *eh= (My_Handler*)arg;
<BR> eh->svc();
<BR> return -1; <FONT COLOR="#FF0000">//de-register from the reactor</FONT>
<BR> }
<P>int main(int argc, char *argv[]){
<BR> ACE_DEBUG((LM_DEBUG,"Starting test \n"));
<BR> My_Handler handler;
<P> while(1){
<BR> ACE_Reactor::instance()->handle_events();
<BR> sleep(3);
<BR> }
<BR>}
<BR>
<BR>
<BR> <A HREF="../Chap_6/ex01.html">Next Example</A>
</BODY>
</HTML>
|