Sophie

Sophie

distrib > Mandriva > 10.0-com > i586 > by-pkgid > 21280410b6ea906d791d7a12afae2579 > files > 434

libace5-doc-5.4-2mdk.i586.rpm

<HTML>
<!-- ex10.html,v 1.2 2000/06/04 22:02:08 brunsch Exp -->
<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>