summaryrefslogtreecommitdiff
path: root/TAO/tao/default_server.cpp
blob: 6dddb5a4578ad67f49071fce1932403c6919e785 (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
// $Id$

#include "tao/default_server.h"
#include "tao/ORB_Core.h"

#if !defined (__ACE_INLINE__)
# include "tao/default_client.i"
#endif /* ! __ACE_INLINE__ */

ACE_RCSID(tao, default_server, "$Id$")

TAO_Default_Server_Strategy_Factory::TAO_Default_Server_Strategy_Factory (void)
  : thread_flags_ (0),
    object_table_size_ (TAO_DEFAULT_SERVER_OBJECT_TABLE_SIZE),
    object_lookup_strategy_ (TAO_DYNAMIC_HASH),
    poa_lock_type_ (TAO_THREAD_LOCK),
    poa_mgr_lock_type_ (TAO_THREAD_LOCK),
    event_loop_lock_type_ (TAO_NULL_LOCK),
    collocation_table_lock_type_ (TAO_THREAD_LOCK),
    cached_connector_lock_type_ (TAO_THREAD_LOCK),
    creation_strategy_ (0),
    concurrency_strategy_ (0)
{
}

TAO_Default_Server_Strategy_Factory::~TAO_Default_Server_Strategy_Factory (void)
{
  // Perform appropriate cleanup.
}

TAO_Default_Server_Strategy_Factory::CREATION_STRATEGY *
TAO_Default_Server_Strategy_Factory::creation_strategy (void)
{
  if (this->creation_strategy_ == 0)
    return &this->default_creation_strategy_;
  else
    return this->creation_strategy_;
}

TAO_Default_Server_Strategy_Factory::CONCURRENCY_STRATEGY *
TAO_Default_Server_Strategy_Factory::concurrency_strategy (void)
{
  if (this->concurrency_strategy_ == 0)
    // If no strategy is specified, use the reactive one.
    return &this->reactive_strategy_;
  else
    return this->concurrency_strategy_;
}

ACE_Lock *
TAO_Default_Server_Strategy_Factory::create_poa_lock (void)
{
  ACE_Lock *the_lock = 0;

  switch (this->poa_lock_type_)
    {
    case TAO_THREAD_LOCK:
#if defined (ACE_HAS_THREADS)
      ACE_NEW_RETURN (the_lock,
                      ACE_Lock_Adapter<ACE_Recursive_Thread_Mutex> (),
                      0);
      break;
#endif /* ACE_HAS_THREADS */
    default:
      ACE_NEW_RETURN (the_lock,
                      ACE_Lock_Adapter<ACE_Null_Mutex> (),
                      0);
      break;
    }

  return the_lock;// Just to make sure we return something
}

ACE_Lock *
TAO_Default_Server_Strategy_Factory::create_poa_mgr_lock (void)
{
  ACE_Lock *the_lock = 0;

  switch (this->poa_mgr_lock_type_)
    {
    case TAO_THREAD_LOCK:
#if defined (ACE_HAS_THREADS)
      ACE_NEW_RETURN (the_lock,
                      ACE_Lock_Adapter<ACE_Thread_Mutex> (),
                      0);
      break;
#endif /* ACE_HAS_THREADS */
    default:
      ACE_NEW_RETURN (the_lock,
                      ACE_Lock_Adapter<ACE_Null_Mutex> (),
                      0);
      break;
    }

  // Just to make sure we return something.
  return the_lock;
}

ACE_Lock *
TAO_Default_Server_Strategy_Factory::create_servant_lock (void)
{
  ACE_Lock *the_lock = 0;

#if defined (ACE_HAS_THREADS)
  if (this->concurrency_strategy_ != &this->reactive_strategy_ &&
      this->concurrency_strategy_ != 0)
      ACE_NEW_RETURN (the_lock,
                      ACE_Lock_Adapter<ACE_Recursive_Thread_Mutex> (),
                      0);
  else
#endif /* ACE_HAS_THREADS */
      ACE_NEW_RETURN (the_lock,
                      ACE_Lock_Adapter<ACE_Null_Mutex> (),
                      0);

  return the_lock;
}

ACE_Lock *
TAO_Default_Server_Strategy_Factory::create_event_loop_lock (void)
{
  ACE_Lock *the_lock = 0;

  if (this->event_loop_lock_type_ == TAO_NULL_LOCK)
    ACE_NEW_RETURN (the_lock,
		    ACE_Lock_Adapter<ACE_SYNCH_NULL_MUTEX> (),
		    0);
  else
    ACE_NEW_RETURN (the_lock,
		    ACE_Lock_Adapter<ACE_SYNCH_RECURSIVE_MUTEX> (),
		    0);

  return the_lock;
}

ACE_Lock *
TAO_Default_Server_Strategy_Factory::create_collocation_table_lock (void)
{
  ACE_Lock *the_lock = 0;

  if (this->collocation_table_lock_type_ == TAO_NULL_LOCK)
    ACE_NEW_RETURN (the_lock,
		    ACE_Lock_Adapter<ACE_SYNCH_NULL_MUTEX> (),
		    0);
  else
    ACE_NEW_RETURN (the_lock,
		    ACE_Lock_Adapter<ACE_SYNCH_MUTEX> (),
		    0);

  return the_lock;
}

ACE_Lock *
TAO_Default_Server_Strategy_Factory::create_cached_connector_lock (void)
{
  ACE_Lock *the_lock = 0;

  if (this->cached_connector_lock_type_ == TAO_NULL_LOCK)
    ACE_NEW_RETURN (the_lock,
		    ACE_Lock_Adapter<ACE_SYNCH_NULL_MUTEX> (),
		    0);
  else
    ACE_NEW_RETURN (the_lock,
		    ACE_Lock_Adapter<ACE_SYNCH_MUTEX> (),
		    0);

  return the_lock;
}

TAO_Object_Table_Impl *
TAO_Default_Server_Strategy_Factory::create_object_table (void)
{
  // Create the appropriate-sized object table based on passed
  // arguments.
  TAO_Object_Table_Impl *objtable = 0;

  switch (this->object_lookup_strategy_)
    {
    case TAO_LINEAR:
      ACE_NEW_RETURN (objtable,
                      TAO_Linear_ObjTable (this->object_table_size_),
                      0);
      break;
      // Don't do this one right now until we determine how to deal
      // with its reliance on a global singleton.
    case TAO_USER_DEFINED:
      // it is assumed that the user would have used the hooks to
      // supply a user-defined instance of the object table
      //
      // Note that the usage below doesn't really fit very well now.
      // We need for the userdef stuff to provide a creation hook--IF
      // we decide to keep the whole demultiplexing strategy creation
      // the way it is.  IMHO, the way that userdef stuff should be
      // done is to create the User_Server_Strategy_Factory and just
      // link it in.  The default server would only encompass the
      // strategies that are "shipped", so to speak. --cjc
      objtable = TAO_ORB_Core_instance()->oa_params()->userdef_lookup_strategy ();
      break;
    case TAO_ACTIVE_DEMUX:
      ACE_NEW_RETURN (objtable,
                      TAO_Active_Demux_ObjTable (this->object_table_size_),
                      0);
      break;
    case TAO_DYNAMIC_HASH:
    case TAO_NONE:
    default:
      ACE_NEW_RETURN (objtable,
                      TAO_Dynamic_Hash_ObjTable (this->object_table_size_),
                      0);
      break;
    }

  return objtable;
}

// Evil macros b/c I'm lazy!
#define TAO_BEGINCHECK  if (0)
#define TAO_CHECKANDSET(sym) else if (ACE_OS::strcmp (flag, #sym) == 0) ACE_SET_BITS (this->thread_flags_, sym)
#define TAO_ENDCHECK

void
TAO_Default_Server_Strategy_Factory::tokenize (char *flag_string)
{
  char *lasts = 0;

  for (char *flag = ACE_OS::strtok_r (flag_string, "|", &lasts);
       flag != 0;
       flag = ACE_OS::strtok_r (0, "|", &lasts))
    {
      TAO_BEGINCHECK;
      TAO_CHECKANDSET (THR_DETACHED);
      TAO_CHECKANDSET (THR_BOUND);
      TAO_CHECKANDSET (THR_NEW_LWP);
      TAO_CHECKANDSET (THR_SUSPENDED);
#if !defined (ACE_WIN32)
      TAO_CHECKANDSET (THR_DAEMON);
#endif /* ACE_WIN32 */
      TAO_ENDCHECK;
    }
}

int
TAO_Default_Server_Strategy_Factory::init (int argc, char *argv[])
{
  return this->parse_args (argc, argv);
}

int
TAO_Default_Server_Strategy_Factory::open (void)
{
  TAO_ORB_Core *orb_core = TAO_ORB_Core_instance ();

  if (reactive_strategy_.open (orb_core->reactor ()) == 0
      && threaded_strategy_.open (orb_core->thr_mgr (),
                                  this->thread_flags_) == 0)
    return 0;
  else
    return -1;
}

int
TAO_Default_Server_Strategy_Factory::parse_args (int argc, char *argv[])
{
  ACE_TRACE ("TAO_Default_Server_Strategy_Factory::parse_args");

  int curarg;

  for (curarg = 0; curarg < argc && argv[curarg]; curarg++)
    if (ACE_OS::strcmp (argv[curarg], "-ORBconcurrency") == 0)
      {
        curarg++;
        if (curarg < argc)
          {
            char *name = argv[curarg];

            if (ACE_OS::strcasecmp (name, "reactive") == 0)
              this->concurrency_strategy_ = &reactive_strategy_;
            else if (ACE_OS::strcasecmp (name, "thread-per-connection") == 0)
              this->concurrency_strategy_ = &threaded_strategy_;
          }
      }
    else if (ACE_OS::strcmp (argv[curarg], "-ORBtablesize") == 0)
      {
        curarg++;
        if (curarg < argc)
          this->object_table_size_ = ACE_OS::strtoul (argv[curarg], 0, 10);
      }
    else if (ACE_OS::strcmp (argv[curarg], "-ORBdemuxstrategy") == 0)
      {
        curarg++;
        if (curarg < argc)
          {
            char *name = argv[curarg];

            if (ACE_OS::strcasecmp (name, "dynamic") == 0)
              this->object_lookup_strategy_ = TAO_DYNAMIC_HASH;
            else if (ACE_OS::strcasecmp (name, "linear") == 0)
              this->object_lookup_strategy_ = TAO_LINEAR;
            else if (ACE_OS::strcasecmp (name, "active") == 0)
              this->object_lookup_strategy_ = TAO_ACTIVE_DEMUX;
            else if (ACE_OS::strcasecmp (name, "user") == 0)
              this->object_lookup_strategy_ = TAO_USER_DEFINED;
          }
      }
    else if (ACE_OS::strcmp (argv[curarg], "-ORBpoalock") == 0)
      {
        curarg++;
        if (curarg < argc)
          {
            char *name = argv[curarg];

            if (ACE_OS::strcasecmp (name, "thread") == 0)
              this->poa_lock_type_ = TAO_THREAD_LOCK;
            else if (ACE_OS::strcasecmp (name, "null") == 0)
              this->poa_lock_type_ = TAO_NULL_LOCK;
          }
      }
    else if (ACE_OS::strcmp (argv[curarg], "-ORBpoamgrlock") == 0)
      {
        curarg++;
        if (curarg < argc)
          {
            char *name = argv[curarg];

            if (ACE_OS::strcasecmp (name, "thread") == 0)
              this->poa_mgr_lock_type_ = TAO_THREAD_LOCK;
            else if (ACE_OS::strcasecmp (name, "null") == 0)
              this->poa_mgr_lock_type_ = TAO_NULL_LOCK;
          }
      }
    else if (ACE_OS::strcmp (argv[curarg], "-ORBeventlock") == 0)
      {
        curarg++;
        if (curarg < argc)
          {
            char *name = argv[curarg];

            if (ACE_OS::strcasecmp (name, "thread") == 0)
              this->poa_mgr_lock_type_ = TAO_THREAD_LOCK;
            else if (ACE_OS::strcasecmp (name, "null") == 0)
              this->poa_mgr_lock_type_ = TAO_NULL_LOCK;
          }
      }
    else if (ACE_OS::strcmp (argv[curarg], "-ORBcoltbllock") == 0)
      {
        curarg++;
        if (curarg < argc)
          {
            char *name = argv[curarg];

            if (ACE_OS::strcasecmp (name, "thread") == 0)
              this->collocation_table_lock_type_ = TAO_THREAD_LOCK;
            else if (ACE_OS::strcasecmp (name, "null") == 0)
              this->collocation_table_lock_type_ = TAO_NULL_LOCK;
          }
      }
    else if (ACE_OS::strcmp (argv[curarg], "-ORBconnectorlock") == 0)
      {
        curarg++;
        if (curarg < argc)
          {
            char *name = argv[curarg];

            if (ACE_OS::strcasecmp (name, "thread") == 0)
              this->cached_connector_lock_type_ = TAO_THREAD_LOCK;
            else if (ACE_OS::strcasecmp (name, "null") == 0)
              this->cached_connector_lock_type_ = TAO_NULL_LOCK;
          }
      }
    else if (ACE_OS::strcmp (argv[curarg], "-ORBthreadflags") == 0)
      {
        curarg++;

        if (curarg < argc)
          this->tokenize (argv[curarg]);
      }

  return 0;
}

u_long
TAO_Default_Server_Strategy_Factory::object_table_size (void) const
{
  return this->object_table_size_;
}

TAO_Default_Server_Creation_Strategy::
TAO_Default_Server_Creation_Strategy (ACE_Thread_Manager *t)
  :  ACE_Creation_Strategy<TAO_Server_Connection_Handler> (t)
{
}

int
TAO_Default_Server_Creation_Strategy::
make_svc_handler (TAO_Server_Connection_Handler *&sh)
{
  if (sh == 0)
    {
      // Maybe this show be cached in the constructor, but it is
      // possible that this method is invoked in several threads
      // during the lifetime of this object, and the ORB_Core is a
      // TSS singleton.
      TAO_ORB_Core *orb_core = TAO_ORB_Core_instance ();
      ACE_NEW_RETURN (sh,
		      TAO_Server_Connection_Handler (orb_core),
		      -1);
    }
  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class TAO_Reactive_Strategy<TAO_Server_Connection_Handler>;
template class ACE_Reactive_Strategy<TAO_Server_Connection_Handler>;
template class ACE_Thread_Strategy<TAO_Server_Connection_Handler>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate TAO_Reactive_Strategy<TAO_Server_Connection_Handler>
#pragma instantiate ACE_Reactive_Strategy<TAO_Server_Connection_Handler>
#pragma instantiate ACE_Thread_Strategy<TAO_Server_Connection_Handler>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

ACE_FACTORY_DEFINE (TAO, TAO_Default_Server_Strategy_Factory)