00001
00002
00003 #include "orbsvcs/Naming/Naming_Server.h"
00004 #include "orbsvcs/Naming/Transient_Naming_Context.h"
00005 #include "orbsvcs/Naming/Persistent_Context_Index.h"
00006 #include "orbsvcs/Naming/Storable_Naming_Context.h"
00007 #include "orbsvcs/Naming/Storable_Naming_Context_Activator.h"
00008 #include "orbsvcs/Naming/Flat_File_Persistence.h"
00009 #include "orbsvcs/CosNamingC.h"
00010
00011 #include "tao/debug.h"
00012 #include "tao/default_ports.h"
00013 #include "tao/ORB_Core.h"
00014
00015 #include "tao/IORTable/IORTable.h"
00016
00017 #if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0
00018 #include "tao/Messaging/Messaging.h"
00019 #endif
00020
00021 #include "tao/AnyTypeCode/Any.h"
00022
00023 #include "ace/Arg_Shifter.h"
00024 #include "ace/Auto_Ptr.h"
00025 #include "ace/Get_Opt.h"
00026 #include "ace/OS_NS_unistd.h"
00027
00028 ACE_RCSID (Naming,
00029 Naming_Server,
00030 "$Id: Naming_Server.cpp 79178 2007-08-02 14:16:18Z vzykov $")
00031
00032 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00033
00034 TAO_Naming_Server::TAO_Naming_Server (void)
00035 : naming_context_ (),
00036 ior_multicast_ (0),
00037 naming_service_ior_ (),
00038 context_index_ (0),
00039 ior_file_name_ (0),
00040 pid_file_name_ (0),
00041 context_size_ (ACE_DEFAULT_MAP_SIZE),
00042 persistence_file_name_ (0),
00043 base_address_ (TAO_NAMING_BASE_ADDR),
00044 multicast_ (0),
00045 use_storable_context_ (0),
00046 use_servant_activator_ (false),
00047 servant_activator_ (0),
00048 use_redundancy_(0),
00049 round_trip_timeout_ (0),
00050 use_round_trip_timeout_ (0)
00051 {
00052 }
00053
00054 TAO_Naming_Server::TAO_Naming_Server (CORBA::ORB_ptr orb,
00055 PortableServer::POA_ptr poa,
00056 size_t context_size,
00057 ACE_Time_Value *timeout,
00058 int resolve_for_existing_naming_service,
00059 const ACE_TCHAR *persistence_location,
00060 void *base_addr,
00061 int enable_multicast,
00062 int use_storable_context,
00063 int round_trip_timeout,
00064 int use_round_trip_timeout)
00065 : naming_context_ (),
00066 ior_multicast_ (0),
00067 naming_service_ior_ (),
00068 context_index_ (0),
00069 ior_file_name_ (0),
00070 pid_file_name_ (0),
00071 context_size_ (ACE_DEFAULT_MAP_SIZE),
00072 persistence_file_name_ (0),
00073 base_address_ (TAO_NAMING_BASE_ADDR),
00074 multicast_ (0),
00075 use_storable_context_ (use_storable_context),
00076 use_servant_activator_ (false),
00077 servant_activator_ (0),
00078 use_redundancy_(0),
00079 round_trip_timeout_ (0),
00080 use_round_trip_timeout_ (0)
00081 {
00082 if (this->init (orb,
00083 poa,
00084 context_size,
00085 timeout,
00086 resolve_for_existing_naming_service,
00087 persistence_location,
00088 base_addr,
00089 enable_multicast,
00090 use_storable_context,
00091 round_trip_timeout,
00092 use_round_trip_timeout) == -1)
00093 ACE_ERROR ((LM_ERROR,
00094 "(%P|%t) %p\n",
00095 "TAO_Naming_Server::init"));
00096 }
00097
00098
00099 int
00100 TAO_Naming_Server::init (CORBA::ORB_ptr orb,
00101 PortableServer::POA_ptr poa,
00102 size_t context_size,
00103 ACE_Time_Value *timeout,
00104 int resolve_for_existing_naming_service,
00105 const ACE_TCHAR *persistence_location,
00106 void *base_addr,
00107 int enable_multicast,
00108 int use_storable_context,
00109 int round_trip_timeout,
00110 int use_round_trip_timeout)
00111 {
00112 if (resolve_for_existing_naming_service)
00113 {
00114 try
00115 {
00116
00117 CORBA::Object_var naming_obj =
00118 orb->resolve_initial_references ("NameService", timeout);
00119
00120 if (!CORBA::is_nil (naming_obj.in ()))
00121 {
00122
00123
00124
00125 if (TAO_debug_level > 0)
00126 ACE_DEBUG ((LM_DEBUG,
00127 "\nNameService found!\n"));
00128
00129 this->naming_context_ =
00130 CosNaming::NamingContext::_narrow (naming_obj.in ());
00131
00132 this->naming_service_ior_ =
00133 orb->object_to_string (naming_obj.in ());
00134
00135 return 0;
00136 }
00137 }
00138 catch (const CORBA::Exception& ex)
00139 {
00140 ex._tao_print_exception ("TAO_Naming_Server::init");
00141 }
00142 }
00143
00144 if (TAO_debug_level > 0)
00145 ACE_DEBUG ((LM_DEBUG,
00146 "\nWe'll become a NameService\n"));
00147
00148
00149 return this->init_new_naming (orb,
00150 poa,
00151 persistence_location,
00152 base_addr,
00153 context_size,
00154 enable_multicast,
00155 use_storable_context,
00156 round_trip_timeout,
00157 use_round_trip_timeout);
00158 }
00159
00160 int
00161 TAO_Naming_Server::parse_args (int argc,
00162 ACE_TCHAR *argv[])
00163 {
00164 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT)
00165 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("b:do:p:s:f:m:u:r:z:"));
00166 #else
00167 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("b:do:p:s:f:m:z:"));
00168 #endif
00169
00170 int c;
00171 int size, result;
00172
00173
00174
00175
00176 #if ACE_SIZEOF_VOID_P == ACE_SIZEOF_LONG_LONG
00177 ptrdiff_t address;
00178 #else
00179 long int address;
00180 #endif
00181
00182
00183 int f_opt_used = 0;
00184 int u_opt_used = 0;
00185 int r_opt_used = 0;
00186
00187 while ((c = get_opts ()) != -1)
00188 switch (c)
00189 {
00190 case 'd':
00191 TAO_debug_level++;
00192 break;
00193 case 'o':
00194 this->ior_file_name_ = get_opts.opt_arg ();
00195 break;
00196 case 'p':
00197 this->pid_file_name_ = get_opts.opt_arg ();
00198 break;
00199 case 's':
00200 size = ACE_OS::atoi (get_opts.opt_arg ());
00201 if (size >= 0)
00202 this->context_size_ = size;
00203 break;
00204 case 'b':
00205 result = ::sscanf (ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ()),
00206 #if ACE_SIZEOF_VOID_P == ACE_SIZEOF_LONG_LONG
00207 ACE_INT64_FORMAT_SPECIFIER,
00208 #else
00209 "%ld",
00210 #endif
00211 &address);
00212 if (result == 0 || result == EOF)
00213 ACE_ERROR_RETURN ((LM_ERROR,
00214 "Unable to process <-b> option"),
00215 -1);
00216 this->base_address_ = (void *) address;
00217 break;
00218 case 'm':
00219 this->multicast_ = ACE_OS::atoi(get_opts.opt_arg ());
00220 break;
00221 case 'f':
00222 this->persistence_file_name_ = get_opts.opt_arg ();
00223 f_opt_used = 1;
00224 break;
00225 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT)
00226 case 'r':
00227 this->use_redundancy_ = 1;
00228 this->use_storable_context_ = 1;
00229 this->persistence_file_name_ = get_opts.opt_arg ();
00230 r_opt_used = 1;
00231 break;
00232 case 'u':
00233 this->use_storable_context_ = 1;
00234 this->persistence_file_name_ = get_opts.opt_arg ();
00235 u_opt_used = 1;
00236 break;
00237 #endif
00238 case 'z':
00239 this->use_round_trip_timeout_ = 1;
00240 this->round_trip_timeout_ = (int)1.0e7 * ACE_OS::atoi (get_opts.opt_arg ());
00241 break;
00242 case '?':
00243 default:
00244 const ACE_TCHAR *reqNonMinCorba=
00245 #if (TAO_HAS_MINIMUM_POA == 0)
00246 ACE_TEXT (" -u <storable_persistence_directory (not used with -f)> ")
00247 ACE_TEXT (" -r <redundant_persistence_directory> ");
00248 #else
00249 ACE_TEXT ("");
00250 #endif
00251 ACE_ERROR_RETURN ((LM_ERROR,
00252 ACE_TEXT ("usage: %s ")
00253 ACE_TEXT ("-d ")
00254 ACE_TEXT ("-o <ior_output_file> ")
00255 ACE_TEXT ("-p <pid_file_name> ")
00256 ACE_TEXT ("-s <context_size> ")
00257 ACE_TEXT ("-b <base_address> ")
00258 ACE_TEXT ("-m <1=enable multicast, 0=disable multicast(default) ")
00259 ACE_TEXT ("-f <persistence_file_name> %s")
00260 ACE_TEXT ("-z <relative round trip timeout> ")
00261 ACE_TEXT ("\n"),
00262 argv [0], reqNonMinCorba),
00263 -1);
00264 }
00265
00266 if (f_opt_used + u_opt_used + r_opt_used > 1)
00267 ACE_ERROR_RETURN ((LM_ERROR,
00268 ACE_TEXT ("Only one persistence option can be passed")
00269 ACE_TEXT ("\n")),
00270 -1);
00271
00272 return 0;
00273 }
00274
00275 int
00276 TAO_Naming_Server::init_with_orb (int argc,
00277 ACE_TCHAR *argv [],
00278 CORBA::ORB_ptr orb)
00279 {
00280 int result;
00281
00282 try
00283 {
00284
00285 this->orb_ = CORBA::ORB::_duplicate (orb);
00286
00287
00288 CORBA::Object_var poa_object =
00289 orb->resolve_initial_references ("RootPOA");
00290
00291 if (CORBA::is_nil (poa_object.in ()))
00292 {
00293 ACE_ERROR_RETURN ((LM_ERROR,
00294 ACE_TEXT(" (%P|%t) Unable to initialize the POA.\n")),
00295 -1);
00296 }
00297
00298
00299
00300
00301 result = this->parse_args (argc, argv);
00302
00303 if (result < 0)
00304 return result;
00305
00306
00307 this->root_poa_ = PortableServer::POA::_narrow (poa_object.in ());
00308
00309
00310 PortableServer::POAManager_var poa_manager =
00311 this->root_poa_->the_POAManager ();
00312
00313 poa_manager->activate ();
00314
00315 int numPolicies = 2;
00316 #if (TAO_HAS_MINIMUM_POA == 0)
00317 if (this->use_storable_context_)
00318 {
00319 this->use_servant_activator_ = true;
00320 }
00321
00322 if (this->use_servant_activator_) {
00323 numPolicies += 2;
00324 }
00325 #endif
00326
00327 CORBA::PolicyList policies (numPolicies);
00328 policies.length (numPolicies);
00329
00330
00331 policies[0] =
00332 this->root_poa_->create_id_assignment_policy (PortableServer::USER_ID);
00333
00334
00335 policies[1] =
00336 this->root_poa_->create_lifespan_policy (PortableServer::PERSISTENT);
00337
00338 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT)
00339 if (this->use_servant_activator_)
00340 {
00341
00342 policies[2] =
00343 this->root_poa_->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER);
00344
00345
00346 policies[3] =
00347 this->root_poa_->create_servant_retention_policy (PortableServer::RETAIN);
00348 }
00349 #endif
00350
00351
00352
00353 this->ns_poa_ = this->root_poa_->create_POA ("NameService",
00354 poa_manager.in (),
00355 policies);
00356
00357
00358
00359
00360 for (CORBA::ULong i = 0;
00361 i < policies.length ();
00362 ++i)
00363 {
00364 CORBA::Policy_ptr policy = policies[i];
00365 policy->destroy ();
00366 }
00367
00368 result = this->init (orb,
00369 this->ns_poa_.in (),
00370 this->context_size_,
00371 0,
00372 0,
00373 this->persistence_file_name_,
00374 this->base_address_,
00375 this->multicast_,
00376 this->use_storable_context_,
00377 this->round_trip_timeout_,
00378 this->use_round_trip_timeout_);
00379 if (result == -1)
00380 return result;
00381 }
00382 catch (const CORBA::Exception& ex)
00383 {
00384 ex._tao_print_exception (
00385 "TAO_Naming_Server::init_with_orb");
00386 return -1;
00387 }
00388
00389 if (this->ior_file_name_ != 0)
00390 {
00391 FILE *iorf = ACE_OS::fopen (this->ior_file_name_, ACE_TEXT("w"));
00392 if (iorf == 0)
00393 {
00394 ACE_ERROR_RETURN ((LM_ERROR,
00395 ACE_TEXT("Unable to open %s for writing:(%u) %p\n"),
00396 this->ior_file_name_,
00397 errno,
00398 ACE_TEXT("TAO_Naming_Server::init_with_orb")),
00399 -1);
00400 }
00401
00402 CORBA::String_var str = this->naming_service_ior ();
00403
00404 ACE_OS::fprintf (iorf, "%s\n", str.in ());
00405 ACE_OS::fclose (iorf);
00406 }
00407
00408 if (this->pid_file_name_ != 0)
00409 {
00410 FILE *pidf = ACE_OS::fopen (this->pid_file_name_, ACE_TEXT("w"));
00411 if (pidf != 0)
00412 {
00413 ACE_OS::fprintf (pidf,
00414 "%ld\n",
00415 static_cast<long> (ACE_OS::getpid ()));
00416 ACE_OS::fclose (pidf);
00417 }
00418 }
00419
00420 return 0;
00421 }
00422
00423 int
00424 TAO_Naming_Server::init_new_naming (CORBA::ORB_ptr orb,
00425 PortableServer::POA_ptr poa,
00426 const ACE_TCHAR *persistence_location,
00427 void *base_addr,
00428 size_t context_size,
00429 int enable_multicast,
00430 int use_storable_context,
00431 int round_trip_timeout,
00432 int use_round_trip_timeout)
00433 {
00434 try
00435 {
00436 if (use_storable_context)
00437 {
00438
00439
00440
00441 TAO_Naming_Service_Persistence_Factory* pf = 0;
00442 ACE_NEW_RETURN(pf, TAO_NS_FlatFileFactory, -1);
00443 auto_ptr<TAO_Naming_Service_Persistence_Factory> persFactory(pf);
00444
00445
00446
00447
00448
00449 if (persistence_location == 0)
00450 {
00451
00452 persistence_location = ACE_TEXT("NameService");
00453 }
00454
00455
00456 if (ACE_OS::access (persistence_location, W_OK|X_OK))
00457 {
00458 ACE_ERROR ((LM_ERROR, "Invalid persistence directory\n"));
00459 return -1;
00460 }
00461
00462 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT)
00463 if (this->use_servant_activator_)
00464 {
00465 ACE_NEW_THROW_EX (this->servant_activator_,
00466 TAO_Storable_Naming_Context_Activator (orb,
00467 persFactory.get(),
00468 persistence_location,
00469 context_size),
00470 CORBA::NO_MEMORY ());
00471 this->ns_poa_->set_servant_manager(this->servant_activator_);
00472 }
00473 #endif
00474
00475 this->naming_context_ =
00476 TAO_Storable_Naming_Context::recreate_all (orb,
00477 poa,
00478 TAO_ROOT_NAMING_CONTEXT,
00479 context_size,
00480 0,
00481 persFactory.get(),
00482 persistence_location,
00483 use_redundancy_);
00484
00485 if (this->use_servant_activator_)
00486 persFactory.release();
00487 }
00488 else if (persistence_location != 0)
00489
00490
00491
00492 {
00493
00494 ACE_NEW_RETURN (this->context_index_,
00495 TAO_Persistent_Context_Index (orb, poa),
00496 -1);
00497
00498 if (this->context_index_->open (persistence_location,
00499 base_addr) == -1
00500 || this->context_index_->init (context_size) == -1)
00501 {
00502 if (TAO_debug_level >0)
00503 ACE_DEBUG ((LM_DEBUG,
00504 "TAO_Naming_Server: context_index initialization failed\n"));
00505 return -1;
00506 }
00507
00508
00509 this->naming_context_ =
00510 this->context_index_->root_context ();
00511 }
00512 else
00513 {
00514
00515
00516
00517 this->naming_context_ =
00518 TAO_Transient_Naming_Context::make_new_context (poa,
00519 TAO_ROOT_NAMING_CONTEXT,
00520 context_size);
00521
00522 }
00523
00524
00525
00526
00527 orb->register_initial_reference ("NameService",
00528 this->naming_context_.in ());
00529
00530
00531 this->naming_service_ior_=
00532 orb->object_to_string (this->naming_context_.in ());
00533
00534 CORBA::Object_var table_object =
00535 orb->resolve_initial_references ("IORTable");
00536
00537 IORTable::Table_var adapter =
00538 IORTable::Table::_narrow (table_object.in ());
00539 if (CORBA::is_nil (adapter.in ()))
00540 {
00541 ACE_ERROR ((LM_ERROR, "Nil IORTable\n"));
00542 }
00543 else
00544 {
00545 CORBA::String_var ior =
00546 orb->object_to_string (this->naming_context_.in ());
00547 adapter->bind ("NameService", ior.in ());
00548 }
00549
00550 #if defined (ACE_HAS_IP_MULTICAST)
00551 if (enable_multicast)
00552 {
00553
00554
00555
00556
00557
00558
00559
00560
00561 ACE_Reactor *reactor = orb->orb_core()->reactor ();
00562
00563
00564 ACE_CString mde (orb->orb_core ()->orb_params ()->mcast_discovery_endpoint ());
00565
00566
00567
00568 u_short port =
00569 orb->orb_core ()->orb_params ()->service_port (TAO::MCAST_NAMESERVICE);
00570
00571 if (port == 0)
00572 {
00573
00574 const char *port_number =
00575 ACE_OS::getenv ("NameServicePort");
00576
00577 if (port_number != 0)
00578 port = static_cast<u_short> (ACE_OS::atoi (port_number));
00579 }
00580
00581
00582
00583 if (port == 0)
00584 port = TAO_DEFAULT_NAME_SERVER_REQUEST_PORT;
00585
00586
00587
00588 ACE_NEW_RETURN (this->ior_multicast_,
00589 TAO_IOR_Multicast (),
00590 -1);
00591
00592 if (mde.length () != 0)
00593 {
00594 if (this->ior_multicast_->init (this->naming_service_ior_.in (),
00595 mde.c_str (),
00596 TAO_SERVICEID_NAMESERVICE) == -1)
00597 return -1;
00598 }
00599 else
00600 {
00601 if (this->ior_multicast_->init (this->naming_service_ior_.in (),
00602 port,
00603 #if defined (ACE_HAS_IPV6)
00604 ACE_DEFAULT_MULTICASTV6_ADDR,
00605 #else
00606 ACE_DEFAULT_MULTICAST_ADDR,
00607 #endif
00608 TAO_SERVICEID_NAMESERVICE) == -1)
00609 return -1;
00610 }
00611
00612
00613 if (reactor->register_handler (this->ior_multicast_,
00614 ACE_Event_Handler::READ_MASK) == -1)
00615 {
00616 if (TAO_debug_level > 0)
00617 ACE_DEBUG ((LM_DEBUG,
00618 "TAO_Naming_Server: cannot register Event handler\n"));
00619 return -1;
00620 }
00621
00622 if (TAO_debug_level > 0)
00623 ACE_DEBUG ((LM_DEBUG,
00624 "TAO_Naming_Server: The multicast server setup is done.\n"));
00625 }
00626 #else
00627 ACE_UNUSED_ARG (enable_multicast);
00628 #endif
00629
00630 #if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0
00631 if (use_round_trip_timeout == 1)
00632 {
00633 TimeBase::TimeT roundTripTimeoutVal = round_trip_timeout;
00634 CORBA::Any anyObjectVal;
00635 anyObjectVal <<= roundTripTimeoutVal;
00636 CORBA::PolicyList polList (1);
00637 polList.length (1);
00638 polList[0] = orb->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
00639 anyObjectVal);
00640
00641
00642
00643 CORBA::Object_var orbPolicyManagerObj =
00644 orb->resolve_initial_references ("ORBPolicyManager");
00645
00646 CORBA::PolicyManager_var orbPolicyManager =
00647 CORBA::PolicyManager::_narrow (orbPolicyManagerObj.in ());
00648 orbPolicyManager->set_policy_overrides (polList, CORBA::SET_OVERRIDE);
00649
00650 polList[0]->destroy ();
00651 polList[0] = CORBA::Policy::_nil ();
00652 }
00653 #else
00654 ACE_UNUSED_ARG (use_round_trip_timeout);
00655 ACE_UNUSED_ARG (round_trip_timeout);
00656 #endif
00657 }
00658 catch (const CORBA::Exception& ex)
00659 {
00660 ex._tao_print_exception (
00661 "TAO_Naming_Server::init_new_naming");
00662 return -1;
00663 }
00664
00665 return 0;
00666 }
00667
00668 int
00669 TAO_Naming_Server::fini (void)
00670 {
00671
00672
00673 try
00674 {
00675 this->ns_poa_->destroy (1, 1);
00676
00677 CORBA::Object_var table_object =
00678 this->orb_->resolve_initial_references ("IORTable");
00679
00680 IORTable::Table_var adapter =
00681 IORTable::Table::_narrow (table_object.in ());
00682 if (CORBA::is_nil (adapter.in ()))
00683 {
00684 ACE_ERROR ((LM_ERROR, "Nil IORTable\n"));
00685 }
00686 else
00687 {
00688 adapter->unbind ("NameService");
00689 }
00690 }
00691 catch (const CORBA::Exception&)
00692 {
00693
00694 }
00695
00696 if (this->ior_multicast_ != 0)
00697 {
00698 orb_->orb_core()->reactor ()->remove_handler (this->ior_multicast_,
00699 ACE_Event_Handler::READ_MASK | ACE_Event_Handler::DONT_CALL);
00700 delete this->ior_multicast_;
00701 }
00702
00703 delete this->context_index_;
00704
00705 return 0;
00706 }
00707
00708 char*
00709 TAO_Naming_Server::naming_service_ior (void)
00710 {
00711 return CORBA::string_dup (this->naming_service_ior_.in ());
00712 }
00713
00714 CosNaming::NamingContext_ptr
00715 TAO_Naming_Server::operator-> (void) const
00716 {
00717 return this->naming_context_.ptr ();
00718 }
00719
00720 TAO_Naming_Server::~TAO_Naming_Server (void)
00721 {
00722 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT)
00723 if (this->use_servant_activator_)
00724 delete this->servant_activator_;
00725 #endif
00726 }
00727
00728 TAO_END_VERSIONED_NAMESPACE_DECL