Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / LongUpcalls / AMI_Manager.cpp
blobfbd6a6cff16c6a2b9f11cddf739a5a2cfabf8bb7
2 #include "AMI_Manager.h"
3 #include "ace/OS_NS_unistd.h"
5 static void
6 validate_connection (Test::Controller_ptr controller)
8 try
10 #if (TAO_HAS_CORBA_MESSAGING == 1)
11 CORBA::PolicyList_var unused;
12 controller->_validate_connection (unused);
13 #else
14 controller->_is_a ("Not_an_IDL_Type");
15 #endif
17 catch (const CORBA::Exception&)
22 AMI_Manager::AMI_Manager (CORBA::ORB_ptr orb)
23 : orb_ (CORBA::ORB::_duplicate (orb))
27 void
28 AMI_Manager::start_workers (CORBA::Short worker_count,
29 CORBA::Long milliseconds,
30 Test::Controller_ptr controller)
32 ACE_Thread_Manager thread_manager;
34 validate_connection(controller);
36 // ACE_DEBUG ((LM_DEBUG, "Starting %d workers\n", worker_count));
37 Worker worker (&thread_manager,
38 controller, milliseconds,
39 this->orb_.in ());
41 worker.activate (THR_NEW_LWP | THR_JOINABLE, worker_count);
43 thread_manager.wait ();
46 void
47 AMI_Manager::shutdown ()
49 this->orb_->shutdown (false);
52 // ****************************************************************
54 Worker::Worker (ACE_Thread_Manager *thr_mgr,
55 Test::Controller_ptr controller,
56 CORBA::Long milliseconds,
57 CORBA::ORB_ptr orb)
58 : ACE_Task_Base (thr_mgr)
59 , controller_ (Test::Controller::_duplicate (controller))
60 , milliseconds_ (milliseconds)
61 , orb_ (CORBA::ORB::_duplicate (orb))
65 int
66 Worker::svc ()
68 // ACE_DEBUG ((LM_DEBUG, "Worker starts\n"));
69 try
71 TAO_SYNCH_MUTEX mutex;
72 int pending_requests = 2;
73 Test::AMI_ControllerHandler_var handler;
76 Controller_Handler *handler_impl;
77 ACE_NEW_RETURN (handler_impl,
78 Controller_Handler (&mutex,
79 &pending_requests),
80 -1);
82 CORBA::Object_var poa_object =
83 this->orb_->resolve_initial_references("RootPOA");
85 PortableServer::POA_var root_poa =
86 PortableServer::POA::_narrow (poa_object.in ());
88 PortableServer::ObjectId_var id =
89 root_poa->activate_object (handler_impl);
91 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
93 PortableServer::ServantBase_var auto_destroy (handler_impl);
94 handler = Test::AMI_ControllerHandler::_narrow (object.in ());
97 validate_connection(this->controller_.in());
99 this->controller_->sendc_worker_started (handler.in ());
101 // ACE_DEBUG ((LM_DEBUG, "Worker start reported\n"));
103 ACE_Time_Value tv (0, 1000 * this->milliseconds_);
104 ACE_OS::sleep (tv);
106 this->controller_->sendc_worker_finished (handler.in ());
108 // ACE_DEBUG ((LM_DEBUG, "Worker completion reported\n"));
110 for (;;)
112 ACE_Time_Value tv (0, 1000 * this->milliseconds_);
113 this->orb_->run (tv);
115 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, mutex, -1);
116 if (pending_requests == 0)
117 break;
120 catch (const CORBA::Exception& ex)
122 ex._tao_print_exception ("Exception in svc() method\n");
124 return 0;
127 // ****************************************************************
129 Controller_Handler::Controller_Handler (TAO_SYNCH_MUTEX *mutex,
130 int *pending_replies)
131 : mutex_ (mutex)
132 , pending_replies_ (pending_replies)
136 void
137 Controller_Handler::worker_started ()
139 ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, *this->mutex_);
140 (*this->pending_replies_)--;
143 void
144 Controller_Handler::worker_started_excep
145 (::Messaging::ExceptionHolder* h)
149 h->raise_exception ();
151 catch (const CORBA::Exception& ex)
153 ex._tao_print_exception (
154 "Controller_Handler exception raised in"" worker_started");
158 void
159 Controller_Handler::worker_finished ()
161 ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, *this->mutex_);
162 (*this->pending_replies_)--;
165 void
166 Controller_Handler::worker_finished_excep
167 (::Messaging::ExceptionHolder *h)
171 h->raise_exception ();
173 catch (const CORBA::Exception& ex)
175 ex._tao_print_exception (
176 "Controller_Handler exception raised in"" worker_finished");