summaryrefslogtreecommitdiff
path: root/DAnCE/dance/DomainApplicationManager/Node_Locator.cpp
blob: 85e0f9f5f67067156b03e9cb130f8e7015c859de (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
#include "Node_Locator.h"
#include "ace/Read_Buffer.h"
#include "dance/Logger/Log_Macros.h"
#include "Config_Handlers/XML_File_Intf.h"
#include "Config_Handlers/Common.h"
#include "dance/DAnCE_Utility.h"
#include "dance/DAnCE_PropertiesC.h"


namespace DAnCE
{
  Node_Locator::Node_Locator (CORBA::ORB_ptr orb,
                              CosNaming::NamingContext_ptr nc)
    : orb_ (CORBA::ORB::_duplicate (orb)),
      nc_ (CosNaming::NamingContext::_duplicate (nc))
  {
  }

  ::Deployment::NodeManager_ptr
  Node_Locator::locate_node (const char *name)
  {
    ACE_CString ior;

    if (this->nodes_.find (name, ior) == 0)
      {
        return this->resolve_ior (name, ior.c_str ());
      }
    else
      {
        return this->ns_lookup (name);
      }
  }

  bool
  Node_Locator::process_cdd (const ACE_TCHAR *filename)
    {
      DANCE_TRACE ("Node_Locator::process_cdd");

      if (!filename)
        {
          DANCE_ERROR (DANCE_LOG_ERROR,
                       (LM_ERROR, DLINFO
                        ACE_TEXT("Node_Locator::process_cdd - ")
                        ACE_TEXT("Error: Provided with nil filename\n")));
          return false;
        }

      ::DAnCE::Config_Handlers::XML_File_Intf file (filename);
      file.add_search_path (ACE_TEXT ("DANCE_ROOT"), ACE_TEXT ("/docs/schema/"));
      ::Deployment::Domain *plan = file.release_domain ();

      if (!plan)
        {
          DANCE_ERROR (DANCE_LOG_ERROR,
                       (LM_ERROR, DLINFO
                        ACE_TEXT("Node_Locator::process_cdd - ")
                        ACE_TEXT("Error: Processing file <%C>\n"), filename));
          return false;
        }

      // install nodes
      for (CORBA::ULong i=0; i < plan->node.length (); ++i)
        {
          ::Deployment::Resource resource;

           if (!get_resource_value (DAnCE::NODE_RESOURCE_TYPE,
                                    plan->node[i].resource,
                                    resource))
             {
               DANCE_ERROR (DANCE_LOG_ERROR,
                            (LM_ERROR,
                             DLINFO ACE_TEXT("Node_Locator::process_cdd - ")
                             ACE_TEXT("Error: Resource <%C> not found.\n"),
                             DAnCE::NODE_RESOURCE_TYPE));
               return false;
             }
          const ACE_TCHAR *val = 0;
          if (!::DAnCE::Utility::get_satisfierproperty_value (DAnCE::NODE_IOR,
                                                              resource.property,
                                                              val))
            {
              DANCE_ERROR (DANCE_LOG_ERROR,
                           (LM_ERROR,
                            DLINFO ACE_TEXT("Node_Locator::process_cdd - ")
                            ACE_TEXT("Error: Property <%C> not found.\n"),
                            DAnCE::NODE_IOR));
              return false;

            }
          ACE_CString ior(val);
          ior += "/";
          ior += plan->node[i].name;
          ior += ".NodeManager";

          ACE_CString destination (plan->node[i].name);

          DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                      (LM_INFO, DLINFO ACE_TEXT("Node_Locator::process_cdd - ")
                       ACE_TEXT("Storing IOR %C for destination %C\n"),
                       ior.c_str (), destination.c_str ()));

          this->nodes_.bind (destination, ior);
      }

    return true;
  }

  ::Deployment::NodeManager_ptr
  Node_Locator::resolve_ior (const char *name, const char *ior)
  {
    DANCE_TRACE ("Node_Locator::resolve_ior");

    DANCE_DEBUG (DANCE_LOG_MINOR_EVENT,
                 (LM_DEBUG, DLINFO
                  ACE_TEXT("Node_Locator::resolve_ior - ")
                  ACE_TEXT("Resolving ior %C for destination %C\n"),
                  ior, name));

    CORBA::Object_var obj = this->orb_->string_to_object (ior);

    ::Deployment::NodeManager_var nm = ::Deployment::NodeManager::_narrow (obj.in ());

    if (CORBA::is_nil (nm.in ()))
      {
        DANCE_ERROR (DANCE_LOG_ERROR,
                     (LM_ERROR, DLINFO ACE_TEXT("Node_Locator::resolve_ior - ")
                      ACE_TEXT("Error: Unable to retrieve reference for destination ")
                      ACE_TEXT("%C and ior %C\n"),
                      name, ior));
      }

    return nm._retn ();
  }

  void
  Node_Locator::store_ior (const char *name, const char *ior)
  {
    DANCE_TRACE ("Node_Locator::store_ior");
    this->nodes_.bind (name, ior);
  }

  ::Deployment::NodeManager_ptr
  Node_Locator::ns_lookup (const char *nodename)
  {
    DANCE_TRACE ("Node_Locator::ns_lookup");

    if (CORBA::is_nil (this->nc_.in ()))
      {
        DANCE_ERROR (DANCE_LOG_ERROR,
                     (LM_ERROR, DLINFO ACE_TEXT("Node_Locator::ns_lookup - ")
                      ACE_TEXT("Nameservice lookup of %C failed because there is no naming service.\n"),
                      nodename));
        return ::Deployment::NodeManager::_nil ();
      }

    try
      {
        CosNaming::Name name;
        name.length (1);

        name[0].id = nodename;
        name[0].kind = "NodeManager";

        CORBA::Object_var obj = this->nc_->resolve (name);

        ::Deployment::NodeManager_var nm = ::Deployment::NodeManager::_narrow (obj.in ());

        if (CORBA::is_nil (nm.in ()))
          {
            DANCE_ERROR (DANCE_LOG_ERROR,
                         (LM_ERROR, DLINFO ACE_TEXT("Node_Locator::ns_lookup - ")
                          ACE_TEXT("Unable to narrow provided reference for node %C\n"),
                          nodename));
          }

        return nm._retn ();
      }
    catch (const CORBA::Exception &e)
      {
        DANCE_ERROR (DANCE_LOG_ERROR,
                     (LM_ERROR, DLINFO ACE_TEXT("Node_Locator::ns_lookup - ")
                      ACE_TEXT("Caught CORBA exception while looking up name %C:%C\n"),
                      nodename, e._info ().c_str ()));
      }
    catch (...)
      {
        DANCE_ERROR (DANCE_LOG_ERROR,
                     (LM_ERROR, DLINFO ACE_TEXT("Node_Locator::ns_lookup - ")
                      ACE_TEXT("Caught unexpected exception while looking up name %C\n"),
                      nodename));
      }

    return ::Deployment::NodeManager::_nil ();
  }

  bool
  Node_Locator::get_resource_value (const char *type,
                                      const ::Deployment::Resources &resources,
                                     ::Deployment::Resource  &val)
  {
    DANCE_TRACE ("Node_Locator::get_resource_value<const char *>");

    DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                 (LM_TRACE, DLINFO
                  ACE_TEXT("Node_Locator::get_resource_value - ")
                  ACE_TEXT("Finding resource for type '%C'\n"),
                  type));

    for (CORBA::ULong i = 0; i < resources.length (); ++i)
      {
        // search for the resource with resourceType
        for (CORBA::ULong k = 0;k < resources[i].resourceType.length ();k++)
          {
            if (ACE_OS::strcmp (type,
                                 resources[i].resourceType[k]) == 0)
              {
                DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                             (LM_TRACE, DLINFO
                              ACE_TEXT("Node_Locator::get_resource_value - ")
                              ACE_TEXT("Found resource for type '%C'\n"),
                              type));

                val = resources[i];
                return true;
              }
          }
        }
      DANCE_ERROR (DANCE_LOG_ERROR,
                   (LM_WARNING, DLINFO
                    ACE_TEXT("Node_Locator::get_resource_value - ")
                    ACE_TEXT("Failed to extract resource for %C\n"),
                    type));
      return false;
    }

}