summaryrefslogtreecommitdiff
path: root/CIAO/DAnCE/NodeApplication/NodeApplication_Impl.cpp
blob: 1d13615ca5b2f2438da1ff5ac911c12d5f3b0ddc (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// $Id$
#include "NodeApplication_Impl.h"

#include "ace/OS_Memory.h"

#include "ace/streams.h"
#include "tao/AnyTypeCode/Any.h"
#include "tao/Object.h"
#include "tao/ORB.h"
#include "tao/AnyTypeCode/TypeCode.h"
#include "DAnCE/Logger/Log_Macros.h"
#include "Deployment/Deployment_BaseC.h"
#include "Deployment/Deployment_ApplicationC.h"
#include "Deployment/Deployment_PlanErrorC.h"
#include "Deployment/Deployment_ApplicationManagerC.h"
#include "DAnCE/DAnCE_Utility.h"
#include "DAnCE/DAnCE_PropertiesC.h"
#include "DAnCE/DAnCE_LocalityManagerC.h"

#include "ComponentAttributesSetter.h"

#include <string>

#ifdef GEN_OSTREAM_OPS
#include <iostream>
#include <sstream>
#endif /* GEN_OSTREAM_OPS */

using namespace DAnCE;
using namespace DAnCE::Utility;

NodeApplication_Impl::NodeApplication_Impl (CORBA::ORB_ptr orb,
                                            PortableServer::POA_ptr poa,
                                            const ::Deployment::DeploymentPlan& plan,
                                            const ACE_CString& node_name,
                                            const PROPERTY_MAP &properties)
  : orb_ (CORBA::ORB::_duplicate (orb)),
    poa_ (PortableServer::POA::_duplicate (poa)),
    plan_ (plan),
    node_name_ (node_name),
    properties_ (),
    handler_ (properties,
              orb,
              poa)
{
  DANCE_TRACE ("NodeApplication_Impl::NodeApplication_Impl");
  PROPERTY_MAP::const_iterator i = properties.begin ();
  while (!i.done ())
    {
      DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("NodeApplication_Impl::NodeApplication_Impl - ")
                    ACE_TEXT("Binding value for property '%C'\n"), i->key ().c_str ()));
      this->properties_.bind (i->key (), i->item ());
      i.advance ();
    }
  this->init ();
}

NodeApplication_Impl::~NodeApplication_Impl()
{
  DANCE_TRACE( "NodeApplication_Impl::~NodeApplication_Impl()");
}

void
NodeApplication_Impl::init()
{
  DANCE_TRACE( "NodeApplication_Impl::init()");

  /* TODO:  Lets move this stuff to the constructor, shall we?!? */
  /* TODO:  Might be nice to use a component configurator here to load the proper versions
     of the serveractivator.  */
  
  const ACE_TCHAR *ior = 0;

  if (get_property_value (DAnCE::INSTANCE_NC, this->properties_, ior) ||
      get_property_value (DAnCE::DOMAIN_NC, this->properties_, ior))
    {
      try
        {
          CORBA::Object_var obj = this->orb_->string_to_object (ior);
          this->instance_nc_ = CosNaming::NamingContext::_narrow (obj);
        }
      catch (const CORBA::Exception &e)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("NodeApplication_Impl::init - ")
                        ACE_TEXT("Unable to resolve the instance naming context:%C\n"),
                        e._info ().c_str ()));
        }
      DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("NodeApplication_Impl::init - ")
                    ACE_TEXT("Successfully resolved the instance naming context.\n")));
    }
  else DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("NodeApplication_Impl::init - ")
                     ACE_TEXT("No instance NC was provided\n")));
}

