summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/CosEC/Factory/FactoryCosEventChannel_i.cpp
blob: 8622888f03589f5bd3be1b6a7cb3f0dfdd7aaaa7 (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
// -*- C++ -*-
// $Id$

#include "FactoryCosEventChannel_i.h"

// @@ Pradeep: remember to use "" not <> in your includes.
#include <ace/Auto_Ptr.h>

// @@ Pradeep: if you are using the new TAO_EC_Event_Channel you
//    *don't* need a scheduler.
//    Ohh, and you don't need to create the TAO_EC_Basic_Factory, I
//    have changed the EC to dynamically load the right factory,
//    configurable through the svc.conf file! We can discuss later
//    what is the right configuration for the real-time EC.
//    In general the code here looks a bit bulky, I apologize that I
//    cannot take a better look, please remind me to do so once I'm
//    back to work.

FactoryCosEventChannel_i::FactoryCosEventChannel_i (void)
  :poa_ (PortableServer::POA::_nil ()),
   ec_servant_ (0),
   cosec_servant_ (0),
   scheduler_ (RtecScheduler::Scheduler::_nil ()),
   rtec_ (RtecEventChannelAdmin::EventChannel::_nil ()),
   cosec_ (CosEventChannelAdmin::EventChannel::_nil ())
{
  // No-Op.
}

FactoryCosEventChannel_i::~FactoryCosEventChannel_i (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "In FactoryCosEventChannel_i dtor"));
}

int
FactoryCosEventChannel_i::init (PortableServer::POA_ptr poa,
                                CORBA::Environment &ACE_TRY_ENV)
{
  ACE_ASSERT (poa != PortableServer::POA::_nil ());
  this->poa_ = poa;

  // Create the Scheduler servant
  ACE_Config_Scheduler* _sched_servant;
  ACE_NEW_RETURN (_sched_servant,
                  ACE_Config_Scheduler (),
                  -1);
  auto_ptr<ACE_Config_Scheduler> auto_sched_servant_ (_sched_servant);

  // Create the RtEC servant.
  // @@ Pradeep: Please make sure that the POA policies are similar to
  //    the RootPOA, this is the POA used by the RTEC to activate its
  //    internal objects.
  TAO_EC_Event_Channel* _ec_servant;
  ACE_NEW_RETURN (_ec_servant,
                  TAO_EC_Event_Channel (this->poa_.in (),
                                        this->poa_.in ()),
                  -1);
  auto_ptr<TAO_EC_Event_Channel> auto_ec_servant_ (_ec_servant);

  // Create the CosEC servant.
  TAO_CosEC_EventChannel_i* _cosec_servant;
  ACE_NEW_RETURN (_cosec_servant,
                  TAO_CosEC_EventChannel_i (),
                  -1);
  auto_ptr<TAO_CosEC_EventChannel_i> auto_cosec_servant_ (_cosec_servant);

  // if all the servants where allocated then set the class pointers.
  this->sched_servant_ = auto_sched_servant_.release ();
  this->ec_servant_ = auto_ec_servant_.release ();
  this->cosec_servant_ = auto_cosec_servant_.release ();

  return 0; // success.
}

