1 #include "ace/OS_NS_stdio.h"
2 #include "ace/OS_NS_unistd.h"
3 #include "ace/Get_Opt.h"
6 int num_calls
= 10; // total calls client is going to make
8 // This should equal num_calls within 'sleep * num_calls' seconds
9 int calls_received
= 0;
12 parse_args (int argc
, ACE_TCHAR
*argv
[])
14 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("n:"));
17 while ((c
= get_opts ()) != -1)
21 num_calls
= ACE_OS::atoi (get_opts
.opt_arg ());
30 /***************************/
31 /*** Servant Declaration ***/
34 : public virtual POA_Test::AMH_Roundtrip
37 ST_AMH_Servant (CORBA::ORB_ptr orb
);
39 void test_method (Test::AMH_RoundtripResponseHandler_ptr _tao_rh
,
40 Test::Timestamp send_time
);
46 /***************************/
47 /*** Servant Definition ***/
49 ST_AMH_Servant::ST_AMH_Servant (CORBA::ORB_ptr orb
)
50 : orb_ (CORBA::ORB::_duplicate (orb
))
55 ST_AMH_Servant::test_method (Test::AMH_RoundtripResponseHandler_ptr
,
59 ACE_DEBUG ((LM_DEBUG
, "Received Timestamp # %d on %T\n", calls_received
));
62 // When _tao_rh destructor is called, it shouldn't send anything to
66 /*** Server Declaration ***/
69 Class that performs all 'dirty' initialisation work that is common to
70 all the AMH servers and 'hides' all the common ORB functions.
75 ST_AMH_Server (int *argc
, ACE_TCHAR
**argv
);
76 virtual ~ST_AMH_Server () = default;
78 /// ORB initialization stuff
79 int start_orb_and_poa ();
81 /// register the servant with the poa
82 virtual void register_servant (ST_AMH_Servant
*servant
);
84 /// orb-perform_work () abstraction
85 virtual void run_event_loop ();
87 /// do post-run cleanup. This is necessary here because the servant
88 /// supplied to register_servant happens to be allocated on the
89 /// stack, and done after the instance of ST_AMH_Server is
90 /// created. This leads to the servant being destroyed before this
91 /// class's destructor. And alternative solution would be allocate
92 /// the servant on the stack and rely on the reference count to
94 virtual void cleanup ();
97 /// Accesor method (for servants) to the initialized ORB
98 CORBA::ORB_ptr
orb () { return this->orb_
.in (); }
103 char *ior_output_file_
;
105 PortableServer::POA_var root_poa_
;
108 /// Write servant IOR to file specified with the '-o' option
109 int write_ior_to_file (CORBA::String_var ior
);
112 /*** Server Declaration ***/
114 ST_AMH_Server::ST_AMH_Server (int* argc
, ACE_TCHAR
**argv
)
118 this->ior_output_file_
= const_cast<char*> ("test.ior");
122 ST_AMH_Server::cleanup ()
126 this->root_poa_
->destroy (1, 1);
128 this->orb_
->destroy ();
130 catch (const CORBA::Exception
& ex
)
132 ex
._tao_print_exception ("Exception caught:");
138 ST_AMH_Server::start_orb_and_poa ()
142 this->orb_
= CORBA::ORB_init (*(this->argc_
),
145 CORBA::Object_var poa_object
=
146 this->orb_
->resolve_initial_references("RootPOA");
148 if (CORBA::is_nil (poa_object
.in ()))
149 ACE_ERROR_RETURN ((LM_ERROR
,
150 " (%P|%t) Unable to initialize the POA.\n"),
153 this->root_poa_
= PortableServer::POA::_narrow (poa_object
.in ());
155 PortableServer::POAManager_var poa_manager
=
156 this->root_poa_
->the_POAManager ();
158 poa_manager
->activate ();
160 catch (const CORBA::Exception
& ex
)
162 ex
._tao_print_exception ("Exception caught:");
170 ST_AMH_Server::register_servant (ST_AMH_Servant
*servant
)
174 CORBA::Object_var poa_object
=
175 this->orb_
->resolve_initial_references("RootPOA");
177 PortableServer::POA_var root_poa
=
178 PortableServer::POA::_narrow (poa_object
.in ());
180 PortableServer::ObjectId_var id
=
181 root_poa
->activate_object (servant
);
183 CORBA::Object_var object
= root_poa
->id_to_reference (id
.in ());
185 Test::Roundtrip_var roundtrip
=
186 Test::Roundtrip::_narrow (object
.in ());
188 CORBA::String_var ior
=
189 this->orb_
->object_to_string (roundtrip
.in ());
191 (void) this->write_ior_to_file (ior
);
193 catch (const CORBA::Exception
& ex
)
195 ex
._tao_print_exception ("Exception caught:");
200 ST_AMH_Server::run_event_loop ()
206 ACE_Time_Value
period (0, 11000);
207 this->orb_
->perform_work (&period
);
209 // when all calls from client have been received, exit
210 if (calls_received
== num_calls
)
214 catch (const CORBA::Exception
&)
219 ST_AMH_Server::write_ior_to_file (CORBA::String_var ior
)
221 // If the ior_output_file exists, output the ior to it
222 FILE *output_file
= ACE_OS::fopen (ST_AMH_Server::ior_output_file_
,
224 if (output_file
== 0)
226 ACE_ERROR ((LM_ERROR
,
227 "Cannot open output file for writing IOR: %s",
228 ST_AMH_Server::ior_output_file_
));
232 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
233 ACE_OS::fclose (output_file
);
239 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
241 ST_AMH_Server
amh_server (&argc
, argv
);
243 amh_server
.start_orb_and_poa ();
245 if (parse_args (argc
, argv
) != 0)
248 ST_AMH_Servant
servant (amh_server
.orb ());
250 amh_server
.register_servant (&servant
);
252 amh_server
.run_event_loop ();
254 amh_server
.cleanup ();