1 //=============================================================================
5 * Implementation of the server.
7 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
8 * @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
10 //=============================================================================
14 #include "tao/debug.h"
15 #include "ace/OS_NS_stdio.h"
16 #include "ace/Get_Opt.h"
17 #include "ace/OS_NS_unistd.h"
19 const ACE_TCHAR
*ior_output_file
= 0;
20 const ACE_TCHAR
*input_ior
= 0;
21 A::RunMode mode_flag
= A::RM_SLAVE
;
22 CORBA::ULong max_count
= 20;
25 parse_args (int argc
, ACE_TCHAR
*argv
[])
27 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:k:c:dm"));
30 while ((c
= get_opts ()) != -1)
34 ior_output_file
= get_opts
.opt_arg ();
37 input_ior
= get_opts
.opt_arg ();
40 mode_flag
= A::RM_MASTER
;
41 if (ior_output_file
== 0)
42 ior_output_file
= ACE_TEXT ("master.ior");
45 max_count
= ACE_OS::atoi (get_opts
.opt_arg ());
52 ACE_ERROR_RETURN ((LM_ERROR
,
61 if (ior_output_file
== 0)
62 ior_output_file
= ACE_TEXT ("slave.ior");
65 input_ior
= (mode_flag
== A::RM_SLAVE
? ACE_TEXT ("file://master.ior") : ACE_TEXT ("file://slave.ior"));
67 // Indicates successful parsing of the command line
72 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
77 CORBA::ORB_init (argc
, argv
);
79 CORBA::Object_var poa_object
=
80 orb
->resolve_initial_references("RootPOA");
82 if (CORBA::is_nil (poa_object
.in ()))
83 ACE_ERROR_RETURN ((LM_ERROR
,
84 " (%P|%t) Unable to initialize the POA.\n"),
87 PortableServer::POA_var root_poa
=
88 PortableServer::POA::_narrow (poa_object
.in ());
90 PortableServer::POAManager_var poa_manager
=
91 root_poa
->the_POAManager ();
93 if (parse_args (argc
, argv
) != 0)
96 // create, activate and initialize AMI reply handler
97 Test_Reply_i
test_i_rh_srv(orb
.in (),
100 PortableServer::ObjectId_var id
=
101 root_poa
->activate_object (&test_i_rh_srv
);
103 CORBA::Object_var object
= root_poa
->id_to_reference (id
.in ());
105 A::AMI_TestHandler_var rh
=
106 A::AMI_TestHandler::_narrow (object
.in ());
108 test_i_rh_srv
.test_handler ().set_reply_handler (rh
.in ());
110 // create and activate test servant
111 Test_i
test_i_srv (orb
.in (), rh
.in (), mode_flag
);
113 id
= root_poa
->activate_object (&test_i_srv
);
115 object
= root_poa
->id_to_reference (id
.in ());
117 A::Test_var test_var
=
118 A::Test::_narrow (object
.in ());
120 CORBA::String_var ior
=
121 orb
->object_to_string (test_var
.in ());
123 ACE_DEBUG ((LM_DEBUG
, "Servant activated\n"));
125 // If the ior_output_file exists, output the ior to it
126 if (ior_output_file
!= 0)
128 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
129 if (output_file
== 0)
130 ACE_ERROR_RETURN ((LM_ERROR
,
131 "Cannot open output file for writing IOR: %s",
134 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
135 ACE_OS::fclose (output_file
);
138 poa_manager
->activate ();
140 A::Test_var opponent
;
142 if (mode_flag
== A::RM_SLAVE
)
143 ACE_OS::sleep (ACE_Time_Value (0, 100));
145 // get object reference for opponent
146 object
= orb
->string_to_object (input_ior
);
147 opponent
= A::Test::_narrow (object
.in ());
148 } while (mode_flag
== A::RM_SLAVE
&& CORBA::is_nil (opponent
.in ()));
150 if (CORBA::is_nil (opponent
.in ()))
152 ACE_ERROR_RETURN ((LM_ERROR
,
153 "Cannot resolve opponent IOR: %s",
159 test_i_srv
.set_opponent (opponent
.in ());
160 test_i_rh_srv
.test_handler ().set_opponent (opponent
.in ());
163 if (mode_flag
== A::RM_MASTER
)
164 test_i_rh_srv
.test_handler ().start ();
168 root_poa
->destroy (true, // ethernalize objects
169 false); // wait for completion
173 ACE_DEBUG ((LM_DEBUG
, "event loop finished\n"));
175 catch (const CORBA::Exception
& ex
)
177 ex
._tao_print_exception ("Caught exception:");