summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Notify_Default_EMO_Factory.cpp
blob: a725bed6eb4dadca37985c0846f4c221c786faaf (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
// $Id$

#include "Notify_Default_EMO_Factory.h"
#include "Notify_Event_Manager.h"
#include "Notify_Event_Map.h"
#include "Notify_Event_Processor.h"
#include "Notify_Worker_Task.h"
#include "Notify_Factory.h"
#include "Notify_Event.h"
#include "Notify_MT_Worker_Task.h"

#include "ace/Arg_Shifter.h"
#include "ace/Get_Opt.h"

#include "tao/debug.h"

ACE_RCSID(Notify, Notify_Default_EMO_Factory, "$Id$")

#define ASYNCH_UPDATES_THREADS 1
  // The number of threads to send subscription/publication updates.
  // As this is a relatively adminstrative function. let's just use 1
  // thread to send the updates asynchronously.


  TAO_EMO_Options::TAO_EMO_Options (void)
    : mt_dispatching_ (0),
      mt_source_eval_ (0),
      mt_lookup_ (0),
      mt_listener_eval_ (0),
      asynch_updates_ (0),
      alloc_task_per_proxy_ (0),
      dispatching_threads_ (1),
      source_threads_ (1),
      lookup_threads_ (1),
      listener_threads_ (1)
{
}

int
TAO_Notify_Default_EMO_Factory::init_svc (void)
{
  return ACE_Service_Config::static_svcs ()->
    insert (&ace_svc_desc_TAO_Notify_Default_EMO_Factory);
}

TAO_Notify_Default_EMO_Factory::TAO_Notify_Default_EMO_Factory (void)
  :prealloc_source_eval_task_ (0),
   prealloc_listener_eval_task_ (0),
   prealloc_dispatching_task_ (0)
{
}

TAO_Notify_Default_EMO_Factory::~TAO_Notify_Default_EMO_Factory ()
{
  delete prealloc_source_eval_task_;
  delete prealloc_listener_eval_task_;
  delete prealloc_dispatching_task_;
}

int
TAO_Notify_Default_EMO_Factory::init (int argc, ACE_TCHAR* argv[])
{
  // ACE_DEBUG ((LM_DEBUG, "TAO_Notify_Default_EMO_Factory::init\n"));

  ACE_Arg_Shifter arg_shifter (argc, argv);

  const ACE_TCHAR *current_arg = 0;
  while (arg_shifter.is_anything_left ())
    {
      const ACE_TCHAR *arg = arg_shifter.get_current ();

      if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-MTDispatching")) == 0)
        {
          EMO_OPTIONS::instance ()->mt_dispatching_ = 1;
          arg_shifter.consume_arg ();
        }
      else if ((current_arg = arg_shifter.get_the_parameter (ACE_LIB_TEXT("-DispatchingThreads"))))
        {
          EMO_OPTIONS::instance ()->dispatching_threads_ = ACE_OS::atoi (current_arg);
          arg_shifter.consume_arg ();
        }
      else if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-MTSourceEval")) == 0)
        {
          EMO_OPTIONS::instance ()->mt_source_eval_ = 1;
          arg_shifter.consume_arg ();
        }
      else if ((current_arg = arg_shifter.get_the_parameter (ACE_LIB_TEXT("-SourceThreads"))))
        {
          EMO_OPTIONS::instance ()->source_threads_ = ACE_OS::atoi (current_arg);
          arg_shifter.consume_arg ();
        }
      else if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-MTLookup")) == 0)
        {
          EMO_OPTIONS::instance ()->mt_lookup_ = 1;
          arg_shifter.consume_arg ();
        }
      else if ((current_arg = arg_shifter.get_the_parameter (ACE_LIB_TEXT("-LookupThreads"))))
        {
          EMO_OPTIONS::instance ()->lookup_threads_ = ACE_OS::atoi (current_arg);
          arg_shifter.consume_arg ();
        }
      else if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-MTListenerEval")) == 0)
        {
          EMO_OPTIONS::instance ()->mt_listener_eval_ = 1;
          arg_shifter.consume_arg ();
        }
      else if ((current_arg = arg_shifter.get_the_parameter (ACE_LIB_TEXT("-ListenerThreads"))))
        {
          EMO_OPTIONS::instance ()->listener_threads_ = ACE_OS::atoi (current_arg);
          arg_shifter.consume_arg ();
        }
      else if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-AsynchUpdates")) == 0)
        {
          EMO_OPTIONS::instance ()->asynch_updates_ = 1;
          arg_shifter.consume_arg ();
        }
      else if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-AllocateTaskperProxy")) == 0)
        {
          EMO_OPTIONS::instance ()->alloc_task_per_proxy_ = 1;
          arg_shifter.consume_arg ();
        }
      else
        {
          ACE_DEBUG ((LM_DEBUG,"EMO Factory did not understand %s",arg));
          arg_shifter.ignore_arg ();
        }
    }

  return 0;
}

TAO_Notify_Worker_Task*
TAO_Notify_Default_EMO_Factory::create_task (int mt, int tp_size ACE_ENV_ARG_DECL)
{
  TAO_Notify_Worker_Task* task;

  int threads_flags =
    THR_SCHED_DEFAULT|THR_BOUND|THR_NEW_LWP;

  //  int dispatching_threads_priority

  // Later: give the user options to specify threads flags and thread priority for each task.

  if (mt == 1)
    ACE_NEW_THROW_EX (task, TAO_Notify_MT_Worker_Task (tp_size,
                                                       threads_flags,
                                                       0),
                      CORBA::NO_MEMORY ());
  else
    ACE_NEW_THROW_EX (task,
                      TAO_Notify_Worker_Task (),
                      CORBA::NO_MEMORY ());
  return task;
}


int
TAO_Notify_Default_EMO_Factory::init_instance (void)
{
  if (EMO_OPTIONS::instance ()->alloc_task_per_proxy_ == 0) // preallocate all tasks.
    {
      return this->preallocate_tasks ();
    }
  return 0;
}

int
TAO_Notify_Default_EMO_Factory::preallocate_tasks (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;

  this->prealloc_source_eval_task_ =
    create_task (EMO_OPTIONS::instance ()->mt_source_eval_, EMO_OPTIONS::instance ()->source_threads_
                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  this->prealloc_listener_eval_task_ =
    create_task (EMO_OPTIONS::instance ()->mt_listener_eval_, EMO_OPTIONS::instance ()->listener_threads_
                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  this->prealloc_dispatching_task_ =
    create_task (EMO_OPTIONS::instance ()->mt_dispatching_, EMO_OPTIONS::instance ()->dispatching_threads_
                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  return 0;
}

int
TAO_Notify_Default_EMO_Factory::fini (void)
{
  //  ACE_DEBUG ((LM_DEBUG, "TAO_Notify_Default_EMO_Factory::fini\n"));
  return 0;
}

TAO_Notify_Event_Manager*
TAO_Notify_Default_EMO_Factory::create_event_manager (TAO_Notify_EventChannel_i* channel ACE_ENV_ARG_DECL)
{
  TAO_Notify_Event_Manager* event_manager;
  ACE_NEW_THROW_EX (event_manager,
                    TAO_Notify_Event_Manager (channel, this),
                    CORBA::NO_MEMORY ());
  return event_manager;
}

TAO_Notify_Event_Map*
TAO_Notify_Default_EMO_Factory::create_event_map (ACE_ENV_SINGLE_ARG_DECL)
{
  TAO_Notify_Event_Map* event_map;
  ACE_NEW_THROW_EX (event_map,
                    TAO_Notify_Event_Map (this),
                    CORBA::NO_MEMORY ());
  return event_map;
}

TAO_Notify_Event_Processor*
TAO_Notify_Default_EMO_Factory::create_event_processor (TAO_Notify_Event_Manager* event_manager ACE_ENV_ARG_DECL)
{
  TAO_Notify_Event_Processor* event_processor;
  ACE_NEW_THROW_EX (event_processor,
                    TAO_Notify_Event_Processor (event_manager),
                    CORBA::NO_MEMORY ());
  return event_processor;
}


TAO_Notify_Worker_Task*
TAO_Notify_Default_EMO_Factory::create_source_eval_task (ACE_ENV_SINGLE_ARG_DECL)
{
  if (EMO_OPTIONS::instance ()->alloc_task_per_proxy_ == 1)
    return create_task (EMO_OPTIONS::instance ()->mt_source_eval_,
                        EMO_OPTIONS::instance ()->source_threads_
                        ACE_ENV_ARG_PARAMETER);
  else
    return prealloc_source_eval_task_;
}

TAO_Notify_Worker_Task*
TAO_Notify_Default_EMO_Factory::create_lookup_task (ACE_ENV_SINGLE_ARG_DECL)
{
  return create_task (EMO_OPTIONS::instance ()->mt_lookup_,
                      EMO_OPTIONS::instance ()->lookup_threads_ ACE_ENV_ARG_PARAMETER);
}

TAO_Notify_Worker_Task*
TAO_Notify_Default_EMO_Factory::create_listener_eval_task (ACE_ENV_SINGLE_ARG_DECL)
{
  if (EMO_OPTIONS::instance ()->alloc_task_per_proxy_ == 1)
    return create_task (EMO_OPTIONS::instance ()->mt_listener_eval_,
                        EMO_OPTIONS::instance ()->listener_threads_
                        ACE_ENV_ARG_PARAMETER);
  else
    return prealloc_listener_eval_task_;
}

TAO_Notify_Worker_Task*
TAO_Notify_Default_EMO_Factory::create_dispatching_task (ACE_ENV_SINGLE_ARG_DECL)
{
  if (EMO_OPTIONS::instance ()->alloc_task_per_proxy_ == 1)
    return create_task (EMO_OPTIONS::instance ()->mt_dispatching_,
                        EMO_OPTIONS::instance ()->dispatching_threads_
                        ACE_ENV_ARG_PARAMETER);
  else
    return prealloc_dispatching_task_;
}

TAO_Notify_Worker_Task*
TAO_Notify_Default_EMO_Factory::create_updates_task (ACE_ENV_SINGLE_ARG_DECL)
{
  return create_task (EMO_OPTIONS::instance ()->asynch_updates_, ASYNCH_UPDATES_THREADS ACE_ENV_ARG_PARAMETER);
}

void
TAO_Notify_Default_EMO_Factory::destroy_source_eval_task (TAO_Notify_Worker_Task* task)
{
  if (EMO_OPTIONS::instance ()->alloc_task_per_proxy_ == 1)
    delete task;
}

void
TAO_Notify_Default_EMO_Factory::destroy_listener_eval_task (TAO_Notify_Worker_Task* task)
{
  if (EMO_OPTIONS::instance ()->alloc_task_per_proxy_ == 1)
    delete task;
}

void
TAO_Notify_Default_EMO_Factory::destroy_dispatching_task (TAO_Notify_Worker_Task* task)
{
  if (EMO_OPTIONS::instance ()->alloc_task_per_proxy_ == 1)
    delete task;
}

void
TAO_Notify_Default_EMO_Factory::destroy_lookup_task (TAO_Notify_Worker_Task* task)
{
  delete task;
}

void
TAO_Notify_Default_EMO_Factory::destroy_updates_task (TAO_Notify_Worker_Task* task)
{
  delete task;
}


void
TAO_Notify_Default_EMO_Factory::print_values (void)
{
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                "EMO Factory = "
                "mt_dispatching %d "
                "mt_source_eval %d "
                " mt_lookup %d "
                " mt_listener_eval %d"
                " dispatching_threads %d "
                " source_threads %d "
                " lookup_threads %d "
                " listener_threads_ %d "
                " AsynchUpdates %d"
                " AllocateTaskperProxy %d",
                EMO_OPTIONS::instance ()->mt_dispatching_,
                EMO_OPTIONS::instance ()->mt_source_eval_,
                EMO_OPTIONS::instance ()->mt_lookup_,
                EMO_OPTIONS::instance ()->mt_listener_eval_,
                EMO_OPTIONS::instance ()->dispatching_threads_,
                EMO_OPTIONS::instance ()->source_threads_,
                EMO_OPTIONS::instance ()->lookup_threads_,
                EMO_OPTIONS::instance ()->listener_threads_,
                EMO_OPTIONS::instance ()->asynch_updates_,
                EMO_OPTIONS::instance ()->alloc_task_per_proxy_
              ));
}

// ****************************************************************

ACE_STATIC_SVC_DEFINE (TAO_Notify_Default_EMO_Factory,
                       ACE_TEXT (TAO_NOTIFY_DEF_EMO_FACTORY_NAME),
                       ACE_SVC_OBJ_T,
                       &ACE_SVC_NAME (TAO_Notify_Default_EMO_Factory),
                       ACE_Service_Type::DELETE_THIS | ACE_Service_Type::DELETE_OBJ,
                       0)
ACE_FACTORY_DEFINE (TAO_Notify, TAO_Notify_Default_EMO_Factory)

// ****************************************************************

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Singleton<TAO_EMO_Options, ACE_Null_Mutex>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Singleton<TAO_EMO_Options, ACE_Null_Mutex>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */