summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_5/ex02.html
blob: 8652d2b806fd964cb52d09da71e619bca3befbc9 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<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 2</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 2</FONT>
<BR><FONT COLOR="#000099">#include</FONT><FONT COLOR="#006600"> "ace/Reactor.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/SOCK_Acceptor.h"</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">PORT_NO
19998</FONT>
<BR>typedef ACE_SOCK_Acceptor Acceptor;
<BR><FONT COLOR="#FF0000">//forward declaration</FONT>
<BR>class My_Accept_Handler;

<P>class
<BR>My_Input_Handler: public ACE_Event_Handler{
<BR>public:
<BR><FONT COLOR="#FF0000">&nbsp;//Constructor</FONT>
<BR>&nbsp;My_Input_Handler(){
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,?Constructor\n?);
<BR>&nbsp;}
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//Called back to handle any input receieved</FONT>
<BR>&nbsp;int
<BR>&nbsp;handle_input(ACE_HANDLE){
<BR><FONT COLOR="#FF0000">&nbsp; //receive the data</FONT>
<BR>&nbsp; peer_i().recv_n(data,12);
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,?%s\n?,data));
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000"> // do something with the input received.</FONT>
<BR><FONT COLOR="#FF0000">&nbsp; // ...</FONT>

<P><FONT COLOR="#FF0000">&nbsp; //keep yourself registered with the reactor</FONT>
<BR>&nbsp; return 0;
<BR>&nbsp; }
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//Used by the reactor to determine the
underlying handle</FONT>
<BR>&nbsp;ACE_HANDLE
<BR>&nbsp;get_handle()const {
<BR>&nbsp; return this->peer_i().get_handle();
<BR>&nbsp; }
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//Returns a reference to the underlying
stream.</FONT>
<BR>&nbsp;ACE_SOCK_Stream &amp;
<BR>&nbsp;peer_i(){
<BR>&nbsp; return this->peer_;
<BR>&nbsp; }

<P>private:
<BR>&nbsp;ACE_SOCK_Stream peer_;
<BR>&nbsp;char data [12];
<BR>};
<BR>&nbsp;

<P>class
<BR>My_Accept_Handler: public ACE_Event_Handler{
<BR>public:
<BR><FONT COLOR="#FF0000">//Constructor</FONT>
<BR>&nbsp;My_Accept_Handler(ACE_Addr &amp;addr){
<BR>&nbsp; this->open(addr);
<BR>&nbsp; }

<P><FONT COLOR="#FF0000">//Open the peer_acceptor so it starts to ?listen?</FONT>
<BR><FONT COLOR="#FF0000">//for incoming clients.</FONT>
<BR>&nbsp;int
<BR>&nbsp;open(ACE_Addr &amp;addr){
<BR>&nbsp; peer_acceptor.open(addr);
<BR>&nbsp; return 0;
<BR>&nbsp; }

<P><FONT COLOR="#FF0000">//Overload the handle input method</FONT>
<BR>&nbsp;int
<BR>&nbsp;handle_input(ACE_HANDLE handle){
<BR>&nbsp;<FONT COLOR="#FF0000"> //Client has requested connection to server.</FONT>
<BR><FONT COLOR="#FF0000">&nbsp; //Create a handler to handle the connection</FONT>
<BR>&nbsp; My_Input_Handler *eh= new My_Input_Handler();

<P>&nbsp; <FONT COLOR="#FF0000">//Accept the connection ?into? the Event
Handler</FONT>
<BR>&nbsp; if (this->peer_acceptor.accept (eh->peer (), <FONT COLOR="#FF0000">//
stream</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, <FONT COLOR="#FF0000">// remote address</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, <FONT COLOR="#FF0000">// timeout</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1) ==-1) <FONT COLOR="#FF0000">//restart
if interrupted</FONT>
<BR>&nbsp;&nbsp;&nbsp; ACE_DEBUG((LM_ERROR,"Error in connection\n"));

<P>&nbsp; ACE_DEBUG((LM_DEBUG,"Connection established\n"));

<P><FONT COLOR="#FF0000">&nbsp; //Register the input event handler for
reading</FONT>
<BR>&nbsp; ACE_Reactor::instance()->
<BR>&nbsp;&nbsp; register_handler(eh,ACE_Event_Handler::READ_MASK);

<P><FONT COLOR="#FF0000">&nbsp; //Unregister as the acceptor is not expecting
new clients</FONT>
<BR>&nbsp; return -1;
<BR>&nbsp; }

<P><FONT COLOR="#FF6666">&nbsp;//Used by the reactor to determine the underlying
handle</FONT>
<BR>&nbsp;ACE_HANDLE
<BR>&nbsp;get_handle(void) const{
<BR>&nbsp; return this->peer_acceptor.get_handle();
<BR>&nbsp; }
<BR>private:
<BR>&nbsp;Acceptor peer_acceptor;
<BR>};

<P>int main(int argc, char * argv[]){
<BR><FONT COLOR="#FF0000">&nbsp;//Create an address on which to receive
connections</FONT>
<BR>&nbsp;ACE_INET_Addr addr(PORT_NO);

<P><FONT COLOR="#FF0000">//Create the Accept Handler which automatically
begins to "listen"</FONT>
<BR><FONT COLOR="#FF0000">//for client requests for connections</FONT>
<BR>&nbsp;My_Accept_Handler *eh=new My_Accept_Handler(addr);

<P><FONT COLOR="#FF0000">//Register the reactor to call back when incoming
client connects</FONT>
<BR>ACE_Reactor::instance()->register_handler(eh,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ACE_Event_Handler::ACCEPT_MASK);

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

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