void 
NodeApplication_Impl::init_instances (void)
{
  DANCE_TRACE ("NodeApplication::init_instances");
  
  for (CORBA::ULong i = 0; i < plan_.instance.length (); ++i)
    {
      CORBA::ULong impl_idx = plan_.instance[i].implementationRef;
      
      if (impl_idx >= plan_.implementation.length ())
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO
                           ACE_TEXT("NodeApplication_Impl::init_instances - ")
                           ACE_TEXT("Invalid impelmentation index\n")));
        }

      const char *instance_type = 
        DAnCE::Utility::get_instance_type (plan_.implementation[impl_idx].execParameter);
      
      if (instance_type)
        DANCE_DEBUG (6, (LM_DEBUG, DLINFO
                         ACE_TEXT("NodeApplication_Impl::init_instances - ")
                         ACE_TEXT("Considering instance %u:%C with type %C\n"),
                         i,
                         plan_.instance[i].name.in (),
                         instance_type));
      else
        {
          DANCE_DEBUG (4, (LM_DEBUG, DLINFO
                           ACE_TEXT ("NodeApplication_Impl::init_instances - ")
                           ACE_TEXT ("Skipping instance %u:%C\n"),
                           i,
                           plan_.instance[i].name.in ()
                           ));
          continue;
        }

      if (ACE_OS::strcmp (instance_type,
                          DANCE_LOCALITYMANAGER) == 0)
        {
          DANCE_DEBUG (6, (LM_DEBUG, DLINFO
                           ACE_TEXT("NodeApplication_Impl::init_instances - ")
                           ACE_TEXT("Found LocalityManager instance, deploying\n")));
          
          CORBA::Any_var reference;
          this->handler_.install_instance (this->plan_,
                                           i,
                                           reference.out ());
          
          ::DAnCE::LocalityManager_var lm_ref;
          if (reference.in ()  >>= lm_ref)
            {
              this->localities_[plan_.instance[i].name.in ()] = lm_ref._retn ();
              DANCE_DEBUG (4, (LM_INFO, DLINFO
                               ACE_TEXT("NodeApplication_Impl::init_instances - ")
                               ACE_TEXT("Successfully started Locality %C\n"),
                               plan_.instance[i].name.in ()));
            }
          else
            {
              DANCE_ERROR (1, (LM_ERROR, DLINFO
                               ACE_TEXT("NodeApplication_Impl::init_instances - ")
                               ACE_TEXT("Unable to resolve LocalityManager object reference\n")));
              throw ::Deployment::StartError (plan_.instance[i].name.in (),
                                              "Unable to resolve LocalityManager object ref\n");
            }
        }
    }
}

