summaryrefslogtreecommitdiff
path: root/CIAO/DAnCE/DomainApplicationManager/Node_Locator.cpp
blob: 12db00a8530d2c8d670dd1d6f9297d5303416a65 (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
// $Id$

#include "Node_Locator.h"

#include "ace/Read_Buffer.h"
#include "DAnCE/Logger/Log_Macros.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 ACE_TCHAR *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_node_map (const ACE_TCHAR *filename)
  {
    DANCE_TRACE ("Node_Locator::process_node_map");

    if (filename == 0)
      {
        DANCE_ERROR ((LM_ERROR, DLINFO "Node_Locator::process_node_map - "
                      "Error: Provided with nil filename\n"));
        return false;
      }

    FILE *inf = ACE_OS::fopen (filename, ACE_TEXT("r"));

    if (inf == 0)
      {
        DANCE_ERROR ((LM_ERROR, DLINFO "Node_Locator::process_node_map - "
                      "Fail to open node manager map data file: <%s>\n",
                      filename));
        return false;
      }

    ACE_Read_Buffer reader (inf, true);

    char* string = 0;
    // Read from the file line by line
    while ((string = reader.read ('\n')) != 0)
      {
        if (ACE_OS::strlen (string) == 0) continue;

        // Search from the right to the first space
        const char* ior_start = ACE_OS::strrchr (string, ' ');
        // Search from the left to the first space
        const char* dest_end = ACE_OS::strchr (string, ' ');

        // The destination is first followed by some spaces
        ACE_CString destination (string, dest_end - string);
        // And then the IOR
        ACE_CString ior (ior_start + 1,  ACE_OS::strlen (ior_start + 1));
        reader.alloc ()->free (string);

        DANCE_DEBUG ((LM_INFO, DLINFO "Node_Locator::process_node_map - "
                      "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 ACE_TCHAR *name, const ACE_TCHAR *ior)
  {
    DANCE_TRACE ("Node_Locator::resolve_ior");

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

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

    if (CORBA::is_nil (obj.in ()))
      {
        DANCE_ERROR ((LM_ERROR, DLINFO "Node_Locator::resolve_ior - "
                      "Error:  Unable to resolve object reference for destination "
                      "%s and ior %s\n",
                      name, ior));
        return false;
      }

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

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

    return nm._retn ();
  }

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

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

    if (CORBA::is_nil (this->nc_.in ()))
      {
        DANCE_ERROR ((LM_ERROR, DLINFO "Node_Locator::ns_lookup - "
                      "Nameservice lookup of %s 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 ((LM_ERROR, DLINFO "Node_Locator::ns_lookup - "
                          "Unable to narrow provided reference for node %s\n",
                          nodename));
            return ::Deployment::NodeManager::_nil ();
          }

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

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