summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_6/ex10.html
blob: 872c1c44a82845418ef46a1cda4a25520e99c732 (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
<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 10</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 Acceptor/Connector"&nbsp;
(Connection Initialization)</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 10</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Reactor.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Svc_Handler.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Acceptor.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Synch.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_NUM
10101</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">DATA_SIZE
12</FONT>

<P><FONT COLOR="#FF0000">//forward declaration</FONT>
<BR>class My_Svc_Handler;

<P><FONT COLOR="#FF0000">//Create the Acceptor class</FONT>
<BR>typedef ACE_Acceptor&lt;My_Event_Handler,ACE_SOCK_ACCEPTOR>
<BR>MyAcceptor;

<P><FONT COLOR="#FF0000">//Create an event handler similar to as seen in
example 2.</FONT>
<BR><FONT COLOR="#FF0000">//We have to overload the get_handle() method
and write the peer()</FONT>
<BR><FONT COLOR="#FF0000">//method. We also provide the data member peer_
as the underlying</FONT>
<BR><FONT COLOR="#FF0000">//stream which is used.</FONT>
<BR>class My_Event_Handler:
<BR>&nbsp;public ACE_Event_Handler{
<BR>private:
<BR>char* data;
<BR><FONT COLOR="#FF0000">//Add a new attribute for the underlying stream
which will be used by the Event Handler</FONT>
<BR>ACE_SOCK_Stream peer_;
<BR>public:
<BR>My_Event_Handler(){
<BR>&nbsp;data= new char[DATA_SIZE];
<BR>&nbsp;}

<P>int
<BR>open(void*){
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Connection established\n"));
<BR><FONT COLOR="#FF0000">&nbsp;//Register the event handler with the reactor</FONT>
<BR>&nbsp;ACE_Reactor::instance()->register_handler(this, ACE_Event_Handler::READ_MASK);
<BR>&nbsp;return 0;
<BR>&nbsp;}

<P>int
<BR>handle_input(ACE_HANDLE){
<BR>&nbsp;<FONT COLOR="#FF0000">// After using the peer() method of our
ACE_Event_Handler to obtain a</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//reference to the underlying stream of
the service handler class we</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//call recv_n() on it to read the data
which has been received. This</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//data is stored in the data array and
then printed out</FONT>
<BR>&nbsp;peer().recv_n(data,DATA_SIZE);
<BR>&nbsp;ACE_OS::printf("&lt;&lt; %s\n",data);

<P>&nbsp;<FONT COLOR="#FF0000">// keep yourself registered with the reactor</FONT>
<BR>&nbsp;return 0;
<BR>&nbsp;}

<P><FONT COLOR="#FF0000">// new method which returns the handle to the
reactor when it asks for it.</FONT>
<BR>ACE_HANDLE
<BR>get_handle(void) const{
<BR>&nbsp;return this->peer_.get_handle();
<BR>&nbsp;}

<P><FONT COLOR="#FF0000">//new method which returns a reference to the
peer stream</FONT>
<BR>ACE_SOCK_Stream &amp;
<BR>peer(void) const{
<BR>&nbsp;return (ACE_SOCK_Stream &amp;) this->peer_;
<BR>&nbsp;}
<BR>};

<P>int main(int argc, char* argv[]){
<BR>&nbsp;ACE_INET_Addr addr(PORT_NUM);
<BR>&nbsp;<FONT COLOR="#FF0000">//create the acceptor</FONT>
<BR>&nbsp;MyAcceptor acceptor(addr,<FONT COLOR="#FF0000"> //address to
accept on</FONT>
<BR>&nbsp; ACE_Reactor::instance()); <FONT COLOR="#FF0000">//the reactor
to use</FONT>
<BR>while(1)<FONT COLOR="#FF0000"> // Start the reactors event loop</FONT>
<BR>&nbsp;ACE_Reactor::instance()->handle_events();
<BR>}
<BR>&nbsp;
<BR>&nbsp;<A HREF="../Chap_7/ex01.html">Next Example</A>
</BODY>
</HTML>