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
|
//
// $Id$
//
#include "AMI_Manager.h"
#include "ace/OS_NS_unistd.h"
ACE_RCSID (LongUpcalls,
AMI_Manager,
"$Id$")
static void
validate_connection (Test::Controller_ptr controller)
{
try
{
#if (TAO_HAS_CORBA_MESSAGING == 1)
CORBA::PolicyList_var unused;
controller->_validate_connection (unused);
#else
controller->_is_a ("Not_an_IDL_Type");
#endif
}
catch (const CORBA::Exception&)
{
}
}
AMI_Manager::AMI_Manager (CORBA::ORB_ptr orb)
: orb_ (CORBA::ORB::_duplicate (orb))
{
}
void
AMI_Manager::start_workers (CORBA::Short worker_count,
CORBA::Long milliseconds,
Test::Controller_ptr controller)
{
ACE_Thread_Manager thread_manager;
validate_connection(controller);
// ACE_DEBUG ((LM_DEBUG, "Starting %d workers\n", worker_count));
Worker worker (&thread_manager,
controller, milliseconds,
this->orb_.in ());
worker.activate (THR_NEW_LWP | THR_JOINABLE, worker_count);
thread_manager.wait ();
}
void
AMI_Manager::shutdown (void)
{
this->orb_->shutdown (0);
}
// ****************************************************************
Worker::Worker (ACE_Thread_Manager *thr_mgr,
Test::Controller_ptr controller,
CORBA::Long milliseconds,
CORBA::ORB_ptr orb)
: ACE_Task_Base (thr_mgr)
, controller_ (Test::Controller::_duplicate (controller))
, milliseconds_ (milliseconds)
, orb_ (CORBA::ORB::_duplicate (orb))
{
}
int
Worker::svc (void)
{
// ACE_DEBUG ((LM_DEBUG, "Worker starts\n"));
try
{
TAO_SYNCH_MUTEX mutex;
int pending_requests = 2;
Test::AMI_ControllerHandler_var handler;
{
Controller_Handler *handler_impl;
ACE_NEW_RETURN (handler_impl,
Controller_Handler (&mutex,
&pending_requests),
-1);
CORBA::Object_var poa_object =
this->orb_->resolve_initial_references("RootPOA");
PortableServer::POA_var root_poa =
PortableServer::POA::_narrow (poa_object.in ());
PortableServer::ObjectId_var id =
root_poa->activate_object (handler_impl);
CORBA::Object_var object = root_poa->id_to_reference (id.in ());
PortableServer::ServantBase_var auto_destroy (handler_impl);
handler = Test::AMI_ControllerHandler::_narrow (object.in ());
}
validate_connection(this->controller_.in());
this->controller_->sendc_worker_started (handler.in ());
// ACE_DEBUG ((LM_DEBUG, "Worker start reported\n"));
ACE_Time_Value tv (0, 1000 * this->milliseconds_);
ACE_OS::sleep (tv);
this->controller_->sendc_worker_finished (handler.in ());
// ACE_DEBUG ((LM_DEBUG, "Worker completion reported\n"));
for (;;)
{
ACE_Time_Value tv (0, 1000 * this->milliseconds_);
this->orb_->run (tv);
ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, mutex, -1);
if (pending_requests == 0)
break;
}
}
catch (const CORBA::Exception& ex)
{
ex._tao_print_exception ("Exception in svc() method\n");
}
return 0;
}
// ****************************************************************
Controller_Handler::Controller_Handler (TAO_SYNCH_MUTEX *mutex,
int *pending_replies)
: mutex_ (mutex)
, pending_replies_ (pending_replies)
{
}
void
Controller_Handler::worker_started (void)
{
ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, *this->mutex_);
(*this->pending_replies_)--;
}
void
Controller_Handler::worker_started_excep
(::Messaging::ExceptionHolder* h)
{
try
{
h->raise_exception ();
}
catch (const CORBA::Exception& ex)
{
ex._tao_print_exception (
"Controller_Handler exception raised in"" worker_started");
}
}
void
Controller_Handler::worker_finished (void)
{
ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, *this->mutex_);
(*this->pending_replies_)--;
}
void
Controller_Handler::worker_finished_excep
(::Messaging::ExceptionHolder *h)
{
try
{
h->raise_exception ();
}
catch (const CORBA::Exception& ex)
{
ex._tao_print_exception (
"Controller_Handler exception raised in"" worker_finished");
}
}
|