<HTML> <!-- ex07.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 7</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 7</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">#include </FONT><FONT COLOR="#006600">"ace/Thread.h"</FONT> <BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">NETWORK_SPEED 3</FONT> <BR>class MyServiceHandler;<FONT COLOR="#FF0000"> //forward declaration</FONT> <BR>typedef ACE_Singleton<ACE_Reactor,ACE_Null_Mutex> Reactor; <BR>typedef ACE_Acceptor<MyServiceHandler,ACE_SOCK_ACCEPTOR> Acceptor; <P>class MyServiceHandler: <BR>public ACE_Svc_Handler<ACE_SOCK_STREAM,ACE_MT_SYNCH>{ <BR><FONT COLOR="#FF0000">// The message sender and creator threads are handled here.</FONT> <BR>ACE_thread_t thread_names[2]; <P>public: <BR>int open(void*){ <BR> ACE_DEBUG((LM_DEBUG, "Acceptor: received new connection \n")); <P><FONT COLOR="#FF0000"> //Register with the reactor to remember this handler..</FONT> <BR> Reactor::instance() ->register_handler(this,ACE_Event_Handler::READ_MASK); <BR> ACE_DEBUG((LM_DEBUG,"Acceptor: ThreadID:(%t) open\n")); <P><FONT COLOR="#CC0000"> //Create two new threads to create and send messages to the</FONT> <BR><FONT COLOR="#CC0000"> //remote machine.</FONT> <BR> activate(THR_NEW_LWP, <BR> 2, <FONT COLOR="#FF0000">//2 new threads</FONT> <BR> 0, <BR> ACE_DEFAULT_THREAD_PRIORITY, <BR> -1, <BR> this, <BR> 0, <BR> 0, <BR> 0, <BR> thread_names); <FONT COLOR="#FF0000">// identifiers in thread_handles</FONT> <BR> <BR> <FONT COLOR="#FF0000">//keep the service handler registered with the acceptor.</FONT> <BR> return 0; <BR> } <P>void send_message(void){ <BR><FONT COLOR="#FF0000"> //Dequeue the message and send it off</FONT> <BR> ACE_DEBUG((LM_DEBUG,"(%t)Sending message >>")); <P><FONT COLOR="#FF0000"> //dequeue the message from the message queue</FONT> <BR> ACE_Message_Block *mb; <BR> ACE_ASSERT(this->getq(mb)!=-1); <BR> int length=mb->length(); <BR> char *data =mb->rd_ptr(); <P> <BR><FONT COLOR="#FF0000"> //Send the data to the remote peer</FONT> <BR> ACE_DEBUG((LM_DEBUG,"%s \n",data,length)); <BR> peer().send_n(data,length); <P> <FONT COLOR="#FF0000">//Simulate very SLOW network.</FONT> <BR> ACE_OS::sleep(NETWORK_SPEED); <P><FONT COLOR="#FF0000"> //release the message block</FONT> <BR> mb->release(); <BR> } <FONT COLOR="#FF0000">//end send_message</FONT> <P>int construct_message(void){ <BR><FONT COLOR="#FF0000"> // A very fast message creation algorithm</FONT> <BR><FONT COLOR="#FF0000"> // would lead to the need for queuing messages..</FONT> <BR><FONT COLOR="#FF0000"> // here. These messages are created and then sent</FONT> <BR><FONT COLOR="#FF0000"> // using the SLOW send_message() routine which is</FONT> <BR><FONT COLOR="#FF0000"> // running in a different thread so that the message</FONT> <BR><FONT COLOR="#FF0000"> //construction thread isn?t blocked.</FONT> <BR> ACE_DEBUG((LM_DEBUG,"(%t)Constructing message >> ")); <P> <FONT COLOR="#FF0000">// Create a new message to send</FONT> <BR> ACE_Message_Block *mb; <BR> char *data="Hello Connector"; <BR> ACE_NEW_RETURN (mb,ACE_Message_Block (16,<FONT COLOR="#FF0000">//Message 16 bytes long</FONT> <BR> ACE_Message_Block::MB_DATA,<FONT COLOR="#FF0000">//Set header to data</FONT> <BR> 0,<FONT COLOR="#FF0000">//No continuations</FONT>. <BR> data<FONT COLOR="#FF0000">//The data we want to send</FONT> <BR> ), 0); <BR> mb->wr_ptr(16); <FONT COLOR="#FF0000">//Set the write pointer.</FONT> <BR> <BR> <FONT COLOR="#FF0000">// Enqueue the message into the message queue</FONT> <BR><FONT COLOR="#FF0000"> // we COULD have done a timed wait for enqueuing in case</FONT> <BR><FONT COLOR="#FF0000"> // someone else holds the lock to the queue so it doesn?t block</FONT> <BR><FONT COLOR="#FF0000"> //forever..</FONT> <BR> ACE_ASSERT(this->putq(mb)!=-1); <BR> ACE_DEBUG((LM_DEBUG,"Enqueued msg successfully\n")); <BR>} <BR> <BR>int svc(void){ <BR> ACE_DEBUG( (LM_DEBUG,?(%t) Svc thread \n?)); <BR> <BR><FONT COLOR="#FF0000"> //call the message creator thread</FONT> <BR> if(ACE_Thread::self()== thread_names[0]) <BR> while(1) construct_message(); <FONT COLOR="#FF0000">//create messages forever</FONT> <BR> else <BR> while(1) send_message(); <FONT COLOR="#FF0000">//send messages forever</FONT> <BR> return 0; <FONT COLOR="#FF0000">// keep the compiler happy.</FONT> <BR>} <BR> <BR>int handle_input(ACE_HANDLE){ <BR> ACE_DEBUG((LM_DEBUG,"(%t) handle_input ")); <BR> char* data= new char[13]; <BR> <BR> <FONT COLOR="#FF0000">//Check if peer aborted the connection</FONT> <BR> if(peer().recv_n(data,12)==0){ <BR> ACE_OS::printf("Peer probably aborted connection"); <BR> return -1; <FONT COLOR="#FF0000">//de-register from the Reactor.</FONT> <BR> } <BR> <BR><FONT COLOR="#FF0000"> //Show what you got..</FONT> <BR> ACE_OS::printf("<< %s\n",data); <BR> <BR><FONT COLOR="#FF0000"> //keep yourself registered</FONT> <BR> return 0; <BR> <BR> } <BR>}; <P>int main(int argc, char* argv[]){ <BR> ACE_INET_Addr addr(10101); <BR> ACE_DEBUG((LM_DEBUG,"Thread: (%t) main\n")); <P> <FONT COLOR="#FF0000">//Prepare to accept connections</FONT> <BR> Acceptor myacceptor(addr,Reactor::instance()); <P><FONT COLOR="#FF0000"> // wait for something to happen.</FONT> <BR> while(1) <BR> Reactor::instance()->handle_events(); <BR> <BR> return 0; <BR>} <P> <A HREF="ex08.html">Next Example</A> </BODY> </HTML>