summaryrefslogtreecommitdiff
path: root/docs/tutorials/011/page02.html
blob: 476121bcbb73cf8699aa465e414d8de07f72b14c (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
<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>


<HR WIDTH="100%">
<P>

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%">
<P>
<PRE>
#include "task.h"
#include "block.h"
#include "data.h"

int run_test (int iterations, int threads)
{
  Task task;

  if (task.open (threads) == -1)
  {
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), -1);
  }

  ACE_OS::sleep (ACE_Time_Value (1));

  int i;
  for (i = 0; i < iterations; ++i)
  {
    /*
      Construct a Data object that we'll put into the Queue.
    */
      Data data (i);

    /*
      Create a block large enough for our Data object as well
      as a text message.
    */
    Block *message = new Block (sizeof (data) + 128);

    /*
      As before, put a text message into the block.
    */
    ACE_OS::sprintf (message->wr_ptr (), "This is message %d.", i);
    message->wr_ptr (strlen (message->rd_ptr ()));

    *(message->wr_ptr()) = 0;    // Null-terminate the string we just wrote
    message->wr_ptr(1);          // Move beyond the NULL

    /*
      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.
    */
    message->copy ((const char *) &data, sizeof (data));
    message->wr_ptr (sizeof (data));

    if (task.putq (message) == -1)
    {
      break;
    }
  }

  Block *message = new Block ();
  message->msg_type (ACE_Message_Block::MB_HANGUP);
  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;

  (void) run_test (iterations, threads);

  ACE_DEBUG ((LM_DEBUG, "(%P|%t) Application exiting\n"));

  exit (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>

</BODY>
</HTML>