void 
NodeApplication_Impl::prepare_instances (void)
{
  DANCE_TRACE ("NodeApplication_Impl::prepare_instances");

  typedef DAnCE::Split_Plan < DAnCE::Locality_Splitter > Splitter;
  Splitter split_plan;
  split_plan.split_plan (this->plan_);

  Splitter::TSubPlans &plans = split_plan.plans ();
  
  // @@ Ideally, it would be nice to have the key for the subplans
  // be the name of the locality instance.
  
  CORBA::ULong plan (0);
  // for each sub plan
  for (Splitter::TSubPlanIterator i = plans.begin ();
       i != plans.end ();
       ++i)
    {
      DANCE_DEBUG (9, (LM_TRACE, DLINFO
                       "NodeApplication_Impl::prepare_instances - "
                       "Considering sub-plan %u\n",
                       plan++));
      ::Deployment::DeploymentPlan &sub_plan = 
        (*i).int_id_;
      
      bool locality_found (false);

      // We need to find the locality
      for (CORBA::ULong j = 0; j < sub_plan.instance.length (); ++j)
        {
          ::Deployment::InstanceDeploymentDescription &inst = 
            sub_plan.instance[j];
          ::Deployment::MonolithicDeploymentDescription &mdd =
              sub_plan.implementation[inst.implementationRef];
          
          const char *instance_type = 
            DAnCE::Utility::get_instance_type (mdd.execParameter);
          
          if (instance_type &&
              ACE_OS::strcmp (instance_type,
                              DANCE_LOCALITYMANAGER) == 0)
            {
              locality_found = true;
              DANCE_DEBUG (4, (LM_DEBUG, DLINFO
                               "NodeApplication_Impl::prepare_instances - "
                               "Invoking preparePlan on locality %C\n",
                               inst.name.in ()));
              this->prepare_instance (inst.name.in (),
                                      sub_plan);
            }
        }
      
      if (!locality_found &&
          sub_plan.instance.length () != 0)
        {
          // Need to spawn default locality. 
          DANCE_DEBUG (4, (LM_DEBUG, DLINFO
                           "NodeApplication_Impl::prepare_instances - "
                           "No locality manager found, creating a default locality "
                           "named <%C>\n",
                           sub_plan.instance[0].name.in ()
                           ));
          
          // Create minimal plan required for locality manager spawn
          // This is a temporary workaround.
          ::Deployment::DeploymentPlan tmp_plan;
          tmp_plan.UUID = this->plan_.UUID;
          tmp_plan.implementation.length (1);
          tmp_plan.instance.length (1);
          tmp_plan.instance[0].implementationRef = 0;
          tmp_plan.instance[0].name = sub_plan.instance[0].name.in ();
          
          CORBA::Any_var reference;
          this->handler_.install_instance (tmp_plan,
                                           0,
                                           reference.out ());
          
          ::DAnCE::LocalityManager_var lm_ref;
          if (reference.in ()  >>= lm_ref)
            {
              this->localities_[tmp_plan.instance[0].name.in ()] = lm_ref._retn ();
              DANCE_DEBUG (4, (LM_INFO, DLINFO
                               ACE_TEXT("NodeApplication_Impl::prepare_instances - ")
                               ACE_TEXT("Successfully started Locality %C\n"),
                               tmp_plan.instance[0].name.in ()));
            }
          else
            {
              DANCE_ERROR (1, (LM_ERROR, DLINFO
                               ACE_TEXT("NodeApplication_Impl::prepare_instances - ")
                               ACE_TEXT("Unable to resolve LocalityManager object reference\n")));
              throw ::Deployment::StartError (tmp_plan.instance[0].name.in (),
                                              "Unable to resolve default LocalityManager object ref\n");
            }

          this->prepare_instance (sub_plan.instance[0].name.in (),
                                  sub_plan);
        }
      
    }
}

void 
NodeApplication_Impl::prepare_instance (const char *name,
                                        const Deployment::DeploymentPlan &plan)
{
  DANCE_TRACE ("NodeApplication_Impl::prepare_instance");

  ::Deployment::ApplicationManager_var app;

  try
    {
      app = this->localities_[name]->preparePlan (plan,
                                                 0);
      DANCE_DEBUG (6, (LM_DEBUG, DLINFO
                       "NodeApplication_Impl::prepare_instance - "
                       "Locality <%C> successfully prepared.\n",
                       name));
    }
  catch (CORBA::Exception &ex)
    {
      DANCE_ERROR (2, (LM_ERROR, DLINFO
                       "NodeApplication_Impl::prepare_instance - "
                       "Caugt unexpected CORBA exception while invoking preparePlan %C\n",
                       ex._info ().c_str ()));
    }

  if (CORBA::is_nil (app.in ()))
    {
      DANCE_ERROR (1, (LM_ERROR, DLINFO "NodeApplication_Impl::prepare_instance - "
                       "Error: Didn't get a valid reference back from LM preparePlan "
                       "for locality %C\n",
                       name));
      
      // For the time being, we don't need to cache this reference.
      // it's the same as the reference we used to invoke preparePlan.
    }
  
}

