Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Big_AMI / client.cpp
blob355ead34c61c632ad175a4704cdb393fc780f31f
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * A very simple client which uses the AMI callback model.
8 * @author Johnny Willemsen <jwillemsen@remedy.nl>
9 */
10 //=============================================================================
13 #include "ace/Get_Opt.h"
14 #include "ace/Task.h"
16 #include "ami_testS.h"
18 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
19 int niterations = 5;
20 int shutdown_flag = 0;
21 int debug = 1;
22 CORBA::ULong payload_size = 128000;
24 int
25 parse_args (int argc, ACE_TCHAR *argv[])
27 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("db:k:i:x"));
28 int c;
30 while ((c = get_opts ()) != -1)
31 switch (c)
33 case 'd':
34 debug = 1;
35 break;
36 case 'k':
37 ior = get_opts.opt_arg ();
38 break;
39 case 'i':
40 niterations = ACE_OS::atoi (get_opts.opt_arg ());
41 break;
42 case 'x':
43 shutdown_flag = 1;
44 break;
45 case 'b':
46 payload_size = ACE_OS::atoi (get_opts.opt_arg ());
47 break;
48 case '?':
49 default:
50 ACE_ERROR_RETURN ((LM_ERROR,
51 "usage: %s "
52 "-k <ior> "
53 "-i <niterations> "
54 "-b <payload size> "
55 "-x "
56 "\n",
57 argv [0]),
58 -1);
60 // Indicates successful parsing of the command line
61 return 0;
64 class Handler : public POA_A::AMI_AMI_TestHandler
66 public:
67 /// Constructor.
68 Handler () : reply_count_ (0) {};
70 /// Destructor.
71 ~Handler () = default;
73 /// Get the reply count
74 long reply_count () const
76 return reply_count_.value ();
79 void foo (CORBA::Long ami_return_val,
80 CORBA::Long out_l)
82 ++reply_count_;
83 if (debug)
85 ACE_DEBUG ((LM_DEBUG,
86 "Callback method <foo> called: result <%d>, out_arg <%d>\n",
87 ami_return_val,
88 out_l));
92 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
94 ++reply_count_;
96 ACE_DEBUG ((LM_DEBUG,
97 "Callback method <foo_excep> called:\n"
98 "Testing proper exception handling ...\n"));
99 try
101 excep_holder->raise_exception ();
103 catch (const CORBA::Exception&)
105 ACE_DEBUG ((LM_DEBUG,
106 "... caught the wrong exception -> ERROR\n"));
110 private:
111 ACE_Atomic_Op <TAO_SYNCH_MUTEX, long> reply_count_;
115 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
119 CORBA::ORB_var orb =
120 CORBA::ORB_init (argc, argv);
122 CORBA::Object_var object_var =
123 orb->resolve_initial_references ("RootPOA");
125 PortableServer::POA_var poa_var =
126 PortableServer::POA::_narrow (object_var.in ());
128 PortableServer::POAManager_var poa_manager_var =
129 poa_var->the_POAManager ();
131 poa_manager_var->activate ();
133 if (parse_args (argc, argv) != 0)
134 return 1;
136 // We reuse the object_var smart pointer!
137 CORBA::Object_var ior_object = orb->string_to_object (ior);
139 A::AMI_Test_var ami_test_var =
140 A::AMI_Test::_narrow (ior_object.in ());
142 if (CORBA::is_nil (ami_test_var.in ()))
144 ACE_ERROR_RETURN ((LM_ERROR,
145 "Object reference <%s> is nil.\n",
146 ior),
150 // Instantiate the ReplyHandler and register that with the POA.
151 Handler handler;
152 PortableServer::ObjectId_var id =
153 poa_var->activate_object (&handler);
155 CORBA::Object_var object = poa_var->id_to_reference (id.in ());
157 A::AMI_AMI_TestHandler_var the_handler_var =
158 A::AMI_AMI_TestHandler::_narrow (object.in ());
160 CORBA::Long l = 931247;
161 A::Payload payload (payload_size);
162 payload.length (payload_size);
164 for (CORBA::ULong j = 0; j != payload_size; ++j)
166 payload[j] = j % 256;
169 for (ssize_t ni = 0; ni < niterations; ni++)
171 if (debug)
173 ACE_DEBUG ((LM_DEBUG,
174 "Sending asynch message: %d\n",
175 ni));
178 ami_test_var->sendc_foo (the_handler_var.in (),
180 "Let's talk AMI.",
181 payload);
184 if (debug)
186 ACE_DEBUG ((LM_DEBUG,
187 "<%d> Asynchronous methods issued\n",
188 niterations));
191 if (debug)
193 ACE_DEBUG ((LM_DEBUG,
194 "Issuing a synchronous method to collect the AMI replies\n"));
197 CORBA::Long number = ami_test_var->foo (l,
199 "Let's talk SMI.",
200 payload);
202 if (debug)
204 ACE_DEBUG ((LM_DEBUG,
205 "Received the following number: %d\n",
206 number));
209 if (shutdown_flag)
211 ami_test_var->shutdown ();
214 poa_var->destroy (1, // ethernalize objects
215 0); // wait for completion
217 orb->destroy ();
219 catch (const CORBA::Exception& ex)
221 ex._tao_print_exception ("Caught exception:");
222 return 1;
225 return 0;