Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Portable_Interceptors / Service_Context_Manipulation / server.cpp
blobc028ce679b19ae2185ac04e0a7bfaa7a1d501bab
1 #include "ace/Get_Opt.h"
2 #include "test_i.h"
3 #include "Server_ORBInitializer.h"
5 #include "tao/ORBInitializer_Registry.h"
6 #include "ace/OS_NS_stdio.h"
8 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
10 int
11 parse_args (int argc, ACE_TCHAR *argv[])
13 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
14 int c;
16 while ((c = get_opts ()) != -1)
17 switch (c)
19 case 'o':
20 ior_output_file = get_opts.opt_arg ();
21 break;
22 case '?':
23 default:
24 ACE_ERROR_RETURN ((LM_ERROR,
25 "usage: %s "
26 "-o <iorfile>"
27 "\n",
28 argv [0]),
29 -1);
31 // Indicates successful parsing of the command line
32 return 0;
35 int
36 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
38 try
40 PortableInterceptor::ORBInitializer_ptr temp_initializer;
42 ACE_NEW_RETURN (temp_initializer,
43 Server_ORBInitializer,
44 -1); // No exceptions yet!
45 PortableInterceptor::ORBInitializer_var initializer =
46 temp_initializer;
48 PortableInterceptor::register_orb_initializer (initializer.in ());
50 // Now we can create the ORB
51 CORBA::ORB_var orb =
52 CORBA::ORB_init (argc, argv);
54 CORBA::Object_var poa_object =
55 orb->resolve_initial_references ("RootPOA");
57 if (CORBA::is_nil (poa_object.in ()))
58 ACE_ERROR_RETURN ((LM_ERROR,
59 " (%P|%t) Unable to initialize the POA.\n"),
60 1);
62 PortableServer::POA_var root_poa =
63 PortableServer::POA::_narrow (poa_object.in ());
65 PortableServer::POAManager_var poa_manager =
66 root_poa->the_POAManager ();
68 poa_manager->activate ();
70 if (parse_args (argc, argv) != 0)
71 return 1;
73 Visual_i server_impl (orb.in ());
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_Interceptors::Visual_var server =
82 Test_Interceptors::Visual::_narrow (test_obj.in ());
84 CORBA::String_var ior =
85 orb->object_to_string (server.in ());
87 ACE_DEBUG ((LM_DEBUG,
88 "Test_Interceptors::Visual: <%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 "Cannot open output file for writing IOR: %s",
98 ior_output_file),
99 1);
100 ACE_OS::fprintf (output_file, "%s", ior.in ());
101 ACE_OS::fclose (output_file);
104 orb->run ();
106 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
108 root_poa->destroy (1, 1);
110 orb->destroy ();
112 catch (const CORBA::Exception& ex)
114 ex._tao_print_exception ("Caught exception in server:");
115 return 1;
118 return 0;