Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Portable_Interceptors / Bug_1559 / server.cpp
blob8b40121a747638d30712ce873b3e41155598972c
1 #include "ace/Get_Opt.h"
2 #include "ace/OS_NS_stdio.h"
3 #include "test_i.h"
4 #include "Server_ORBInitializer.h"
5 #include "tao/ORBInitializer_Registry.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
9 int
10 parse_args (int argc, ACE_TCHAR *argv[])
12 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
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;
21 case '?':
22 default:
23 ACE_ERROR_RETURN ((LM_ERROR,
24 "usage: %s "
25 "-o <iorfile>"
26 "\n",
27 argv [0]),
28 -1);
30 // Indicates successful parsing of the command line
31 return 0;
34 int
35 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
37 try
39 Server_ORBInitializer * temp_initializer = 0;
41 ACE_NEW_RETURN (temp_initializer,
42 Server_ORBInitializer,
43 -1); // No exceptions yet!
44 PortableInterceptor::ORBInitializer_var initializer =
45 temp_initializer;
47 PortableInterceptor::register_orb_initializer (initializer.in ());
49 // Now we can create the ORB
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 Visual_i server_impl (orb.in ());
74 PortableServer::ObjectId_var id =
75 root_poa->activate_object (&server_impl);
77 // Create a second object to receive forwarded requests
78 Visual_i forward_server_impl (orb.in ());
80 PortableServer::ObjectId_var forward_id =
81 root_poa->activate_object (&forward_server_impl);
83 CORBA::Object_var forward_test_obj =
84 root_poa->id_to_reference (forward_id.in ());
86 Echo_Server_Request_Interceptor * server_interceptor =
87 temp_initializer->server_interceptor ();
89 if (server_interceptor == 0)
90 ACE_ERROR_RETURN ((LM_ERROR,
91 "(%P|%t) Could not obtain reference to "
92 "server request interceptor.\n"),
93 -1);
95 server_interceptor->forward_reference (forward_test_obj.in ());
97 CORBA::Object_var test_obj =
98 root_poa->id_to_reference (id.in ());
100 Test_Interceptors::Visual_var server =
101 Test_Interceptors::Visual::_narrow (test_obj.in ());
103 CORBA::String_var ior =
104 orb->object_to_string (server.in ());
106 ACE_DEBUG ((LM_DEBUG,
107 "Test_Interceptors::Visual: <%C>\n",
108 ior.in ()));
110 // If the ior_output_file exists, output the ior to it
111 if (ior_output_file != 0)
113 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
114 if (output_file == 0)
115 ACE_ERROR_RETURN ((LM_ERROR,
116 "Cannot open output file for writing IOR: %s",
117 ior_output_file),
119 ACE_OS::fprintf (output_file, "%s", ior.in ());
120 ACE_OS::fclose (output_file);
123 orb->run ();
125 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
127 root_poa->destroy (1, 1);
129 orb->destroy ();
131 catch (const CORBA::Exception& ex)
133 ex._tao_print_exception ("Caught exception in server:");
134 return 1;
137 return 0;