Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_1495_Regression / Threaded_Server.cpp
blob493f3b3dc2046e62a66b2919250aecf2d5e64696
1 #include "ace/Get_Opt.h"
2 #include "ace/OS_NS_stdio.h"
3 #include "test_i.h"
4 #include "server_interceptor.h"
5 #include "Server_ORBInitializer.h"
6 #include "tao/ORBInitializer_Registry.h"
8 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
9 const ACE_TCHAR *ior_input_file = ACE_TEXT("file://thr_server.ior");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("i:o:"));
15 int c;
18 while ((c = get_opts ()) != -1)
19 switch (c)
21 case 'i':
22 ior_input_file = get_opts.opt_arg ();
23 break;
24 case 'o':
25 ior_output_file = get_opts.opt_arg ();
26 break;
27 case '?':
28 default:
29 ACE_ERROR_RETURN ((LM_ERROR,
30 "usage: %s "
31 "-i <iorfile>"
32 "-o <iorfile>"
33 "\n",
34 argv [0]),
35 -1);
37 // Indicates successful parsing of the command line
38 return 0;
41 int
42 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
44 try
46 // Create the ORB initializer.
47 Server_ORBInitializer *temp_initializer = 0;
48 ACE_NEW_RETURN (temp_initializer,
49 Server_ORBInitializer,
50 -1);
52 PortableInterceptor::ORBInitializer_var initializer =
53 temp_initializer;
55 PortableInterceptor::register_orb_initializer (initializer.in ());
57 // Now initialize the ORB.
58 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv,
59 "Remote_Server_ORB");
61 CORBA::Object_var poa_object =
62 orb->resolve_initial_references ("RootPOA");
64 if (CORBA::is_nil (poa_object.in ()))
66 ACE_ERROR_RETURN ((LM_ERROR,
67 " (%P|%t) Unable to initialize the POA.\n" ),
68 1);
71 PortableServer::POA_var root_poa =
72 PortableServer::POA::_narrow (poa_object.in ());
74 PortableServer::POAManager_var poa_manager =
75 root_poa->the_POAManager ();
77 poa_manager->activate ();
79 if (parse_args (argc, argv) != 0)
81 return 1;
84 // Create the interceptor.
85 Echo_Server_Request_Interceptor * server_interceptor =
86 temp_initializer->server_interceptor();
88 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);
96 // Pull in the ior from the remote server to use as the forward location.
97 CORBA::Object_var forward_location = orb->string_to_object (ior_input_file);
99 if (CORBA::is_nil (forward_location.in ()))
101 ACE_ERROR_RETURN ((LM_ERROR,
102 "Object reference <%s> is nil\n",
103 ior_input_file),
107 server_interceptor->forward_reference (forward_location.in ());
109 Bug1495_i server_impl (orb.in ());
111 PortableServer::ObjectId_var id =
112 root_poa->activate_object (&server_impl);
114 CORBA::Object_var test_obj =
115 root_poa->id_to_reference (id.in ());
117 Bug1495_Regression::Bug1495_var server =
118 Bug1495_Regression::Bug1495::_narrow (test_obj.in ());
120 CORBA::String_var ior =
121 orb->object_to_string (server.in ());
123 // Output the server IOR to a file
124 if (ior_output_file != 0)
126 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
128 if (output_file == 0)
130 ACE_ERROR_RETURN ((LM_ERROR,
131 "Cannot open output file for writing the "
132 "server IOR: %s", ior_output_file),
136 ACE_OS::fprintf (output_file, "%s", ior.in ());
137 ACE_OS::fclose (output_file);
140 ACE_Time_Value tv (15, 0);
142 orb->run (tv);
144 if (server_interceptor->forward_location_done() == false)
146 ACE_ERROR ((LM_ERROR, "ERROR: Forward location has not occurred!\n"));
149 ACE_DEBUG ((LM_DEBUG, "Threaded Server event loop finished\n"));
150 root_poa->destroy (1, 1);
152 orb->destroy ();
154 catch (const CORBA::Exception& ex)
156 ex._tao_print_exception ("Caught an exception in server:");
157 return 1;
160 ACE_DEBUG ((LM_DEBUG, "Threaded Server ready\n"));
162 return 0;