<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">//// Chapter: "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">//// 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"> //Constructor</FONT> <BR> My_Input_Handler(){ <BR> ACE_DEBUG((LM_DEBUG,?Constructor\n?); <BR> } <BR> <BR><FONT COLOR="#FF0000"> //Called back to handle any input receieved</FONT> <BR> int <BR> handle_input(ACE_HANDLE){ <BR><FONT COLOR="#FF0000"> //receive the data</FONT> <BR> peer_i().recv_n(data,12); <BR> ACE_DEBUG((LM_DEBUG,?%s\n?,data)); <BR> <BR> <FONT COLOR="#FF0000"> // do something with the input received.</FONT> <BR><FONT COLOR="#FF0000"> // ...</FONT> <P><FONT COLOR="#FF0000"> //keep yourself registered with the reactor</FONT> <BR> return 0; <BR> } <BR> <BR><FONT COLOR="#FF0000"> //Used by the reactor to determine the underlying handle</FONT> <BR> ACE_HANDLE <BR> get_handle()const { <BR> return this->peer_i().get_handle(); <BR> } <BR> <BR><FONT COLOR="#FF0000"> //Returns a reference to the underlying stream.</FONT> <BR> ACE_SOCK_Stream & <BR> peer_i(){ <BR> return this->peer_; <BR> } <P>private: <BR> ACE_SOCK_Stream peer_; <BR> char data [12]; <BR>}; <BR> <P>class <BR>My_Accept_Handler: public ACE_Event_Handler{ <BR>public: <BR><FONT COLOR="#FF0000">//Constructor</FONT> <BR> My_Accept_Handler(ACE_Addr &addr){ <BR> this->open(addr); <BR> } <P><FONT COLOR="#FF0000">//Open the peer_acceptor so it starts to ?listen?</FONT> <BR><FONT COLOR="#FF0000">//for incoming clients.</FONT> <BR> int <BR> open(ACE_Addr &addr){ <BR> peer_acceptor.open(addr); <BR> return 0; <BR> } <P><FONT COLOR="#FF0000">//Overload the handle input method</FONT> <BR> int <BR> handle_input(ACE_HANDLE handle){ <BR> <FONT COLOR="#FF0000"> //Client has requested connection to server.</FONT> <BR><FONT COLOR="#FF0000"> //Create a handler to handle the connection</FONT> <BR> My_Input_Handler *eh= new My_Input_Handler(); <P> <FONT COLOR="#FF0000">//Accept the connection ?into? the Event Handler</FONT> <BR> if (this->peer_acceptor.accept (eh->peer (), <FONT COLOR="#FF0000">// stream</FONT> <BR> 0, <FONT COLOR="#FF0000">// remote address</FONT> <BR> 0, <FONT COLOR="#FF0000">// timeout</FONT> <BR> 1) ==-1) <FONT COLOR="#FF0000">//restart if interrupted</FONT> <BR> ACE_DEBUG((LM_ERROR,"Error in connection\n")); <P> ACE_DEBUG((LM_DEBUG,"Connection established\n")); <P><FONT COLOR="#FF0000"> //Register the input event handler for reading</FONT> <BR> ACE_Reactor::instance()-> <BR> register_handler(eh,ACE_Event_Handler::READ_MASK); <P><FONT COLOR="#FF0000"> //Unregister as the acceptor is not expecting new clients</FONT> <BR> return -1; <BR> } <P><FONT COLOR="#FF6666"> //Used by the reactor to determine the underlying handle</FONT> <BR> ACE_HANDLE <BR> get_handle(void) const{ <BR> return this->peer_acceptor.get_handle(); <BR> } <BR>private: <BR> Acceptor peer_acceptor; <BR>}; <P>int main(int argc, char * argv[]){ <BR><FONT COLOR="#FF0000"> //Create an address on which to receive connections</FONT> <BR> 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> 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> ACE_Event_Handler::ACCEPT_MASK); <P><FONT COLOR="#FF0000">//Start the event loop</FONT> <BR>while(1) <BR> ACE_Reactor::instance()->handle_events(); <BR>} <P> <A HREF="ex03.html">Next Example</A> </BODY> </HTML>