00001
00002
00003 #include "ace/Get_Opt.h"
00004 #include "ace/Naming_Context.h"
00005 #include "ace/Remote_Name_Space.h"
00006 #include "ace/Local_Name_Space_T.h"
00007 #include "ace/Registry_Name_Space.h"
00008 #include "ace/MMAP_Memory_Pool.h"
00009 #include "ace/RW_Process_Mutex.h"
00010 #include "ace/OS_NS_string.h"
00011 #include "ace/OS_NS_unistd.h"
00012 #if defined (ACE_HAS_TRACE)
00013 # include "ace/OS_NS_strings.h"
00014 # include "ace/Trace.h"
00015 #endif
00016
00017 ACE_RCSID(ace, Naming_Context, "$Id: Naming_Context.cpp 81286 2008-04-09 07:27:30Z johnnyw $")
00018
00019 #if !defined (__ACE_INLINE__)
00020 #include "ace/Naming_Context.inl"
00021 #endif
00022
00023 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00024
00025
00026
00027 typedef ACE_Local_Name_Space <ACE_MMAP_MEMORY_POOL, ACE_RW_Process_Mutex> LOCAL_NAME_SPACE;
00028 typedef ACE_Local_Name_Space <ACE_LITE_MMAP_MEMORY_POOL, ACE_RW_Process_Mutex> LITE_LOCAL_NAME_SPACE;
00029
00030
00031
00032
00033 int
00034 ACE_Naming_Context::info (ACE_TCHAR **strp,
00035 size_t length) const
00036 {
00037 ACE_TRACE ("ACE_Naming_Context::info");
00038
00039 ACE_TCHAR buf[BUFSIZ];
00040
00041 ACE_OS::sprintf (buf,
00042 ACE_TEXT ("%s\t#%s\n"),
00043 ACE_TEXT ("ACE_Naming_Context"),
00044 ACE_TEXT ("Proxy for making calls to a Name Server"));
00045
00046 if (*strp == 0 && (*strp = ACE_OS::strdup (buf)) == 0)
00047 return -1;
00048 else
00049 ACE_OS::strsncpy (*strp, buf, length);
00050 return static_cast<int> (ACE_OS::strlen (buf));
00051 }
00052
00053 int
00054 ACE_Naming_Context::local (void)
00055 {
00056 ACE_TRACE ("ACE_Naming_Context::local");
00057 return ACE_OS::strcmp (this->netnameserver_host_,
00058 ACE_TEXT ("localhost")) == 0
00059 || ACE_OS::strcmp (this->netnameserver_host_,
00060 this->hostname_) == 0;
00061 }
00062
00063 int
00064 ACE_Naming_Context::open (Context_Scope_Type scope_in, int lite)
00065 {
00066 ACE_TRACE ("ACE_Naming_Context::open");
00067 ACE_OS::hostname (this->hostname_,
00068 (sizeof this->hostname_ / sizeof (ACE_TCHAR)));
00069
00070 this->netnameserver_host_ =
00071 this->name_options_->nameserver_host ();
00072 this->netnameserver_port_ =
00073 this->name_options_->nameserver_port ();
00074
00075
00076
00077
00078 #if (defined (ACE_WIN32) && defined (ACE_USES_WCHAR))
00079
00080
00081 if (this->name_options_->use_registry ())
00082
00083 ACE_NEW_RETURN (this->name_space_,
00084 ACE_Registry_Name_Space (this->name_options_),
00085 -1);
00086 #endif
00087 if (!this->name_options_->use_registry ())
00088 {
00089 if (scope_in == ACE_Naming_Context::NET_LOCAL && this->local () == 0)
00090 {
00091
00092 ACE_NEW_RETURN (this->name_space_,
00093 ACE_Remote_Name_Space (this->netnameserver_host_,
00094 (u_short) this->netnameserver_port_),
00095 -1);
00096 }
00097 else
00098 {
00099 if (lite)
00100 ACE_NEW_RETURN (this->name_space_,
00101 LITE_LOCAL_NAME_SPACE (scope_in,
00102 this->name_options_),
00103 -1);
00104 else
00105 ACE_NEW_RETURN (this->name_space_,
00106 LOCAL_NAME_SPACE (scope_in,
00107 this->name_options_),
00108 -1);
00109 }
00110 }
00111
00112 if (ACE_LOG_MSG->op_status () != 0 || this->name_space_ == 0)
00113 ACE_ERROR_RETURN ((LM_ERROR,
00114 ACE_TEXT ("NAME_SPACE::NAME_SPACE\n")),
00115 -1);
00116 return 0;
00117 }
00118
00119 int
00120 ACE_Naming_Context::close_down (void)
00121 {
00122 ACE_TRACE ("ACE_Naming_Context::close_down");
00123
00124 delete this->name_options_;
00125 this->name_options_ = 0;
00126
00127 return this->close ();
00128 }
00129
00130 int
00131 ACE_Naming_Context::close (void)
00132 {
00133 ACE_TRACE ("ACE_Naming_Context::close");
00134
00135 delete this->name_space_;
00136 this->name_space_ = 0;
00137
00138 return 0;
00139 }
00140
00141 ACE_Naming_Context::ACE_Naming_Context (void)
00142 : name_options_ (0),
00143 name_space_ (0)
00144 {
00145 ACE_TRACE ("ACE_Naming_Context::ACE_Naming_Context");
00146
00147 ACE_NEW (this->name_options_,
00148 ACE_Name_Options);
00149 }
00150
00151 ACE_Naming_Context::ACE_Naming_Context (Context_Scope_Type scope_in,
00152 int lite)
00153 : name_options_ (0),
00154 name_space_ (0),
00155 netnameserver_host_ (0)
00156 {
00157 ACE_TRACE ("ACE_Naming_Context::ACE_Naming_Context");
00158
00159 ACE_NEW (this->name_options_,
00160 ACE_Name_Options);
00161
00162
00163 if (this->open (scope_in, lite) == -1)
00164 ACE_ERROR ((LM_ERROR,
00165 ACE_TEXT ("%p\n"),
00166 ACE_TEXT ("ACE_Naming_Context::ACE_Naming_Context")));
00167 }
00168
00169 ACE_Name_Options *
00170 ACE_Naming_Context::name_options (void)
00171 {
00172 return this->name_options_;
00173 }
00174
00175 int
00176 ACE_Naming_Context::bind (const ACE_NS_WString &name_in,
00177 const ACE_NS_WString &value_in,
00178 const char *type_in)
00179 {
00180 ACE_TRACE ("ACE_Naming_Context::bind");
00181 return this->name_space_->bind (name_in, value_in, type_in);
00182 }
00183
00184 int
00185 ACE_Naming_Context::bind (const char *name_in,
00186 const char *value_in,
00187 const char *type_in)
00188 {
00189 ACE_TRACE ("ACE_Naming_Context::bind");
00190 return this->bind (ACE_NS_WString (name_in),
00191 ACE_NS_WString (value_in),
00192 type_in);
00193 }
00194
00195 int
00196 ACE_Naming_Context::rebind (const ACE_NS_WString &name_in,
00197 const ACE_NS_WString &value_in,
00198 const char *type_in)
00199 {
00200 ACE_TRACE ("ACE_Naming_Context::rebind");
00201 return this->name_space_->rebind (name_in,
00202 value_in,
00203 type_in);
00204 }
00205
00206 int
00207 ACE_Naming_Context::rebind (const char *name_in,
00208 const char *value_in,
00209 const char *type_in)
00210 {
00211 ACE_TRACE ("ACE_Naming_Context::rebind");
00212 return rebind (ACE_NS_WString (name_in),
00213 ACE_NS_WString (value_in),
00214 type_in);
00215 }
00216
00217 int
00218 ACE_Naming_Context::resolve (const ACE_NS_WString &name_in,
00219 ACE_NS_WString &value_out,
00220 char *&type_out)
00221 {
00222 ACE_TRACE ("ACE_Naming_Context::resolve");
00223 return this->name_space_->resolve (name_in,
00224 value_out,
00225 type_out);
00226 }
00227
00228 int
00229 ACE_Naming_Context::resolve (const char *name_in,
00230 ACE_NS_WString &value_out,
00231 char *&type_out)
00232 {
00233 ACE_TRACE ("ACE_Naming_Context::resolve");
00234 return this->resolve (ACE_NS_WString (name_in),
00235 value_out,
00236 type_out);
00237 }
00238
00239 int
00240 ACE_Naming_Context::resolve (const char *name_in,
00241 char *&value_out,
00242 char *&type_out)
00243 {
00244 ACE_TRACE ("ACE_Naming_Context::resolve");
00245 ACE_NS_WString val_str;
00246
00247 if (this->resolve (ACE_NS_WString (name_in),
00248 val_str,
00249 type_out) == -1)
00250 return -1;
00251
00252
00253
00254 value_out = val_str.char_rep ();
00255
00256 return value_out == 0 ? -1 : 0;
00257 }
00258
00259 int
00260 ACE_Naming_Context::unbind (const ACE_NS_WString &name_in)
00261 {
00262 ACE_TRACE ("ACE_Naming_Context::unbind");
00263 return this->name_space_->unbind (name_in);
00264 }
00265
00266 int
00267 ACE_Naming_Context::unbind (const char *name_in)
00268 {
00269 ACE_TRACE ("ACE_Naming_Context::unbind");
00270 return this->unbind (ACE_NS_WString (name_in));
00271 }
00272
00273 int
00274 ACE_Naming_Context::list_names (ACE_PWSTRING_SET &set_out,
00275 const ACE_NS_WString &pattern_in)
00276 {
00277 ACE_TRACE ("ACE_Naming_Context::list_names");
00278 return this->name_space_->list_names (set_out,
00279 pattern_in);
00280 }
00281
00282 int
00283 ACE_Naming_Context::list_names (ACE_PWSTRING_SET &set_out,
00284 const char *pattern_in)
00285 {
00286 ACE_TRACE ("ACE_Naming_Context::list_names");
00287 return this->list_names (set_out,
00288 ACE_NS_WString (pattern_in));
00289 }
00290
00291 int
00292 ACE_Naming_Context::list_values (ACE_PWSTRING_SET &set_out,
00293 const ACE_NS_WString &pattern_in)
00294 {
00295 ACE_TRACE ("ACE_Naming_Context::list_values");
00296 return this->name_space_->list_values (set_out,
00297 pattern_in);
00298 }
00299
00300 int
00301 ACE_Naming_Context::list_values (ACE_PWSTRING_SET &set_out,
00302 const char *pattern_in)
00303 {
00304 ACE_TRACE ("ACE_Naming_Context::list_values");
00305 return this->list_values (set_out,
00306 ACE_NS_WString (pattern_in));
00307 }
00308
00309 int
00310 ACE_Naming_Context::list_types (ACE_PWSTRING_SET &set_out,
00311 const ACE_NS_WString &pattern_in)
00312 {
00313 ACE_TRACE ("ACE_Naming_Context::list_types");
00314 return this->name_space_->list_types (set_out,
00315 pattern_in);
00316 }
00317
00318 int
00319 ACE_Naming_Context::list_types (ACE_PWSTRING_SET &set_out,
00320 const char *pattern_in)
00321 {
00322 ACE_TRACE ("ACE_Naming_Context::list_types");
00323 return this->list_types (set_out,
00324 ACE_NS_WString (pattern_in));
00325 }
00326
00327 int
00328 ACE_Naming_Context::list_name_entries (ACE_BINDING_SET &set_out,
00329 const ACE_NS_WString &pattern_in)
00330 {
00331 ACE_TRACE ("ACE_Naming_Context::list_name_entries");
00332 return this->name_space_->list_name_entries (set_out,
00333 pattern_in);
00334 }
00335
00336 int
00337 ACE_Naming_Context::list_name_entries (ACE_BINDING_SET &set_out,
00338 const char *pattern_in)
00339 {
00340 ACE_TRACE ("ACE_Naming_Context::list_name_entries");
00341 return this->list_name_entries (set_out,
00342 ACE_NS_WString (pattern_in));
00343 }
00344
00345 int
00346 ACE_Naming_Context::list_value_entries (ACE_BINDING_SET &set_out,
00347 const ACE_NS_WString &pattern_in)
00348 {
00349 ACE_TRACE ("ACE_Naming_Context::list_value_entries");
00350 return this->name_space_->list_value_entries (set_out,
00351 pattern_in);
00352 }
00353
00354 int
00355 ACE_Naming_Context::list_value_entries (ACE_BINDING_SET &set_out,
00356 const char *pattern_in)
00357 {
00358 ACE_TRACE ("ACE_Naming_Context::list_value_entries");
00359 return this->list_value_entries (set_out,
00360 ACE_NS_WString (pattern_in));
00361 }
00362
00363 int
00364 ACE_Naming_Context::list_type_entries (ACE_BINDING_SET &set_out,
00365 const ACE_NS_WString &pattern_in)
00366 {
00367 ACE_TRACE ("ACE_Naming_Context::list_type_entries");
00368 return this->name_space_->list_type_entries (set_out,
00369 pattern_in);
00370 }
00371
00372 int
00373 ACE_Naming_Context::list_type_entries (ACE_BINDING_SET &set_out,
00374 const char *pattern_in)
00375 {
00376 ACE_TRACE ("ACE_Naming_Context::list_type_entries");
00377 return this->list_type_entries (set_out,
00378 ACE_NS_WString (pattern_in));
00379 }
00380
00381 ACE_Naming_Context::~ACE_Naming_Context (void)
00382 {
00383 ACE_TRACE ("ACE_Naming_Context::~ACE_Naming_Context");
00384
00385 this->close_down ();
00386 }
00387
00388 void
00389 ACE_Naming_Context::dump ()
00390 {
00391 #if defined (ACE_HAS_DUMP)
00392 ACE_TRACE ("ACE_Naming_Context::dump");
00393 this->name_space_->dump();
00394 #endif
00395 }
00396
00397 int
00398 ACE_Naming_Context::init (int argc, ACE_TCHAR *argv[])
00399 {
00400 if (ACE::debug ())
00401 ACE_DEBUG ((LM_DEBUG,
00402 ACE_TEXT ("ACE_Naming_Context::init\n")));
00403 this->name_options_->parse_args (argc, argv);
00404 return this->open (this->name_options_->context ());
00405 }
00406
00407 int
00408 ACE_Naming_Context::fini (void)
00409 {
00410 if (ACE::debug ())
00411 ACE_DEBUG ((LM_DEBUG,
00412 ACE_TEXT ("ACE_Naming_Context::fini\n")));
00413 this->close_down ();
00414 return 0;
00415 }
00416
00417 ACE_Name_Options::ACE_Name_Options (void)
00418 : debugging_ (0),
00419 verbosity_ (0),
00420 use_registry_ (false),
00421 nameserver_port_ (ACE_DEFAULT_SERVER_PORT),
00422 nameserver_host_ (ACE_OS::strdup (ACE_DEFAULT_SERVER_HOST)),
00423 process_name_ (0),
00424 database_ (ACE_OS::strdup (ACE_DEFAULT_LOCALNAME)),
00425 base_address_ (ACE_DEFAULT_BASE_ADDR)
00426 {
00427 ACE_TRACE ("ACE_Name_Options::ACE_Name_Options");
00428
00429 #if defined (ACE_DEFAULT_NAMESPACE_DIR)
00430 this->namespace_dir_ = ACE_OS::strdup (ACE_DEFAULT_NAMESPACE_DIR);
00431 #else
00432 size_t pathsize = (MAXPATHLEN + 1) * sizeof (ACE_TCHAR);
00433 this->namespace_dir_ = static_cast <ACE_TCHAR *> (ACE_OS::malloc (pathsize));
00434
00435 if (ACE::get_temp_dir (this->namespace_dir_, MAXPATHLEN) == -1)
00436 {
00437 ACE_ERROR ((LM_ERROR,
00438 ACE_TEXT ("Temporary path too long, ")
00439 ACE_TEXT ("defaulting to current directory\n")));
00440 ACE_OS::strcpy (this->namespace_dir_, ACE_TEXT ("."));
00441 ACE_OS::strcat (this->namespace_dir_, ACE_DIRECTORY_SEPARATOR_STR);
00442 }
00443 #endif
00444 }
00445
00446 ACE_Name_Options::~ACE_Name_Options (void)
00447 {
00448 ACE_TRACE ("ACE_Name_Options::~ACE_Name_Options");
00449
00450 ACE_OS::free ((void *) this->nameserver_host_);
00451 ACE_OS::free ((void *) this->namespace_dir_ );
00452 ACE_OS::free ((void *) this->process_name_ );
00453 ACE_OS::free ((void *) this->database_ );
00454 }
00455
00456 void
00457 ACE_Name_Options::nameserver_port (int port)
00458 {
00459 ACE_TRACE ("ACE_Name_Options::nameserver_port");
00460 this->nameserver_port_ = port;
00461 }
00462
00463 int
00464 ACE_Name_Options::nameserver_port (void)
00465 {
00466 ACE_TRACE ("ACE_Name_Options::nameserver_port");
00467 return this->nameserver_port_;
00468 }
00469
00470 void
00471 ACE_Name_Options::namespace_dir (const ACE_TCHAR *dir)
00472 {
00473 ACE_TRACE ("ACE_Name_Options::namespace_dir");
00474 ACE_OS::free ((void *) this->namespace_dir_ );
00475 this->namespace_dir_ = ACE_OS::strdup (dir);
00476 }
00477
00478 void
00479 ACE_Name_Options::process_name (const ACE_TCHAR *pname)
00480 {
00481 ACE_TRACE ("ACE_Name_Options::process_name");
00482 const ACE_TCHAR *t = ACE::basename (pname, ACE_DIRECTORY_SEPARATOR_CHAR);
00483 ACE_OS::free ((void *) this->process_name_ );
00484 this->process_name_ = ACE_OS::strdup (t);
00485 }
00486
00487 void
00488 ACE_Name_Options::nameserver_host (const ACE_TCHAR *host)
00489 {
00490 ACE_TRACE ("ACE_Name_Options::nameserver_host");
00491 ACE_OS::free ((void *) this->nameserver_host_);
00492 this->nameserver_host_ = ACE_OS::strdup (host);
00493 }
00494
00495 const ACE_TCHAR *
00496 ACE_Name_Options::nameserver_host (void)
00497 {
00498 ACE_TRACE ("ACE_Name_Options::nameserver_host");
00499 return this->nameserver_host_;
00500 }
00501
00502 const ACE_TCHAR *
00503 ACE_Name_Options::database (void)
00504 {
00505 ACE_TRACE ("ACE_Name_Options::database");
00506 return this->database_;
00507 }
00508
00509 void
00510 ACE_Name_Options::database (const ACE_TCHAR *db)
00511 {
00512 ACE_TRACE ("ACE_Name_Options::database");
00513 ACE_OS::free ((void *) this->database_);
00514 this->database_ = ACE_OS::strdup (db);
00515 }
00516
00517 char *
00518 ACE_Name_Options::base_address (void)
00519 {
00520 ACE_TRACE ("ACE_Name_Options::base_address");
00521 return this->base_address_;
00522 }
00523
00524 void
00525 ACE_Name_Options::base_address (char *base_address)
00526 {
00527 ACE_TRACE ("ACE_Name_Options::base_address");
00528 this->base_address_ = base_address;
00529 }
00530
00531 ACE_Naming_Context::Context_Scope_Type
00532 ACE_Name_Options::context (void)
00533 {
00534 ACE_TRACE ("ACE_Name_Options::context");
00535 return this->context_;
00536 }
00537
00538 void
00539 ACE_Name_Options::context (ACE_Naming_Context::Context_Scope_Type context)
00540 {
00541 ACE_TRACE ("ACE_Name_Options::context");
00542 this->context_ = context;
00543 }
00544
00545 void
00546 ACE_Name_Options::parse_args (int argc, ACE_TCHAR *argv[])
00547 {
00548 ACE_TRACE ("ACE_Name_Options::parse_args");
00549
00550 const ACE_TCHAR* program_name = 0;
00551
00552
00553 if (argc > 0)
00554 program_name = argv[0];
00555
00556 ACE_LOG_MSG->open (program_name);
00557 this->process_name (program_name);
00558
00559
00560 this->context (ACE_Naming_Context::PROC_LOCAL);
00561
00562
00563
00564
00565 this->database (this->process_name ());
00566
00567 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("b:c:dh:l:P:p:s:T:vr"));
00568
00569 for (int c; (c = get_opt ()) != -1; )
00570 switch (c)
00571 {
00572 case 'c':
00573 {
00574 if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("PROC_LOCAL")) == 0)
00575 this->context (ACE_Naming_Context::PROC_LOCAL);
00576 else if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("NODE_LOCAL")) == 0)
00577 this->context (ACE_Naming_Context::NODE_LOCAL);
00578 else if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("NET_LOCAL")) == 0)
00579 this->context (ACE_Naming_Context::NET_LOCAL);
00580 }
00581 break;
00582 case 'd':
00583 this->debugging_ = 1;
00584 break;
00585 case 'r':
00586 this->use_registry_ = true;
00587 break;
00588 case 'h':
00589 this->nameserver_host (get_opt.opt_arg ());
00590 break;
00591 case 'l':
00592 this->namespace_dir (get_opt.opt_arg ());
00593 break;
00594 case 'P':
00595 this->process_name (get_opt.opt_arg ());
00596 break;
00597 case 'p':
00598 this->nameserver_port (ACE_OS::atoi (get_opt.opt_arg ()));
00599 break;
00600 case 's':
00601 this->database (get_opt.opt_arg ());
00602 break;
00603 case 'b':
00604 this->base_address
00605 (static_cast<char *> (ACE_OS::atop (get_opt.opt_arg ())));
00606 break;
00607 case 'T':
00608 #if defined (ACE_HAS_TRACE)
00609 if (ACE_OS::strcasecmp (get_opt.opt_arg (), ACE_TEXT ("ON")) == 0)
00610 ACE_Trace::start_tracing ();
00611 else if (ACE_OS::strcasecmp (get_opt.opt_arg (), ACE_TEXT ("OFF")) == 0)
00612 ACE_Trace::stop_tracing ();
00613 #endif
00614 break;
00615 case 'v':
00616 this->verbosity_ = 1;
00617 break;
00618 default:
00619 ACE_OS::fprintf (stderr, "%s\n"
00620 "\t[-d] (enable debugging)\n"
00621 "\t[-h nameserver host]\n"
00622 "\t[-l namespace directory]\n"
00623 "\t[-P processname]\n"
00624 "\t[-p nameserver port]\n"
00625 "\t[-s database name]\n"
00626 "\t[-b base address]\n"
00627 "\t[-v] (verbose) \n"
00628 "\t[-r] (use Win32 Registry) \n",
00629 argv[0]);
00630
00631 break;
00632 }
00633 }
00634
00635 ACE_END_VERSIONED_NAMESPACE_DECL
00636
00637
00638
00639
00640
00641 ACE_FACTORY_DEFINE (ACE, ACE_Naming_Context)
00642 ACE_STATIC_SVC_DEFINE (ACE_Naming_Context,
00643 ACE_TEXT ("ACE_Naming_Context"),
00644 ACE_SVC_OBJ_T,
00645 &ACE_SVC_NAME (ACE_Naming_Context),
00646 ACE_Service_Type::DELETE_THIS |
00647 ACE_Service_Type::DELETE_OBJ,
00648 0)
00649 ACE_STATIC_SVC_REQUIRE (ACE_Naming_Context)