int
FactoryCosEventChannel_i::activate (ACE_CString& str_channel_id,
                                    CORBA::Environment &ACE_TRY_ENV)
{
  ACE_ASSERT (this->poa_ != PortableServer::POA::_nil ());

  // @@ Pradeep: don't use the same POA to activate the objects
  //    created by the factory and your internal objects: what if the
  //    user creates two event channels with names "foo" and
  //    "foo_Sched"?

  // Start the scheduler.
    PortableServer::ObjectId_var oid_sched =
    TAO_POA::string_to_ObjectId ((str_channel_id + "_Sched").c_str ());

  this->poa_->activate_object_with_id (oid_sched,
                                       this->sched_servant_,
                                       ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);
  CORBA::Object_var obj_sched =
    this->poa_->id_to_reference (oid_sched,
                                 ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  // Give the ownership to the POA.
  this->sched_servant_->_remove_ref (ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  this->scheduler_ =
    RtecScheduler::Scheduler::_narrow (obj_sched.in (),
                                       ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  if (ACE_Scheduler_Factory::server (this->scheduler_.in ()) == -1)
    return -1;

  //Activate the Rtec
  this->ec_servant_->activate (ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  // Start the RtEC

  PortableServer::ObjectId_var oid_rtec =
    TAO_POA::string_to_ObjectId ((str_channel_id + "_RtEC").c_str ());

 this->poa_->activate_object_with_id (oid_rtec,
                                      this->ec_servant_,
                                      ACE_TRY_ENV);
 ACE_CHECK_RETURN (-1);

 CORBA::Object_var obj_rtec =
    this->poa_->id_to_reference (oid_rtec,
                                 ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  // Give the ownership to the POA.
  this->ec_servant_->_remove_ref (ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  this->rtec_ =
    RtecEventChannelAdmin::EventChannel::_narrow (obj_rtec.in (),
                                                  ACE_TRY_ENV);

  ACE_CHECK_RETURN (-1);

  // Initialize the CosEC servant.
  RtecScheduler::handle_t supp_handle =
    this->scheduler_->create ("supplier",
                              ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  this->supplier_qos_.insert (1,
                              ACE_ES_EVENT_ANY,
                              supp_handle,
                              1);

  RtecScheduler::handle_t cons_handle =
    this->scheduler_->create ("consumer",
                              ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  this->consumer_qos_.start_disjunction_group ();
  this->consumer_qos_.insert_source (1, // default = 1
                                     cons_handle);

  const RtecEventChannelAdmin::ConsumerQOS &consumerqos =
    this->consumer_qos_.get_ConsumerQOS ();

  const RtecEventChannelAdmin::SupplierQOS &supplierqos =
    this->supplier_qos_.get_SupplierQOS ();

  if (this->cosec_servant_->init (consumerqos,
                                  supplierqos,
                                  this->rtec_.in (),
                                  ACE_TRY_ENV) != 0)
    return -1;
  ACE_CHECK_RETURN (-1);

    // Start the CosEC

  PortableServer::ObjectId_var oid_cosec =
   TAO_POA::string_to_ObjectId ((str_channel_id + "_CosEC").c_str ());

  this->poa_->activate_object_with_id (oid_cosec,
                                       this->cosec_servant_,
                                       ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  CORBA::Object_var obj_cosec =
    this->poa_->id_to_reference (oid_cosec,
                                 ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  // Give the ownership to the POA.
  this->cosec_servant_->_remove_ref (ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  this->cosec_ =
    CosEventChannelAdmin::EventChannel::_narrow (obj_cosec.in (),
                                                 ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);
}

CosEventChannelAdmin::ConsumerAdmin_ptr
FactoryCosEventChannel_i::for_consumers (CORBA::Environment &ACE_TRY_ENV)
{
  return this->cosec_->for_consumers (ACE_TRY_ENV);
}

CosEventChannelAdmin::SupplierAdmin_ptr
FactoryCosEventChannel_i::for_suppliers (CORBA::Environment &ACE_TRY_ENV)
{
  return this->cosec_->for_suppliers (ACE_TRY_ENV);
}

void
FactoryCosEventChannel_i::destroy (CORBA::Environment &ACE_TRY_ENV)
{
  // Deactivate all those we control...

  // first the cosec.
  PortableServer::ObjectId* oid_cosec =
    this->poa_->servant_to_id (this->cosec_servant_,
                               ACE_TRY_ENV);
  ACE_CHECK;

  // deactivate from the poa.
  this->poa_->deactivate_object (*oid_cosec,
                                 ACE_TRY_ENV);
  ACE_CHECK;

  // next the rtec.
  PortableServer::ObjectId* oid_rtec =
    this->poa_->servant_to_id (this->ec_servant_,
                               ACE_TRY_ENV);
  ACE_CHECK;

  // deactivate from the poa.
  this->poa_->deactivate_object (*oid_rtec,
                                 ACE_TRY_ENV);
  ACE_CHECK;

  // lastly the scheduler.
 PortableServer::ObjectId* oid_sched =
    this->poa_->servant_to_id (this->sched_servant_,
                               ACE_TRY_ENV);
 ACE_CHECK;

 // deactivate from the poa.
  this->poa_->deactivate_object (*oid_sched,
                                 ACE_TRY_ENV);
  ACE_CHECK;
}