Include changes memory
[ACE_TAO.git] / ACE / examples / APG / Active_Objects / AO2.cpp
blobd552b2c2d483868aba60b7b0f17624b81724d7f9
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 class HA_ControllerAgent
13 // Proxy to the HA_Controller that is on the network.
14 public:
15 HA_ControllerAgent ()
17 ACE_TRACE("HA_ControllerAgent::HA_ControllerAgent");
18 status_result_ = 1;
21 int status_update ()
23 ACE_TRACE ("HA_ControllerAgent::status_update");
24 ACE_DEBUG ((LM_DEBUG,
25 ACE_TEXT ("Obtaining a status_update in %t ")
26 ACE_TEXT ("thread of control\n")));
27 // Simulate sending message to controller
28 // and getting 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_;
43 class StatusUpdate : public ACE_Method_Request
45 public:
46 StatusUpdate (HA_ControllerAgent& controller,
47 ACE_Future<int>& returnVal)
48 : controller_(controller), returnVal_(returnVal)
50 ACE_TRACE ("StatusUpdate::StatusUpdate");
53 virtual int call ()
55 ACE_TRACE ("StatusUpdate::call");
57 // status_update with the controller.
58 this->returnVal_.set (this->controller_.status_update ());
59 return 0;
62 private:
63 HA_ControllerAgent& controller_;
64 ACE_Future<int> returnVal_;
67 class ExitMethod : public ACE_Method_Request
69 public:
70 virtual int call ()
72 // Cause exit.
73 return -1;
77 class Scheduler : public ACE_Task_Base
79 public:
80 Scheduler ()
82 ACE_TRACE ("Scheduler::Scheduler");
83 this->activate ();
86 virtual int svc ()
88 ACE_TRACE ("Scheduler::svc");
90 while (1)
92 // Dequeue the next method object
93 std::unique_ptr<ACE_Method_Request> request (this->activation_queue_.dequeue ());
95 // Invoke the method request.
96 if (request->call () == -1)
97 break;
100 return 0;
103 int enqueue (ACE_Method_Request *request)
105 ACE_TRACE ("Scheduler::enqueue");
106 return this->activation_queue_.enqueue (request);
109 private:
110 ACE_Activation_Queue activation_queue_;
113 class HA_ControllerAgentProxy
115 // This acts as a Proxy to the controller impl object.
116 public:
117 ACE_Future<int> status_update ()
119 ACE_TRACE("HA_ControllerAgentProxy::status_update");
120 ACE_Future<int> result;
122 // Create and enqueue a method request on the scheduler.
123 this->scheduler_.enqueue
124 (new StatusUpdate (this->controller_, result));
126 // Return Future to the client.
127 return result;
130 //FUZZ: disable check_for_lack_ACE_OS
131 void exit ()
133 //FUZZ: enable check_for_lack_ACE_OS
135 ACE_TRACE ("HA_ControllerAgentProxy::exit");
136 this->scheduler_.enqueue (new ExitMethod);
139 private:
140 Scheduler scheduler_;
141 HA_ControllerAgent controller_;
144 // Listing 1 code/ch15
145 class CompletionCallBack : public ACE_Future_Observer<int>
147 public:
148 CompletionCallBack (HA_ControllerAgentProxy& proxy)
149 : proxy_(proxy)
152 virtual void update (const ACE_Future<int>& future)
154 int result = 0;
155 ((ACE_Future<int>)future).get (result);
156 ACE_DEBUG ((LM_INFO,
157 ACE_TEXT ("(%t) New Status %d\n"), result));
158 if (result == 10)
159 this->proxy_.exit ();
162 private:
163 HA_ControllerAgentProxy& proxy_;
165 // Listing 1
166 // Listing 2 code/ch15
167 int ACE_TMAIN (int, ACE_TCHAR *[])
169 HA_ControllerAgentProxy controller;
170 ACE_Future<int> results[10];
171 CompletionCallBack cb (controller);
173 for (int i = 0 ; i < 10; i++)
175 results[i] = controller.status_update ();
176 results[i].attach (&cb);
179 ACE_Thread_Manager::instance ()->wait ();
180 return 0;
182 // Listing 2
184 #else
185 #include "ace/OS_main.h"
186 #include "ace/OS_NS_stdio.h"
188 int ACE_TMAIN (int, ACE_TCHAR *[])
190 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
191 return 0;
194 #endif /* ACE_HAS_THREADS */