~ubuntu-branches/ubuntu/breezy/ace/breezy

« back to all changes in this revision

Viewing changes to TAO/examples/Buffered_AMI/client.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad, Benjamin Montgomery, Adam Conrad
  • Date: 2005-09-18 22:51:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge) (0.1.2 woody)
  • Revision ID: james.westby@ubuntu.com-20050918225138-seav22q6fyylb536
Tags: 5.4.7-3ubuntu1
[ Benjamin Montgomery ]
* Added a patch for amd64 and powerpc that disables the compiler
  option -fvisibility-inlines-hidden

[ Adam Conrad ]
* Added DPATCH_OPTION_CPP=1 to debian/patches/00options to make
  Benjamin's above changes work correctly with dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// client.cpp,v 1.14 2003/12/24 17:46:34 bala Exp
2
 
 
3
 
// ================================================================
4
 
//
5
 
// = FILENAME
6
 
//     client.cpp
7
 
//
8
 
// = DESCRIPTION
9
 
//     This is a client that uses buffered AMI calls.
10
 
//
11
 
// = AUTHOR
12
 
//     Irfan Pyarali
13
 
//
14
 
// ================================================================
15
 
 
16
 
#include "testS.h"
17
 
#include "tao/Messaging/Messaging.h"
18
 
#include "tao/TAOA.h"
19
 
#include "ace/Get_Opt.h"
20
 
#include "ace/Read_Buffer.h"
21
 
 
22
 
ACE_RCSID (Buffered_AMI, 
23
 
           client, 
24
 
           "client.cpp,v 1.14 2003/12/24 17:46:34 bala Exp")
25
 
 
26
 
// Name of file contains ior.
27
 
static const char *IOR = "file://ior";
28
 
 
29
 
// Default iterations.
30
 
static CORBA::ULong iterations = 20;
31
 
 
32
 
// Default number of invocations to buffer before flushing.
33
 
static CORBA::Long message_count = iterations / 4;
34
 
 
35
 
// Time interval between invocation (in milli seconds).
36
 
static long interval = 1000;
37
 
 
38
 
// Flag indicates whether to shutdown remote server or not upon client
39
 
// shutdown.
40
 
static int shutdown_server = 0;
41
 
 
42
 
// AMI call or regular call.
43
 
static int invoke_ami_style = 1;
44
 
 
45
 
// Setup buffering or not.
46
 
static int setup_buffering = 1;
47
 
 
48
 
// Flag indicates that all replies have been received
49
 
static int received_all_replies = 0;
50
 
 
51
 
class Reply_Handler : public POA_AMI_testHandler
52
 
