summaryrefslogtreecommitdiff
path: root/TAO/performance-tests/Cubit/TAO/MT_Cubit/server.cpp
blob: 4158bd5fc2cca8aa04428f02739691bdde659c45 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/tests
//
// = FILENAME
//    svr.cpp
//
// = AUTHOR
//    Andy Gokhale, Sumedh Mungee, and Sergio Flores-Gaitan
//
// ============================================================================

#include "server.h"

Cubit_Task::Cubit_Task (void)
  : orb_ptr_ (0),
    oa_ptr_ (0)
{
  // No-op.
}

Cubit_Task::Cubit_Task (const char *args,
                        const char *orbname,
                        u_int num_of_objs)
  : orbname_ ((char *) orbname),
    orbargs_ ((char *) args),
    num_of_objs_ (num_of_objs),
    orb_ptr_ (0),
    oa_ptr_ (0)
{
}

int
Cubit_Task::svc (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Beginning Cubit task with args = '%s'\n",
              orbargs_));
  this->initialize_orb ();
  this->create_servants ();

  // Handle requests for this object until we're killed, or one of the
  // methods asks us to exit.
  orb_ptr_->run ();

  // Shut down the OA -- recycles all underlying resources (e.g. file
  // descriptors, etc).

  //  oa_ptr_->clean_shutdown (env);

  // Need to clean up and do a CORBA::release on everything we've
  // created!
  for (u_int i = 0; i < num_of_objs_; i++)
    delete servants_ [i];

  // Free resources
  CORBA::release (orb_ptr_);
  CORBA::release (oa_ptr_);

  return 0;
}

int
Cubit_Task::initialize_orb (void)
{
  CORBA::Environment env;
 
  ACE_ARGV args (this->orbargs_);

  int argc = args.argc ();
  char **argv = args.argv ();

  // Initialize the ORB.
  orb_ptr_ = CORBA::ORB_init (argc, 
                              argv, 
                              this->orbname_, 
                              env);

  if (env.exception () != 0)
    {
      env.print_exception ("ORB init");
      return 1;
    }

  // Initialize the Object Adapter.
  oa_ptr_ = orb_ptr_->POA_init (argc, argv, "POA");

  if (oa_ptr_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       " (%P|%t) Unable to initialize the POA.\n"),
                      1);
  return 0;
}

int
Cubit_Task::create_servants ()
{
  // Create implementation object with user specified key.

  ACE_NEW_RETURN (servants_,
                  Cubit_i *[num_of_objs_],
                  -1);

  u_int i;

  // This loop creates multiple servants, and prints out their IORs
  for (i = 0;
       i < num_of_objs_;
       i++)
    {
      CORBA::String obj_str =
        CORBA::string_alloc (ACE_OS::strlen ((char *) key) 
                             + OBJECT_STRING_SIZE);

      ACE_OS::sprintf (obj_str,
                       "%s%02d",
                       (char *) key,
                       i);

      ACE_NEW_RETURN (servants_[i],
                      Cubit_i (obj_str),
                      -1);

      if (servants_[i] == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
			   " (%P|%t) Unable to create "
			   "implementation object&d\n",
			   i), 2);

      TAO_opaque obj_key (ACE_OS::strlen (obj_str));
      for (CORBA::ULong i = 0;
	   i < obj_key.maximum ();
	   ++i)
	{
	  obj_key[i] = obj_str[i];
	}
      obj_key.length (obj_key.maximum ());

      CORBA::Object_ptr obj = 0;

      if (this->oa_ptr_->find (obj_key, obj) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to locate object with key '%s', %p\n",
                           key),
                          3);

      // Stringify the objref we'll be implementing, and print it to
      // stdout.  Someone will take that string and give it to some
      // client.  Then release the object.

      CORBA::String str;

      CORBA::Environment env;

      str = orb_ptr_->object_to_string (obj, env);

      if (env.exception () != 0)
        {
          env.print_exception ("object2string");
          return 1;
        }

      ACE_OS::puts ((char *) str);
      ACE_OS::fflush (stdout);
      ACE_DEBUG ((LM_DEBUG,"Object Created at: '%ul'", obj));
      CORBA::string_free (obj_str);
    }
  return 0;
}

// Global options used to configure various parameters.
static char *hostname = NULL;
static int base_port = 0;
static int num_of_objs = 1;

// Parses the command line arguments and returns an error status.