void 
NodeApplication_Impl::start_launch_instances (const Deployment::Properties &prop,
                                              Deployment::Connections_out providedReference)
{
  DANCE_TRACE ("NodeApplication_Impl::start_launch_instances");
  Deployment::Connections *tmp (0);
  
  ACE_NEW_THROW_EX (tmp,
                    Deployment::Connections (this->plan_.connection.length ()),
                    CORBA::NO_MEMORY ());
  
  Deployment::Connections_var retval (tmp);
  
  for (LOCALITY_MAP::const_iterator i = this->localities_.begin ();
       i != this->localities_.end (); ++i)
    {
      DANCE_DEBUG (4, (LM_INFO, DLINFO
                       "NodeApplication_Impl::start_launch_instances - "
                       "StartLaunching locality <%C>\n",
                       i->first.c_str ()));
      
      try
        {
          i->second->startLaunch (prop,
                                  providedReference);
        }
      catch (Deployment::PlanError &ex)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO
                           "NodeApplication_Impl::start_launch_instances - "
                           "Caught PlanError Exception %C:%C\n",
                           ex.name.in (),
                           ex.reason.in ()));
          throw;
        }
      catch (CORBA::Exception &ex)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO
                           "NodeApplication_Impl::start_launch_instances - "
                           "Caught CORBA Exception %C\n",
                           ex._info ().c_str ()));
          throw;
        }
    }
}

void
NodeApplication_Impl::finishLaunch (const ::Deployment::Connections & providedReference,
                                    ::CORBA::Boolean start)
{
  DANCE_TRACE ("NodeApplication_Impl::finishLaunch");

  
  for (LOCALITY_MAP::const_iterator i = this->localities_.begin ();
       i != this->localities_.end (); ++i)
    {
      DANCE_DEBUG (4, (LM_INFO, DLINFO
                       "NodeApplication_Impl::finish_launch_instances - "
                       "FinishLaunching locality <%C>\n",
                       i->first.c_str ()));
      
      try
        {
          i->second->finishLaunch (providedReference,
                                   start);
        }
      catch (CORBA::Exception &ex)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO
                           "NodeApplication_Impl::finish_launch_instances - "
                           "Caught CORBA Exception %C\n",
                           ex._info ().c_str ()));
          throw;
        }
    }
}

void
NodeApplication_Impl::start ()
{
  DANCE_TRACE( "NodeApplication_Impl::start");

  for (LOCALITY_MAP::const_iterator i = this->localities_.begin ();
       i != this->localities_.end (); ++i)
    {
      DANCE_DEBUG (4, (LM_INFO, DLINFO
                       "NodeApplication_Impl::start - "
                       "Starting locality <%C>\n",
                       i->first.c_str ()));
      
      try
        {
          i->second->start ();
        }
      catch (CORBA::Exception &ex)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO
                           "NodeApplication_Impl::start - "
                           "Caught CORBA Exception %C\n",
                           ex._info ().c_str ()));
          throw;
        }
    }
}

void 
NodeApplication_Impl::remove_instances (void)
{
  DANCE_TRACE ("NodeApplication_Impl::remove_instances");
  
  ::Deployment::StopError final_exception;
  bool flag (false);
  
  
    for (LOCALITY_MAP::iterator i = this->localities_.begin ();
       i != this->localities_.end (); ++i)
    {
      DANCE_DEBUG (4, (LM_INFO, DLINFO
                       "NodeApplication_Impl::remove_instances - "
                       "Removing locality <%C>\n",
                       i->first.c_str ()));
      
      try
        {
          CORBA::Any ref;
          ref <<= DAnCE::LocalityManager::_duplicate (i->second);
          
          i->second->destroyApplication (0);

          this->handler_.remove_instance (this->plan_,
                                          0, /* not needed at this time */
                                          ref);
                                           
        }
      catch (::Deployment::StopError &ex)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO
                           "NodeApplication_Impl::remove_instances - "
                           "Caught StopError final_exception %C, %C\n",
                           ex.name.in (),
                           ex.reason.in ()));
          Utility::test_and_set_exception (flag,
                                             final_exception,
                                             ex.name.in (),
                                             ex.reason.in ());
                                             
        }
      catch (CORBA::Exception &ex)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO
                           "NodeApplication_Impl::remove_instances - "
                           "Caught CORBA Final_Exception %C\n",
                           ex._info ().c_str ()));
          Utility::test_and_set_exception (flag,
                                             final_exception,
                                             "Unknown CORBA Final_Exception",
                                             ex._info ().c_str ());
        }
      
      //this->localities_.erase (i);
    }
    
    if (flag)
      throw final_exception;
}