Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / RTScheduling / MIF_Scheduler / test.cpp
blob612fc46bb0f1f7d9ead5ffe31c7d00b8ba26ba24
1 #include "test.h"
2 #include "../Task_Stats.h"
3 #include "../Synch_i.h"
5 #include "tao/RTScheduling/RTScheduler_Manager.h"
6 #include "tao/ORB_Core.h"
8 #include "ace/Arg_Shifter.h"
10 #include "ace/Event_Handler.h"
11 #include "ace/Sig_Handler.h"
13 class TestShutdown : public ACE_Event_Handler
15 public:
16 TestShutdown (CORBA::ORB_ptr orb)
17 : orb_(CORBA::ORB::_duplicate (orb))
19 #if !defined(ACE_LACKS_UNIX_SIGNALS)
20 this->shutdown_.register_handler (SIGTERM, this);
21 this->shutdown_.register_handler (SIGINT, this);
22 #elif defined(ACE_WIN32)
23 this->shutdown_.register_handler (SIGINT, this);
24 #endif
27 ~TestShutdown (void)
29 #if !defined(ACE_LACKS_UNIX_SIGNALS)
30 this->shutdown_.remove_handler (SIGTERM);
31 this->shutdown_.remove_handler (SIGINT);
32 #elif defined(ACE_WIN32)
33 this->shutdown_.remove_handler (SIGINT);
34 #endif
37 virtual int handle_signal (int, siginfo_t*, ucontext_t*)
39 ACE_DEBUG ((LM_DEBUG, "Shutting down...\n"));
40 this->orb_->shutdown ();
41 return 0;
44 private:
45 CORBA::ORB_var orb_;
47 ACE_Sig_Handler shutdown_;
50 DT_Test::DT_Test (void)
54 int
55 DT_Test::init (int argc, ACE_TCHAR *argv [])
57 orb_ = CORBA::ORB_init (argc, argv);
59 dt_creator_->orb (orb_.in ());
61 CORBA::Object_var manager_obj = orb_->resolve_initial_references ("RTSchedulerManager");
63 TAO_RTScheduler_Manager_var manager = TAO_RTScheduler_Manager::_narrow (manager_obj.in ());
66 ACE_NEW_RETURN (scheduler_,
67 MIF_Scheduler (orb_.in ()), -1);
69 manager->rtscheduler (scheduler_.in ());
71 CORBA::Object_var object =
72 orb_->resolve_initial_references ("RTScheduler_Current");
74 current_ =
75 RTScheduling::Current::_narrow (object.in ());
77 return 0;
80 void
81 DT_Test::run (int argc, ACE_TCHAR* argv [])
83 init (argc,argv);
85 TestShutdown killer (this->orb_.in ());
87 TASK_STATS::instance ()->init (this->dt_creator_->total_load ());
88 if (this->dt_creator_->resolve_naming_service () == -1)
89 return;
91 this->dt_creator_->activate_root_poa ();
93 this->dt_creator_->activate_poa_list ();
94 this->dt_creator_->activate_job_list ();
95 this->dt_creator_->activate_schedule ();
97 this->dt_creator_->register_synch_obj ();
99 ACE_DEBUG ((LM_DEBUG,
100 "Registered Synch Object\n"));
102 this->dt_creator_->create_distributable_threads (current_.in ());
104 ACE_Thread_Manager::instance ()->wait ();
106 this->orb_->destroy ();
108 ACE_DEBUG ((LM_DEBUG,
109 "Test Terminating......\n"));
113 void
114 DT_Test::dt_creator (MIF_DT_Creator* dt_creator)
116 this->dt_creator_ = dt_creator;
120 MIF_Scheduler*
121 DT_Test::scheduler (void)
123 return this->scheduler_.in ();
127 DT_Test::activate_task (void)
130 long flags;
131 flags = THR_NEW_LWP | THR_JOINABLE;
132 flags |=
133 orb_->orb_core ()->orb_params ()->scope_policy () |
134 orb_->orb_core ()->orb_params ()->sched_policy ();
136 if (this->activate (flags,
137 1) == -1)
139 if (ACE_OS::last_error () == EPERM)
140 ACE_ERROR_RETURN ((LM_ERROR,
141 ACE_TEXT ("Insufficient privilege to run this test.\n")),
142 -1);
144 return 0;
148 DT_Test::svc (void)
152 dt_creator_->create_distributable_threads (current_.in ());
155 catch (const CORBA::Exception& ex)
157 ex._tao_print_exception ("Caught exception:");
158 return -1;
161 return 0;
166 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
168 int status = 0;
170 ACE_Service_Config::static_svcs ()->insert (&ace_svc_desc_MIF_DT_Creator);
174 ACE_DEBUG ((LM_DEBUG,
175 "%t Main Thread\n"));
176 DT_TEST::instance ()->run (argc, argv);
178 catch (const CORBA::Exception& ex)
180 ex._tao_print_exception ("Caught exception:");
181 status = 1;
184 ACE_Service_Config::static_svcs ()->remove (ACE_TEXT ("MIF_DT_Creator"));
186 ACE_DEBUG ((LM_DEBUG, "END\n"));
187 return status;
190 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, DT_Test, TAO_SYNCH_MUTEX);