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"
8 #include "ace/Future.h"
11 class HA_ControllerAgent
13 // Proxy to the HA_Controller that is on the network.
17 ACE_TRACE("HA_ControllerAgent::HA_ControllerAgent");
23 ACE_TRACE ("HA_ControllerAgent::status_update");
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.
30 return next_result_id ();
36 ACE_TRACE ("HA_ControllerAgent::next_cmd_id");
37 return status_result_
++;
43 class StatusUpdate
: public ACE_Method_Request
46 StatusUpdate (HA_ControllerAgent
& controller
,
47 ACE_Future
<int>& returnVal
)
48 : controller_(controller
), returnVal_(returnVal
)
50 ACE_TRACE ("StatusUpdate::StatusUpdate");
55 ACE_TRACE ("StatusUpdate::call");
57 // status_update with the controller.
58 this->returnVal_
.set (this->controller_
.status_update ());
63 HA_ControllerAgent
& controller_
;
64 ACE_Future
<int> returnVal_
;
67 class ExitMethod
: public ACE_Method_Request
77 class Scheduler
: public ACE_Task_Base
82 ACE_TRACE ("Scheduler::Scheduler");
88 ACE_TRACE ("Scheduler::svc");
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)
103 int enqueue (ACE_Method_Request
*request
)
105 ACE_TRACE ("Scheduler::enqueue");
106 return this->activation_queue_
.enqueue (request
);
110 ACE_Activation_Queue activation_queue_
;
113 class HA_ControllerAgentProxy
115 // This acts as a Proxy to the controller impl object.
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.
130 //FUZZ: disable check_for_lack_ACE_OS
133 //FUZZ: enable check_for_lack_ACE_OS
135 ACE_TRACE ("HA_ControllerAgentProxy::exit");
136 this->scheduler_
.enqueue (new ExitMethod
);
140 Scheduler scheduler_
;
141 HA_ControllerAgent controller_
;
144 // Listing 1 code/ch15
145 class CompletionCallBack
: public ACE_Future_Observer
<int>
148 CompletionCallBack (HA_ControllerAgentProxy
& proxy
)
152 virtual void update (const ACE_Future
<int>& future
)
155 ((ACE_Future
<int>)future
).get (result
);
157 ACE_TEXT ("(%t) New Status %d\n"), result
));
159 this->proxy_
.exit ();
163 HA_ControllerAgentProxy
& proxy_
;
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 ();
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."));
194 #endif /* ACE_HAS_THREADS */