summaryrefslogtreecommitdiff
path: root/modules/CIAO/DAnCE/RedirectionService/NameServiceRedirection.cpp
blob: 702d7e7c40094f9f4e96ef23446dd05d98145483 (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
// $Id$

#include "NameServiceRedirection.h"
#include "DAnCE/Logger/Log_Macros.h"

using namespace DAnCE;

NameServiceRedirection::NameServiceRedirection (CosNaming::NamingContext_ptr naming,
                                                CosNaming::NamingContext_ptr domain)
  : naming_ (CosNaming::NamingContext::_duplicate (naming)), 
    domain_ (CosNaming::NamingContext::_duplicate (domain))
{
}

NameServiceRedirection::~NameServiceRedirection()
{
}

void
NameServiceRedirection::start_binding (const ACE_CString& node, const ACE_CString& plan)
{
  DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::start_binding for node %C plan %C.\n"),
                 node.c_str(),
                 plan.c_str()));
  ACE_CString key = node + plan;
  TRecords* records = 0;
  if (0 != this->transactions_.find (key, records))
    {
      records = new TRecords();
      this->transactions_.rebind (key, records);
    }
  else
    {
      records->clear();
    }
}

void
NameServiceRedirection::bind (const ACE_CString& node, const ACE_CString& plan, const ACE_CString& component, const ACE_CString& port, CORBA::Object_ptr obj)
{
  DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::bind for node %C plan %C component %C and port %C is started.\n"),
                 node.c_str(),
                 plan.c_str(),
                 component.c_str(),
                 port.c_str()));
  SRecord record;
  record.name.length (4);

  ACE_CString kind = "";
  CosNaming::NamingContext_var node_context = this->resolve_context (node, kind, this->naming_.in());
  record.name[0].id = CORBA::string_dup (node.c_str());
  record.name[0].kind = CORBA::string_dup (kind.c_str());

  kind = "DeploymentPlan";
  CosNaming::NamingContext_var plan_context = this->resolve_context (plan, kind, node_context.in());
  record.name[1].id = CORBA::string_dup (plan.c_str());
  record.name[1].kind = CORBA::string_dup (kind.c_str());

  kind = "";
  CosNaming::NamingContext_var component_context = this->resolve_context (component, kind, plan_context.in());
  record.name[2].id = CORBA::string_dup (component.c_str());
  record.name[2].kind = CORBA::string_dup (kind.c_str());

  record.name[3].id = CORBA::string_dup (port.c_str());
  record.name[3].kind = CORBA::string_dup ("Port");

  ACE_CString key = node + plan;
  TRecords* records = 0;
  if (0 != this->transactions_.find (key, records))
    {
      DANCE_ERROR ((LM_ERROR, DLINFO ACE_TEXT("NameServiceRedirection::bind - Can't find record %C for closing transaction.\n"), key.c_str()));
      return;
    }

  record.obj = CORBA::Object::_duplicate (obj);
  records->push_back (record);

  //    try{
  //        component_context->bind(name, obj);
  //    }
  //    catch(CosNaming::NamingContext::AlreadyBound&)
  //    {
  //        component_context->rebind(name, obj);
  //    }
  DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::bind has finished.\n")));
}

void
NameServiceRedirection::bind (const ACE_CString& node, const ACE_CString& plan, const ACE_CString& component, CORBA::Object_ptr obj)
{
  DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::bind for node %C plan %C and component %C is started.\n"),
                 node.c_str(),
                 plan.c_str(),
                 component.c_str()));
  SRecord record;
  record.name.length (3);

  ACE_CString kind = "";
  CosNaming::NamingContext_var node_context = this->resolve_context (node, kind, this->naming_.in());

  record.name[0].id = CORBA::string_dup (node.c_str());
  record.name[0].kind = CORBA::string_dup (kind.c_str());

  kind = "DeploymentPlan";
  CosNaming::NamingContext_var plan_context = this->resolve_context (plan, kind, node_context.in());

  record.name[1].id = CORBA::string_dup (plan.c_str());
  record.name[1].kind = CORBA::string_dup (kind.c_str());

  ACE_CString key = node + plan;
  TRecords* records = 0;
  if (0 != this->transactions_.find (key, records))
    {
      DANCE_ERROR ((LM_ERROR, DLINFO ACE_TEXT("NameServiceRedirection::bind - Can't find record %C for closing transaction.\n"), key.c_str()));
      return;
    }

  record.name[2].id = CORBA::string_dup (component.c_str());
  record.name[2].kind = CORBA::string_dup ("Component");

  record.obj = CORBA::Object::_duplicate (obj);

  records->push_back (record);
  //    try{
  //        plan_context->bind(name, obj);
  //    }
  //    catch(CosNaming::NamingContext::AlreadyBound&)
  //    {
  //        DANCE_DEBUG((LM_DEBUG, "[%M] NameServiceRedirection::bind - Already bound exception was thrown. Rebinding\n"));
  //        plan_context->rebind(name, obj);
  //    }
  DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::bind has finished.\n")));
}

