Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / PICurrent / Client.cpp
blob36e248801f44065a9b031124958ef5260400cbf0
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 ()
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 ()
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;
72 class Client_Req_Int
73 : public virtual PortableInterceptor::ClientRequestInterceptor,
74 public virtual ::CORBA::LocalObject
76 public:
77 virtual char * name ()
79 return CORBA::string_dup ("Client_Req_Int");
82 virtual void destroy ()
86 virtual void send_poll (PortableInterceptor::ClientRequestInfo_ptr)
90 virtual void receive_reply (PortableInterceptor::ClientRequestInfo_ptr)
94 virtual void receive_other (PortableInterceptor::ClientRequestInfo_ptr)
98 virtual void receive_exception (PortableInterceptor::ClientRequestInfo_ptr)
102 virtual void send_request (PortableInterceptor::ClientRequestInfo_ptr ri)
104 if (CORBA::is_nil (this->pic_.in()))
106 ACE_GUARD (TAO_SYNCH_MUTEX, g, this->lock_);
107 if (CORBA::is_nil (this->pic_.in()))
109 int argc = 0;
110 ACE_TCHAR **argv = 0;
111 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
112 CORBA::Object_var obj = orb->resolve_initial_references ("PICurrent");
113 this->pic_ = PortableInterceptor::Current::_narrow (obj.in ());
117 CORBA::String_var op = ri->operation ();
118 if (ACE_OS::strcmp (op.in (), "send_message") != 0)
119 return;
121 CORBA::ULong v (0);
122 CORBA::Any_var out = this->pic_->get_slot (slot_id);
123 if (out >>= v)
125 ACE_DEBUG ((LM_DEBUG,
126 ACE_TEXT("(%P|%t) send_request got %u\n"), v));
127 IOP::ServiceContext sc;
128 sc.context_id = ::service_id;
130 ri->add_request_service_context (sc, 0);
132 else
134 ACE_ERROR ((LM_ERROR,
135 ACE_TEXT("(%P|%t) send_request failed to get slot data\n")));
136 ACE_ASSERT (0);
139 private:
140 TAO_SYNCH_MUTEX lock_;
141 PortableInterceptor::Current_var pic_;
144 class ORB_Initializer : public virtual PortableInterceptor::ORBInitializer,
145 public virtual ::CORBA::LocalObject
147 public:
148 virtual void
149 pre_init (PortableInterceptor::ORBInitInfo_ptr)
153 virtual void
154 post_init (PortableInterceptor::ORBInitInfo_ptr info)
156 slot_id = info->allocate_slot_id ();
157 ACE_DEBUG ((LM_DEBUG, "Allocated slot with id %d.\n", slot_id));
158 PortableInterceptor::ClientRequestInterceptor_ptr interceptor =
159 PortableInterceptor::ClientRequestInterceptor::_nil ();
161 // Install the Echo client request interceptor
162 ACE_NEW_THROW_EX (interceptor,
163 Client_Req_Int (),
164 CORBA::NO_MEMORY ());
166 PortableInterceptor::ClientRequestInterceptor_var
167 client_interceptor = interceptor;
169 info->add_client_request_interceptor (client_interceptor.in ());
174 parse_args (int argc, ACE_TCHAR *argv[])
176 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
177 int c;
179 while ((c = get_opts ()) != -1)
180 switch (c)
182 case 'k':
183 ior = get_opts.opt_arg ();
184 break;
186 case '?':
187 default:
188 ACE_ERROR_RETURN ((LM_ERROR,
189 ACE_TEXT("usage: %s ")
190 ACE_TEXT("-k <ior> ")
191 ACE_TEXT("\n"),
192 argv [0]),
193 -1);
195 // Indicates successful parsing of the command line
196 return 0;
200 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
204 PortableInterceptor::ORBInitializer_var orb_initializer =
205 new ORB_Initializer ();
206 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
208 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
210 if (parse_args (argc, argv) != 0)
211 return 1;
213 CORBA::Object_var obj = orb->string_to_object(ior);
214 Messenger_var messenger = Messenger::_narrow(obj.in());
216 obj = orb->resolve_initial_references ("PICurrent");
217 pic = PortableInterceptor::Current::_narrow (obj.in ());
219 Worker w (messenger.in ());
220 w.activate (THR_JOINABLE | THR_NEW_LWP, 3);
221 w.wait ();
223 orb->destroy ();
225 catch(const CORBA::Exception& ex) {
226 ACE_DEBUG ((LM_DEBUG,
227 ACE_TEXT ("Client main() Caught CORBA::Exception: %C\n"),
228 ex._name ()));
229 return 1;
232 return 0;