Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / examples / Kokyu_dsrt_schedulers / fp_example / server.cpp
blob60dce97a3fe18120f38ab6a6fab99a4afc3a8a16
1 #include "test_i.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
4 #include "ace/High_Res_Timer.h"
5 #include "tao/RTScheduling/RTScheduler_Manager.h"
6 #include "FP_Scheduler.h"
7 #include "Task_Stats.h"
9 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
11 int nthreads = 2;
12 int enable_dynamic_scheduling = 0;
13 const CORBA::Short max_importance = 100;
14 int enable_yield = 1;
16 int
17 parse_args (int argc, ACE_TCHAR *argv[])
19 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:n:ds"));
20 int c;
22 while ((c = get_opts ()) != -1)
23 switch (c)
25 case 'o':
26 ior_output_file = get_opts.opt_arg ();
27 break;
29 case 'n':
30 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
31 break;
33 case 'd':
34 enable_dynamic_scheduling = 1;
35 break;
37 case 's':
38 enable_yield = 0;
39 break;
41 case '?':
42 default:
43 ACE_ERROR_RETURN ((LM_ERROR,
44 "usage: %s "
45 "-o <iorfile>"
46 "-d (enable dynamic scheduling)"
47 "-s (disable yield)"
48 "\n",
49 argv [0]),
50 -1);
52 // Indicates successful parsing of the command line
53 return 0;
56 /**
57 * Run a server thread
59 * Use the ACE_Task_Base class to run server threads
61 class Worker : public ACE_Task_Base
63 public:
64 Worker (CORBA::ORB_ptr orb);
65 // ctor
67 virtual int svc ();
68 // The thread entry point.
70 private:
71 CORBA::ORB_var orb_;
72 // The orb
75 Task_Stats task_stats;
77 int
78 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
80 Fixed_Priority_Scheduler* scheduler=0;
81 RTScheduling::Current_var current;
82 long flags;
83 int sched_policy = ACE_SCHED_RR;
84 int sched_scope = ACE_SCOPE_THREAD;
86 if (sched_policy == ACE_SCHED_RR)
87 flags = THR_NEW_LWP | THR_BOUND | THR_JOINABLE | THR_SCHED_RR;
88 else
89 flags = THR_NEW_LWP | THR_BOUND | THR_JOINABLE | THR_SCHED_FIFO;
91 task_stats.init (100000);
93 try
95 RTScheduling::Scheduler_var sched_owner;
97 CORBA::ORB_var orb =
98 CORBA::ORB_init (argc, argv);
100 CORBA::Object_var poa_object =
101 orb->resolve_initial_references("RootPOA");
103 if (CORBA::is_nil (poa_object.in ()))
104 ACE_ERROR_RETURN ((LM_ERROR,
105 " (%P|%t) Unable to initialize the POA.\n"),
108 PortableServer::POA_var root_poa =
109 PortableServer::POA::_narrow (poa_object.in ());
111 PortableServer::POAManager_var poa_manager =
112 root_poa->the_POAManager ();
114 if (parse_args (argc, argv) != 0)
115 return 1;
117 if (enable_dynamic_scheduling)
119 CORBA::Object_var manager_obj =
120 orb->resolve_initial_references ("RTSchedulerManager");
122 TAO_RTScheduler_Manager_var manager =
123 TAO_RTScheduler_Manager::_narrow (manager_obj.in ());
125 Kokyu::DSRT_Dispatcher_Impl_t disp_impl_type;
126 if (enable_yield)
128 disp_impl_type = Kokyu::DSRT_CV_BASED;
130 else
132 disp_impl_type = Kokyu::DSRT_OS_BASED;
135 ACE_NEW_RETURN (scheduler,
136 Fixed_Priority_Scheduler (orb.in (),
137 disp_impl_type,
138 sched_policy,
139 sched_scope), -1);
140 sched_owner = scheduler;
142 manager->rtscheduler (scheduler);
144 CORBA::Object_var object =
145 orb->resolve_initial_references ("RTScheduler_Current");
147 current =
148 RTScheduling::Current::_narrow (object.in ());
151 Simple_Server_i server_impl (orb.in (),
152 current.in (),
153 task_stats,
154 enable_yield);
156 Simple_Server_var server =
157 server_impl._this ();
159 CORBA::String_var ior =
160 orb->object_to_string (server.in ());
162 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
164 // If the ior_output_file exists, output the ior to it
165 if (ior_output_file != 0)
167 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
168 if (output_file == 0)
169 ACE_ERROR_RETURN ((LM_ERROR,
170 "Cannot open output file for writing IOR: %s",
171 ior_output_file),
173 ACE_OS::fprintf (output_file, "%s", ior.in ());
174 ACE_OS::fclose (output_file);
177 poa_manager->activate ();
179 Worker worker (orb.in ());
180 if (worker.activate (flags,
181 nthreads,
183 ACE_Sched_Params::priority_max(sched_policy,
184 sched_scope)) != 0)
186 ACE_ERROR ((LM_ERROR,
187 "Cannot activate threads in RT class.",
188 "Trying to activate in non-RT class\n"));
190 flags = THR_NEW_LWP | THR_JOINABLE | THR_BOUND;
191 if (worker.activate (flags, nthreads) != 0)
193 ACE_ERROR_RETURN ((LM_ERROR,
194 "Cannot activate server threads\n"),
199 worker.wait ();
201 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
203 ACE_DEBUG ((LM_DEBUG, "shutting down scheduler\n"));
204 scheduler->shutdown ();
206 orb->destroy ();
208 catch (const CORBA::Exception& ex)
210 ex._tao_print_exception ("Exception caught:");
211 return 1;
214 ACE_DEBUG ((LM_DEBUG, "Exiting main...\n"));
215 task_stats.dump_samples (ACE_TEXT("timeline.txt"),
216 ACE_TEXT("Time\t\tGUID"));
217 return 0;
220 // ****************************************************************
222 Worker::Worker (CORBA::ORB_ptr orb)
223 : orb_ (CORBA::ORB::_duplicate (orb))
228 Worker::svc ()
230 ACE_Time_Value tv(120);
234 this->orb_->run (tv);
236 catch (const CORBA::Exception&)
239 ACE_DEBUG ((LM_DEBUG, "(%t|%T): Worker thread exiting...\n"));
240 return 0;