summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/RTCosScheduling/client.cpp
blob: 1ad8bfc30f252fcf7116550384cfbebc8eb8e977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* -*- C++ -*- */

//=============================================================================
/**
 *  @file    client.cpp
 *
 *  $Id$
 *
 *  @author Matt Murphy <murphym@cs.uri.edu>
 */
//=============================================================================


#include "testSchedC.h"
#include <orbsvcs/orbsvcs/RTCosScheduling/RTCosScheduling_ClientScheduler_i.h>
#include "ace/Get_Opt.h"
#include "tao/debug.h"
#include "tao/Version.h"
#include "ace/OS_main.h"
#include "ace/OS_NS_unistd.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
#pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/Task.h"
#include "tao/ORB_Core.h"


/// In the test, these are the default values that specify
/// how long (in seconds) the test methods should run before going
/// to the server, while on the server, and after returning from
/// the server
CORBA::Long  before_    = 1;
CORBA::Long  remote_    = 1;
CORBA::Long  after_     = 1;
u_int use_realtime_     = 1;

/// This is the name of the node that the client executes on.
char *node_ = NULL;

/// the name of the config file holding the scheduling information
char *file_ = NULL;

/// the name of the activity to run (the name of the string passed in
/// schedule_activity(activity_)
char * activity_ = NULL;

/// The name of the output file
char *client_output_file_ = NULL;

CORBA::String_var serv_output_;

char date_and_time[35];
char client_output_[2048];
char buf[100];


/// For the timestampe
const int time_size = 35;
ACE_TCHAR day_and_time[time_size];

void do_work(const CORBA::Long value);

ACE_RCSID(
   tests,
   client,
   "client.cpp,v 1.0 2003/08/07 15:59:21 murphy_m Exp")

/// Standard parse args method
/*
 *  parse_args allows the user to configure the test to use values other
 *  than those specified above.
 */
int
parse_args (int argc, char *argv[])
{
  /// We set the '-' flag for getopts because we have to do this
  /// parsing before the ORB does it's parsing, and we'd confuse it to reorder
  ACE_Get_Opt get_opts (argc, argv, "-B:R:A:X:N:C:F:T:?:");
  int c;
  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
          case 'B':
            before_ =ACE_OS::atoi (get_opts.opt_arg ());
            break;
          case 'R':
            remote_ =ACE_OS::atoi (get_opts.opt_arg ());
            break;
          case 'A':
            after_ =ACE_OS::atoi (get_opts.opt_arg ());
            break;
          case 'X':
            use_realtime_ =ACE_OS::atoi (get_opts.opt_arg ());
            break;
          case 'N':
            node_ = ACE_OS::strdup(get_opts.opt_arg ());
            break;
          case 'C':
            client_output_file_ = ACE_OS::strdup(get_opts.opt_arg ());
            break;
          case 'F':
            file_ = ACE_OS::strdup(get_opts.opt_arg ());
            break;
          case 'T':
            activity_ = ACE_OS::strdup(get_opts.opt_arg ());
            break;
          case '?':
            ACE_DEBUG ((LM_DEBUG,
                        "usage: %n [-b precall_execution] "
                                   "[-r remote_execution] "
                                   "[-a postcall_workload] "
                                   "[-N node] "
                                   "[-F config_file] "
                                   "[-c activity_name]\n"));
            ACE_OS::exit (1);
        }
    }
  if (node_ == NULL)
    node_ = ACE_OS::strdup("1");
  if (file_ == NULL)
    file_ = ACE_OS::strdup("schedule.cfg");
  if (activity_ == NULL)
    activity_ = ACE_OS::strdup("");
  if (client_output_file_ == NULL)
    client_output_file_ = ACE_OS::strdup("");

  return 0;
}

/**
 * This class runs the test as a new thread since the main thread
 * cannot set RT priorities as of TAO 1.3.3
 *
 */
