<!-- page02.html,v 1.13 2000/11/27 17:56:43 othman Exp --> <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="James CE Johnson"> <TITLE>ACE Tutorial 011</TITLE> </HEAD> <BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F"> <CENTER><B><FONT SIZE=+2>ACE Tutorial 011</FONT></B></CENTER> <CENTER><B><FONT SIZE=+2>Passing non-trivial data through an ACE_Message_Queue</FONT></B></CENTER> <P> <HR WIDTH="100%"> We'll look first at <A HREF="message_queue.cpp">main()</A>. A large part of this is the same as before, so I've only commented the changes. <P> <HR WIDTH="100%"> <PRE> <font color=red>// page02.html,v 1.13 2000/11/27 17:56:43 othman Exp</font> <font color=red>/* Most of this is the same as the previous tutorial, so I'll just point out the differences. */</font> <font color=blue>#include</font> "<font color=green>task.h</font>" <font color=blue>#include</font> "<font color=green>block.h</font>" <font color=blue>#include</font> "<font color=green>data.h</font>" static int run_test (int iterations, int threads) { Task task (threads); if (task.open () == -1) ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green>open</font>"), -1); <font color=#008888>ACE_OS::sleep</font> (ACE_Time_Value (1)); int i; for (i = 0; i < iterations; ++i) { <font color=red>/* Construct a Data object that we'll put into the Queue. */</font> Data data (i); <font color=red>/* Create a block large enough for our Data object as well as a text message. */</font> Block *message; ACE_NEW_RETURN (message, Block (sizeof (data) + 128), -1); <font color=red>/* As before, put a text message into the block. */</font> <font color=#008888>ACE_OS::sprintf</font> (message->wr_ptr (), "<font color=green>This is message %d.</font>", i); message->wr_ptr (strlen (message->rd_ptr ())); message->wr_ptr (1); <font color=red>// Move beyond the NULL</font> <font color=red>/* To copy arbitrary data into a message block, we use the copy() method. Since it wants a 'const char*', we have to cast our Data pointer. Note that copy() will advance the wr_ptr() for us. This means we don't have to do it ourselves! If you do advance it, it will be way beyond what you want. */</font> message->copy ((const char *) &data, sizeof (data)); if (task.putq (message) == -1) break; } Block *message; ACE_NEW_RETURN (message, Block, -1); message->msg_type (<font color=#008888>ACE_Message_Block::MB_HANGUP</font>); task.putq (message); task.wait (); return 0; } int main (int argc, char *argv[]) { int iterations = argc > 1 ? atoi (argv[1]) : 4; int threads = argc > 2 ? atoi (argv[2]) : 2; run_test (iterations, threads); ACE_DEBUG ((LM_DEBUG, "<font color=green>(%P|%t) Application exiting\n</font>")); return 0; } </PRE> <HR WIDTH="100%"> <P> The new trick here is the use of copy() to copy our abstract data object into the message block memory. Notice that it's OK to let the Data object go out of scope at that point since we've got a separate copy. If you've got something with a non-trivial ctor/dtor then this won't work. We'll address that in the next tutorial. <P><HR WIDTH="100%"> <CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page03.html">Continue This Tutorial</A>]</CENTER>