2 //=============================================================================
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"
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;
23 parse_args (int argc
, ACE_TCHAR
**argv
)
25 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("f:o:"));
28 while ((c
= get_opts ()) != -1)
32 forward_to_ior
= get_opts
.opt_arg ();
36 ior_output_file
= get_opts
.opt_arg ();
40 ACE_ERROR_RETURN ((LM_ERROR
,
43 "-o output file for IOR\n"
49 if (ior_output_file
== 0)
50 ACE_ERROR_RETURN ((LM_ERROR
,
51 "output IOR file not specified\n"),
54 // Indicates successful parsing of command line.
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);
65 // Tell the POA to use a servant manager.
67 root_poa
->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER
);
69 // Allow implicit activation.
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",
83 // Creation of childPOAs is over. Destroy the Policy objects.
84 for (CORBA::ULong i
= 0;
85 i
< policies
.length ();
88 policies
[i
]->destroy ();
91 return child_poa
._retn ();
95 create_servant_manager (CORBA::ORB_ptr orb
,
96 PortableServer::POA_ptr child_poa
)
98 CORBA::Object_var forward_to
;
102 orb
->string_to_object (forward_to_ior
);
105 ServantActivator
*activator
= 0;
106 ACE_NEW_RETURN (activator
,
107 ServantActivator (orb
,
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 (),
124 ACE_NEW_RETURN (servant
,
131 PortableServer::ServantBase_var
servant_var (servant
);
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",
148 ACE_OS::fprintf (output_file
,
151 ACE_OS::fclose (output_file
);
158 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
162 // Initialize the ORB first.
164 CORBA::ORB_init (argc
, argv
);
167 parse_args (argc
, argv
);
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 (),
190 poa_manager
->activate ();
198 catch (const CORBA::Exception
& ex
)
200 ex
._tao_print_exception ("Exception caught in server");