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 // Listing 1 code/ch15
12 class HA_ControllerAgent
14 // Proxy to the HA_Controller that is on the network.
18 ACE_TRACE("HA_ControllerAgent::HA_ControllerAgent");
24 ACE_TRACE ("HA_ControllerAgent::status_update");
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.
30 return next_result_id ();
36 ACE_TRACE ("HA_ControllerAgent::next_cmd_id");
37 return status_result_
++;
43 // Listing 2 code/ch15
44 class StatusUpdate
: public ACE_Method_Request
47 StatusUpdate (HA_ControllerAgent
& controller
,
48 ACE_Future
<int>& returnVal
)
49 : controller_(controller
), returnVal_(returnVal
)
51 ACE_TRACE ("StatusUpdate::StatusUpdate");
56 ACE_TRACE ("StatusUpdate::call");
58 // status_update with the controller.
59 this->returnVal_
.set (this->controller_
.status_update ());
64 HA_ControllerAgent
& controller_
;
65 ACE_Future
<int> returnVal_
;
68 // Listing 3 code/ch15
69 class ExitMethod
: public ACE_Method_Request
79 // Listing 4 code/ch15
80 class Scheduler
: public ACE_Task_Base
85 ACE_TRACE ("Scheduler::Scheduler");
91 ACE_TRACE ("Scheduler::svc");
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)
106 int enqueue (ACE_Method_Request
*request
)
108 ACE_TRACE ("Scheduler::enqueue");
109 return this->activation_queue_
.enqueue (request
);
113 ACE_Activation_Queue activation_queue_
;
116 // Listing 5 code/ch15
117 class HA_ControllerAgentProxy
119 // This acts as a Proxy to the controller impl object.
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.
134 //FUZZ: disable check_for_lack_ACE_OS
137 //FUZZ: enable check_for_lack_ACE_OS
138 ACE_TRACE ("HA_ControllerAgentProxy::exit");
139 this->scheduler_
.enqueue (new ExitMethod
);
143 Scheduler scheduler_
;
144 HA_ControllerAgent controller_
;
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.
159 for (int j
= 0; j
< 10; j
++)
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.
169 ACE_Thread_Manager::instance ()->wait ();
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."));
184 #endif /* ACE_HAS_THREADS */