blob: 5d57ca7d99e55f6e7e29aad8e1cbb9bd1e5c1807 (
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
|
<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 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">//// Chapter: "The Acceptor/Connector"
(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">//// 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<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> 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> data= new char[DATA_SIZE];
<BR> }
<P>int
<BR>open(void*){
<BR> ACE_DEBUG((LM_DEBUG,"Connection established\n"));
<BR><FONT COLOR="#FF0000"> //Register the event handler with the reactor</FONT>
<BR> ACE_Reactor::instance()->register_handler(this, ACE_Event_Handler::READ_MASK);
<BR> return 0;
<BR> }
<P>int
<BR>handle_input(ACE_HANDLE){
<BR> <FONT COLOR="#FF0000">// After using the peer() method of our
ACE_Event_Handler to obtain a</FONT>
<BR><FONT COLOR="#FF0000"> //reference to the underlying stream of
the service handler class we</FONT>
<BR><FONT COLOR="#FF0000"> //call recv_n() on it to read the data
which has been received. This</FONT>
<BR><FONT COLOR="#FF0000"> //data is stored in the data array and
then printed out</FONT>
<BR> peer().recv_n(data,DATA_SIZE);
<BR> ACE_OS::printf("<< %s\n",data);
<P> <FONT COLOR="#FF0000">// keep yourself registered with the reactor</FONT>
<BR> return 0;
<BR> }
<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> return this->peer_.get_handle();
<BR> }
<P><FONT COLOR="#FF0000">//new method which returns a reference to the
peer stream</FONT>
<BR>ACE_SOCK_Stream &
<BR>peer(void) const{
<BR> return (ACE_SOCK_Stream &) this->peer_;
<BR> }
<BR>};
<P>int main(int argc, char* argv[]){
<BR> ACE_INET_Addr addr(PORT_NUM);
<BR> <FONT COLOR="#FF0000">//create the acceptor</FONT>
<BR> MyAcceptor acceptor(addr,<FONT COLOR="#FF0000"> //address to
accept on</FONT>
<BR> ACE_Reactor::instance()); <FONT COLOR="#FF0000">//the reactor
to use</FONT>
<BR>while(1)<FONT COLOR="#FF0000"> // Start the reactors event loop</FONT>
<BR> ACE_Reactor::instance()->handle_events();
<BR>}
<BR>
<BR> <A HREF="../Chap_7/ex01.html">Next Example</A>
</BODY>
</HTML>
|