summaryrefslogtreecommitdiff
path: root/modules/CIAO/DAnCE/LocalityManager/Daemon/Locality_Manager_Impl.cpp
blob: e0f373fe4a33f40b9202409033ac5a5875ada1d3 (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
// -*- C++ -*-
// $Id$

// TAO_IDL - Generated from 
// be/be_codegen.cpp:1579

#include "Locality_Manager_Impl.h"
#include "Logger/Log_Macros.h"


/*
 * CIAO Instance handlers
 * @TODO:  Needs to be replaced by dynamic provisioning system.
 */
#include "ciao/Deployment/Handlers/Container_Handler.h"
#include "ciao/Deployment/Handlers/Component_Handler.h"
#include "ciao/Deployment/Handlers/Home_Handler.h"
#include "ciao/Deployment/Handlers/Homed_Component_Handler.h"
#include "Deployment_Interceptors.h"
//

namespace DAnCE
{
  // Implementation skeleton constructor
  LocalityManager_i::LocalityManager_i (const ACE_TString &uuid,
                                        CORBA::ORB_ptr orb,
                                        PortableServer::POA_ptr poa)
    : uuid_ (uuid),
      orb_ (CORBA::ORB::_duplicate  (orb)),
      poa_ (PortableServer::POA::_duplicate (poa))
  {
    DANCE_TRACE ("LocalityManager_i::LocalityManager_i");
  }

  // Implementation skeleton destructor
  LocalityManager_i::~LocalityManager_i (void)
  {
    DANCE_TRACE ("LocalityManager_i::~LocalityManager_i");
  }
  
  void
  LocalityManager_i::init (Deployment::Properties *)
  {
    DANCE_TRACE ("LocalityManager_i::init");
    
    // Initialize the CIAO instance handlers
    ::DAnCE::InstanceDeploymentHandler_ptr tmp;
    
    DAnCE::Utility::PROPERTY_MAP props;
    
    ACE_NEW_THROW_EX (tmp,
                      CIAO::Container_Handler_i (props,
                                                 this->orb_,
                                                 this->poa_),
                      CORBA::NO_MEMORY ());

    this->instance_handlers_[tmp->instance_type ()] = Handler (tmp);
    this->handler_order_.push_back (tmp->instance_type ());
    
    ACE_NEW_THROW_EX (tmp,
                      CIAO::Home_Handler_i,
                      CORBA::NO_MEMORY ());

    this->instance_handlers_[tmp->instance_type ()] = Handler (tmp);
    this->handler_order_.push_back (tmp->instance_type ());

    ACE_NEW_THROW_EX (tmp,
                      CIAO::Component_Handler_i (),
                      CORBA::NO_MEMORY ());

    this->instance_handlers_[tmp->instance_type ()] = Handler (tmp);                                     
    this->handler_order_.push_back (tmp->instance_type ());
    
    ACE_NEW_THROW_EX (tmp,
                      CIAO::Homed_Component_Handler_i (),
                      CORBA::NO_MEMORY ());
    
    this->instance_handlers_[tmp->instance_type ()] = Handler (tmp);
    this->handler_order_.push_back (tmp->instance_type ());
    
    ACE_NEW_THROW_EX (this->ii_interceptor_,
                      DAnCE_StoreReferences_i (this->orb_.in ()),
                      CORBA::NO_MEMORY ());
  }

  ::Deployment::Properties *
  LocalityManager_i::configuration (void)
  {
    DANCE_TRACE ("LocalityManager_i::configuration");
    throw CORBA::NO_IMPLEMENT ();
  }

  ::Deployment::ApplicationManager_ptr
  LocalityManager_i::preparePlan (const ::Deployment::DeploymentPlan & plan,
                                  ::Deployment::ResourceCommitmentManager_ptr)
  {
    DANCE_TRACE ("LocalityManager_i::preparePlan");
    
    this->plan_ = plan;
    
    // populate the impl_type_table.
    for (CORBA::ULong i = 0;
         i < plan_.instance.length ();
         ++i)
      {
        CORBA::ULong implRef = plan.instance[i].implementationRef;

        const char  *instance_type = 
          Utility::get_instance_type (plan.implementation[implRef].execParameter);
        
        instance_handlers_[instance_type].instances_.push_back (i);
      }
    
    return this->_this ();
    // Add your implementation here
  }

  ::Deployment::Application_ptr
  LocalityManager_i::startLaunch (const ::Deployment::Properties &,
                                  ::Deployment::Connections_out providedReference)
  {
    DANCE_TRACE ("LocalityManager_i::startLaunch");
    
    
    for (HANDLER_ORDER::const_iterator i = this->handler_order_.begin ();
         i != this->handler_order_.end ();
         ++i)
      {
        DANCE_DEBUG (8, (LM_TRACE, DLINFO
                         "LocalityManager_i::startLaunch - "
                         "Starting installation of %C type instances\n",
                         i->c_str ()));

        ::DAnCE::InstanceDeploymentHandler_ptr handler = 
          this->instance_handlers_[*i].handler_;
        INSTANCE_LIST &inst_list = this->instance_handlers_[*i].instances_;

        for (INSTANCE_LIST::const_iterator j = inst_list.begin ();
             j != inst_list.end ();
             ++j)
          {
            DANCE_DEBUG (7, (LM_TRACE, DLINFO
                             "LocalityManager_i::startLaunch - "
                             "Starting installation of instance %C\n",
                             this->plan_.instance[*j].name.in ()));
                             
                             
            this->ii_interceptor_->instance_pre_install (this->plan_,
                                                         *j);

            CORBA::Any_var reference;
            handler->install_instance (this->plan_,
                                       *j,
                                       reference.out ());
            
            CORBA::Any exception;

            this->ii_interceptor_->instance_post_install (this->plan_,
                                                          *j,
                                                          reference.in (),
                                                          exception);
            this->instance_references_[*j] = reference._retn ();
            

            // @@ todo: post-interceptor
          }
      }

    ::Deployment::Connections *conn_cmp;
    
    ACE_NEW_THROW_EX (conn_cmp,
                      ::Deployment::Connections (this->plan_.connection.length ()),
                      CORBA::NO_MEMORY ());
    
    CORBA::ULong pos (0);

    for (CORBA::ULong i = 0;
         i < this->plan_.connection.length ();
         ++i)
      {
        const ::Deployment::PlanConnectionDescription &conn = 
          this->plan_.connection[i];
        for (CORBA::ULong j = 0;
             j != conn.internalEndpoint.length ();
             ++j)
          {
            if (conn.internalEndpoint[j].provider)
              {
                CORBA::ULong instRef =
                  conn.internalEndpoint[j].instanceRef;
                CORBA::ULong implRef = 
                  this->plan_.instance[instRef].implementationRef;
                
                const char  *inst_type = 
                  Utility::get_instance_type (this->plan_.implementation[implRef].execParameter);
                
                CORBA::Any_var ref;

                this->instance_handlers_[inst_type].handler_->
                  provide_endpoint_reference (this->plan_,
                                              i,
                                              ref.out ());
                
                CORBA::Object_var obj_ref;

                ref >>= CORBA::Any::to_object (obj_ref);
                conn_cmp->length (pos + 1);
                (*conn_cmp)[pos].name = conn.name.in ();
                (*conn_cmp)[pos].endpoint.length (1);
                (*conn_cmp)[pos].endpoint[0] = obj_ref;                
                ++pos;
              }
          }
      }
         
    
    providedReference = conn_cmp;

    return this->_this ();
  }

  void
  LocalityManager_i::finishLaunch (const ::Deployment::Connections & providedReference,
                                   ::CORBA::Boolean start)
  {
    DANCE_TRACE ("LocalityManager_i::finishLaunch");
    // Add your implementation here
    
    typedef std::map < std::string, CORBA::ULong > ConnMap;
    ConnMap conns;
    
    DANCE_DEBUG (6, (LM_TRACE, DLINFO 
                     "LocalityManager_i::finishLaunch - "
                     "Starting finsihLaunch, received %u references, "
                     "have %u connections\n",
                     providedReference.length (),
                     this->plan_.connection.length ()
                     ));

    for (CORBA::ULong i = 0; i < this->plan_.connection.length (); ++i)
      {
        conns[this->plan_.connection[i].name.in ()] = i;
      }
    
    for (CORBA::ULong i = 0; i < providedReference.length (); ++i)
      {
        const char * name = providedReference[i].name.in ();
        ConnMap::const_iterator conn_ref = conns.find (name);
        
        if (conn_ref == conns.end ())
          continue;
        
        CORBA::ULong j (0);
        
        const ::Deployment::PlanConnectionDescription &conn = 
          this->plan_.connection[conn_ref->second];
        
        DANCE_DEBUG (9, (LM_TRACE, DLINFO
                         "LocalityManager_i::finishLaunch - "
                         "Connection <%C> has %u endpoints\n",
                         conn.name.in (),
                         conn.internalEndpoint.length ()));

        if (conn.internalEndpoint.length () == 2)
          {
            if (!conn.internalEndpoint[1].provider)
              j = 1;
          }
        else if (conn.internalEndpoint[0].provider &&
                 conn.externalReference.length () == 0)
          {
            DANCE_DEBUG (9, (LM_TRACE, DLINFO
                             "LocalityManager_i::finishLaunch - "
                             "Skipping connection <%C>\n",
                             conn.name.in ()));
            continue;
          }
          
        DANCE_DEBUG (6, (LM_DEBUG, DLINFO 
                         "LocalityManager_i::finishLaunch - "
                         "Starting connection <%C>\n",
                         name));
        
        CORBA::Any reference;
        
        reference <<= providedReference[i].endpoint[0];

        CORBA::ULong instRef =
          conn.internalEndpoint[j].instanceRef;
        CORBA::ULong implRef = 
          this->plan_.instance[instRef].implementationRef;
        
        const char  *inst_type = 
          Utility::get_instance_type (this->plan_.implementation[implRef].execParameter);
        
        // @@ placeholder for pre_connect interceptor
        this->instance_handlers_[inst_type].handler_->
          connect_instance (this->plan_,
                            conn_ref->second,
                            reference);
        // @@ placeholder for post_connect interceptor
      }

    
    for (HANDLER_ORDER::const_iterator i = this->handler_order_.begin ();
         i != this->handler_order_.end ();
         ++i)
      {
        ::DAnCE::InstanceDeploymentHandler_ptr handler = 
          this->instance_handlers_[*i].handler_;
        INSTANCE_LIST &inst_list = this->instance_handlers_[*i].instances_;
        
        for (INSTANCE_LIST::const_iterator i = inst_list.begin ();
             i != inst_list.end ();
             ++i)
          {
            CORBA::Any_var reference;
            handler->instance_configured (this->plan_,
                                          *i);
          }
      }

    if (start)
      this->start ();
  }

  void
  LocalityManager_i::start (void)
  {
    DANCE_TRACE ("LocalityManager_i::start");
    
    for (HANDLER_ORDER::const_iterator i = this->handler_order_.begin ();
         i != this->handler_order_.end ();
         ++i)
      {
        ::DAnCE::InstanceDeploymentHandler_ptr handler = 
          this->instance_handlers_[*i].handler_;
        INSTANCE_LIST &inst_list = this->instance_handlers_[*i].instances_;
        
        for (INSTANCE_LIST::const_iterator i = inst_list.begin ();
             i != inst_list.end ();
             ++i)
          {
            CORBA::Any_var reference;
            handler->activate_instance (this->plan_,
                                        *i,
                                        reference.in ());
          }
      }
  }

  void 
  LocalityManager_i::destroyApplication (::Deployment::Application_ptr)
  {
    DANCE_TRACE ("LocalityManager_i::destroyApplication");
    
    for (HANDLER_ORDER::const_reverse_iterator i = this->handler_order_.rbegin ();
         i != this->handler_order_.rend ();
         ++i)
      {
        ::DAnCE::InstanceDeploymentHandler_ptr handler = 
          this->instance_handlers_[*i].handler_;
        INSTANCE_LIST &inst_list = this->instance_handlers_[*i].instances_;
        
        for (INSTANCE_LIST::const_iterator i = inst_list.begin ();
             i != inst_list.end ();
             ++i)
          {
            CORBA::Any_var reference;
            handler->passivate_instance (this->plan_,
                                         *i,
                                         reference.in ());
          }
      }
    
    for (HANDLER_ORDER::const_reverse_iterator i = this->handler_order_.rbegin ();
           i != this->handler_order_.rend ();
         ++i)
      {
        ::DAnCE::InstanceDeploymentHandler_ptr handler = 
          this->instance_handlers_[*i].handler_;
        INSTANCE_LIST &inst_list = this->instance_handlers_[*i].instances_;
        
        for (INSTANCE_LIST::const_iterator i = inst_list.begin ();
             i != inst_list.end ();
             ++i)
          {
            CORBA::Any_var reference;
            handler->remove_instance (this->plan_,
                                         *i,
                                         reference.in ());
          }
      }
    // Add your implementation here
  }


  void 
  LocalityManager_i::destroyManager (::Deployment::ApplicationManager_ptr)
  {
    DANCE_TRACE ("LocalityManager_i::destroyManager");
    // Add your implementation here
  }
  
  
  void 
  LocalityManager_i::shutdown (void)
  {
    DANCE_TRACE ("LocalityManager_i::shutdown");
    
    DANCE_DEBUG (6, (LM_DEBUG, DLINFO
                     "DAnCE LocalityManager shutdown request received for UUID <%C>\n",
                     uuid_.c_str ()));
    
    this->orb_->shutdown ();
    // Add your implementation here
  }
}