Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Portable_Interceptors / AdvSlotDblCpy / server.cpp
blobe42d0b6dadb1a5f8696449be66123823ea34baf8
1 // author : Boris Kolpackov <boris@kolpackov.net>
2 #include "tao/AnyTypeCode/AnyTypeCode_methods.h"
4 #include "tao/PI/PI.h"
5 #include "tao/PI_Server/PI_Server.h"
7 #include "tao/LocalObject.h"
8 #include "tao/ORBInitializer_Registry.h"
10 #include "ace/OS_NS_stdio.h"
11 #include "ace/Get_Opt.h"
13 #include "InvokeMeS.h"
15 using namespace CORBA;
16 using namespace PortableServer;
17 using namespace PortableInterceptor;
19 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
21 int
22 parse_args (int argc, ACE_TCHAR *argv[])
24 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
25 int c;
27 while ((c = get_opts ()) != -1)
28 switch (c)
30 case 'o':
31 ior_output_file = get_opts.opt_arg ();
32 break;
34 case '?':
35 default:
36 ACE_ERROR_RETURN ((LM_ERROR,
37 "usage: %s "
38 "-o <iorfile>"
39 "\n",
40 argv [0]),
41 -1);
43 // Indicates successful parsing of the command line
44 return 0;
48 namespace
50 PortableInterceptor::SlotId slot_id;
52 CORBA::Long getMySlot(
53 PortableInterceptor::ServerRequestInfo_ptr ri,
54 PortableInterceptor::Current_ptr pi_current,
55 const char *location,
56 const CORBA::Long correctTSCvalue,
57 const CORBA::Long correctRSCvalue)
59 CORBA::Long number= 0;
60 CORBA::String_var
61 op= ri->operation ();
63 const bool correctOperation=
64 (0 == ACE_OS::strcmp( op.in (), "invoke_me" ));
66 if (correctOperation)
68 CORBA::Any_var data;
70 ACE_DEBUG ((LM_DEBUG, "SERVER %C -> get_slot(TSC) -> is ", location));
71 data= pi_current->get_slot(slot_id);
72 CORBA::TypeCode_var
73 tc= data->type();
74 if (tc->kind() == CORBA::tk_null)
76 ACE_DEBUG ((LM_DEBUG, "EMPTY -> "));
77 if (0 != correctTSCvalue)
79 ACE_DEBUG ((LM_DEBUG, "WRONG\n"));
80 throw INTERNAL ();
82 ACE_DEBUG ((LM_DEBUG, "OK\n"));
84 else if (data.in() >>= number)
86 ACE_DEBUG ((LM_DEBUG, "long (%d) -> ", number));
87 if ((0 != correctTSCvalue) && (correctTSCvalue != number))
89 ACE_DEBUG ((LM_DEBUG, "WRONG\n"));
90 throw INTERNAL ();
92 ACE_DEBUG ((LM_DEBUG, "OK\n"));
94 else
96 ACE_DEBUG ((LM_DEBUG, "UNKNOWN TYPE\n"));
97 throw INTERNAL ();
100 ACE_DEBUG ((LM_DEBUG, "SERVER %C -> get_slot(RSC) -> is ", location));
101 data= ri->get_slot(slot_id);
103 tc= data->type();
104 if (tc->kind() == CORBA::tk_null)
106 ACE_DEBUG ((LM_DEBUG, "EMPTY -> "));
107 if (0 != correctRSCvalue)
109 ACE_DEBUG ((LM_DEBUG, "WRONG\n"));
110 throw INTERNAL ();
112 ACE_DEBUG ((LM_DEBUG, "OK\n"));
113 #if TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1
114 number= 61;
115 #else
116 number= 62;
117 #endif /* TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1 */
119 else if (data.in() >>= number)
121 ACE_DEBUG ((LM_DEBUG, "long (%d) -> ", number));
122 if ((0 != correctRSCvalue) && (correctRSCvalue != number))
124 ACE_DEBUG ((LM_DEBUG, "WRONG\n"));
125 throw INTERNAL ();
127 ACE_DEBUG ((LM_DEBUG, "OK\n"));
128 ++number;
130 else
132 ACE_DEBUG ((LM_DEBUG, "WRONG TYPE\n"));
133 throw INTERNAL();
137 return number;
140 void getAndSetMySlot(
141 PortableInterceptor::ServerRequestInfo_ptr ri,
142 PortableInterceptor::Current_ptr pi_current,
143 const char *location,
144 const CORBA::Long correctTSCvalue,
145 const CORBA::Long correctRSCvalue)
147 const CORBA::Long number=
148 getMySlot(
150 pi_current,
151 location,
152 correctTSCvalue,
153 correctRSCvalue);
155 if (number && (0 != ACE_OS::strcmp( location, "receive_request" )))
157 // Insert data into the RSC (request scope current).
158 CORBA::Any data;
159 data <<= number;
161 ACE_DEBUG ((LM_DEBUG, "SERVER %C -> set_slot(RSC)", location));
162 ri->set_slot( slot_id, data);
164 ACE_DEBUG ((LM_DEBUG, " -> long (%d)\n", number));
166 getMySlot(
168 pi_current,
169 location,
170 correctTSCvalue,
171 number);
177 class InvokeMeImpl: public virtual POA_InvokeMe
179 public:
180 InvokeMeImpl (ORB_ptr orb)
181 : orb_ (ORB::_duplicate (orb))
185 virtual void invoke_me()
187 ACE_DEBUG ((LM_DEBUG, "invoke_me\n"));
190 //FUZZ: disable check_for_lack_ACE_OS
191 virtual void
192 shutdown ()
194 ACE_DEBUG ((LM_DEBUG, "\nServer is shutting down.\n"));
195 this->orb_->shutdown (false);
197 //FUZZ: enable check_for_lack_ACE_OS
199 private:
200 ORB_var orb_;
205 class ReplicaController: public virtual ServerRequestInterceptor,
206 public virtual ::CORBA::LocalObject
208 PortableInterceptor::Current_ptr pi_current_;
209 public:
210 ReplicaController (PortableInterceptor::Current_ptr pi_current )
211 : pi_current_( pi_current )
215 virtual char*
216 name ()
218 return string_dup ("ReplicaController");
221 virtual void
222 destroy ()
226 #if TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1
227 virtual void
228 tao_ft_interception_point (ServerRequestInfo_ptr ri, OctetSeq_out)
230 getAndSetMySlot( ri, this->pi_current_, "tao_ft_interception_point", 0, 0);
232 #endif /*TAO_HAS_EXTENDED_FT_INTERCEPTORS*/
234 virtual void
235 receive_request_service_contexts (ServerRequestInfo_ptr ri)
237 #if TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1
238 getAndSetMySlot( ri, this->pi_current_, "receive_request_service_contexts", 0, 61);
239 #else
240 getAndSetMySlot( ri, this->pi_current_, "receive_request_service_contexts", 0, 0);
241 #endif /* TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1 */
244 virtual void
245 receive_request (ServerRequestInfo_ptr ri)
247 getAndSetMySlot( ri, this->pi_current_, "receive_request", 62, 62);
250 virtual void
251 send_reply (ServerRequestInfo_ptr ri)
253 getAndSetMySlot( ri, this->pi_current_, "send_reply", 62, 62);
256 virtual void
257 send_exception (ServerRequestInfo_ptr ri)
259 getAndSetMySlot( ri, this->pi_current_, "send_exception", 62, 62);
262 virtual void
263 send_other (ServerRequestInfo_ptr ri)
265 getAndSetMySlot( ri, this->pi_current_, "send_other", 62, 62);
269 class ORB_Initializer : public virtual ORBInitializer,
270 public virtual ::CORBA::LocalObject
272 public:
273 virtual void
274 pre_init (ORBInitInfo_ptr)
278 virtual void
279 post_init (ORBInitInfo_ptr info)
281 CORBA::Object_var obj =
282 info->resolve_initial_references ("PICurrent");
284 PortableInterceptor::Current_var pi_current =
285 PortableInterceptor::Current::_narrow (obj.in ());
287 slot_id = info->allocate_slot_id ();
288 ACE_DEBUG ((LM_DEBUG, "Allocated slot with id %d.\n", slot_id));
290 ServerRequestInterceptor_var
291 interceptor (new ReplicaController (pi_current.in ()));
292 info->add_server_request_interceptor (interceptor.in ());
297 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
299 ORBInitializer_var orb_initializer (new ORB_Initializer ());
300 register_orb_initializer (orb_initializer.in ());
302 ORB_var orb (ORB_init (argc, argv));
304 if (parse_args (argc, argv) != 0)
305 return 1;
307 Object_var obj (orb->resolve_initial_references ("RootPOA"));
309 POA_var root_poa (POA::_narrow (obj.in ()));
310 POAManager_var poa_manager (root_poa->the_POAManager ());
312 InvokeMeImpl* impl = new InvokeMeImpl (orb.in ());
313 ServantBase_var impl_var (impl);
315 PortableServer::ObjectId_var id_act =
316 root_poa->activate_object (impl);
318 CORBA::Object_var object = root_poa->id_to_reference (id_act.in ());
320 InvokeMe_var ref = InvokeMe::_narrow (object.in ());
321 String_var ior (orb->object_to_string (ref.in ()));
323 poa_manager->activate ();
325 // Dump the ior.
327 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
328 if (output_file == 0)
330 ACE_ERROR_RETURN ((LM_ERROR,
331 "Cannot open output file <%s> for writing "
332 "IOR: %C",
333 ior_output_file,
334 ior.in ()),
338 ACE_OS::fprintf (output_file, "%s", ior.in ());
339 ACE_OS::fclose (output_file);
341 ACE_DEBUG ((LM_DEBUG, "Server is ready, IOR is in '%s'\n", ior_output_file));
343 // Run the ORB event loop.
345 orb->run ();
347 root_poa->destroy (true, true);
348 orb->destroy ();
350 ACE_DEBUG ((LM_DEBUG, "Event loop finished.\n"));
352 return 0;