Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / Monitor / Constraint / constraint.cpp
bloba85aa9c70cd815e2fa3e814a76031f0c88f7f801
1 #include "ace/OS_NS_unistd.h"
2 #include "ace/Monitor_Control_Action.h"
4 #include "ace/Monitor_Control/Monitor_Control.h"
6 #include "examples/Monitor/MC_Test_Utilities.h"
8 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
10 using namespace ACE_VERSIONED_NAMESPACE_NAME::ACE::Monitor_Control;
12 /// Two control actions that will be associated with two different
13 /// constraints on the same monitor.
15 class Trigger8k : public Control_Action
17 virtual void execute (const char* /* command */)
19 ACE_DEBUG ((LM_DEBUG, "Total bytes received is above 8k\n"));
23 class Trigger16k : public Control_Action
25 virtual void execute (const char* /* command */)
27 ACE_DEBUG ((LM_DEBUG, "Total bytes received is above 16k\n"));
31 /// Subclass of ACE_Task_Base, meaning that the override of
32 /// the svc() method below will run in a new thread when
33 /// activate() is called on a class instance.
34 class Monitor_Checker : public ACE_Task_Base
36 public:
37 int svc ()
39 /// Get an instance of the MC service singleton.
40 MC_ADMINMANAGER* mgr =
41 ACE_Dynamic_Service<MC_ADMINMANAGER>::instance ("MC_ADMINMANAGER");
43 /// Call on the administrator class to look up the desired monitors.
44 Monitor_Base *bytes_monitor =
45 mgr->admin ().monitor_point ("OS/Network/BytesReceived");
47 if (bytes_monitor != 0)
49 /// Query the monitor for its data every 2 seconds, and call the
50 /// appropriate display function.
51 for (int i = 0; i < 10; ++i)
53 ACE_OS::sleep (2);
55 Monitor_Control_Types::Data data (bytes_monitor->type ());
56 bytes_monitor->retrieve (data);
57 MC_Test_Utilities::display_bytes_received (data);
60 bytes_monitor->remove_ref ();
63 return 0;
67 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */
69 int
70 ACE_TMAIN (int /* argc */, ACE_TCHAR * /* argv */ [])
72 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
74 /// Set the timer for # of threads check at 2 sec.
75 Monitor_Base *bytes_monitor =
76 create_os_monitor<BYTES_RECEIVED_MONITOR> (0, ACE_Time_Value (2));
78 /// Add two constraints, each with its own triggered action.
80 Trigger8k *trigger8k = new Trigger8k;
81 long id8 = bytes_monitor->add_constraint ("value > 8192", trigger8k);
83 ACE_DEBUG ((LM_DEBUG, "trigger8k id = %d\n", id8));
85 Trigger16k *trigger16k = new Trigger16k;
86 long id16 = bytes_monitor->add_constraint ("value > 16384", trigger16k);
88 ACE_DEBUG ((LM_DEBUG, "trigger16k id = %d\n", id16));
90 /// Create a query and register it to be called periodically.
91 Monitor_Query query ("OS/Network/BytesReceived");
92 Monitor_Point_Auto_Query *auto_query = new Monitor_Point_Auto_Query;
93 auto_query->reference_counting_policy ().value (
94 ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
95 ACE_Event_Handler_var safety (auto_query);
96 ADD_PERIODIC_QUERY (auto_query, &query, ACE_Time_Value (2));
98 /// Runs the reactor's event loop in a separate thread so the timer(s)
99 /// can run concurrently with the application.
100 START_PERIODIC_MONITORS;
102 /// Run the monitor checker in a separate thread. This class will
103 /// fetch the monitor's value directly, and its output will be
104 /// separate from the output from triggered actions.
105 Monitor_Checker monitor_checker;
106 monitor_checker.activate ();
108 ACE_OS::sleep (20);
110 /// Can do a remove_ref() on this returned value or on the original
111 /// control action 'trigger8k', but not on both, since they point to
112 /// the same thing.
113 Control_Action *removed_action = bytes_monitor->remove_constraint (id8);
114 ACE_DEBUG ((LM_DEBUG, "8k trigger removed\n"));
116 ACE_OS::sleep (5);
118 /// End the reactor's event loop, stopping the timer(s).
119 STOP_PERIODIC_MONITORS;
121 /// Do this instead of 'delete' since they are refcounted.
122 removed_action->remove_ref ();
123 trigger16k->remove_ref ();
124 bytes_monitor->remove_ref ();
126 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */
128 return 0;