Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / AMH / Sink_Server / Base_Server.cpp
blob19a8ab7a5f366c6e6bb5f094373b52cd14dabce8
1 #include "ace/Sched_Params.h"
2 #include "ace/Get_Opt.h"
4 #include "AMH_Servant.h"
5 #include "Base_Server.h"
6 #include "tao/Strategies/advanced_resource.h"
8 #include "TestC.h"
10 #if !defined (__ACE_INLINE__)
11 # include "Base_Server.inl"
12 #endif /* __ACE_INLINE__ */
14 Base_Server::Base_Server (int &argc, ACE_TCHAR **argv)
15 : argc_ (argc)
16 , argv_ (argv)
17 , ior_output_file_ (ACE_TEXT("test.ior"))
21 Base_Server::~Base_Server (void)
25 int
26 Base_Server::parse_args (void)
28 // *** To get correct behaviour, set ** POSIXLY_CORECT=1 ** on Linux
29 // systems!!! ***
30 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("o:"));
31 int c;
32 int count_argv = 0;
34 while ((c = get_opts ()) != -1)
36 ++count_argv;
37 switch (c)
39 case 'o':
41 this->ior_output_file_ = get_opts.opt_arg ();
43 // Remove the option '-o' from argv []
44 // to avoid any confusion that might result.
46 for (int i = count_argv; i <= this->argc_; ++i)
47 this->argv_ [i] = this->argv_ [i+2];
50 // Decrement the value of this->argc_ to reflect the removal
51 // of '-o' option.
52 this->argc_ = this->argc_ - 2;
54 return 1;
56 case '?':
57 default:
58 // Don't do anything.
59 break;
63 return 0;
66 void
67 Base_Server::try_RT_scheduling (void)
69 int priority =
70 (ACE_Sched_Params::priority_min (ACE_SCHED_FIFO)
71 + ACE_Sched_Params::priority_max (ACE_SCHED_FIFO)) / 2;
72 priority = ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
73 priority);
75 // Enable FIFO scheduling, e.g., RT scheduling class on Solaris.
76 if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO,
77 priority,
78 ACE_SCOPE_PROCESS)) != 0)
80 if (errno == EPERM)
82 ACE_DEBUG ((LM_DEBUG,
83 "server (%P|%t): user is not superuser, "
84 "test runs in time-shared class\n"));
86 else
87 ACE_ERROR ((LM_ERROR,
88 "server (%P|%t): sched_params failed\n"));
92 int
93 Base_Server::shutdown_orb_and_poa (void)
95 try
97 this->root_poa_->destroy (1, 1);
98 this->root_poa_ = PortableServer::POA::_nil ();
100 this->orb_->destroy ();
101 this->orb_ = CORBA::ORB::_nil ();
103 catch (const CORBA::Exception& ex)
105 ex._tao_print_exception ("Exception raised shutting down ORB or POA");
106 return -1;
109 // If we have got to this point, everything has gone well. return
110 // normally
111 return 1;
115 Base_Server::start_orb_and_poa (void)
119 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
121 CORBA::Object_var poa_object =
122 this->orb_->resolve_initial_references("RootPOA");
124 if (CORBA::is_nil (poa_object.in ()))
125 ACE_ERROR_RETURN ((LM_ERROR,
126 " (%P|%t) Unable to initialize the POA.\n"),
129 this->root_poa_ = PortableServer::POA::_narrow (poa_object.in ());
131 PortableServer::POAManager_var poa_manager =
132 this->root_poa_->the_POAManager ();
134 poa_manager->activate ();
136 catch (const CORBA::Exception& ex)
138 ex._tao_print_exception ("Exception raised initialising ORB or POA");
139 return -1;
142 // If we have got to this point, everything has gone well. return
143 // normally
144 return 1;
147 void
148 Base_Server::register_servant (AMH_Servant *servant)
152 Test::Roundtrip_var roundtrip =
153 servant->_this ();
155 CORBA::String_var ior =
156 this->orb_->object_to_string (roundtrip.in ());
158 (void) this->write_ior_to_file (ior.in ());
160 catch (const CORBA::Exception& ex)
162 ex._tao_print_exception ("Exception raised while registering servant");
167 void
168 Base_Server::run_event_loop (void)
172 ACE_Time_Value period (0, 11000);
173 while (1)
175 // @@ Mayur, where's the work_pending() call?
177 // Mayur: Nope. There is purposely no work_pending call. The
178 // reactor doesn't acknowledge expired timers as work. The
179 // reactor only thinks I/O events as work. Thus, timers
180 // that have expired after all the client requests are made
181 // are never handled. This results in the server not sending
182 // (some) replies to the client and the client just ends up
183 // waiting (forever) for them.
184 this->orb_->perform_work (period);
187 catch (const CORBA::Exception& ex)
189 ex._tao_print_exception (
190 "Caught exceptionin Base_Server::run_event_loop");
195 Base_Server::write_ior_to_file (const char * ior)
197 // If the ior_output_file exists, output the ior to it
198 FILE *output_file =
199 ACE_OS::fopen (this->ior_output_file_, "w");
201 if (output_file == 0)
203 ACE_ERROR ((LM_ERROR,
204 "Cannot open output file for writing IOR: %s",
205 this->ior_output_file_));
206 return -1;
209 ACE_OS::fprintf (output_file, "%s", ior);
210 ACE_OS::fclose (output_file);
211 return 0;