Sophie

Sophie

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

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

<HTML>
<!-- ex04.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 4</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="#FF0000">//Example 4</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Reactor.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Event_Handler.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Synch_T.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Thread_Manager.h"</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">WAIT_TIME
1</FONT>
<BR><FONT COLOR="#000099">#define</FONT><FONT COLOR="#663366"> SLEEP_TIME
2</FONT>

<P>class My_Handler: public ACE_Event_Handler{
<BR>&nbsp;public:

<P><FONT COLOR="#FF0000">&nbsp;//Start the event handling process.</FONT>
<BR>&nbsp;My_Handler(){
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,"Event Handler created\n"));
<BR>&nbsp; ACE_Reactor::instance()->max_notify_iterations(5);
<BR>&nbsp; return 0;
<BR>&nbsp; }

<P><FONT COLOR="#FF0000">//Perform the notifications i.e., notify the reactor
10 times</FONT>
<BR>&nbsp;void perform_notifications(){
<BR>&nbsp; for(int i=0;i&lt;10;i++)
<BR>&nbsp;&nbsp; ACE_Reactor::instance()->
<BR>&nbsp;&nbsp;&nbsp;&nbsp; notify(this,ACE_Event_Handler::READ_MASK);
<BR>&nbsp; }
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//The actual handler which in this case
will handle the notifications</FONT>
<BR>&nbsp;int handle_input(int){
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,"Got notification # %d\n",no));
<BR>&nbsp; no++;
<BR>&nbsp; return 0;
<BR>&nbsp; }
<BR>private:
<BR>&nbsp;static int no;
<BR>};

<P><FONT COLOR="#FF0000">//Static members</FONT>
<BR>int My_Handler::no=1;
<BR>&nbsp;

<P>int main(int argc, char *argv[]){
<BR><FONT COLOR="#FF0000">&nbsp; //Instantiating the handler</FONT>
<BR>&nbsp;My_Handler handler;
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//The done flag is set to not done yet.</FONT>
<BR>&nbsp;int done=0;

<P>while(1){
<BR>&nbsp;<FONT COLOR="#FF0000"> //After WAIT_TIME the handle_events will
fall through if no events arrive.</FONT>
<BR>&nbsp; ACE_Reactor::instance()->handle_events(ACE_Time_Value(WAIT_TIME));
<BR>&nbsp; if(!done){
<BR>&nbsp;&nbsp; handler.perform_notifications();
<BR>&nbsp;&nbsp; done=1;
<BR>&nbsp;&nbsp; }
<BR>&nbsp; sleep(SLEEP_TIME);
<BR>&nbsp;}
<BR>}

<P>&nbsp;<A HREF="ex05.html">Next Example</A>
</BODY>
</HTML>