Sophie

Sophie

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

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

<HTML>
<!-- ex02.html,v 1.3 2000/06/17 13:08:45 schmidt 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 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>