class ORB_Thread : public ACE_Task<ACE_SYNCH>
{

public:
ORB_Thread(CORBA::ORB_var orb,
           int argc,
           char **argv)
: orb_(orb),
  argc_(argc),
  argv_(argv)
{
}

int svc(void) {
  ACE_TRY_NEW_ENV
    {
      if (parse_args (argc_, argv_) != 0)
        {
          return 1;
        }

       if (strcmp(activity_,"Client2") == 0
         || strcmp(activity_,"Client3") == 0)
          {
            ACE_OS::sleep(1);
          }

      CORBA::Object_var obj =
        orb_->string_to_object ("file://server.ior"
                                ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;


      if (CORBA::is_nil (obj.in ()))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("(%P) orb_->string_to_object ")
                             ACE_TEXT ("(\"file://server.ior\") failed.\n")),
                             -1);
        }

      testSched::Object1_var object1 =
        testSched::Object1::_narrow (obj.in ()ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;


      if (CORBA::is_nil (object1.in ()))
        {
           ACE_ERROR_RETURN ((LM_ERROR,
             "ERROR: Object reference is nil, Aborting\n"), 1);
        }


      if (use_realtime_)
        {
          /// Create the ClientScheduler, note that you must pass in the
          /// orb and the node name.  The node name is required so that the
          /// appropriate information can be read from the config file
          TAO::RTCosScheduling_ClientScheduler_i *client_sched = 0;
          ACE_NEW_THROW_EX(client_sched,
                           TAO::RTCosScheduling_ClientScheduler_i (
                             orb_,
                             node_,
                             file_),
                           CORBA::NO_MEMORY());
          ACE_TRY_CHECK;


          // Check to see that the ClientScheduler started correctly
          if (CORBA::is_nil (client_sched))
            {
              /// There was an error in setting up the RTORB,
              /// RT Policies were not set, throw an exception
             ACE_OS::exit(1);
            }



          /// Create a Current object so we can check on the priority locally
          obj = orb_->resolve_initial_references ("RTCurrent"
                                                  ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
          RTCORBA::Current_var current =
            RTCORBA::Current::_narrow (obj.in () ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;


          /// Test to make sure the priority model is exposed
          CORBA::Policy_var policy =
            object1->_get_policy (RTCORBA::PRIORITY_MODEL_POLICY_TYPE
                                  ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          RTCORBA::PriorityModelPolicy_var priority_policy =
            RTCORBA::PriorityModelPolicy::_narrow (policy.in ()
                                                   ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          if (CORBA::is_nil (priority_policy.in ()))
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 "ERROR: Priority Model not exposed!\n"),
                                 1);
            }

          /// Test to make sure we are using Server Declared Priority model
          RTCORBA::PriorityModel priority_model =
            priority_policy->priority_model (ACE_ENV_SINGLE_ARG_PARAMETER);
          ACE_TRY_CHECK;

          if (priority_model != RTCORBA::SERVER_DECLARED)
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 "ERROR: priority_model != "
                                 "RTCORBA::SERVER_DECLARED!\n"),
                                 1);
            }

          do_work(before_);

          ACE_TRY_EX(sched_act)
            {
               client_sched->schedule_activity (::activity_
                                                ACE_ENV_ARG_PARAMETER);
               ACE_TRY_CHECK_EX(sched_act);
            }
          ACE_CATCHANY
            {
               ACE_PRINT_EXCEPTION (ex,
                                    "Invalid activity name\n");
               return -1;
            }
          ACE_ENDTRY;

          ACE_OS::sprintf(buf,
                          "%s\t%s\tBeginning activity at priority %d\n",
                          ACE::timestamp(date_and_time, time_size),
                          activity_,
                          current->the_priority());
          ACE_DEBUG ((LM_DEBUG,
                      "%s",
                      buf));
          ACE_OS::strcat(client_output_, buf);

          do_work(before_);
          ACE_TRY_CHECK;


          ACE_OS::sprintf(buf,
                          "%s\t%s\tCalling method1 at priority %d\n",
                          ACE::timestamp(date_and_time, time_size),
                          activity_,
                          current->the_priority());
          ACE_DEBUG ((LM_DEBUG,
                      "%s",
                      buf));
          ACE_OS::strcat(client_output_, buf);
      serv_output_ = ACE_OS::strdup("");
          object1->method1 (activity_, remote_, serv_output_.inout());

