summaryrefslogtreecommitdiff
path: root/ACE/examples/Monitor/Message_Queue_Size/message_queue_size.cpp
blob: 1d23ce1e951a2e66a9c26473105ceee4bef26093 (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
// $Id$

#include "ace/OS_NS_unistd.h"

#include "ace/Monitor_Control/Monitor_Control.h"

#include "examples/Monitor/MC_Test_Utilities.h"

#if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)

/// Subclass of ACE_Task_Base, meaning that the override of
/// the svc() method below will run in a new thread when
/// activate() is called on a class instance.
class Monitor_Checker : public ACE_Task_Base
{
private:
  void *addr_;

public:
  Monitor_Checker (void *addr)
    : addr_ (addr)
  {
  }

  int svc (void)
  {
    /// Reconstruct the monitor's unique name using the queue's hex address.
    const int nibbles = 2 * sizeof (ptrdiff_t);
    char buf[nibbles + 1];
    ACE_OS::sprintf (buf, "%p", this->addr_);
    buf[nibbles] = '\0';
    ACE_CString name_str ("Message_Queue_");
    name_str += buf;

    /// Get an instance of the MC service singleton.
    MC_ADMINMANAGER* mgr =
      ACE_Dynamic_Service<MC_ADMINMANAGER>::instance ("MC_ADMINMANAGER");

    /// Call on the administrator class to look up the desired monitors.
    ACE::Monitor_Control::Monitor_Base *mq_monitor =
      mgr->admin ().monitor_point (name_str.c_str ());

    if (mq_monitor != 0)
      {
        ACE_OS::sleep (1);

        /// Query each monitor for its data every 2 seconds, and call the
        /// appropriate display function.
        for (int i = 0; i < 10; ++i)
          {
            ACE_OS::sleep (1);

            Monitor_Control_Types::Data data (mq_monitor->type ());
            mq_monitor->retrieve (data);
            MC_Test_Utilities::display_mq_size (data);
          }

        mq_monitor->remove_ref ();
      }

    return 0;
  }
};

#endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */

int
ACE_TMAIN (int /* argc */, ACE_TCHAR * /* argv */ [])
{
#if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)

  /// Create a message queue with a built-in monitor (since ACE was
  /// compiled with monitors enabled) and add the monitor to the
  /// registry (some ACE activities create a message queue under
  /// the hood, so we must make the registration explicit).
  ACE_Message_Queue<ACE_NULL_SYNCH> monitored_queue;

  /// The message string is 11 bytes long so the message queue will
  /// grow and shrink in 11-byte increments.
  ACE_Message_Block *mb = 0;
  const char *msg = "Hidely Ho!";

  /// Run the monitor checker in a separate thread.
  Monitor_Checker monitor_checker (&monitored_queue);
  monitor_checker.activate ();

  for (int i = 0; i < 10; ++i)
    {
      ACE_OS::sleep (1);

      /// Add 6 message blocks to the queue, then remove
      /// 4 of them.
      if (i < 6)
        {
          mb = new ACE_Message_Block (ACE_OS::strlen (msg) + 1);
          mb->copy (msg);
          monitored_queue.enqueue_tail (mb);
        }
      else
        {
          monitored_queue.dequeue_head (mb);
          mb->release ();
        }
    }

  /// This makes Purify much happier, but doesn't seem necessary for
  /// a regular run.
  monitor_checker.wait ();

  /// Clean up the remaining message queue resources.
  monitored_queue.flush ();

#endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */

  return 0;
}