<HTML> <!-- ex09.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 9</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 9</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Reactor.h"</FONT> <BR><FONT COLOR="#000099">#include</FONT><FONT COLOR="#663366"> </FONT><FONT COLOR="#006600">"ace/Svc_Handler.h"</FONT> <BR><FONT COLOR="#000099">#include</FONT><FONT COLOR="#006600"> "ace/Connector.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_Connector.h"</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/INET_Addr.h"</FONT> <P><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">PORT_NUM 10101</FONT> <BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">DATA_SIZE 16</FONT> <P><FONT COLOR="#FF0000">//forward declaration</FONT> <BR>class My_Svc_Handler; <BR><FONT COLOR="#FF0000">//Function prototype</FONT> <BR>static void make_connections(void *arg); <P><FONT COLOR="#FF0000">// Template specializations for the hashing function for the</FONT> <BR><FONT COLOR="#FF0000">// hash_map which is used by the cache. The cache is used internally by the</FONT> <BR><FONT COLOR="#FF0000">// Cached Connection Strategy . Here we use ACE_Hash_Addr</FONT> <BR><FONT COLOR="#FF0000">// as our external identifier. This utility class has already</FONT> <BR><FONT COLOR="#FF0000">// overloaded the == operator and the hash() method. (The</FONT> <BR><FONT COLOR="#FF0000">// hashing function). The hash() method delgates the work to</FONT> <BR><FONT COLOR="#FF0000">// hash_i() and we use the IP address and port to get a</FONT> <BR><FONT COLOR="#FF0000">// a unique integer hash value.</FONT> <BR>size_t <BR>ACE_Hash_Addr<ACE_INET_Addr>::hash_i (const ACE_INET_Addr &addr) const <BR>{ <BR> return addr.get_ip_address () + addr.get_port_number (); <BR>} <P><FONT COLOR="#FF0000">//instantiate a strategy acceptor</FONT> <BR>typedef ACE_Strategy_Connector<My_Svc_Handler,ACE_SOCK_CONNECTOR> <BR>STRATEGY_CONNECTOR; <P><FONT COLOR="#FF0000">//Instantiate the Creation Strategy</FONT> <BR>typedef ACE_NOOP_Creation_Strategy<My_Svc_Handler> <BR> NULL_CREATION_STRATEGY; <BR><FONT COLOR="#FF0000">//Instantiate the Concurrency Strategy</FONT> <BR>typedef ACE_NOOP_Concurrency_Strategy<My_Svc_Handler> <BR> NULL_CONCURRENCY_STRATEGY; <BR><FONT COLOR="#FF0000">//Instantiate the Connection Strategy</FONT> <BR>typedef ACE_Cached_Connect_Strategy<My_Svc_Handler, <BR> ACE_SOCK_CONNECTOR, <BR> ACE_SYNCH_RW_MUTEX> <BR> CACHED_CONNECT_STRATEGY; <BR> <BR>class My_Svc_Handler: <BR>public ACE_Svc_Handler <ACE_SOCK_STREAM,ACE_MT_SYNCH>{ <BR>private: <BR>char* data; <P>public: <BR>My_Svc_Handler(){ <BR>data= new char[DATA_SIZE]; <BR> } <BR>My_Svc_Handler(ACE_Thread_Manager* tm){ <BR>data= new char[DATA_SIZE]; <BR> } <BR><FONT COLOR="#FF0000">//Called before the service handler is recycled..</FONT> <BR>int <BR>recycle (void *a=0){ <BR> ACE_DEBUG ((LM_DEBUG, <BR> "(%P|%t) recycling Svc_Handler %d with handle %d\n", <BR> this, this->peer ().get_handle ())); <BR> return 0; <BR> } <P>int open(void*){ <BR> ACE_DEBUG((LM_DEBUG,"(%t)Connection established \n")); <BR> <BR> <BR> <P><FONT COLOR="#FF0000"> //Register the service handler with the reactor</FONT> <BR> ACE_Reactor::instance() ->register_handler(this,ACE_Event_Handler::READ_MASK); <BR> activate(THR_NEW_LWP|THR_DETACHED); <BR> return 0; <BR> } <P>int handle_input(ACE_HANDLE){ <BR> ACE_DEBUG((LM_DEBUG,"Got input in thread: (%t) \n")); <BR> peer().recv_n(data,DATA_SIZE); <BR> ACE_DEBUG((LM_DEBUG,"<< %s\n",data)); <P> <FONT COLOR="#FF0000">//keep yourself registered with the reactor</FONT> <BR> return 0; <BR>} <P>int svc(void){ <BR><FONT COLOR="#FF0000"> //send a few messages and then mark connection as idle so that it can be recycled later.</FONT> <BR> ACE_DEBUG((LM_DEBUG,"Started the service routine \n")); <P> for(int i=0;i<3;i++){ <BR> ACE_DEBUG((LM_DEBUG,"(%t)>>Hello World\n")); <BR> ACE_OS::fflush(stdout); <BR> peer().send_n("Hello World",sizeof("Hello World")); <BR> } <P> <FONT COLOR="#FF0000">//Mark the service handler as being idle now and let the other threads reuse this connection</FONT> <BR> this->idle(1); <P> <FONT COLOR="#FF0000">//Wait for the thread to die</FONT> <BR> this->thr_mgr()->wait(); <BR> return 0; <BR> } <BR>}; <BR>ACE_INET_Addr *addr; <P>int main(int argc, char* argv[]){ <BR> addr= new ACE_INET_Addr(PORT_NUM,argv[1]); <BR><FONT COLOR="#FF0000"> //Creation Strategy</FONT> <BR> NULL_CREATION_STRATEGY creation_strategy; <P><FONT COLOR="#FF0000"> //Concurrency Strategy</FONT> <BR> NULL_CONCURRENCY_STRATEGY concurrency_strategy; <P><FONT COLOR="#FF0000"> //Connection Strategy</FONT> <BR> CACHED_CONNECT_STRATEGY caching_connect_strategy; <BR> <P><FONT COLOR="#FF0000"> //instantiate the connector</FONT> <BR> STRATEGY_CONNECTOR connector( <BR> ACE_Reactor::instance(),<FONT COLOR="#FF0000"> //the reactor to use</FONT> <BR> &creation_strategy, <BR> &caching_connect_strategy, <BR> &concurrency_strategy); <BR><FONT COLOR="#FF0000"> //Use the thread manager to spawn a single thread to</FONT> <BR><FONT COLOR="#FF0000"> //connect multiple times passing it the address</FONT> <BR><FONT COLOR="#FF0000"> //of the strategy connector</FONT> <BR> if(ACE_Thread_Manager::instance()->spawn( <BR> (ACE_THR_FUNC) make_connections, <BR> (void *) &connector, <BR> THR_NEW_LWP) == -1) <BR> ACE_ERROR ((LM_ERROR, "(%P|%t) %p\n%a", "client thread spawn failed")); <P>while(1) /* Start the reactor?s event loop */ <BR> ACE_Reactor::instance()->handle_events(); <BR>} <P><FONT COLOR="#FF0000">//Connection establishment function, tries to establish connections</FONT> <BR><FONT COLOR="#FF0000">//to the same server again and re-uses the connections from the</FONT> <BR><FONT COLOR="#FF0000">//cache</FONT> <BR>void make_connections(void *arg){ <BR> ACE_DEBUG((LM_DEBUG,"(%t)Prepared to connect \n")); <BR> STRATEGY_CONNECTOR *connector= (STRATEGY_CONNECTOR*) arg; <BR> for (int i = 0; i < 10; i++){ <BR> My_Svc_Handler *svc_handler = 0; <BR> <P> <FONT COLOR="#FF0000">// Perform a blocking connect to the server using the Strategy</FONT> <BR><FONT COLOR="#FF0000"> // Connector with a connection caching strategy. Since we are</FONT> <BR><FONT COLOR="#FF0000"> // connecting to the same <server_addr> these calls will return the</FONT> <BR><FONT COLOR="#FF0000"> // same dynamically allocated <Svc_Handler> for each <connect> call.</FONT> <BR> if (connector->connect (svc_handler, *addr) == -1){ <BR> ACE_ERROR ((LM_ERROR, "(%P|%t) %p\n", "connection failed\n")); <BR> return; <BR> } <P><FONT COLOR="#FF0000"> // Rest for a few seconds so that the connection has been freed up</FONT> <BR> ACE_OS::sleep (5); <BR> } <BR>} <BR> <BR> <A HREF="ex10.html">Next Example</A> </BODY> </HTML>