{
53
 
public:
54
 
  void method (CORBA::ULong reply_number
55
 
               ACE_ENV_ARG_DECL_NOT_USED)
56
 
    ACE_THROW_SPEC ((CORBA::SystemException))
57
 
    {
58
 
      ACE_DEBUG ((LM_DEBUG,
59
 
                  "client: AMI Reply %d @ %T\n",
60
 
                  reply_number));
61
 
 
62
 
      // Last reply flips the flag.
63
 
      if (reply_number == iterations)
64
 
        received_all_replies = 1;
65
 
    }
66
 
 
67
 
  void method_excep (AMI_testExceptionHolder *holder
68
 
                     ACE_ENV_ARG_DECL)
69
 
    ACE_THROW_SPEC ((CORBA::SystemException))
70
 
  {
71
 
    ACE_TRY
72
 
      {
73
 
        holder->raise_method (ACE_ENV_SINGLE_ARG_PARAMETER);
74
 
        ACE_TRY_CHECK;
75
 
      }
76
 
    ACE_CATCH(CORBA::SystemException, ex)
77
 
      {
78
 
        ACE_PRINT_EXCEPTION (ex, "Reply_Handler::method_excep: ");
79
 
      }
80
 
    ACE_ENDTRY;
81
 
  }
82
 
 
83
 
  void shutdown (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
84
 
    ACE_THROW_SPEC ((CORBA::SystemException))
85
 
  {
86
 
  }
87
 
 
88
 
  void shutdown_excep (AMI_testExceptionHolder *holder
89
 
                       ACE_ENV_ARG_DECL)
90
 
    ACE_THROW_SPEC ((CORBA::SystemException))
91
 
  {
92
 
    ACE_TRY
93
 
      {
94
 
        holder->raise_shutdown (ACE_ENV_SINGLE_ARG_PARAMETER);
95
 
        ACE_TRY_CHECK;
96
 
      }
97
 
    ACE_CATCH(CORBA::SystemException, ex)
98
 
      {
99
 
        ACE_PRINT_EXCEPTION (ex, "Reply_Handler::shutdown_excep: ");
100
 
      }
101
 
    ACE_ENDTRY;
102
 
  }
103
 
};
104
 
 
105
 
static int
106
 
parse_args (int argc, char **argv)
107
 
{
108
 
  ACE_Get_Opt get_opts (argc, argv, "a:b:k:m:i:t:x");
109
 
  int c;
110
 
 
111
 
  while ((c = get_opts ()) != -1)
112
 
    switch (c)
113
 
      {
114
 
      case 'k':
115
 
        IOR = get_opts.opt_arg ();
116
 
        break;
117
 
 
118
 
      case 'm':
119
 
        message_count = ::atoi (get_opts.opt_arg ());
120
 
        break;
121
 
 
122
 
      case 'a':
123
 
        invoke_ami_style = ::atoi (get_opts.opt_arg ());
124
 
        break;
125
 
 
126
 
      case 'b':
127
 
        setup_buffering = ::atoi (get_opts.opt_arg ());
128
 
        break;
129
 
 
130
 
      case 'i':
131
 
        iterations = ::atoi (get_opts.opt_arg ());
132
 
        break;
133
 
 
134
 
      case 't':
135
 
        interval = ::atoi (get_opts.opt_arg ());
136
 
        break;
137
 
 
138
 
      case 'x':
139
 
        shutdown_server = 1;
140
 
        break;
141
 
 
142
 
      case '?':
143
 
      default:
144
 
        ACE_ERROR_RETURN ((LM_ERROR,
145
 
                           "usage:  %s "
146
 
                           "-k IOR "
147
 
                           "-m message count "
148
 
                           "-a invoke AMI style [0/1] "
149
 
                           "-b setup buffering [0/1] "
150
 
                           "-i iterations "
151
 
                           "-t interval between calls "
152
 
                           "-x shutdown server "
153
 
                           "\n",
154
 
                           argv [0]),
155
 
                          -1);
156
 
      }
157
 
 
158
 
  if (IOR == 0)
159
 
    ACE_ERROR_RETURN ((LM_ERROR,
160
 
                       "Please specify the IOR for the servant\n"), -1);
161
 
 
162
 
  // Without AMI, replies are immediate.
163
 
  if (!invoke_ami_style)
164
 
    received_all_replies = 1;
165
 
 
166
 
  // Message count must be a multiple of iterations; otherwise we'll
167
 
  // have some unsent messages left in the buffered queue.  Even
168
 
  // though we can explicitly flush the queue, I am being lazy and
169
 
  // forcing the user to give the right numbers.
170
 
  if ((iterations % message_count) != 0)
171
 
    {
172
 
      ACE_ERROR_RETURN ((LM_ERROR,
173
 
                         "<message_count> must be a multiple <iterations> "
174
 
                         "or the program should be changed to flush explicitly \n"),
175
 
                        -1);
176
 
    }
177
 
 
178
 
  // Indicates successful parsing of command line.
179
 
  return 0;
180
 
}
181
 
 
182
 
void
183
 
setup_buffering_constraints (CORBA::ORB_ptr orb
184
 
                             ACE_ENV_ARG_DECL)
185
 
{
186
 
  // Obtain PolicyCurrent.
187
 
  CORBA::Object_var object = orb->resolve_initial_references ("PolicyCurrent"
188
 
                                                              ACE_ENV_ARG_PARAMETER);
189
 
  ACE_CHECK;
190
 
 
191
 
  // Narrow down to correct type.
192
 
  CORBA::PolicyCurrent_var policy_current =
193
 
    CORBA::PolicyCurrent::_narrow (object.in ()
194
 
                                   ACE_ENV_ARG_PARAMETER);
195
 
  ACE_CHECK;
196
 
 
197
 
  // Start off with no constraints.
198
 
  TAO::BufferingConstraint buffering_constraint;
199
 
  buffering_constraint.mode = TAO::BUFFER_MESSAGE_COUNT;
200
 
  buffering_constraint.message_count = message_count;
201
 
  buffering_constraint.message_bytes = 0;
202
 
  buffering_constraint.timeout = 0;
203
 
 
204
 
  // Setup the buffering constraint any.
205
 
  CORBA::Any buffering_constraint_any;
206
 
  buffering_constraint_any <<= buffering_constraint;
207
 
 
208
 
  // Setup the buffering constraint policy list.
209
 
  CORBA::PolicyList buffering_constraint_policy_list (1);
210
 
  buffering_constraint_policy_list.length (1);
211
 
 
212
 
  // Setup the buffering constraint policy.
213
 
  buffering_constraint_policy_list[0] =
214
 
    orb->create_policy (TAO::BUFFERING_CONSTRAINT_POLICY_TYPE,
215
 
                        buffering_constraint_any
216
 
                        ACE_ENV_ARG_PARAMETER);
217
 
  ACE_CHECK;
218
 
 
219
 
  // Setup the constraints (at the ORB level).
220
 
  policy_current->set_policy_overrides (buffering_constraint_policy_list,
221
 
                                        CORBA::ADD_OVERRIDE
222
 
                                        ACE_ENV_ARG_PARAMETER);
223
 
  ACE_CHECK;
224
 
 
225
 
  // We are done with the policy.
226
 
  buffering_constraint_policy_list[0]->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
227
 
  ACE_CHECK;
228
 
 
229
 
  // Setup the none sync scope policy, i.e., the ORB will buffer AMI
230
 
  // calls.
231
 
  Messaging::SyncScope sync_none = Messaging::SYNC_NONE;
232
 
 
233
 
  // Setup the none sync scope any.
234
 
  CORBA::Any sync_none_any;
235
 
  sync_none_any <<= sync_none;
236
 
 
237
 
  // Setup the none sync scope policy list.
238
 
  CORBA::PolicyList sync_none_policy_list (1);
239
 
  sync_none_policy_list.length (1);
240
 
 
241
 
  // Setup the none sync scope policy.
242
 
  sync_none_policy_list[0] =
243
 
    orb->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
244
 
                        sync_none_any
245
 
                        ACE_ENV_ARG_PARAMETER);
246
 
  ACE_CHECK;
247
 
 
248
 
  // Setup the none sync scope (at the ORB level).
249
 
  policy_current->set_policy_overrides (sync_none_policy_list,
250
 
                                        CORBA::ADD_OVERRIDE
251
 
                                        ACE_ENV_ARG_PARAMETER);
252
 
  ACE_CHECK;
253
 
 
254
 
  // We are now done with these policies.
255
 
  sync_none_policy_list[0]->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
256
 
  ACE_CHECK;
257
 
}
258
 
 
259
 
