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

// @(#)svr.cpp	05/14/97
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// TEST:	simple IIOP server for "cubit.idl" interface.
//
// Starts up, builds an objref, prints its string in debug mode,
// listens for messages, responds to them.
//
// Modified version of Cubit Example written by Sun Microsystems Inc.
// Modified by: Brian Mendel

#include "pbroker/corba/orb.h"
#include "pbroker/corba/request.h"
#include "pbroker/corba/environ.h"
#include "pbroker/unixsvc/unixsvc.h"
#include "pbroker/corba/xpsorb.h"
#include "pbroker/corba/xpsboa.h"
#include "pbroker/corba/boad/boafull.h"   // will force usage of "Full" BOA API
#include "ace/Get_Opt.h"
#include "ace/Log_Msg.h"
#include "ace/Task.h"
#include "ace/ARGV.h"
#include "ace/Sched_Params.h"

#include "cubit_i.h"

ACE_RCSID(server, svr, "$Id$")

// Global Variables
static CORBA::String key = (CORBA::String) "Cubit";


class Cubit_Task : public ACE_Task<ACE_NULL_SYNCH>
// = TITLE
//    Encapsulates an ORB for the Cubit application
{
public:
  Cubit_Task (const char* args, const char* orbname, unsigned int num_of_objs); //

  virtual int svc (void);

protected:
  Cubit_Task () {};

private:
  char* orbname_;
  char* orbargs_;
  unsigned int num_of_objs_;
};

Cubit_Task::Cubit_Task (const char* args, const char* orbname, unsigned int num_of_objs)
  : orbname_ ((char*)orbname),
    orbargs_ ((char*)args),
    num_of_objs_ (num_of_objs)
{}


int
Cubit_Task::svc (void)
{
  CORBA::Environment env;
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) Beginning Cubit task with args = '%s'\n", orbargs_));
  ACE_ARGV args (orbargs_);

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

  XpsORB_ptr       orb_ptr = XpsORB::_nil();
  XpsBOA *         oa_ptr  = XpsBOA::_nil ();
  CORBA_Object_ptr obj_ptr = CORBA_Object::_nil();

  try {
    // Initialize the ORB pointer
    orb_ptr = XpsORB::init (argc, argv); 
    
    // Initialize the Object Adapter
    oa_ptr = XpsBOA::init (argc, argv);
    
    if (CORBA::is_nil(orb_ptr) || CORBA::is_nil(oa_ptr))
      {
	ACE_ERROR_RETURN ((LM_ERROR, 
			   " (%P|%t) Unable to initialize the ORB and/or the BOA\n"),
			  1);
      }
    
  // Create implementation object with user specified key.
  Cubit_i_ptr *my_cubit = new Cubit_i_ptr[num_of_objs_];

  u_int i;
  for (i = 0; i < num_of_objs_; i++)
    {
      CORBA::String obj_str = CORBA::string_alloc (ACE_OS::strlen ((char *) key)+30);

#if 0
      ACE_OS::sprintf (obj_str, "%s-%u-%02d", (char *) key, ACE_Thread::self (), i);
#else
      ACE_OS::sprintf (obj_str, "%s%02d", (char *) key, i);
#endif

      my_cubit[i] = new Cubit_i (obj_str);

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

      //CORBA::OctetSeq obj_key;
      //obj_key.buffer = (CORBA::Octet *) obj_str;
      //obj_key.length = obj_key.maximum = ACE_OS::strlen (obj_str);

      //CORBA::Object_ptr obj = 0;

      //      if (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;

      //  str = orb_ptr->object_to_string (obj);
      str = orb_ptr->object_to_url (my_cubit[i]);
      
      ACE_DEBUG ((LM_DEBUG, "listening as object '%s'\n", str));
      CORBA_string_free (str);

      oa_ptr->obj_is_ready (my_cubit[i], nil);
      oa_ptr->registerAlias (my_cubit[i], obj_str);

      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);
    }

  // Handle requests for this object until we're killed, or one of the
  // methods asks us to exit.
  XpsEventService  eventService;
  eventService.mainloop();   // control will not return to this point
  //  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 (i = 0; i < num_of_objs_; i++)
    {
      delete my_cubit[i];
    }

  // Free resources
  CORBA::release (orb_ptr);
  CORBA::release (oa_ptr);
  }
  catch(const CORBA::SystemException& exc)
    {
      ACE_DEBUG ((LM_DEBUG, 
		  "CORBA::SystemException caught: %s (minor: %d): svr.cpp\n\n",
		  exc._repository_id (),
		  exc.minor ()));
    }
  catch(const CORBA::UserException& exc)
    {
      ACE_DEBUG ((LM_DEBUG, 
		  "CORBA::UserException caught: %s: svr.cpp\n\n",
		  exc._repository_id ()));
    }
  catch(...)
    {
      ACE_DEBUG ((LM_DEBUG, 
		  "Exception caught: svr.cpp\n\n"));
    }

  return 0;
}

static char *hostname = NULL;
static int base_port = 0;

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

static int num_of_objs = 1;

static int
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt opts (argc, argv, "h:p:n:");
  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 'n':
	num_of_objs = ACE_OS::atoi (opts.optarg);
	break;
      case '?':
      default:
	ACE_ERROR_RETURN ((LM_ERROR,
			   "usage:  %s"
			   " -p port"
			   " -h my_hostname"
                           " -n num_objects"
			   "\n", argv [0]), 1);
      }

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

// Standard command line parsing utilities used.

int
main (int argc, char *argv[])
{

#if 0
  parse_args (argc, argv);

  if ((hostname == NULL) || (base_port == 0)) 
    {
      ACE_ERROR_RETURN ((LM_ERROR, 
			 "usage: %s "
			 " -p port -h my_hostname"
			 "\n",argv [0]), 1);
      
    }
#endif
  
  const size_t arg_size =
    strlen("rate20 -ORBport %d -ORBhost %s")
    + 5 /* port number */
    + MAXHOSTNAMELEN /* hostname */
    + 1 /* zero terminator */;
  
  char *args1 = 0;
  ACE_NEW_RETURN (args1, char[arg_size], -1);

  ::sprintf (args1, "rate20");
  Cubit_Task task1 (args1 , "internet", 1);
  ACE_Sched_Priority priority;
#if defined(VXWORKS)
  priority = ACE_Sched_Params::priority_max (ACE_SCHED_FIFO, ACE_SCOPE_THREAD) + 6;
#else
  priority = ACE_Sched_Params::priority_max (ACE_SCHED_FIFO, ACE_SCOPE_THREAD);
#endif  
  ACE_DEBUG ((LM_DEBUG, "Creating servant with high priority\n"));
  task1.activate (THR_BOUND, 1, 0, priority);


  char *args2 = 0;
  ACE_NEW_RETURN (args2, char[arg_size], -1);

  ::sprintf (args2, "rate10");//, base_port+1, hostname);
  Cubit_Task task2 (args2 , "internet", 1);
  priority =
    ACE_Sched_Params::priority_min (ACE_SCHED_FIFO, ACE_SCOPE_THREAD);
  ACE_DEBUG ((LM_DEBUG, "Creating servant with low priority\n"));
  task2.activate (THR_BOUND, 1, 0, priority);

  ACE_Thread_Manager::instance ()->wait ();

  delete [] args1;
  delete [] args2;
  
  return 0;
}