Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Faults / middle.cpp
blob889f297e49129c2fcf3e755e8a511142e4ce2373
1 #include "ace/Get_Opt.h"
2 #include "test_i.h"
3 #include "ace/OS_NS_stdio.h"
5 const ACE_TCHAR *ior_output_file = ACE_TEXT("middle.ior");
6 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
7 int niterations = 10;
9 int
10 parse_args (int argc, ACE_TCHAR *argv[])
12 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:k:i:"));
13 int c;
15 while ((c = get_opts ()) != -1)
16 switch (c)
18 case 'o':
19 ior_output_file = get_opts.opt_arg ();
20 break;
22 case 'k':
23 ior = get_opts.opt_arg ();
24 break;
26 case 'i':
27 niterations = ACE_OS::atoi (get_opts.opt_arg ());
28 break;
30 case '?':
31 default:
32 ACE_ERROR_RETURN ((LM_ERROR,
33 "usage: %s "
34 "-o <iorfile>"
35 "-k <ior> "
36 "-i <niterations> "
37 "\n",
38 argv [0]),
39 -1);
41 // Indicates successful parsing of the command line
42 return 0;
45 int
46 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
48 try
50 CORBA::ORB_var orb =
51 CORBA::ORB_init (argc, argv);
53 CORBA::Object_var poa_object =
54 orb->resolve_initial_references("RootPOA");
56 if (CORBA::is_nil (poa_object.in ()))
57 ACE_ERROR_RETURN ((LM_ERROR,
58 " (%P|%t) Unable to initialize the POA.\n"),
59 1);
61 PortableServer::POA_var root_poa =
62 PortableServer::POA::_narrow (poa_object.in ());
64 PortableServer::POAManager_var poa_manager =
65 root_poa->the_POAManager ();
67 poa_manager->activate ();
69 if (parse_args (argc, argv) != 0)
70 return 1;
72 CORBA::Object_var object =
73 orb->string_to_object (ior);
75 Simple_Server_var server =
76 Simple_Server::_narrow (object.in ());
78 if (CORBA::is_nil (server.in ()))
80 ACE_ERROR_RETURN ((LM_ERROR,
81 "Object reference <%s> is nil.\n",
82 ior),
83 1);
86 Middle_i *middle_impl = 0;
87 ACE_NEW_RETURN (middle_impl,
88 Middle_i (orb.in (),
89 server.in ()),
90 -1);
92 PortableServer::ServantBase_var owner_transfer(middle_impl);
94 PortableServer::ObjectId_var id =
95 root_poa->activate_object (middle_impl);
97 CORBA::Object_var object_act = root_poa->id_to_reference (id.in ());
99 Simple_Server_var middle =
100 Simple_Server::_narrow (object_act.in ());
102 CORBA::String_var ior =
103 orb->object_to_string (middle.in ());
105 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
107 // If the ior_output_file exists, output the ior to it
108 if (ior_output_file != 0)
110 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
111 if (output_file == 0)
112 ACE_ERROR_RETURN ((LM_ERROR,
113 "Cannot open output file for writing IOR: %s",
114 ior_output_file),
116 ACE_OS::fprintf (output_file, "%s", ior.in ());
117 ACE_OS::fclose (output_file);
120 orb->run ();
122 ACE_DEBUG ((LM_DEBUG, "middle - event loop finished\n"));
124 root_poa->destroy (1, 1);
127 catch (const CORBA::Exception& ex)
129 ex._tao_print_exception ("Exception caught in middle:");
130 return 1;
133 return 0;