static int
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt opts (argc, argv, "h:p:t:");
  int c;

  while ((c = opts ()) != -1)
    switch (c)
      {
      case 'h':
        hostname = opts.optarg;
        break;
      case 'p':
	base_port = ACE_OS::atoi (opts.optarg);
	break;
      case 't':
	num_of_objs = ACE_OS::atoi (opts.optarg);
	break;
      case '?':
      default:
	ACE_ERROR_RETURN ((LM_ERROR,
			   "usage:  %s"
			   " -p port"
			   " -h my_hostname"
                           " -t num_objects"
			   "\n", argv [0]),
                          1);
      }

  // Indicates successful parsing of command line
  return 0;  
}

int
initialize (int argc, char **argv)
{
#if defined (VXWORKS)
   hostAdd ("mv2604e", "130.38.183.178");
#if defined (VME_DRIVER)
   STATUS status = vmeDrv ();
   if (status != OK)
     printf ("ERROR on call to vmeDrv()\n");
   status = vmeDevCreate ("/vme");
   if (status != OK)
     printf ("ERROR on call to vmeDevCreate()\n");
#endif /* defined (VME_DRIVER) */

#if defined (FORCE_ARGS)
   int argc = 5;
   char *argv[] = { "main",
                    "-p",
                    "10014",
                    "-h",
                    "130.38.183.132" };
   
#endif /* defined (FORCE_ARGS) */
#endif /* defined (VXWORKS) */

   // Standard command line parsing utilities used.
  parse_args (argc, argv);

  if (hostname == 0 || base_port == 0)
	ACE_ERROR_RETURN ((LM_ERROR,
			   "usage:  %s"
			   " -p port"
			   " -h my_hostname"
                           " -t num_objects"
			   "\n", argv [0]),
                          1);
  return 0;
}

// Starts up the servants
int
start_servants ()
{
  char *args1 = new char [4096];
  ACE_OS::sprintf (args1,
                   "rate20 -ORBport %d -ORBhost %s",
                   base_port, 
                   hostname);

  Cubit_Task *high_priority_task;
  ACE_NEW_RETURN (high_priority_task, 
                  Cubit_Task (args1,
                              "internet",
                              1),
                  1);
                    
  ACE_Sched_Priority priority;

  // @@ The ifdef here is temporarily placed here until
  // I figure out how to map NT's thread priorities
  // into pthread's priorities.
#if defined (ACE_THR_PRI_FIFO_DEF)
  priority = ACE_THR_PRI_FIFO_DEF;
#else
  priority = ACE_DEFAULT_THREAD_PRIORITY;
#endif /* ACE_THR_PRI_FIFO_DEF */

  ACE_DEBUG ((LM_DEBUG,
              "Creating servant with high priority\n"));

  // Make the high priority task an active object.
  high_priority_task->activate (THR_BOUND | ACE_SCHED_FIFO, 
                               1, 
                               0, 
                               priority);

  // Create an array to hold pointers to the low priority tasks..
  Cubit_Task **low_priority_task;

  ACE_NEW_RETURN (low_priority_task, 
                  Cubit_Task *[num_of_objs], 
                  -1);

  priority = ACE_Sched_Params::previous_priority (ACE_SCHED_FIFO,
                                                  priority,
                                                  ACE_SCOPE_THREAD);

  ACE_DEBUG ((LM_DEBUG, 
              "Creating %d servants with low priority\n",
              num_of_objs - 1));

  // Create the low priority servants.
  for (int i = 0; i < num_of_objs - 1; i++)
    {

      char *args = new char [4096];

      ACE_OS::sprintf (args, 
                       "rate10 -ORBport %d -ORBhost %s", 
                       base_port + 1 + i,
                       hostname);

      ACE_NEW_RETURN (low_priority_task [i],
                      Cubit_Task (args, "internet", 1),
                      -1);

      priority = ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
                                                  priority,
                                                  ACE_SCOPE_THREAD);

      // Make the low priority task an active object.
      low_priority_task [i]->activate (THR_BOUND | ACE_SCHED_FIFO, 
                                       1, 
                                       0, 
                                       priority);

    }
  return 0;
}

// main routine.

int
main (int argc, char *argv[])
{
  if (initialize (argc, argv) != 0)
    ACE_ERROR_RETURN ((LM_ERROR, "Error in Initialization\n"), 1);
  if(start_servants () != 0)
    ACE_ERROR_RETURN ((LM_ERROR, "Error creating the servants\n"), 1);
  // Wait for all the threads to exit.
  ACE_Thread_Manager::instance ()->wait ();
  return 0;
}