summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_5/ex01.html
blob: 54bb3204b6155eb850ee66b05e0737ca8300c9b8 (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
<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 1</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 1</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">&lt;signal.h></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>

<P><FONT COLOR="#FF0000">//Create our subclass to handle the signal events</FONT>
<BR><FONT COLOR="#FF0000">//that we wish to handle. Since we know that
this particular</FONT>
<BR><FONT COLOR="#FF0000">//event handler is going to be using signals
we only overload the</FONT>
<BR><FONT COLOR="#FF0000">//handle_signal method.</FONT>

<P>class
<BR>MyEventHandler: public ACE_Event_Handler{
<BR>int
<BR>handle_signal(int signum, siginfo_t*,ucontext_t*){
<BR>&nbsp;switch(signum){
<BR>&nbsp; case SIGWINCH:
<BR>&nbsp;&nbsp; ACE_DEBUG((LM_DEBUG, "You pressed SIGWINCH \n"));
<BR>&nbsp;&nbsp; break;

<P>&nbsp; case SIGINT:
<BR>&nbsp;&nbsp; ACE_DEBUG((LM_DEBUG, "You pressed SIGINT \n"));
<BR>&nbsp;&nbsp; break;
<BR>&nbsp;&nbsp; }
<BR>&nbsp;return 0;
<BR>&nbsp;}
<BR>};

<P>int main(int argc, char *argv[]){
<BR><FONT COLOR="#FF0000">&nbsp;//instantiate the handler</FONT>
<BR>&nbsp;MyEventHandler *eh =new MyEventHandler;

<P><FONT COLOR="#FF0000">//Register the handler asking to call back when
either SIGWINCH</FONT>
<BR><FONT COLOR="#FF0000">//or SIGINT signals occur. Note that in both
the cases we asked the</FONT>
<BR><FONT COLOR="#FF0000">//Reactor to call back the same Event_Handler
i.e., MyEventHandler.</FONT>
<BR><FONT COLOR="#FF0000">//This is the reason why we had to write a switch
statement in the handle_signal()</FONT>
<BR><FONT COLOR="#FF0000">//method above. Also note that the</FONT>
<BR><FONT COLOR="#FF0000">//ACE_Reactor is being used as a Singleton object&nbsp;
(Singleton pattern)</FONT>

<P>&nbsp;ACE_Reactor::instance()->register_handler(SIGWINCH,eh);
<BR>&nbsp;ACE_Reactor::instance()->register_handler(SIGINT,eh);
<BR>&nbsp;while(1)
<BR>&nbsp;<FONT COLOR="#FF0000"> //Start the reactors event loop</FONT>
<BR>&nbsp; ACE_Reactor::instance()->handle_events();
<BR>}

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