summaryrefslogtreecommitdiff
path: root/TAO/CIAO/DAnCE/NodeApplicationManager/NodeApplicationManager_Impl.cpp
blob: 2021a7b583928798a421f569b1405da474518446 (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
// $Id$

#include "NodeApplicationManager_Impl.h"
#include "ace/Process.h"
#include "ace/OS_NS_stdio.h"

#if !defined (__ACE_INLINE__)
# include "NodeApplicationManager_Impl.inl"
#endif /* __ACE_INLINE__ */


CIAO::NodeApplicationManager_Impl::~NodeApplicationManager_Impl (void)
{
}

Deployment::NodeApplicationManager_ptr
CIAO::NodeApplicationManager_Impl::init (
    const char *nodeapp_location,
    const CORBA::ULong delay,
    const Deployment::DeploymentPlan & plan,
    const PortableServer::POA_ptr callback_poa
    ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Deployment::InvalidProperty))
{
  ACE_TRY
    {
      if (nodeapp_location == 0)
        ACE_TRY_THROW (CORBA::BAD_PARAM ());

      if (spawn_delay_ == 0)
        ACE_TRY_THROW (CORBA::BAD_PARAM ());

      this->nodeapp_path_.set (nodeapp_location);
      this->spawn_delay_ = delay;

      // Make a copy of the plan for later usage.
      this->plan_ =  plan;

      // Cache the call back POA for callback object.
      this->callback_poa_ = PortableServer::POA::_duplicate (callback_poa);

      // Activate the ourself.
      PortableServer::ObjectId_var oid
        = this->poa_->activate_object (this
                                       ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CORBA::Object_var obj =
        this->poa_->id_to_reference (oid.in ()
                                     ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // And cache the object reference.
      this->objref_ =
        Deployment::NodeApplicationManager::_narrow (obj.in ()
                                                     ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "NodeApplicationManager_Impl::init\t\n");
      ACE_RE_THROW;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (Deployment::NodeApplicationManager::_nil ());

  //return this object reference
  return
    Deployment::NodeApplicationManager::_duplicate (this->objref_.in ());
}

void
CIAO::NodeApplicationManager_Impl::
parse_config_value (ACE_CString & str
                    ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Deployment::InvalidProperty))
{
  // The unused arg is for future improvemnts.
  ACE_UNUSED_ARG (str);

  ACE_THROW ( CORBA::NO_IMPLEMENT() );
}

Deployment::NodeApplication_ptr
CIAO::NodeApplicationManager_Impl::
create_node_application (const ACE_CString & options
                         ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Deployment::StartError,
                   Deployment::InvalidProperty))
{
  Deployment::NodeApplication_var retval;
  Deployment::Properties_var prop;

  ACE_NEW_THROW_EX (prop,
                    Deployment::Properties,
                    CORBA::INTERNAL ());
  ACE_CHECK_RETURN (Deployment::NodeApplication::_nil());

  // @@ Create a new callback servant.
  CIAO::NodeApplication_Callback_Impl * callback_servant = 0;
  ACE_NEW_THROW_EX (callback_servant,
                    CIAO::NodeApplication_Callback_Impl (this->orb_.in (),
                                                    this->callback_poa_.in (),
                                                    this->objref_.in (),
                                                    prop.in ()),
                                                    CORBA::INTERNAL ());
  ACE_CHECK_RETURN (Deployment::NodeApplication::_nil());

  PortableServer::ServantBase_var servant_var (callback_servant);
  PortableServer::ObjectId_var cb_id
    = this->callback_poa_->activate_object (callback_servant
                                            ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (Deployment::NodeApplication::_nil());

  ACE_Process node_application;
  ACE_Process_Options p_options;

  ACE_TRY
    {
      CORBA::Object_var cb_obj =
        this->callback_poa_->id_to_reference (cb_id.in ()
                                              ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CIAO::NodeApplication_Callback_var cb =
        CIAO::NodeApplication_Callback::_narrow (cb_obj.in ()
                                                 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CORBA::String_var cb_ior =
        this->orb_->object_to_string (cb.in ()
                                      ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // spawn the new NodeApplication.
      p_options.command_line ("%s -k %s"
                              "%s",
                              this->nodeapp_path_.c_str (),
                              cb_ior.in (),
                              options.c_str ());

      p_options.avoid_zombies (1);

      if (node_application.spawn (p_options) == -1)
        {
          if (CIAO::debug_level () > 1)
            ACE_DEBUG ((LM_ERROR, "Fail to spawn a NodeApplication process\n"));

          ACE_TRY_THROW (Components::CreateFailure ());
        }

      // wait for nodeApp to pass back its object reference. with a
      // timeout value. using perform_work and stuff.

      int looping = 1;

      ACE_Time_Value timeout (this->spawn_delay_, 0);

      while (looping)
        {
          this->orb_->perform_work (timeout
                                    ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          retval = callback_servant->get_nodeapp_ref ();

          if (timeout == ACE_Time_Value::zero || !CORBA::is_nil (retval.in ()))
            looping = 0;
        }

      if (CORBA::is_nil (retval.in ()))
        {
          if (CIAO::debug_level () > 1)
            ACE_DEBUG ((LM_ERROR, "Fail to acquire the NodeApplication object\n"));

          ACE_TRY_THROW (Components::CreateFailure ());
        }

      {
        //ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
        this->nodeapp_ = Deployment::NodeApplication::_duplicate (retval.in ());
      }
    }
  ACE_CATCHANY
    {
      this->callback_poa_->deactivate_object (cb_id.in ());
      ACE_TRY_CHECK;

      ACE_RE_THROW;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (Deployment::NodeApplication::_nil ());

  this->callback_poa_->deactivate_object (cb_id.in () ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (Deployment::NodeApplication::_nil ());

  if (CIAO::debug_level () > 1)
    ACE_DEBUG ((LM_DEBUG, "CIAO::NodeApplicationManager_Impl::NodeApplication spawned!\n"));

  return retval._retn ();
}

Deployment::Connections *
CIAO::NodeApplicationManager_Impl::
create_connections (ACE_ENV_SINGLE_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Deployment::ResourceNotAvailable,
                   Deployment::StartError,
                   Deployment::InvalidProperty))
{
  Deployment::Connections_var retv;

  ACE_NEW_THROW_EX (retv,
                    Deployment::Connections (),
                    CORBA::INTERNAL ());
  ACE_CHECK_RETURN (0);

  CORBA::ULong len = retv->length ();

  const Component_Iterator end (this->component_map_.end ());
  for (Component_Iterator iter (this->component_map_.begin ());
       iter != end;
       ++iter)
  {
    // Get all the facets first
    Components::FacetDescriptions_var facets =
      ((*iter).int_id_)->get_all_facets (ACE_ENV_SINGLE_ARG_PARAMETER);
    ACE_CHECK_RETURN (0);

    // Get all the event consumers
    Components::ConsumerDescriptions_var consumers =
      ((*iter).int_id_)->get_all_consumers (ACE_ENV_SINGLE_ARG_PARAMETER);
    ACE_CHECK_RETURN (0);

    const CORBA::ULong facet_len = facets->length ();
    const CORBA::ULong consumer_len = consumers->length ();

    retv->length (facet_len + consumer_len);

    CORBA::ULong i = 0;
    for (i = 0; i < facet_len; ++i)
    {
      Deployment::Connection & conn = retv[len];
      conn.instanceName = (*iter).ext_id_.c_str ();
      conn.portName = facets[i]->name ();
      conn.kind = Deployment::Facet;
      conn.endpoint = CORBA::Object::_duplicate (facets[i]->facet_ref ());
      ++len;
    }

    for (i = 0; i < consumer_len; ++i)
    {
      Deployment::Connection & conn = retv[len];
      conn.instanceName = (*iter).ext_id_.c_str ();
      conn.portName = consumers[i]->name ();
      conn.kind = Deployment::EventConsumer;
      conn.endpoint = CORBA::Object::_duplicate (consumers[i]->consumer ());
      ++len;
    }
  }
  return retv._retn ();
}

Deployment::Application_ptr
CIAO::NodeApplicationManager_Impl::
startLaunch (const Deployment::Properties & configProperty,
             Deployment::Connections_out providedReference,
             CORBA::Boolean start
             ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Deployment::ResourceNotAvailable,
                   Deployment::StartError,
                   Deployment::InvalidProperty))
{
  ACE_UNUSED_ARG (configProperty);
  ACE_UNUSED_ARG (start);
  /**
   *  1. First Map properties to TAO/CIAO specific property/configurations
   *  2. Necessary property checking (needed?)
   *  3. Call create_nade_application to spawn new process.
   *  4. Initialize the NodeApplication.
   *  5. get the provided connection endpoints back and return them.
   */

  Deployment::ImplementationInfos infos;

  if (!(infos << (this->plan_)))
  {
    if (CIAO::debug_level () > 1)
      ACE_DEBUG ((LM_DEBUG, "Failed to create Component Implementation Infos!\n"));

    ACE_THROW_RETURN (Deployment::StartError (),
                      Deployment::Application::_nil());
  } //@@ I am not sure about which exception to throw. I will come back to this.

  // Now spawn the NodeApplication process.
  ACE_CString cmd_option ("");
  Deployment::NodeApplication_var tmp =
    create_node_application (cmd_option.c_str () ACE_ENV_ARG_PARAMETER);

  ACE_CHECK_RETURN (Deployment::Application::_nil());

  // This is what we will get back, a sequence of compoent object refs.
  Deployment::ComponentInfos_var comp_info;

  // For debugging.
  if (CIAO::debug_level () > 1)
  {
    const CORBA::ULong info_len = infos.length ();
    for (CORBA::ULong i = 0; i < info_len; ++i)
    {
      ACE_DEBUG ((LM_DEBUG, "The info for installation: \n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n",
                  infos[i].component_instance_name.in (),
                  infos[i].executor_dll.in (),
                  infos[i].executor_entrypt.in (),
                  infos[i].servant_dll.in (),
                  infos[i].servant_entrypt.in () ));
    }
  }

  // This will install all homes and components.
  comp_info = this->nodeapp_->install (infos ACE_ENV_ARG_PARAMETER);

  ACE_CHECK_RETURN (Deployment::Application::_nil());

  // Now fill in the map we have for the components.
  const CORBA::ULong comp_len = comp_info->length ();
  for (CORBA::ULong len = 0;
       len < comp_len;
       ++len)
  {
    //Since we know the type ahead of time...narrow is omitted here.
    if (this->component_map_.
        bind (comp_info[len].component_instance_name.in(),
              Components::CCMObject::_duplicate (comp_info[len].component_ref.in())))
      ACE_THROW_RETURN (Deployment::StartError (), 0);
  }

  providedReference = this->create_connections (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (Deployment::Application::_nil());

  if (providedReference == 0)
    ACE_THROW_RETURN (Deployment::StartError () ,
                      Deployment::Application::_nil());

  return Deployment::NodeApplication::_duplicate (this->nodeapp_.in ());
}


void
CIAO::NodeApplicationManager_Impl::
destroyApplication (Deployment::Application_ptr app
                    ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException
                   , Deployment::StopError))
{
  ACE_UNUSED_ARG (app);

  //ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, this->lock_);
  //@@ Since we know there is only 1 nodeapp so the passed in
  //   parameter is ignored for now.
  if (CORBA::is_nil (this->nodeapp_.in () ))
    ACE_THROW (Deployment::StopError ());

  this->nodeapp_->remove (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}