summaryrefslogtreecommitdiff
path: root/modules/CIAO/DAnCE/TargetManager/Target_Manager_Module.cpp
blob: 63e5a51b6398670e674372135ac213b06ecd3c55 (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
// -*- C++ -*-
// $Id$

#include "Target_Manager_Module.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_stdio.h"
#include "ace/Task.h"
#include "ace/Atomic_Op.h"
#include "tao/IORTable/IORTable.h"
#include "tao/Utils/PolicyList_Destroyer.h"
#include "orbsvcs/CosNamingC.h"
#include "orbsvcs/orbsvcs/Naming/Naming_Loader.h"
#include "DAnCE/Logger/Log_Macros.h"
#include "DAnCE/Deployment/DAnCE_PropertiesC.h"

#include "TargetManager_Impl.h"

ACE_RCSID (DAnCE,
           Repository_Manager_Module,
           "$Id$")

namespace DAnCE
{
  namespace Target_Manager
  {
    bool
    write_IOR (const ACE_TCHAR * ior_file_name, const char* ior)
    {
      FILE* ior_output_file_ =
        ACE_OS::fopen (ior_file_name, ACE_TEXT("w"));

      if (ior_output_file_)
        {
          ACE_OS::fprintf (ior_output_file_,
                           "%s",
                           ior);
          ACE_OS::fclose (ior_output_file_);
          return true;
        }
      return false;
    }
  }
}

DAnCE_TargetManager_Module::DAnCE_TargetManager_Module (void)
{
  //DANCE_TRACE("DAnCE_TargetManager_Module::DAnCE_TargetManager_Module");
}

DAnCE_TargetManager_Module::~DAnCE_TargetManager_Module (void)
{
  DANCE_TRACE ("DAnCE_TargetManager_Module::~DAnCE_TargetManager_Module");

  for (Servant_Map::iterator it = this->rm_map_.begin ();
       it != this->rm_map_.end ();
       ++it)
    {
      delete (*it).int_id_;
    }
}

const char *
DAnCE_TargetManager_Module::usage (void)
{
  DANCE_TRACE ("DAnCE_TargetManager_Module::usage");
  return "Repository Manager Options:\n"
    //"\t-n|--name [name]\t Name to register in the naming service\n"
    "\t-f|--file [name]\t Filename to output IOR.\n"
    "\t-i|--init [name]\t Filename read initial domain descriptor.\n"
    "\t-d|--domain-nc [NC]\t Default naming context for domain objects.\n"
    "\t-h|help\t\t\t print this help message\n"
    ;

}

bool
DAnCE_TargetManager_Module::parse_args (int argc, ACE_TCHAR * argv[])
{
  DANCE_TRACE ("DAnCE_TargetManager_Module::parse_args");

  ACE_Get_Opt get_opts (argc - 1,
                        argv + 1,
                        ACE_TEXT(":hd:f:i:"),
                        0,
                        0,
                        ACE_Get_Opt::RETURN_IN_ORDER,
                        1);

  get_opts.long_option (ACE_TEXT("help"), 'h', ACE_Get_Opt::NO_ARG);
  get_opts.long_option (ACE_TEXT("domain-nc"), 'd', ACE_Get_Opt::ARG_REQUIRED);
  get_opts.long_option (ACE_TEXT("file"), 'f', ACE_Get_Opt::ARG_REQUIRED);
  get_opts.long_option (ACE_TEXT("init"), 'i', ACE_Get_Opt::ARG_REQUIRED);

  char c;
  while ( (c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'd':
          DANCE_DEBUG ((LM_DEBUG, DLINFO "Repository_Manager_Module::parse_args - "
                        "Binding to provided Domain Naming Context: '%s'\n",
                        get_opts.opt_arg ()));
          this->options_.domain_nc_ = get_opts.opt_arg ();
          break;

        case 'f':
          DANCE_DEBUG ((LM_DEBUG, DLINFO "Repository_Manager_Module::parse_args - "
                        "Output filename for IOR is %C\n",
                        get_opts.opt_arg ()));
          this->options_.ior_file_ = get_opts.opt_arg ();
          break;

        case 'i':
          DANCE_DEBUG ((LM_DEBUG, DLINFO "Repository_Manager_Module::parse_args - "
                        "Initial domain file %s\n",
                        get_opts.opt_arg ()));
          this->options_.domain_descr_ = get_opts.opt_arg ();
          break;

        case 'h':
        case '?': // Display help for use of the server.
          DANCE_ERROR_RETURN ( (LM_ERROR,
                                this->usage (),
                                argv [0], c),
                               false);
          break;

        case 0:
            {
              DANCE_ERROR ((LM_ERROR, DLINFO "Repository_Manager_Module::parse_args - "
                            "ERROR: unknown long option %C\n",
                            get_opts.long_option ()));
            }

          break;

        default:
          DANCE_DEBUG ((LM_TRACE, DLINFO "Repository_Manager_Module::parse_args - ignoring unknown option %c:%C\n",
                        c, get_opts.opt_arg ()));
        }

    }

   return true;
}

CORBA::Object_ptr
DAnCE_TargetManager_Module::create_object (CORBA::ORB_ptr orb,
                                               int argc,
                                               ACE_TCHAR *argv[])
{
  DANCE_TRACE ("DAnCE_TargetManager_Module::create_object");

  try
    {
      if (CORBA::is_nil(orb))
        {
          DANCE_ERROR((LM_ERROR, DLINFO "DAnCE_TargetManager_Module::create_object - "
                       "Attempted to create Target Manager with a nil orb.\n"));
          return CORBA::Object::_nil();
        }
      else
        {
          this->orb_ = CORBA::ORB::_duplicate (orb);
        }

      if (ACE_OS::strcmp(orb->id(), this->orb_->id()) != 0)
        {
          DANCE_DEBUG((LM_TRACE, DLINFO "DAnCE_TargetManager_Module::create_object - "
                       "Resetting TM's orb.\n"));
          this->orb_ = CORBA::ORB::_duplicate (orb);
          this->domain_nc_ = CosNaming::NamingContext::_nil();
        }

      if (!this->parse_args (argc, argv))
        {
          return CORBA::Object::_nil ();
        }

      this->create_poas ();

      if (this->options_.domain_nc_)
        {
          try
            {
              DANCE_DEBUG((LM_TRACE, DLINFO "DAnCE_TargetManager_Module::create_object - "
                           "Resolving DomainNC.\n"));
              CORBA::Object_var domain_obj = this->orb_->string_to_object (this->options_.domain_nc_);
              if (!CORBA::is_nil (domain_obj.in ()))
                {
                  this->domain_nc_ = CosNaming::NamingContext::_narrow (domain_obj.in());
                  if (CORBA::is_nil (this->domain_nc_.in ()))
                    {
                      DANCE_ERROR ((LM_ERROR,DLINFO "DAnCE_TargetManager_Module::create_object - "
                                    "Narrow to NamingContext return nil for DomainNC.\n"));
                      return CORBA::Object::_nil ();
                    }
                }
            }
          catch (CORBA::Exception&)
            {
              DANCE_DEBUG  ((LM_WARNING, DLINFO "DAnCE_TargetManager_Module::create_object - "
                             "DomainNC context not found!\n"));
            }
        }


      DANCE_DEBUG ((LM_TRACE, DLINFO "DAnCE_TargetManager_Module::create_object - "
                    "Initializing the IOR Table\n"));
      // Initialize IOR table
      CORBA::Object_var table_object = orb->resolve_initial_references ("IORTable");

      IORTable::Table_var adapter = IORTable::Table::_narrow (table_object.in ());

      if (CORBA::is_nil (adapter.in ()))
        {
          DANCE_ERROR ((LM_ERROR, DLINFO "DAnCE_TargetManager_Module::create_object - "
                         "Unable to RIR the IORTable.\n"));
          return CORBA::Object::_nil ();
        }


      //Creating repository manager servant
      DAnCE::TargetManagerDaemon_i * rm = new DAnCE::TargetManagerDaemon_i (orb);
                                                                            
      PortableServer::ServantBase_var safe_svt (rm);

      ACE_CString repository_manager_oid;

      if (this->options_.name_ == 0)
        repository_manager_oid = "TargetManager";
      else
        {
          repository_manager_oid = ACE_TEXT_ALWAYS_CHAR (this->options_.name_);
          repository_manager_oid += ".TargetManager";
        }

      // Registering servant in poa
      PortableServer::ObjectId_var oid =
        PortableServer::string_to_ObjectId (repository_manager_oid.c_str());
      this->rm_poa_->activate_object_with_id (oid, rm);

      // Getting repository manager ior
      CORBA::Object_var nm_obj = this->rm_poa_->id_to_reference (oid.in ());
      CORBA::String_var ior = orb->object_to_string (nm_obj.in ());

      // Binding ior to IOR Table
      adapter->bind (repository_manager_oid.c_str (), ior.in ());

      // Binding repository menager to DomainNC
      if (!CORBA::is_nil (this->domain_nc_.in ()))
        {
          ACE_CString ns_name;
          if (this->options_.name_ == 0)
            ns_name = "TargetManager";
          else ns_name = ACE_TEXT_ALWAYS_CHAR (this->options_.name_);

          DANCE_DEBUG((LM_TRACE, DLINFO "DAnCE_TargetManager_Module::create_object - "
                       "Registering NM in NC as \"%C\".\n", ns_name.c_str ()));
          CosNaming::Name name (1);
          name.length (1);
          name[0].id = CORBA::string_dup (ns_name.c_str ());
          name[0].kind = CORBA::string_dup ("TargetManager");
          this->domain_nc_->rebind (name, nm_obj.in ());
        }

      // Writing ior to file
      if (0 != this->options_.ior_file_)
        {
          DANCE_DEBUG ((LM_TRACE,  DLINFO "DAnCE_TargetManager_Module::create_object - "
                        "Writing RM IOR %C to file %C.\n", this->options_.ior_file_, ior.in ()));
          if (!DAnCE::Target_Manager::write_IOR (this->options_.ior_file_, ior.in ()))
            DANCE_ERROR ((LM_ERROR, DLINFO "DAnCE_TargetManager_Module::create_object - "
                          "Error: Unable to write IOR to file %C\n",
                          this->options_.ior_file_));
        }

      // Activate POA manager
      PortableServer::POAManager_var mgr = this->root_poa_->the_POAManager ();
      mgr->activate ();

      // Finishing Deployment part
      DANCE_DEBUG ((LM_NOTICE, DLINFO "DAnCE_TargetManager_Module::create_object - "
                    "DAnCE_TargetManager is running...\n"));

      DANCE_DEBUG ((LM_DEBUG, DLINFO "DAnCE_TargetManager_Module::create_object - "
                    "TargetManager IOR: %s\n", ior.in ()));

      return nm_obj._retn ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("DAnCE_TargetManager::main\t\n");
      return CORBA::Object::_nil ();
    }
}

void
DAnCE_TargetManager_Module::create_poas (void)
{
  DANCE_TRACE("DAnCE_Repository_Manager_Module::create_poas");
  // Get reference to Root POA.
  DANCE_DEBUG ((LM_TRACE, DLINFO "DAnCE_TargetManager_Module::create_poas - "
                "Resolving root POA\n"));
  CORBA::Object_var obj = this->orb_->resolve_initial_references ("RootPOA");

  this->root_poa_ = PortableServer::POA::_narrow (obj.in ());

  DANCE_DEBUG ((LM_TRACE, DLINFO "DAnCE_TargetManager_Module::create_poas - "
                "Obtaining the POAManager\n"));
  PortableServer::POAManager_var mgr = this->root_poa_->the_POAManager ();

  TAO::Utils::PolicyList_Destroyer policies (2);
  policies.length (2);

  try
    {
      DANCE_DEBUG ((LM_TRACE, DLINFO "DAnCE_TargetManager_Module::create_poas - "
                    "DAnCE_TargetManager_Module::create_poas - "
                    "Creating the \"Repository\" POA.\n"));

      policies[0] = this->root_poa_->create_id_assignment_policy (PortableServer::USER_ID);
      policies[1] = this->root_poa_->create_lifespan_policy (PortableServer::PERSISTENT);
      this->rm_poa_ = this->root_poa_->create_POA ("Repository",
                                       mgr.in(),
                                       policies);
    }
  catch (const PortableServer::POA::AdapterAlreadyExists &)
    {
      DANCE_DEBUG ((LM_INFO, DLINFO "DAnCE_TargetManager_Module::create_poas - "
                    "Using existing \"Repository\" POA\n"));
      this->rm_poa_ = this->root_poa_->find_POA ("Repository", 0);
    }
}

#ifndef DANCE_TARGETMANAGER_IMPL_BUILD_DLL
ACE_FACTORY_DEFINE (DAnCE_TargetManager_Module, DAnCE_TargetManager_Module)
#endif /* DANCE_TARGETMANAGER_IMPL_BUILD_DLL */