summaryrefslogtreecommitdiff
path: root/CIAO/ciao/Deployment/Handlers/Container_Handler.cpp
blob: dc7172478488548bdbe1460e2f242b763cda22af (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
// $Id$

#include "Container_Handler.h"
#include "ciao/Logger/Log_Macros.h"
#include "ciao/Logger/Logger_Service.h"
#include "ciao/Base/CIAO_PropertiesC.h"
#ifdef DANCE_DEPLOYS_EXTENSION_CONTAINER
  #include "ciao/Containers/Extension/Extension_Container.h"
#else
  #include "ciao/Containers/Session/Session_Container.h"
#endif
#include "ciao/Base/Server_init.h"
#include "dance/DAnCE_Utility.h"
#include "dance/DAnCE_PropertiesC.h"
#include "dance/LocalityManager/Scheduler/Plugin_Manager.h"
#include "CIAO_State.h"

namespace CIAO
{
  Container_Handler_i::Container_Handler_i (void)
  {
    // Also initialize CIAO logger since we reuse parts of CIAO in the locality manager
    CIAO::Logger_Service
      * clf = ACE_Dynamic_Service<CIAO::Logger_Service>::instance ("CIAO_Logger");

    int argc = 0;
    ACE_TCHAR **argv = 0;

    if (clf)
      {
        clf->init (argc, argv);
      }

    CIAO_TRACE ("Container_Handler_i::Container_Handler_i");
  }

  // Destructor
  Container_Handler_i::~Container_Handler_i (void)
  {
    CIAO_TRACE ("Container_Handler_i::~Container_Handler_i");
  }

  void
  Container_Handler_i::close (void)
  {
    try
      {
        // For the time being, we are going to go ahead and delete
        // the default container
        ::Deployment::DeploymentPlan plan;
        plan.instance.length (1);
        plan.instance[0].name = "";

        CORBA::Any any;
        this->remove_instance (plan, 0, any);
      }
    catch (...)
      { //swallow
      }

    DEPLOYMENT_STATE::instance ()->close ();
    DEPLOYMENT_STATE::close ();

    this->orb_ = CORBA::ORB::_nil ();
    this->poa_ = PortableServer::POA::_nil ();
  }

  ::CORBA::StringSeq *
  Container_Handler_i::dependencies (void)
  {
    ::CORBA::StringSeq *retval (0);
    ACE_NEW_THROW_EX (retval,
                      ::CORBA::StringSeq (0),
                      CORBA::NO_MEMORY ());

    return retval;
  }

  char *
  Container_Handler_i::instance_type (void)
  {
    CIAO_TRACE ("Container_Handler_i::instance_type");
    return CORBA::string_dup (CIAO::Deployment::CCM_CONTAINER);
  }

  void
  Container_Handler_i::install_instance (const ::Deployment::DeploymentPlan &plan,
                                         ::CORBA::ULong instanceRef,
                                         ::CORBA::Any_out instance_reference)
  {
    CIAO_TRACE ("Container_Handler_i::install_instance");

    ACE_NEW_THROW_EX (instance_reference,
                      ::CORBA::Any (),
                      CORBA::NO_MEMORY ());

    const char *name (plan.instance[instanceRef].name.in ());
#ifdef DANCE_DEPLOYS_EXTENSION_CONTAINER
    CIAO::Extension_Container *cont (0);

    CIAO_DEBUG (6, (LM_DEBUG, CLINFO
                    "Container_Handler_i::install_instance - "
                    "Creating extension container with id <%C>\n",
                    name));
    ACE_NEW_THROW_EX (cont,
                      CIAO::Extension_Container_i (this->orb_,
                                                   this->poa_),
                      CORBA::NO_MEMORY ());
#else
    CIAO::Session_Container *cont (0);

    CIAO_DEBUG (6, (LM_DEBUG, CLINFO
                    "Container_Handler_i::install_instance - "
                    "Creating session container with id <%C>\n",
                    name));
    ACE_NEW_THROW_EX (cont,
                      CIAO::Session_Container_i (this->orb_,
                                                 this->poa_),
                      CORBA::NO_MEMORY ());
#endif

    CIAO::Container_var container_ref (cont);

    CIAO_DEBUG (8, (LM_DEBUG, CLINFO
                    "Container_Handler_i::install_instance - "
                    "Container <%C> successfully created\n",
                    name));

    container_ref->init (name);

    CIAO_DEBUG (8, (LM_DEBUG, CLINFO
                    "Container_Handler_i::install_instance - "
                    "Container <%C> successfully initialized\n",
                    name));

    DEPLOYMENT_STATE::instance ()->add_container (name, container_ref.in ());

    *instance_reference <<= container_ref;
  }

  void
  Container_Handler_i::activate_instance (const ::Deployment::DeploymentPlan & ,
                                     ::CORBA::ULong ,
                                     const ::CORBA::Any &)
  {
    CIAO_TRACE ("Container_Handler_i::activate_instance");
    // no activation needed.
  }

  void
  Container_Handler_i::passivate_instance (const ::Deployment::DeploymentPlan & ,
                                      ::CORBA::ULong ,
                                      const ::CORBA::Any &)
  {
    CIAO_TRACE ("Container_Handler_i::passivate_instance");
    // no passivation needed.
  }

  void
  Container_Handler_i::provide_endpoint_reference (const ::Deployment::DeploymentPlan &,
                                                   ::CORBA::ULong,
                                                   ::CORBA::Any_out)
  {
    CIAO_TRACE ("Container_Handler_i::provide_endpoint_reference");
    CIAO_ERROR (1, (LM_ERROR, CLINFO
                    "Container_Handler_i::provide_endpoint_reference - "
                    "Unable to provide any endpoints.\n"));
    throw CORBA::NO_IMPLEMENT ();
  }

  void
  Container_Handler_i::remove_instance (
    const ::Deployment::DeploymentPlan & plan,
    ::CORBA::ULong instanceRef,
    const ::CORBA::Any &)
  {
    CIAO_TRACE ("Container_Handler_i::remove_instance");

    const char *name = plan.instance[instanceRef].name.in ();

    ::CIAO::Container_var cont =
        DEPLOYMENT_STATE::instance ()->fetch_container (name);

    if (::CORBA::is_nil (cont.in ()))
      {
        CIAO_ERROR (1, (LM_ERROR, CLINFO
                        "Container_Handler_i::remove_instance - "
                        "Error, no container with id <%C>\n",
                        name));
        throw ::Deployment::StopError (name,
                                       "No container with ID");
      }

    CIAO_DEBUG (8, (LM_TRACE, CLINFO
                    "Container_Handler_i::remove_instance - "
                    "Finalizing container with Id <%C>\n",
                    name));

    // Instructing container to cleanup its state
    cont->fini ();

    CIAO_DEBUG (8, (LM_TRACE, CLINFO
                    "Container_Handler_i::remove_instance - "
                    "Removing container with Id <%C>\n",
                    name));

    DEPLOYMENT_STATE::instance ()->remove_container (name);

    CIAO_DEBUG (5, (LM_TRACE, CLINFO
                    "Container_Handler_i::remove_instance - "
                    "Container with Id <%C> removed.\n",
                    name));
  }

  void
  Container_Handler_i::connect_instance (const ::Deployment::DeploymentPlan &,
                                         ::CORBA::ULong,
                                         const ::CORBA::Any &)
  {
    CIAO_TRACE ("Container_Handler_i::connect_instance");

    CIAO_ERROR (1, (LM_ERROR, CLINFO
                    "Container_Handler_i::connect_instance - ",
                    "No connections allowed for containers.\n"));
    throw CORBA::NO_IMPLEMENT ();
  }


  void
  Container_Handler_i::disconnect_instance (const ::Deployment::DeploymentPlan &,
                                            ::CORBA::ULong)
  {
    CIAO_TRACE ("Container_Handler_i::disconnect_instance");

    CIAO_ERROR (1, (LM_ERROR, CLINFO
                    "Container_Handler_i::disconnect_instance - ",
                    "No connections allowed for containers.\n"));
    throw CORBA::NO_IMPLEMENT ();
  }

  void
  Container_Handler_i::instance_configured (const ::Deployment::DeploymentPlan &,
                                            ::CORBA::ULong)
  {
    CIAO_TRACE ("Container_Handler_i::instance_configured");
    // nothing to be done
  }

  void
  Container_Handler_i::configure (const ::Deployment::Properties &props )
  {
    CIAO_DEBUG (6, (LM_DEBUG, CLINFO
                    "Container_Handler_i::configure - "
                    "Received %u properties for configuration\n",
                    props.length ()));

    this->orb_ = DAnCE::PLUGIN_MANAGER::instance ()->get_orb ();

    if (CORBA::is_nil (this->orb_))
      {
        CIAO_ERROR (1, (LM_ERROR, CLINFO
                        "Container_Handler_i::configure - "
                        "Unable to locate ORB.\n"));
        throw ::Deployment::StartError ("CIAO Container Handler ",
                                        "Unable to locate ORB");
      }

    CORBA::Object_var object =
      this->orb_->resolve_initial_references ("RootPOA");

    this->poa_ =
      PortableServer::POA::_narrow (object.in ());

    if (CORBA::is_nil (this->poa_))
      {
        CIAO_ERROR (1, (LM_ERROR, CLINFO
                        "Container_Handler_i::configure - "
                        "Unable to locate POA.\n"));
        throw ::Deployment::StartError ("CIAO Container Handler ",
                                        "Unable to locate POA");
      }

    CIAO::Server_init (orb_);

    // For the time being, we are going to go ahead and construct a container.
    ::Deployment::DeploymentPlan plan;
    plan.instance.length (1);
    plan.instance[0].name = "";

    ::CORBA::Any_var any;
    this->install_instance (plan, 0, any.out ());
  }
}

extern "C"
{
  ::DAnCE::InstanceDeploymentHandler_ptr
  CIAO_Locality_Handler_Export create_Container_Handler (void)
  {
    return new CIAO::Container_Handler_i ();
  }
}