Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_3524_Regression / server.cpp
blob5f7bebdef933f9fb234a5cd1bf97ce5ee931f988
1 // -*- C++ -*-
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "test_i.h"
6 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
8 int
9 parse_args (int argc, ACE_TCHAR *argv[])
11 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("o:"));
12 int c;
14 while ((c = get_opts ()) != -1)
15 switch (c)
17 case 'o':
18 ior_output_file = get_opts.opt_arg ();
19 break;
20 case '?':
21 default:
22 ACE_ERROR_RETURN ((LM_ERROR,
23 ACE_TEXT ("usage: %s ")
24 ACE_TEXT ("-o <iorfile>")
25 ACE_TEXT ("\n"),
26 argv [0]),
27 -1);
29 // Indicates successful parsing of the command line
30 return 0;
33 int
34 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
36 try
38 CORBA::ORB_var orb =
39 CORBA::ORB_init (argc, argv);
41 ::Test::vtp_init *vtp_factory = 0;
42 ACE_NEW_THROW_EX (vtp_factory,
43 ::Test::vtp_init,
44 CORBA::NO_MEMORY ());
45 CORBA::ValueFactoryBase_var vtp_factory_owner = vtp_factory;
47 orb->register_value_factory (vtp_factory->tao_repository_id (),
48 vtp_factory);
50 CORBA::Object_var poa_object =
51 orb->resolve_initial_references ("RootPOA");
53 if (CORBA::is_nil (poa_object.in ()))
54 ACE_ERROR_RETURN ((LM_ERROR,
55 ACE_TEXT ("Unable to initialize the POA.\n")),
56 1);
58 PortableServer::POA_var root_poa =
59 PortableServer::POA::_narrow (poa_object.in ());
61 PortableServer::POAManager_var poa_manager =
62 root_poa->the_POAManager ();
64 poa_manager->activate ();
66 if (parse_args (argc, argv) != 0)
67 return 1;
69 A_i *server_impl = 0;
70 ACE_NEW_RETURN (server_impl,
71 A_i (orb.in (), vtp_factory),
72 -1);
73 PortableServer::ServantBase_var safe (server_impl);
75 PortableServer::ObjectId_var id =
76 root_poa->activate_object (server_impl);
78 CORBA::Object_var test_obj =
79 root_poa->id_to_reference (id.in ());
81 Test::A_var server =
82 Test::A::_narrow (test_obj.in ());
84 CORBA::String_var ior =
85 orb->object_to_string (server.in ());
87 ACE_DEBUG ((LM_DEBUG,
88 ACE_TEXT ("Test::A: <%C>\n"),
89 ior.in ()));
91 // If the ior_output_file exists, output the ior to it
92 if (ior_output_file != 0)
94 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
95 if (output_file == 0)
96 ACE_ERROR_RETURN ((LM_ERROR,
97 ACE_TEXT ("Cannot open output file ")
98 ACE_TEXT ("for writing IOR: %s"),
99 ior_output_file),
101 ACE_OS::fprintf (output_file, "%s", ior.in ());
102 ACE_OS::fclose (output_file);
105 orb->run ();
107 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("event loop finished\n")));
109 root_poa->destroy (1, 1);
111 orb->destroy ();
113 catch (const CORBA::Exception& ex)
115 ex._tao_print_exception (
116 "Caught exception in server:");
117 return 1;
120 return 0;