Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Portable_Interceptors / AdvSlotExt / server.cpp
blob16a2f0c3df11e4f47b90bc8f39d165288ae128cf
1 // author : Boris Kolpackov <boris@kolpackov.net>
2 #include "tao/LocalObject.h"
3 #include "tao/ORBInitializer_Registry.h"
5 #include "tao/PI/PI.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");
15 int
16 parse_args (int argc, ACE_TCHAR *argv[])
18 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
19 int c;
21 while ((c = get_opts ()) != -1)
22 switch (c)
24 case 'o':
25 ior_output_file = get_opts.opt_arg ();
26 break;
28 case '?':
29 default:
30 ACE_ERROR_RETURN ((LM_ERROR,
31 "usage: %s "
32 "-o <iorfile>"
33 "\n",
34 argv [0]),
35 -1);
37 // Indicates successful parsing of the command line
38 return 0;
41 using namespace CORBA;
42 using namespace PortableServer;
43 using namespace PortableInterceptor;
46 PortableInterceptor::SlotId slot_id;
50 class StateTransferImpl: public virtual POA_StateTransfer
52 public:
53 StateTransferImpl (ORB_ptr orb)
54 : orb_ (ORB::_duplicate (orb))
58 virtual Short
59 number ()
61 // Prepare state update.
63 Any state;
64 CORBA::Long number = 5;
65 state <<= number;
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);
73 return 1;
76 //FUZZ: disable check_for_lack_ACE_OS
77 virtual void
78 shutdown ()
80 ACE_DEBUG ((LM_DEBUG, "Server is shutting down.\n"));
81 this->orb_->shutdown (0);
83 //FUZZ: enable check_for_lack_ACE_OS
85 private:
86 ORB_var orb_;
91 class ReplicaController: public virtual ServerRequestInterceptor,
92 public virtual ::CORBA::LocalObject
94 public:
95 ReplicaController (PortableInterceptor::Current_ptr pi_current)
96 : pi_current_ (PortableInterceptor::Current::_duplicate (pi_current))
100 virtual char*
101 name ()
103 return string_dup ("ReplicaController");
106 virtual void
107 destroy ()
111 #if TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1
112 virtual void
113 tao_ft_interception_point (ServerRequestInfo_ptr, OctetSeq_out)
116 #endif /*TAO_HAS_EXTENDED_FT_INTERCEPTORS*/
118 virtual void
119 receive_request_service_contexts (ServerRequestInfo_ptr)
123 virtual void
124 receive_request (ServerRequestInfo_ptr ri)
126 Any state;
127 CORBA::Long number = 5;
128 state <<= number;
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;
134 state <<= number6;
135 ri->set_slot (slot_id, state);
137 Any_var state_get (pi_current_->get_slot (slot_id));
138 CORBA::Long n (0);
139 state_get >>= n;
141 if (n == 5)
142 ACE_DEBUG ((LM_DEBUG, "State value is correctly %d.\n", n));
143 else
144 ACE_ERROR ((LM_ERROR, "ERROR: State value is incorrectly %d.\n", n));
147 virtual void
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));
156 CORBA::Long n (0);
157 state >>= n;
159 if (n == 5)
160 ACE_DEBUG ((LM_DEBUG, "State value is correctly %d.\n", n));
161 else
162 ACE_ERROR ((LM_ERROR, "ERROR: State value is incorrectly %d.\n", n));
165 virtual void
166 send_exception (ServerRequestInfo_ptr)
170 virtual void
171 send_other (ServerRequestInfo_ptr)
174 PortableInterceptor::Current_var pi_current_;
179 class ORB_Initializer : public virtual ORBInitializer,
180 public virtual ::CORBA::LocalObject
182 public:
183 virtual void
184 pre_init (ORBInitInfo_ptr)
188 virtual void
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)
224 return 1;
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 ();
245 // Dump the ior.
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 "
252 "IOR: %s",
253 ior_output_file,
254 ior.in ()),
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.
266 orb->run ();
268 root_poa->destroy (1, 1);
269 orb->destroy ();
271 ACE_DEBUG ((LM_DEBUG, "Event loop finished.\n"));
273 catch (const CORBA::Exception& ex)
275 ex._tao_print_exception ("Exception caught:");
276 return 1;
279 return 0;