void
NameServiceRedirection::finish_binding (const ACE_CString& node, const ACE_CString& plan)
{
  ACE_CString key = node + plan;
  DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::finish_binding started for %s.\n"), key.c_str()));
  TRecords* records = 0;
  if (0 != this->transactions_.find (key, records))
    {
      DANCE_ERROR ((LM_ERROR, DLINFO ACE_TEXT("NameServiceRedirection::finish_binding - Can't find record %C for closing transaction.\n"), key.c_str()));
      return;
    }
  for (unsigned int i = 0; i < records->size(); i++)
    {
      //this->naming_->bind((*records)[i].name, (*records)[i].obj.in());
      try
        {
          this->naming_->bind ( (*records) [i].name, (*records) [i].obj.in());
        }
      catch (CosNaming::NamingContext::AlreadyBound&)
        {
          DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::finish_bind - Already bound exception was thrown. Rebinding\n")));
          this->naming_->rebind ( (*records) [i].name, (*records) [i].obj.in());
        }

    }
  if (!CORBA::is_nil (this->domain_.in()))
    {
      ACE_CString kind = "";
      CosNaming::NamingContext_var context = this->resolve_context (node, kind, this->naming_.in());
      CosNaming::Name name;
      name.length (1);
      name[0].id = CORBA::string_dup (node.c_str());
      name[0].kind = CORBA::string_dup (kind.c_str());
      try
        {
          this->domain_->bind_context (name, context.in());
        }
      catch (CosNaming::NamingContext::AlreadyBound&)
        {
          this->domain_->rebind_context (name, context.in());
        }
    }
  DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::finish_bind has finished.\n")));
}

void
NameServiceRedirection::unbind_context (const ACE_CString& node, const ACE_CString& plan)
{
  DANCE_DEBUG ( (LM_TRACE, ACE_TEXT("[%M] NameServiceRedirection::unbind_context started...\n")));
  ACE_CString kind = "";
  CosNaming::NamingContext_var node_context = this->resolve_context (node, kind, this->naming_.in());
  kind = "DeploymentPlan";
  CosNaming::NamingContext_var plan_context = this->resolve_context (plan, kind, node_context.in());
  DANCE_DEBUG ( (LM_TRACE, ACE_TEXT("[%M] NameServiceRedirection::unbind_context before clear_context for %s.%s\n"), plan.c_str(), kind.c_str()));
  this->clear_context (plan_context.inout());
  CosNaming::Name name (1);
  name.length (1);
  name[0].id = CORBA::string_dup (plan.c_str());
  name[0].kind = CORBA::string_dup (kind.c_str());
  DANCE_DEBUG ( (LM_TRACE, ACE_TEXT("[%M] NameServiceRedirection::unbind_context before unbinding %s.%s\n")
                 , name[0].id.in(), name[0].kind.in()));
  node_context->unbind (name);
  DANCE_DEBUG ( (LM_TRACE, ACE_TEXT("[%M] NameServiceRedirection::unbind_context before destroying plan context.\n")));
  plan_context->destroy();
  DANCE_DEBUG ( (LM_TRACE, ACE_TEXT("[%M] NameServiceRedirection::unbind_context fininshed.\n")));
}

CosNaming::NamingContext_ptr
NameServiceRedirection::resolve_context (const ACE_CString& context_name, const ACE_CString& context_kind, CosNaming::NamingContext_ptr naming)
{
  DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::resolve_context is started for context %C.%C\n"), context_name.c_str(), context_kind.c_str()));
  if (CORBA::is_nil (naming))
    {
      DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::resolve_context source context is nil!.\n")));
      return CosNaming::NamingContext::_nil ();
    }
  CORBA::Object_var obj;
  CosNaming::Name name (1);
  name.length (1);
  name[0].id = CORBA::string_dup (context_name.c_str());
  name[0].kind = CORBA::string_dup (context_kind.c_str());
  try
    {
      obj = naming->resolve (name);
    }
  catch (const CosNaming::NamingContext::NotFound&)
    {
      obj = naming->bind_new_context (name);
    }
  DANCE_DEBUG ((LM_DEBUG, DLINFO ACE_TEXT("NameServiceRedirection::resolve_context has finished.\n")));
  return CosNaming::NamingContext::_narrow (obj._retn());
}

void
NameServiceRedirection::clear_context (CosNaming::NamingContext_ptr& naming)
{
  CosNaming::BindingList_var bl;
  CosNaming::BindingIterator_var bi;
  naming->list (0, bl.out(), bi.out());

  if (CORBA::is_nil (bi.in()))
    {
      return;
    }
  while (bi->next_n (100, bl.out()))
    {
      for (unsigned int i = 0; i < bl->length(); i++)
        {
          if (bl[i].binding_type == CosNaming::ncontext)
            {
              CORBA::Object_var obj = naming->resolve ( (*bl) [i].binding_name);
              CosNaming::NamingContext_var sub_context = CosNaming::NamingContext::_narrow (obj);
              this->clear_context (sub_context.inout());
              naming->unbind ( (*bl) [i].binding_name);
              sub_context->destroy();
            }
          else
            {
              naming->unbind ( (*bl) [i].binding_name);
            }
        }
    }
}

void
NameServiceRedirection::add_node (const ACE_CString& node)
{
  CosNaming::NamingContext_var new_nc = this->resolve_context (node, "", this->naming_);
  if (!CORBA::is_nil (this->domain_.in()))
    {
      CosNaming::Name name;
      name.length (1);
      name[0].id = CORBA::string_dup (node.c_str());
      name[0].kind = CORBA::string_dup ("");
      this->domain_->bind_context (name, new_nc.in());
    }
}