Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / Mixed_Sync_ASync_Events / main.cpp
blob9735c5a5d77299041c048e6a5a9918f5d375c320
1 //=============================================================================
2 /**
3 * @file main.cpp
5 * Implementation of the server.
7 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
8 * @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
9 */
10 //=============================================================================
13 #include "test_i.h"
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;
24 int
25 parse_args (int argc, ACE_TCHAR *argv[])
27 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:k:c:dm"));
28 int c;
30 while ((c = get_opts ()) != -1)
31 switch (c)
33 case 'o':
34 ior_output_file = get_opts.opt_arg ();
35 break;
36 case 'k':
37 input_ior = get_opts.opt_arg ();
38 break;
39 case 'm':
40 mode_flag = A::RM_MASTER;
41 if (ior_output_file == 0)
42 ior_output_file = ACE_TEXT ("master.ior");
43 break;
44 case 'c':
45 max_count = ACE_OS::atoi (get_opts.opt_arg ());
46 break;
47 case 'd':
48 TAO_debug_level++;
49 break;
50 case '?':
51 default:
52 ACE_ERROR_RETURN ((LM_ERROR,
53 "usage: %s "
54 "-o <iorfile>"
55 "\n",
56 argv [0]),
57 -1);
58 break;
61 if (ior_output_file == 0)
62 ior_output_file = ACE_TEXT ("slave.ior");
64 if (input_ior == 0)
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
68 return 0;
71 int
72 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
74 try
76 CORBA::ORB_var orb =
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"),
85 1);
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)
94 return 1;
96 // create, activate and initialize AMI reply handler
97 Test_Reply_i test_i_rh_srv(orb.in (),
98 max_count,
99 mode_flag);
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",
132 ior_output_file),
134 ACE_OS::fprintf (output_file, "%s", ior.in ());
135 ACE_OS::fclose (output_file);
138 poa_manager->activate ();
140 A::Test_var opponent;
141 do {
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",
154 input_ior),
158 // register opponent
159 test_i_srv.set_opponent (opponent.in ());
160 test_i_rh_srv.test_handler ().set_opponent (opponent.in ());
162 // start the show
163 if (mode_flag == A::RM_MASTER)
164 test_i_rh_srv.test_handler ().start ();
166 orb->run ();
168 root_poa->destroy (true, // ethernalize objects
169 false); // wait for completion
171 orb->destroy ();
173 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
175 catch (const CORBA::Exception& ex)
177 ex._tao_print_exception ("Caught exception:");
178 return 1;
181 return 0;