Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / orbsvcs / examples / Notify / MC / monitor / monitor.cpp
blob49e6acd98738be6a7df0ea38a98f2b3237703c64
1 #include "ace/Get_Opt.h"
2 #include "ace/OS_NS_ctype.h"
4 #include "orbsvcs/Notify/MonitorControl/NotificationServiceMCC.h"
6 static const ACE_TCHAR* monitor_ior = 0;
7 static const char* shutdown_cmd = "shutdown";
8 static const char* rm_consumer = "remove_consumer";
9 static const char* rm_supplier = "remove_supplier";
10 static const char* rm_consumeradmin = "remove_consumeradmin";
11 static const char* rm_supplieradmin = "remove_supplieradmin";
13 static int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("k:"));
17 int c;
19 while ((c = get_opts ()) != -1)
21 switch (c)
23 case 'k':
24 monitor_ior = get_opts.opt_arg ();
25 break;
26 case '?':
27 default:
28 ACE_ERROR_RETURN ((LM_ERROR,
29 "usage: %s "
30 "-k <ior> "
31 "\n",
32 argv [0]),
33 -1);
37 return 0;
40 extern "C" int
41 sorter (const void* a, const void* b)
43 const char* left = *(reinterpret_cast<const char* const*> (a));
44 const char* right = *(reinterpret_cast<const char* const*> (b));
45 return ACE_OS::strcmp (left, right);
48 bool
49 process_command (CosNotification::NotificationServiceMonitorControl_ptr nsm,
50 char* buf,
51 const char* command)
53 if (ACE_OS::strstr (buf, command) == buf)
55 const char* name = buf + ACE_OS::strlen (command);
56 bool space = false;
57 size_t i = 0;
59 while (ACE_OS::ace_isspace (name[i]))
61 space = true;
62 ++i;
65 if (space)
67 try
69 const char* start = name + i;
71 if (ACE_OS::strcmp (command, shutdown_cmd) == 0)
73 nsm->shutdown_event_channel (start);
75 else if (ACE_OS::strcmp (command, rm_consumer) == 0)
77 nsm->remove_consumer (start);
79 else if (ACE_OS::strcmp (command, rm_supplier) == 0)
81 nsm->remove_supplier (start);
83 else if (ACE_OS::strcmp (command, rm_consumeradmin) == 0)
85 nsm->remove_consumeradmin (start);
87 else if (ACE_OS::strcmp (command, rm_supplieradmin) == 0)
89 nsm->remove_supplieradmin (start);
92 catch (const CORBA::Exception& ex)
94 ACE_OS::strcat (buf, ": ");
95 ex._tao_print_exception (buf);
98 return true;
102 return false;
106 ACE_TMAIN (int argc, ACE_TCHAR* argv[])
108 int status = 0;
112 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
114 if (parse_args (argc, argv) != 0)
116 return 1;
119 CORBA::Object_var obj = orb->string_to_object (
120 ACE_TEXT_ALWAYS_CHAR (monitor_ior));
121 CosNotification::NotificationServiceMonitorControl_var nsm =
122 CosNotification::NotificationServiceMonitorControl::_narrow (obj.in ());
124 if (CORBA::is_nil (nsm.in ()))
126 ACE_ERROR_RETURN ((LM_ERROR,
127 "Unable to locate the "
128 "Notification Service Monitor\n"),
132 bool done = false;
133 static const size_t lsize = 1024;
134 char prev[lsize];
136 while (!done)
138 ACE_OS::printf ("NotifyService> ");
139 ACE_OS::fflush (stdout);
141 char line[lsize] = "";
142 char* rl = ACE_OS::fgets (line, lsize, stdin);
144 if (rl != 0)
146 int len = static_cast<int> (ACE_OS::strlen (line));
148 for (int i = 0; i < len && ACE_OS::ace_isspace (rl[0]); ++i)
150 rl++;
153 for (int i = len - 1; i >= 0 && ACE_OS::ace_isspace (line[i]); --i)
155 line[i] = '\0';
158 if (ACE_OS::strlen (rl) == 0 || ACE_OS::strcmp (rl, "!!") == 0)
160 ACE_OS::strcpy (line, prev);
161 rl = line;
164 ACE_OS::strcpy (prev, line);
168 if (rl == 0)
170 done = true;
171 ACE_DEBUG ((LM_DEBUG, "\n"));
173 else if (ACE_OS::strlen (rl) == 0)
176 else if (ACE_OS::strcmp (rl, "quit") == 0)
178 done = true;
180 else if (ACE_OS::strcmp (rl, "help") == 0)
182 ACE_DEBUG ((LM_DEBUG,
183 "names - Get a list of "
184 "currently available statistic names.\n"
185 "quit - Exit the monitor.\n"
186 "remove_consumer - Remove a consumer "
187 "with the given name.\n"
188 "remove_supplier - Remove a supplier "
189 "with the given name.\n"
190 "remove_consumeradmin - Remove a consumer "
191 "admin with the given name.\n"
192 "remove_supplieradmin - Remove a supplier "
193 "admin with the given name.\n"
194 "shutdown - Shut down an "
195 "event channel with the given name.\n"
196 "<statistic name> - Return the "
197 "information for the specified statistic.\n"));
199 else if (ACE_OS::strcmp (rl, "names") == 0)
203 Monitor::NameList_var names = nsm->get_statistic_names ();
204 CORBA::ULong length = names->length ();
205 ACE_DEBUG ((LM_DEBUG, "Statistic names\n"));
207 // It's much easier to read once it's sorted
208 const char** narray = 0;
209 ACE_NEW_THROW_EX (narray,
210 const char* [length],
211 CORBA::NO_MEMORY ());
213 for (CORBA::ULong i = 0; i < length; ++i)
215 narray[i] = names[i].in ();
218 ACE_OS::qsort (narray,
219 length,
220 sizeof (const char*),
221 sorter);
223 for (CORBA::ULong i = 0; i < length; ++i)
225 ACE_DEBUG ((LM_DEBUG, " %s\n", narray[i]));
228 delete [] narray;
230 catch (const CORBA::Exception& ex)
232 ex._tao_print_exception ("names: ");
235 else
237 if (process_command (nsm.in (), rl, shutdown_cmd))
239 continue;
241 else if (process_command (nsm.in (), rl, rm_consumer))
243 continue;
245 else if (process_command (nsm.in (), rl, rm_supplier))
247 continue;
249 else if (process_command (nsm.in (), rl, rm_consumeradmin))
251 continue;
253 else if (process_command (nsm.in (), rl, rm_supplieradmin))
255 continue;
260 Monitor::Data_var data =
261 nsm->get_statistic (rl);
262 ACE_DEBUG ((LM_DEBUG, "%s => ", rl));
264 if (data->data_union._d () == Monitor::DATA_NUMERIC)
266 ACE_DEBUG ((LM_DEBUG,
267 "Last: %g Average: %g\n",
268 data->data_union.num().dlist[0].value,
269 data->data_union.num().average));
271 else
273 Monitor::NameList list = data->data_union.list ();
274 CORBA::ULong const length = list.length ();
276 for (CORBA::ULong i = 0; i < length; ++i)
278 ACE_DEBUG ((LM_DEBUG, "%s ", list[i].in ()));
281 ACE_DEBUG ((LM_DEBUG, "\n"));
284 catch (const CORBA::Exception& ex)
286 ACE_OS::strcat (rl, ": ");
287 ex._tao_print_exception (rl);
292 orb->destroy ();
294 catch (const CORBA::UserException& ex)
296 ex._tao_print_exception ("Notification Service Monitor: ");
297 ++status;
299 catch (const CORBA::Exception& ex)
301 ex._tao_print_exception ("Notification Service Monitor: ");
302 ++status;
304 catch (...)
306 ACE_ERROR ((LM_ERROR,
307 "Notification Service Monitor: "
308 "unexpected exception type\n"));
309 status++;
312 return status;