summaryrefslogtreecommitdiff
path: root/docs/tutorials/011/page02.html
blob: 3aab35aa5c145b949f5e97b4e86a163f74155378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!-- $Id$ -->
<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>// $Id$</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 &lt; 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 ()) = 0;  <font color=red>// Null-terminate the string we just wrote</font>

      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>