Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / MT_Client / server.cpp
blob3100cffb9267be573445ca708112bdc227fe158c
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 = 0;
7 int
8 parse_args (int argc, ACE_TCHAR *argv[])
10 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
11 int c;
13 while ((c = get_opts ()) != -1)
14 switch (c)
16 case 'o':
17 ior_output_file = get_opts.opt_arg ();
18 break;
19 case '?':
20 default:
21 ACE_ERROR_RETURN ((LM_ERROR,
22 "usage: %s "
23 "-o <iorfile>"
24 "\n",
25 argv [0]),
26 -1);
28 // Indicates successful parsing of the command line
29 return 0;
32 int
33 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
35 try
37 CORBA::ORB_var orb =
38 CORBA::ORB_init (argc, argv);
40 CORBA::Object_var poa_object =
41 orb->resolve_initial_references("RootPOA");
43 if (CORBA::is_nil (poa_object.in ()))
44 ACE_ERROR_RETURN ((LM_ERROR,
45 " (%P|%t) Unable to initialize the POA.\n"),
46 1);
48 PortableServer::POA_var root_poa =
49 PortableServer::POA::_narrow (poa_object.in ());
51 PortableServer::POAManager_var poa_manager =
52 root_poa->the_POAManager ();
54 if (parse_args (argc, argv) != 0)
55 return 1;
57 Simple_Server_i server_impl (orb.in ());
59 PortableServer::ObjectId_var id =
60 root_poa->activate_object (&server_impl);
62 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
64 Simple_Server_var server =
65 Simple_Server::_narrow (object.in ());
67 CORBA::String_var ior =
68 orb->object_to_string (server.in ());
70 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
72 // If the ior_output_file exists, output the ior to it
73 if (ior_output_file != 0)
75 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
76 if (output_file == 0)
77 ACE_ERROR_RETURN ((LM_ERROR,
78 "Cannot open output file for writing IOR: %s",
79 ior_output_file),
80 1);
81 ACE_OS::fprintf (output_file, "%s", ior.in ());
82 ACE_OS::fclose (output_file);
85 poa_manager->activate ();
87 orb->run ();
89 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
91 root_poa->destroy (1, 1);
93 orb->destroy ();
95 catch (const CORBA::Exception& ex)
97 ex._tao_print_exception ("Exception caught:");
98 return 1;
101 return 0;