Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / PICurrent / Client.cpp
blob1c0b135a7e48d7c735d44d4dee35394d0d91176d
1 #include "TestC.h"
2 #include <iostream>
3 #include "ace/Get_Opt.h"
4 #include "ace/OS_NS_unistd.h"
5 #include "ace/Task.h"
6 #include "ace/Thread.h"
7 #include "tao/PortableInterceptorC.h"
8 #include "tao/PI/PI.h"
9 #include "tao/LocalObject.h"
10 #include "tao/ORBInitializer_Registry.h"
12 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
13 int number_of_tries = 5;
14 int ofs = 100;
15 TAO_SYNCH_MUTEX ofs_lock;
16 PortableInterceptor::Current_var pic;
17 PortableInterceptor::SlotId slot_id;
18 const IOP::ServiceId service_id = 0xdeadbeef;
20 int get_offset (void)
22 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, mon, ofs_lock, 0);
23 int r = ofs;
24 ofs += 100;
25 return r;
28 class Worker : public ACE_Task_Base
30 public:
31 Messenger_var messenger;
33 Worker (Messenger_ptr m)
34 : messenger(Messenger::_duplicate (m)) {}
36 int svc (void)
38 int offset = get_offset ();
39 CORBA::String_var message = CORBA::string_dup( "Hello!" );
40 char user [100];
41 ACE_OS::snprintf(user, 100, "client %d", offset / 100);
42 int try_count = 0;
43 for (; try_count < number_of_tries; ++try_count)
45 CORBA::Any in;
46 in <<= CORBA::ULong (try_count + offset);
47 ACE_DEBUG ((LM_DEBUG,
48 ACE_TEXT("(%P|%t) set slot data %d\n"),
49 try_count + offset));
50 pic->set_slot (slot_id, in);
51 try
53 messenger->send_message( user, "Test 1", message.inout() );
55 ACE_DEBUG ((LM_INFO,
56 ACE_TEXT("(%P|%t) received response for ")
57 ACE_TEXT("message <%d> to server\n"),
58 try_count));
60 catch (const CORBA::Exception& ex)
62 ACE_DEBUG ((LM_INFO,
63 ACE_TEXT("(%P|%t) ignoring %C\n"),
64 ex._name ()));
67 return 0;
73 class Client_Req_Int
74 : public virtual PortableInterceptor::ClientRequestInterceptor,
75 public virtual ::CORBA::LocalObject
77 public:
78 virtual char * name (void)
80 return CORBA::string_dup ("Client_Req_Int");
83 virtual void destroy (void)
87 virtual void send_poll (PortableInterceptor::ClientRequestInfo_ptr)
91 virtual void receive_reply (PortableInterceptor::ClientRequestInfo_ptr)
95 virtual void receive_other (PortableInterceptor::ClientRequestInfo_ptr)
99 virtual void receive_exception (PortableInterceptor::ClientRequestInfo_ptr)
103 virtual void send_request (PortableInterceptor::ClientRequestInfo_ptr ri)
106 if (CORBA::is_nil (this->pic_.in()))
108 ACE_GUARD (TAO_SYNCH_MUTEX, g, this->lock_);
109 if (CORBA::is_nil (this->pic_.in()))
111 int argc = 0;
112 ACE_TCHAR **argv = 0;
113 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
114 CORBA::Object_var obj = orb->resolve_initial_references ("PICurrent");
115 this->pic_ = PortableInterceptor::Current::_narrow (obj.in ());
119 CORBA::String_var op = ri->operation ();
120 if (ACE_OS::strcmp (op.in (), "send_message") != 0)
121 return;
123 CORBA::ULong v (0);
124 CORBA::Any_var out = this->pic_->get_slot (slot_id);
125 if (out >>= v)
127 ACE_DEBUG ((LM_DEBUG,
128 ACE_TEXT("(%P|%t) send_request got %u\n"), v));
129 IOP::ServiceContext sc;
130 sc.context_id = ::service_id;
132 ri->add_request_service_context (sc, 0);
134 else
136 ACE_ERROR ((LM_ERROR,
137 ACE_TEXT("(%P|%t) send_request failed to get slot data\n")));
138 ACE_ASSERT (0);
141 private:
142 TAO_SYNCH_MUTEX lock_;
143 PortableInterceptor::Current_var pic_;
147 class ORB_Initializer : public virtual PortableInterceptor::ORBInitializer,
148 public virtual ::CORBA::LocalObject
150 public:
151 virtual void
152 pre_init (PortableInterceptor::ORBInitInfo_ptr)
156 virtual void
157 post_init (PortableInterceptor::ORBInitInfo_ptr info)
159 slot_id = info->allocate_slot_id ();
160 ACE_DEBUG ((LM_DEBUG, "Allocated slot with id %d.\n", slot_id));
161 PortableInterceptor::ClientRequestInterceptor_ptr interceptor =
162 PortableInterceptor::ClientRequestInterceptor::_nil ();
164 // Install the Echo client request interceptor
165 ACE_NEW_THROW_EX (interceptor,
166 Client_Req_Int (),
167 CORBA::NO_MEMORY ());
169 PortableInterceptor::ClientRequestInterceptor_var
170 client_interceptor = interceptor;
172 info->add_client_request_interceptor (client_interceptor.in ());
178 parse_args (int argc, ACE_TCHAR *argv[])
180 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
181 int c;
183 while ((c = get_opts ()) != -1)
184 switch (c)
186 case 'k':
187 ior = get_opts.opt_arg ();
188 break;
190 case '?':
191 default:
192 ACE_ERROR_RETURN ((LM_ERROR,
193 ACE_TEXT("usage: %s ")
194 ACE_TEXT("-k <ior> ")
195 ACE_TEXT("\n"),
196 argv [0]),
197 -1);
199 // Indicates successful parsing of the command line
200 return 0;
204 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
208 PortableInterceptor::ORBInitializer_var orb_initializer =
209 new ORB_Initializer ();
210 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
212 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
214 if (parse_args (argc, argv) != 0)
215 return 1;
217 CORBA::Object_var obj = orb->string_to_object( ior );
218 Messenger_var messenger = Messenger::_narrow( obj.in() );
220 obj = orb->resolve_initial_references ("PICurrent");
221 pic = PortableInterceptor::Current::_narrow (obj.in ());
223 Worker w (messenger.in ());
224 w.activate (THR_JOINABLE | THR_NEW_LWP, 3);
225 w.wait ();
227 orb->destroy ();
229 catch(const CORBA::Exception& ex) {
230 ACE_DEBUG ((LM_DEBUG,
231 ACE_TEXT ("Client main() Caught CORBA::Exception: %C\n"),
232 ex._name ()));
233 return 1;
236 return 0;