          ACE_OS::strcat(client_output_, serv_output_.in());


          ACE_OS::sprintf(buf,
                          "%s\t%s\tDone with method1 at priority %d\n",
                          ACE::timestamp(date_and_time, time_size),
                          activity_,
                          current->the_priority());
          ACE_DEBUG ((LM_DEBUG,
                      "%s",
                      buf));
          ACE_OS::strcat(client_output_, buf);


          do_work(after_);

          ACE_OS::sprintf(buf,
                          "%s\t%s\tDone with test at priority %d\n",
                          ACE::timestamp(date_and_time, time_size),
                          activity_,
                          current->the_priority());
          ACE_DEBUG ((LM_DEBUG,
                      "%s",
                      buf));
          ACE_OS::strcat(client_output_, buf);


        }
      else
        {
          ACE_DEBUG ((LM_DEBUG,
                     "%T\t%s\tClient\tBeginning Activity\n",
                     activity_));
          do_work(before_);
          ACE_TRY_CHECK;
          ACE_DEBUG ((LM_DEBUG,
                      "%T\t%s\tClient\tCalling method1\n",
                      activity_));

          object1->method1 (activity_, remote_, serv_output_.inout());
          ACE_OS::strcat(client_output_,serv_output_.in());
          /// Finished with remote call
          ACE_DEBUG ((LM_DEBUG,
                      "%T\t%s\tClient\tDone with method1\n",
                      activity_));
          do_work(after_);
        }

      FILE *fp = ACE_OS::fopen(client_output_file_, "w");
      if (fp)
        {
          ACE_OS::fprintf(fp,
                          "%s",
                           client_output_);
        }
      else
      {
          ACE_DEBUG((LM_DEBUG,"No file to write to\n"));
      }
      ACE_OS::fclose(fp);


      // Finally destroy the ORB
      orb_->destroy ();
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception in running the client\n");
      return -1;
    }
  ACE_ENDTRY;
  return 0;
 }


private:
  CORBA::ORB_var orb_;
  CORBA::ORB_var orb2_;
  int argc_; char ** argv_;
};



int
ACE_TMAIN (int argc, char * argv[])
{
  ACE_TRY_NEW_ENV
    {
    CORBA::ORB_var orb =
    CORBA::ORB_init (argc, argv, "client_orb" ACE_ENV_ARG_PARAMETER);
    ACE_TRY_CHECK;

    ORB_Thread ot(orb, argc, argv);

    // Need to set the main thread pthread scope and pthread policy to
    // the values that are specified in svc.conf.  This change was
    // recommended by irfan@oomworks.com
    long flags = THR_NEW_LWP | THR_JOINABLE |

#if  TAO_MAJOR_VERSION > 1 \
|| (TAO_MAJOR_VERSION==1 && TAO_MINOR_VERSION > 3 ) \
|| (TAO_MAJOR_VERSION==1 && TAO_MAJOR_VERSION==3 && TAO_MINOR_VERSION > 1)

    orb->orb_core ()->orb_params ()->thread_creation_flags ();
    ACE_TRY_CHECK;

#else /* TAO version is 1.3.1 or lower */
    orb->orb_core ()->orb_params ()->scope_policy () |
    orb->orb_core ()->orb_params ()->sched_policy ();
    ACE_TRY_CHECK;
#endif

    ot.activate(flags);
    return ot.wait();
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION(ACE_ANY_EXCEPTION,
                          "ERROR in running the client\n");
    }
  ACE_ENDTRY;
  return 0;
}

void
do_work(const CORBA::Long value)
{
  const int scale_factor = 2000;
  int work;

  /// Simulate some work
  static CORBA::ULong prime_number = 9619;

  work = scale_factor * value;
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG, "test_i::method: %hd units of work\n", work));
  for (; work != 0; work--)
    ACE::is_prime (prime_number, 2, prime_number / 2);

}