summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/MonitorControl/NotificationServiceMonitor_i.cpp
blob: d8806831e2e9db82df4b326d36c0c11b7d91927b (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "orbsvcs/Log_Macros.h"
#include "orbsvcs/Log_Macros.h"
#include "orbsvcs/Notify/MonitorControl/NotificationServiceMonitor_i.h"

#include "tao/Monitor/Monitor_Impl.h"
#include "ace/Auto_Ptr.h"
#include "ace/Monitor_Point_Registry.h"
#include "ace/Monitor_Base.h"

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

#include "orbsvcs/Notify/MonitorControl/Control_Registry.h"

using namespace ACE_VERSIONED_NAMESPACE_NAME::ACE::Monitor_Control;

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

NotificationServiceMonitor_i::NotificationServiceMonitor_i (CORBA::ORB_ptr orb)
  : orb_ (CORBA::ORB::_duplicate (orb))
{
}

Monitor::NameList*
NotificationServiceMonitor_i::get_statistic_names ()
{
  Monitor_Control_Types::NameList name_list =
    Monitor_Point_Registry::instance ()->names ();
  CORBA::ULong the_length = static_cast <CORBA::ULong> (name_list.size ());

  Monitor::NameList* the_names = 0;
  ACE_NEW_RETURN (the_names,
                  Monitor::NameList (the_length),
                  0);
  Monitor::NameList_var safe_names = the_names;
  the_names->length (the_length);
  CORBA::ULong i = 0;

  for (Monitor_Control_Types::NameList::Iterator iter (name_list);
       !iter.done ();
       iter.advance (), ++i)
    {
      ACE_CString* tmp = 0;
      iter.next (tmp);
      safe_names[i] = tmp->c_str ();
    }

  return safe_names._retn ();
}

Monitor::Data*
NotificationServiceMonitor_i::get_statistic (const char* name)
{
  Monitor_Point_Registry* registry = Monitor_Point_Registry::instance ();

  Monitor::NameList invalid;
  Monitor::NameList names (1);
  names.length (1);
  names[0] = name;
  this->get_invalid_names (registry, names, invalid);

  if (invalid.length () > 0)
    {
      throw CosNotification::NotificationServiceMonitorControl::InvalidName (invalid);
    }

  Monitor::Data* data = 0;
  ACE_NEW_THROW_EX (data,
                    Monitor::Data,
                    CORBA::NO_MEMORY ());
  this->get_data (registry, name, *data);
  return data;
}

Monitor::DataList*
NotificationServiceMonitor_i::get_statistics (const Monitor::NameList& names)
{
  Monitor_Point_Registry* registry = Monitor_Point_Registry::instance ();

  Monitor::NameList invalid;
  this->get_invalid_names (registry, names, invalid);

  if (invalid.length () > 0)
    {
      throw CosNotification::NotificationServiceMonitorControl::InvalidName (invalid);
    }

  CORBA::ULong const length = names.length ();
  Monitor::DataList* data = 0;
  ACE_NEW_RETURN (data,
                  Monitor::DataList (
                    length),
                  0);
  ACE_Auto_Basic_Ptr<Monitor::DataList> safe_data (data);

  data->length (length);

  for (CORBA::ULong i = 0; i < length; ++i)
    {
      this->get_data (registry, names[i], (*data)[i]);
    }

  return safe_data.release ();
}

Monitor::DataList*
NotificationServiceMonitor_i::get_and_clear_statistics (
  const Monitor::NameList& names)
{
  Monitor::DataList* data =
    this->get_statistics (names);

  // We've gotten to the point where we have a list of data.
  // It is technically possible for the list of statistics names
  // to change at this point.  So, I'm bypassing the call to
  // clear_statistics() to avoid a possible exception.
  CORBA::ULong length = names.length();
  Monitor_Point_Registry* registry = Monitor_Point_Registry::instance ();

  for (CORBA::ULong i = 0; i < length; ++i)
    {
      Monitor_Base* statistic = registry->get (names[i].in ());

      if (statistic != 0)
        {
          statistic->clear ();
        }
    }

  return data;
}

void
NotificationServiceMonitor_i::clear_statistics (
  const Monitor::NameList& names)
{
  Monitor_Point_Registry* registry = Monitor_Point_Registry::instance ();

  Monitor::NameList invalid;
  this->get_invalid_names (registry, names, invalid);

  if (invalid.length () > 0)
    {
      throw CosNotification::NotificationServiceMonitorControl::InvalidName (invalid);
    }

  CORBA::ULong const length = names.length ();

  for (CORBA::ULong i = 0; i < length; ++i)
    {
      Monitor_Base* statistic = registry->get (names[i].in ());

      if (statistic != 0)
        {
          statistic->clear ();
        }
    }
}

void
NotificationServiceMonitor_i::shutdown_event_channel (const char* name)
{
  this->send_control_command (name, TAO_NS_CONTROL_SHUTDOWN);
}

void
NotificationServiceMonitor_i::remove_consumer (const char* name)
{
  this->send_control_command (name, TAO_NS_CONTROL_REMOVE_CONSUMER);
}

void
NotificationServiceMonitor_i::remove_supplier (const char* name)
{
  this->send_control_command (name, TAO_NS_CONTROL_REMOVE_SUPPLIER);
}

void
NotificationServiceMonitor_i::remove_consumeradmin (const char* name)
{
  this->send_control_command (name, TAO_NS_CONTROL_REMOVE_CONSUMERADMIN);
}

void
NotificationServiceMonitor_i::remove_supplieradmin (const char* name)
{
  this->send_control_command (name, TAO_NS_CONTROL_REMOVE_SUPPLIERADMIN);
}

void
NotificationServiceMonitor_i::send_control_command (const char* name,
                                                    const char* cmd)
{
  TAO_Control_Registry* instance = TAO_Control_Registry::instance ();
  TAO_NS_Control* control = instance->get (name);

  // If we didn't find a control object with the given name, or the
  // execution of the control object failed, we must throw an exception.
  // The control object execution should only return false when the
  // command given does not correspond to one that is supported by the
  // control object.
  if (control == 0 || !control->execute (cmd))
    {
      Monitor::NameList invalid (1);
      invalid.length (1);
      invalid[0] = name;
      throw CosNotification::NotificationServiceMonitorControl::InvalidName (invalid);
    }
}

void
NotificationServiceMonitor_i::shutdown ()
{
  if (!CORBA::is_nil (this->orb_.in ()))
    {
      this->orb_->shutdown ();
    }
}

void
NotificationServiceMonitor_i::get_data (
   Monitor_Point_Registry* registry,
   const char* name,
   Monitor::Data& data)
{
  // Get the statistic by name.
  Monitor_Base* monitor = registry->get (name);

  if (monitor == 0)
    {
      // At the time that the list of names were acquired, this
      // statistic was available.  However, we have failed to
      // retrieve it.  The only reason that could happen is if
      // some one removed the statistic from the registry.
      Monitor::Numeric num;
      num.count = 0;
      num.average = 0;
      num.sum_of_squares = 0;
      num.minimum = 0;
      num.maximum = 0;
      num.last = 0;
    }
  else
    {
      TAO_Monitor::get_monitor_data (monitor, data, false);
    }
}

void
NotificationServiceMonitor_i::get_invalid_names (
   Monitor_Point_Registry* registry,
   const Monitor::NameList& names,
   Monitor::NameList& invalid)
{
  invalid.length (0);

  CORBA::ULong ilength = 0;
  CORBA::ULong const length  = names.length ();

  for (CORBA::ULong i = 0; i < length; ++i)
    {
      if (registry->get (names[i].in ()) == 0)
        {
          if (TAO_debug_level > 7)
            {
              ORBSVCS_DEBUG((LM_INFO,
                ACE_TEXT("(%P|%t) TAO_NotificationServiceMonitor: Client requested invalid statistic name: %s"),
                  names[i].in ()));
            }
          invalid.length (ilength + 1);
          invalid[ilength++] = CORBA::string_dup (names[i]);
        }
    }
}

TAO_END_VERSIONED_NAMESPACE_DECL

#endif /* TAO_HAS_MONITOR_FRAMEWORK==1 */