Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Active_Objects / AO.cpp
blobc2b0dd57a660f05935d92815643d046e889e6d9e
1 #include "ace/config-lite.h"
2 #if defined (ACE_HAS_THREADS)
4 #include "ace/OS_NS_unistd.h"
5 #include "ace/Activation_Queue.h"
6 #include "ace/Method_Request.h"
7 #include "ace/Task.h"
8 #include "ace/Future.h"
9 #include <memory>
11 // Listing 1 code/ch15
12 class HA_ControllerAgent
14 // Proxy to the HA_Controller that is on the network.
15 public:
16 HA_ControllerAgent ()
18 ACE_TRACE("HA_ControllerAgent::HA_ControllerAgent");
19 status_result_ = 1;
22 int status_update ()
24 ACE_TRACE ("HA_ControllerAgent::status_update");
25 ACE_DEBUG ((LM_DEBUG,
26 ACE_TEXT ("Obtaining a status_update in %t ")
27 ACE_TEXT ("thread of control\n")));
28 // Simulate time to send message and get status.
29 ACE_OS::sleep (2);
30 return next_result_id ();
33 private:
34 int next_result_id ()
36 ACE_TRACE ("HA_ControllerAgent::next_cmd_id");
37 return status_result_++;
40 int status_result_;
42 // Listing 1
43 // Listing 2 code/ch15
44 class StatusUpdate : public ACE_Method_Request
46 public:
47 StatusUpdate (HA_ControllerAgent& controller,
48 ACE_Future<int>& returnVal)
49 : controller_(controller), returnVal_(returnVal)
51 ACE_TRACE ("StatusUpdate::StatusUpdate");
54 virtual int call ()
56 ACE_TRACE ("StatusUpdate::call");
58 // status_update with the controller.
59 this->returnVal_.set (this->controller_.status_update ());
60 return 0;
63 private:
64 HA_ControllerAgent& controller_;
65 ACE_Future<int> returnVal_;
67 // Listing 2
68 // Listing 3 code/ch15
69 class ExitMethod : public ACE_Method_Request
71 public:
72 virtual int call ()
74 // Cause exit.
75 return -1;
78 // Listing 3
79 // Listing 4 code/ch15
80 class Scheduler : public ACE_Task_Base
82 public:
83 Scheduler ()
85 ACE_TRACE ("Scheduler::Scheduler");
86 this->activate ();
89 virtual int svc ()
91 ACE_TRACE ("Scheduler::svc");
93 while (1)
95 // Dequeue the next method object
96 std::unique_ptr<ACE_Method_Request> request (this->activation_queue_.dequeue ());
98 // Invoke the method request.
99 if (request->call () == -1)
100 break;
103 return 0;
106 int enqueue (ACE_Method_Request *request)
108 ACE_TRACE ("Scheduler::enqueue");
109 return this->activation_queue_.enqueue (request);
112 private:
113 ACE_Activation_Queue activation_queue_;
115 // Listing 4
116 // Listing 5 code/ch15
117 class HA_ControllerAgentProxy
119 // This acts as a Proxy to the controller impl object.
120 public:
121 ACE_Future<int> status_update ()
123 ACE_TRACE("HA_ControllerAgentProxy::status_update");
124 ACE_Future<int> result;
126 // Create and enqueue a method request on the scheduler.
127 this->scheduler_.enqueue
128 (new StatusUpdate (this->controller_, result));
130 // Return Future to the client.
131 return result;
134 //FUZZ: disable check_for_lack_ACE_OS
135 void exit ()
137 //FUZZ: enable check_for_lack_ACE_OS
138 ACE_TRACE ("HA_ControllerAgentProxy::exit");
139 this->scheduler_.enqueue (new ExitMethod);
142 private:
143 Scheduler scheduler_;
144 HA_ControllerAgent controller_;
146 // Listing 5
147 // Listing 6 code/ch15
148 int ACE_TMAIN (int, ACE_TCHAR *[])
150 HA_ControllerAgentProxy controller;
151 ACE_Future<int> results[10];
153 for (int i = 0 ; i < 10; i++)
154 results[i] = controller.status_update ();
156 ACE_OS::sleep (5); // Do other work.
158 // Get results...
159 for (int j = 0; j < 10; j++)
161 int result = 0;
162 results[j].get (result);
163 ACE_DEBUG ((LM_DEBUG,
164 ACE_TEXT ("New status_update %d\n"), result));
167 // Cause the status_updater threads to exit.
168 controller.exit ();
169 ACE_Thread_Manager::instance ()->wait ();
170 return 0;
172 // Listing 6
174 #else
175 #include "ace/OS_main.h"
176 #include "ace/OS_NS_stdio.h"
178 int ACE_TMAIN (int, ACE_TCHAR *[])
180 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
181 return 0;
184 #endif /* ACE_HAS_THREADS */