1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
// $Id: AO.cpp 94310 2011-07-09 19:10:06Z schmidt $
#include "ace/config-lite.h"
#if defined (ACE_HAS_THREADS)
#include "ace/OS_NS_unistd.h"
#include "ace/Activation_Queue.h"
#include "ace/Method_Request.h"
#include "ace/Task.h"
#include "ace/Future.h"
#include "ace/Auto_Ptr.h"
// Listing 1 code/ch15
class HA_ControllerAgent
{
// Proxy to the HA_Controller that is on the network.
public:
HA_ControllerAgent ()
{
ACE_TRACE("HA_ControllerAgent::HA_ControllerAgent");
status_result_ = 1;
}
int status_update (void)
{
ACE_TRACE ("HA_ControllerAgent::status_update");
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Obtaining a status_update in %t ")
ACE_TEXT ("thread of control\n")));
// Simulate time to send message and get status.
ACE_OS::sleep (2);
return next_result_id ();
}
private:
int next_result_id (void)
{
ACE_TRACE ("HA_ControllerAgent::next_cmd_id");
return status_result_++;
}
int status_result_;
};
// Listing 1
// Listing 2 code/ch15
class StatusUpdate : public ACE_Method_Request
{
public:
StatusUpdate (HA_ControllerAgent& controller,
ACE_Future<int>& returnVal)
: controller_(controller), returnVal_(returnVal)
{
ACE_TRACE ("StatusUpdate::StatusUpdate");
}
virtual int call (void)
{
ACE_TRACE ("StatusUpdate::call");
// status_update with the controller.
this->returnVal_.set (this->controller_.status_update ());
return 0;
}
private:
HA_ControllerAgent& controller_;
ACE_Future<int> returnVal_;
};
// Listing 2
// Listing 3 code/ch15
class ExitMethod : public ACE_Method_Request
{
public:
virtual int call (void)
{
// Cause exit.
return -1;
}
};
// Listing 3
// Listing 4 code/ch15
class Scheduler : public ACE_Task_Base
{
public:
Scheduler ()
{
ACE_TRACE ("Scheduler::Scheduler");
this->activate ();
}
virtual int svc (void)
{
ACE_TRACE ("Scheduler::svc");
while (1)
{
// Dequeue the next method object
auto_ptr<ACE_Method_Request>
request (this->activation_queue_.dequeue ());
// Invoke the method request.
if (request->call () == -1)
break;
}
return 0;
}
int enqueue (ACE_Method_Request *request)
{
ACE_TRACE ("Scheduler::enqueue");
return this->activation_queue_.enqueue (request);
}
private:
ACE_Activation_Queue activation_queue_;
};
// Listing 4
// Listing 5 code/ch15
class HA_ControllerAgentProxy
{
// This acts as a Proxy to the controller impl object.
public:
ACE_Future<int> status_update (void)
{
ACE_TRACE("HA_ControllerAgentProxy::status_update");
ACE_Future<int> result;
// Create and enqueue a method request on the scheduler.
this->scheduler_.enqueue
(new StatusUpdate (this->controller_, result));
// Return Future to the client.
return result;
}
//FUZZ: disable check_for_lack_ACE_OS
void exit (void)
{
//FUZZ: enable check_for_lack_ACE_OS
ACE_TRACE ("HA_ControllerAgentProxy::exit");
this->scheduler_.enqueue (new ExitMethod);
}
private:
Scheduler scheduler_;
HA_ControllerAgent controller_;
};
// Listing 5
// Listing 6 code/ch15
int ACE_TMAIN (int, ACE_TCHAR *[])
{
HA_ControllerAgentProxy controller;
ACE_Future<int> results[10];
for (int i = 0 ; i < 10; i++)
results[i] = controller.status_update ();
ACE_OS::sleep (5); // Do other work.
// Get results...
for (int j = 0; j < 10; j++)
{
int result = 0;
results[j].get (result);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("New status_update %d\n"), result));
}
// Cause the status_updater threads to exit.
controller.exit ();
ACE_Thread_Manager::instance ()->wait ();
return 0;
}
// Listing 6
#else
#include "ace/OS_main.h"
#include "ace/OS_NS_stdio.h"
int ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_OS::puts (ACE_TEXT ("This example requires threads."));
return 0;
}
#endif /* ACE_HAS_THREADS */
|