Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / POA / Forwarding / server.cpp
blob9ab88d53801252fdce95e7847aa54b4d8ed07c8d
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * Server that receives requests. The server can be asked to
7 * forward new requests to another server.
9 * @author Irfan Pyarali Michael Kircher
11 //=============================================================================
14 #include "ace/Get_Opt.h"
15 #include "test_i.h"
16 #include "Servant_Activator.h"
17 #include "ace/OS_NS_stdio.h"
19 static const ACE_TCHAR *ior_output_file = 0;
20 static const ACE_TCHAR *forward_to_ior = 0;
22 static int
23 parse_args (int argc, ACE_TCHAR **argv)
25 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:o:"));
26 int c;
28 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'f':
32 forward_to_ior = get_opts.opt_arg ();
33 break;
35 case 'o':
36 ior_output_file = get_opts.opt_arg ();
37 break;
38 case '?':
39 default:
40 ACE_ERROR_RETURN ((LM_ERROR,
41 "\nusage: %s\n"
42 "-f forward to IOR\n"
43 "-o output file for IOR\n"
44 "\n",
45 argv [0]),
46 -1);
49 if (ior_output_file == 0)
50 ACE_ERROR_RETURN ((LM_ERROR,
51 "output IOR file not specified\n"),
52 -1);
54 // Indicates successful parsing of command line.
55 return 0;
58 PortableServer::POA_ptr
59 setup_poa (PortableServer::POA_ptr root_poa)
61 // Policies for the childPOA to be created.
62 CORBA::PolicyList policies (2);
63 policies.length (2);
65 // Tell the POA to use a servant manager.
66 policies[0] =
67 root_poa->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER);
69 // Allow implicit activation.
70 policies[1] =
71 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
73 PortableServer::POAManager_var poa_manager =
74 root_poa->the_POAManager ();
76 // Create POA as child of RootPOA with the above policies. This POA
77 // will use a SERVANT_ACTIVATOR because of RETAIN policy.
78 PortableServer::POA_var child_poa =
79 root_poa->create_POA ("childPOA",
80 poa_manager.in (),
81 policies);
83 // Creation of childPOAs is over. Destroy the Policy objects.
84 for (CORBA::ULong i = 0;
85 i < policies.length ();
86 ++i)
88 policies[i]->destroy ();
91 return child_poa._retn ();
94 ServantActivator *
95 create_servant_manager (CORBA::ORB_ptr orb,
96 PortableServer::POA_ptr child_poa)
98 CORBA::Object_var forward_to;
99 if (forward_to_ior)
101 forward_to =
102 orb->string_to_object (forward_to_ior);
105 ServantActivator *activator = 0;
106 ACE_NEW_RETURN (activator,
107 ServantActivator (orb,
108 forward_to.in ()),
111 // Set ServantActivator to be the servant activator.
112 child_poa->set_servant_manager (activator);
113 // For the code above, we're using the CORBA 3.0 servant manager
114 // semantics supported by TAO. For CORBA 2.x ORBs you'd need to
115 // use the following code in place of the previous line:
117 // PortableServer::ServantManager_var servant_activator =
118 // activator->_this ();
120 // child_poa->set_servant_manager (servant_activator.in (),
121 //);
123 test_i *servant = 0;
124 ACE_NEW_RETURN (servant,
125 test_i (orb,
126 child_poa,
127 *activator,
128 127),
131 PortableServer::ServantBase_var servant_var (servant);
133 test_var test =
134 servant->_this ();
136 CORBA::String_var ior =
137 orb->object_to_string (test.in ());
139 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
140 if (output_file == 0)
142 ACE_ERROR ((LM_ERROR,
143 "Cannot open output file for writing IOR: %s\n",
144 ior_output_file));
146 else
148 ACE_OS::fprintf (output_file,
149 "%s",
150 ior.in ());
151 ACE_OS::fclose (output_file);
154 return activator;
158 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
162 // Initialize the ORB first.
163 CORBA::ORB_var orb =
164 CORBA::ORB_init (argc, argv);
166 int result =
167 parse_args (argc, argv);
169 if (result == -1)
170 return -1;
172 // Obtain the RootPOA.
173 CORBA::Object_var obj =
174 orb->resolve_initial_references ("RootPOA");
176 PortableServer::POA_var root_poa =
177 PortableServer::POA::_narrow (obj.in ());
179 // Get the POAManager of the RootPOA.
180 PortableServer::POAManager_var poa_manager =
181 root_poa->the_POAManager ();
183 PortableServer::POA_var child_poa =
184 setup_poa (root_poa.in ());
186 ServantActivator *manager =
187 create_servant_manager (orb.in (),
188 child_poa.in ());
190 poa_manager->activate ();
192 orb->run ();
194 orb->destroy ();
196 delete manager;
198 catch (const CORBA::Exception& ex)
200 ex._tao_print_exception ("Exception caught in server");
201 return -1;
204 return 0;