int
260
 
main (int argc, char **argv)
261
 
{
262
 
  ACE_DECLARE_NEW_CORBA_ENV;
263
 
 
264
 
  ACE_TRY
265
 
    {
266
 
      // Initialize the ORB.
267
 
      CORBA::ORB_var orb =
268
 
        CORBA::ORB_init (argc,
269
 
                         argv,
270
 
                         0
271
 
                         ACE_ENV_ARG_PARAMETER);
272
 
      ACE_TRY_CHECK;
273
 
 
274
 
      // Initialize options based on command-line arguments.
275
 
      int parse_args_result = parse_args (argc, argv);
276
 
      if (parse_args_result != 0)
277
 
        return parse_args_result;
278
 
 
279
 
      CORBA::Object_var base =
280
 
        orb->resolve_initial_references ("RootPOA"
281
 
                                         ACE_ENV_ARG_PARAMETER);
282
 
      ACE_TRY_CHECK;
283
 
 
284
 
      PortableServer::POA_var root_poa =
285
 
        PortableServer::POA::_narrow (base.in ()
286
 
                                      ACE_ENV_ARG_PARAMETER);
287
 
      ACE_TRY_CHECK;
288
 
 
289
 
      // Get an object reference from the argument string.
290
 
      base = orb->string_to_object (IOR
291
 
                                    ACE_ENV_ARG_PARAMETER);
292
 
      ACE_TRY_CHECK;
293
 
 
294
 
      PortableServer::POAManager_var poa_manager =
295
 
        root_poa->the_POAManager (ACE_ENV_SINGLE_ARG_PARAMETER);
296
 
      ACE_TRY_CHECK;
297
 
 
298
 
      poa_manager->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
299
 
      ACE_TRY_CHECK;
300
 
 
301
 
      // Try to narrow the object reference to a <test> reference.
302
 
      test_var test_object = test::_narrow (base.in ()
303
 
                                            ACE_ENV_ARG_PARAMETER);
304
 
      ACE_TRY_CHECK;
305
 
 
306
 
      Reply_Handler reply_handler_servant;
307
 
      AMI_testHandler_var reply_handler_object = reply_handler_servant._this (ACE_ENV_SINGLE_ARG_PARAMETER);
308
 
      ACE_TRY_CHECK;
309
 
 
310
 
      if (setup_buffering)
311
 
        {
312
 
          setup_buffering_constraints (orb.in ()
313
 
                                       ACE_ENV_ARG_PARAMETER);
314
 
          ACE_TRY_CHECK;
315
 
        }
316
 
 
317
 
      for (CORBA::ULong i = 1; i <= iterations; ++i)
318
 
        {
319
 
          ACE_DEBUG ((LM_DEBUG,
320
 
                      "client: Iteration %d @ %T\n",
321
 
                      i));
322
 
 
323
 
          if (invoke_ami_style)
324
 
            {
325
 
              // Invoke the AMI method.
326
 
              test_object->sendc_method (reply_handler_object.in (),
327
 
                                         i
328
 
                                         ACE_ENV_ARG_PARAMETER);
329
 
              ACE_TRY_CHECK;
330
 
            }
331
 
          else
332
 
            {
333
 
              CORBA::ULong reply_number = 0;
334
 
 
335
 
              // Invoke the regular method.
336
 
              test_object->method (i,
337
 
                                   reply_number
338
 
                                   ACE_ENV_ARG_PARAMETER);
339
 
              ACE_TRY_CHECK;
340
 
 
341
 
              ACE_DEBUG ((LM_DEBUG,
342
 
                          "client: Regular Reply %d @ %T\n",
343
 
                          reply_number));
344
 
            }
345
 
 
346
 
          // Interval between successive calls.
347
 
          ACE_Time_Value sleep_interval (0,
348
 
                                         interval * 1000);
349
 
 
350
 
          orb->run (sleep_interval ACE_ENV_ARG_PARAMETER);
351
 
          ACE_TRY_CHECK;
352
 
        }
353
 
 
354
 
      // Loop until all replies have been received.
355
 
      while (!received_all_replies)
356
 
        {
357
 
          orb->perform_work ();
358
 
        }
359
 
 
360
 
      // Shutdown server.
361
 
      if (shutdown_server)
362
 
        {
363
 
          test_object->shutdown (ACE_ENV_SINGLE_ARG_PARAMETER);
364
 
          ACE_TRY_CHECK;
365
 
        }
366
 
 
367
 
      root_poa->destroy (1,
368
 
                         1
369
 
                         ACE_ENV_ARG_PARAMETER);
370
 
      ACE_TRY_CHECK;
371
 
 
372
 
      // Destroy the ORB.  On some platforms, e.g., Win32, the socket
373
 
      // library is closed at the end of main().  This means that any
374
 
      // socket calls made after main() fail. Hence if we wait for
375
 
      // static destructors to flush the queues, it will be too late.
376
 
      // Therefore, we use explicit destruction here and flush the
377
 
      // queues before main() ends.
378
 
      orb->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
379
 
      ACE_TRY_CHECK;
380
 
    }
381
 
  ACE_CATCHANY
382
 
    {
383
 
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
384
 
                           "Exception caught:");
385
 
      return -1;
386
 
    }
387
 
  ACE_ENDTRY;
388
 
 
389
 
  ACE_CHECK_RETURN (-1);
390
 
 
391
 
  return 0;
392
 
}