1 // author : Boris Kolpackov <boris@kolpackov.net>
2 #include "tao/LocalObject.h"
3 #include "tao/ORBInitializer_Registry.h"
6 #include "tao/PI_Server/PI_Server.h"
8 #include "ace/OS_NS_stdio.h"
9 #include "ace/Get_Opt.h"
11 #include "StateTransferS.h"
13 const ACE_TCHAR
*ior_output_file
= ACE_TEXT ("test.ior");
16 parse_args (int argc
, ACE_TCHAR
*argv
[])
18 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:"));
21 while ((c
= get_opts ()) != -1)
25 ior_output_file
= get_opts
.opt_arg ();
30 ACE_ERROR_RETURN ((LM_ERROR
,
37 // Indicates successful parsing of the command line
41 using namespace CORBA
;
42 using namespace PortableServer
;
43 using namespace PortableInterceptor
;
46 PortableInterceptor::SlotId slot_id
;
50 class StateTransferImpl
: public virtual POA_StateTransfer
53 StateTransferImpl (ORB_ptr orb
)
54 : orb_ (ORB::_duplicate (orb
))
61 // Prepare state update.
64 CORBA::Long number
= 5;
67 Object_var
obj (orb_
->resolve_initial_references ("PICurrent"));
68 PortableInterceptor::Current_var
pic (
69 PortableInterceptor::Current::_narrow (obj
.in ()));
71 pic
->set_slot (slot_id
, state
);
76 //FUZZ: disable check_for_lack_ACE_OS
80 ACE_DEBUG ((LM_DEBUG
, "Server is shutting down.\n"));
81 this->orb_
->shutdown (0);
83 //FUZZ: enable check_for_lack_ACE_OS
91 class ReplicaController
: public virtual ServerRequestInterceptor
,
92 public virtual ::CORBA::LocalObject
95 ReplicaController (PortableInterceptor::Current_ptr pi_current
)
96 : pi_current_ (PortableInterceptor::Current::_duplicate (pi_current
))
103 return string_dup ("ReplicaController");
111 #if TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1
113 tao_ft_interception_point (ServerRequestInfo_ptr
, OctetSeq_out
)
116 #endif /*TAO_HAS_EXTENDED_FT_INTERCEPTORS*/
119 receive_request_service_contexts (ServerRequestInfo_ptr
)
124 receive_request (ServerRequestInfo_ptr ri
)
127 CORBA::Long number
= 5;
130 // After we have set things to TSC, change the value in RSC, at this
131 // moment no copy should be done anymore.
132 pi_current_
->set_slot (slot_id
, state
);
133 CORBA::Long number6
= 6;
135 ri
->set_slot (slot_id
, state
);
137 Any_var
state_get (pi_current_
->get_slot (slot_id
));
142 ACE_DEBUG ((LM_DEBUG
, "State value is correctly %d.\n", n
));
144 ACE_ERROR ((LM_ERROR
, "ERROR: State value is incorrectly %d.\n", n
));
148 send_reply (ServerRequestInfo_ptr ri
)
150 CORBA::String_var op
= ri
->operation ();
152 if (ACE_OS::strcmp (op
.in (), "number") != 0)
153 return; // Don't mess with PICurrent if not invoking test method.
155 Any_var
state (ri
->get_slot (slot_id
));
160 ACE_DEBUG ((LM_DEBUG
, "State value is correctly %d.\n", n
));
162 ACE_ERROR ((LM_ERROR
, "ERROR: State value is incorrectly %d.\n", n
));
166 send_exception (ServerRequestInfo_ptr
)
171 send_other (ServerRequestInfo_ptr
)
174 PortableInterceptor::Current_var pi_current_
;
179 class ORB_Initializer
: public virtual ORBInitializer
,
180 public virtual ::CORBA::LocalObject
184 pre_init (ORBInitInfo_ptr
)
189 post_init (ORBInitInfo_ptr info
)
191 slot_id
= info
->allocate_slot_id ();
192 ACE_DEBUG ((LM_DEBUG
, "Allocated slot with id %d.\n", slot_id
));
194 CORBA::Object_var obj
=
195 info
->resolve_initial_references ("PICurrent");
197 PortableInterceptor::Current_var pi_current
=
198 PortableInterceptor::Current::_narrow (obj
.in ());
200 if (CORBA::is_nil (pi_current
.in ()))
202 ACE_ERROR ((LM_ERROR
,
203 "(%P|%t) ERROR: Could not resolve PICurrent object.\n"));
205 throw CORBA::INTERNAL ();
208 ServerRequestInterceptor_var
interceptor (new ReplicaController (pi_current
.in ()));
209 info
->add_server_request_interceptor (interceptor
.in ());
214 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
218 ORBInitializer_var
orb_initializer (new ORB_Initializer ());
219 register_orb_initializer (orb_initializer
.in ());
221 ORB_var
orb (ORB_init (argc
, argv
));
223 if (parse_args (argc
, argv
) != 0)
226 Object_var
obj (orb
->resolve_initial_references ("RootPOA"));
228 POA_var
root_poa (POA::_narrow (obj
.in ()));
229 POAManager_var
poa_manager (root_poa
->the_POAManager ());
231 StateTransferImpl
* impl
= new StateTransferImpl (orb
.in ());
232 ServantBase_var
impl_var (impl
);
234 PortableServer::ObjectId_var id_act
=
235 root_poa
->activate_object (impl
);
237 CORBA::Object_var object
= root_poa
->id_to_reference (id_act
.in ());
239 StateTransfer_var ref
= StateTransfer::_narrow (object
.in ());
240 String_var
ior (orb
->object_to_string (ref
.in ()));
242 poa_manager
->activate ();
247 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
248 if (output_file
== 0)
250 ACE_ERROR_RETURN ((LM_ERROR
,
251 "Cannot open output file <%s> for writing "
258 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
259 ACE_OS::fclose (output_file
);
261 ACE_DEBUG ((LM_DEBUG
, "Server is ready, IOR is in '%s'\n", ior_output_file
));
264 // Run the ORB event loop.
268 root_poa
->destroy (1, 1);
271 ACE_DEBUG ((LM_DEBUG
, "Event loop finished.\n"));
273 catch (const CORBA::Exception
& ex
)
275 ex
._tao_print_exception ("Exception caught:");