1 #include "ace/Get_Opt.h"
2 #include "ace/Naming_Context.h"
3 #include "ace/Remote_Name_Space.h"
4 #include "ace/Local_Name_Space_T.h"
5 #include "ace/Registry_Name_Space.h"
6 #include "ace/MMAP_Memory_Pool.h"
7 #include "ace/Lib_Find.h"
8 #include "ace/RW_Process_Mutex.h"
9 #include "ace/OS_NS_string.h"
10 #include "ace/OS_NS_unistd.h"
11 #if defined (ACE_HAS_TRACE)
12 # include "ace/OS_NS_strings.h"
13 # include "ace/Trace.h"
14 #endif /* ACE_HAS_TRACE */
16 #if !defined (__ACE_INLINE__)
17 #include "ace/Naming_Context.inl"
18 #endif /* __ACE_INLINE__ */
20 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
22 // Make life easier later on...
24 using LOCAL_NAME_SPACE
= ACE_Local_Name_Space
<ACE_MMAP_Memory_Pool
, ACE_RW_Process_Mutex
>;
25 using LITE_LOCAL_NAME_SPACE
= ACE_Local_Name_Space
<ACE_Lite_MMAP_Memory_Pool
, ACE_RW_Process_Mutex
>;
27 // The ACE_Naming_Context static service object is now defined
28 // by the ACE_Object_Manager, in Object_Manager.cpp.
31 ACE_Naming_Context::info (ACE_TCHAR
**strp
,
34 ACE_TRACE ("ACE_Naming_Context::info");
36 ACE_TCHAR buf
[BUFSIZ
];
38 ACE_OS::snprintf (buf
, BUFSIZ
,
39 ACE_TEXT ("%s\t#%s\n"),
40 ACE_TEXT ("ACE_Naming_Context"),
41 ACE_TEXT ("Proxy for making calls to a Name Server"));
43 if (*strp
== 0 && (*strp
= ACE_OS::strdup (buf
)) == 0)
46 ACE_OS::strsncpy (*strp
, buf
, length
);
47 return static_cast<int> (ACE_OS::strlen (buf
));
51 ACE_Naming_Context::local ()
53 ACE_TRACE ("ACE_Naming_Context::local");
54 return ACE_OS::strcmp (this->netnameserver_host_
,
55 ACE_TEXT ("localhost")) == 0
56 || ACE_OS::strcmp (this->netnameserver_host_
,
57 this->hostname_
) == 0;
61 ACE_Naming_Context::open (Context_Scope_Type scope_in
, int lite
)
63 ACE_TRACE ("ACE_Naming_Context::open");
64 ACE_OS::hostname (this->hostname_
,
65 (sizeof this->hostname_
/ sizeof (ACE_TCHAR
)));
67 this->netnameserver_host_
=
68 this->name_options_
->nameserver_host ();
69 this->netnameserver_port_
=
70 this->name_options_
->nameserver_port ();
72 // Perform factory operation to select appropriate type of
73 // Name_Space subclass.
75 #if (defined (ACE_WIN32) && defined (ACE_USES_WCHAR))
76 // This only works on Win32 platforms when ACE_USES_WCHAR is turned on
78 if (this->name_options_
->use_registry ())
80 ACE_NEW_RETURN (this->name_space_
,
81 ACE_Registry_Name_Space (this->name_options_
),
83 #endif /* ACE_WIN32 && ACE_USES_WCHAR */
84 if (!this->name_options_
->use_registry ())
86 if (scope_in
== ACE_Naming_Context::NET_LOCAL
&& this->local () == 0)
88 // Use NET_LOCAL name space, set up connection with remote server.
89 ACE_NEW_RETURN (this->name_space_
,
90 ACE_Remote_Name_Space (this->netnameserver_host_
,
91 (u_short
) this->netnameserver_port_
),
94 else // Use NODE_LOCAL or PROC_LOCAL name space.
97 ACE_NEW_RETURN (this->name_space_
,
98 LITE_LOCAL_NAME_SPACE (scope_in
,
102 ACE_NEW_RETURN (this->name_space_
,
103 LOCAL_NAME_SPACE (scope_in
,
104 this->name_options_
),
109 if (ACE_LOG_MSG
->op_status () != 0 || this->name_space_
== 0)
110 ACELIB_ERROR_RETURN ((LM_ERROR
,
111 ACE_TEXT ("NAME_SPACE::NAME_SPACE\n")),
117 ACE_Naming_Context::close_down ()
119 ACE_TRACE ("ACE_Naming_Context::close_down");
121 delete this->name_options_
;
122 this->name_options_
= 0;
124 return this->close ();
128 ACE_Naming_Context::close ()
130 ACE_TRACE ("ACE_Naming_Context::close");
132 delete this->name_space_
;
133 this->name_space_
= 0;
138 ACE_Naming_Context::ACE_Naming_Context ()
141 netnameserver_host_ (0),
142 netnameserver_port_ (0)
144 ACE_TRACE ("ACE_Naming_Context::ACE_Naming_Context");
146 ACE_NEW (this->name_options_
,
150 ACE_Naming_Context::ACE_Naming_Context (Context_Scope_Type scope_in
,
154 netnameserver_host_ (0)
156 ACE_TRACE ("ACE_Naming_Context::ACE_Naming_Context");
158 ACE_NEW (this->name_options_
,
162 if (this->open (scope_in
, lite
) == -1)
163 ACELIB_ERROR ((LM_ERROR
,
165 ACE_TEXT ("ACE_Naming_Context::ACE_Naming_Context")));
169 ACE_Naming_Context::name_options ()
171 return this->name_options_
;
175 ACE_Naming_Context::bind (const ACE_NS_WString
&name_in
,
176 const ACE_NS_WString
&value_in
,
179 ACE_TRACE ("ACE_Naming_Context::bind");
180 return this->name_space_
->bind (name_in
, value_in
, type_in
);
184 ACE_Naming_Context::bind (const char *name_in
,
185 const char *value_in
,
188 ACE_TRACE ("ACE_Naming_Context::bind");
189 return this->bind (ACE_NS_WString (name_in
),
190 ACE_NS_WString (value_in
),
195 ACE_Naming_Context::rebind (const ACE_NS_WString
&name_in
,
196 const ACE_NS_WString
&value_in
,
199 ACE_TRACE ("ACE_Naming_Context::rebind");
200 return this->name_space_
->rebind (name_in
,
206 ACE_Naming_Context::rebind (const char *name_in
,
207 const char *value_in
,
210 ACE_TRACE ("ACE_Naming_Context::rebind");
211 return rebind (ACE_NS_WString (name_in
),
212 ACE_NS_WString (value_in
),
217 ACE_Naming_Context::resolve (const ACE_NS_WString
&name_in
,
218 ACE_NS_WString
&value_out
,
221 ACE_TRACE ("ACE_Naming_Context::resolve");
222 return this->name_space_
->resolve (name_in
,
228 ACE_Naming_Context::resolve (const char *name_in
,
229 ACE_NS_WString
&value_out
,
232 ACE_TRACE ("ACE_Naming_Context::resolve");
233 return this->resolve (ACE_NS_WString (name_in
),
239 ACE_Naming_Context::resolve (const char *name_in
,
243 ACE_TRACE ("ACE_Naming_Context::resolve");
244 ACE_NS_WString val_str
;
246 if (this->resolve (ACE_NS_WString (name_in
),
251 // Note that <char_rep> *allocates* the memory! Thus, caller is
252 // responsible for deleting it!
253 value_out
= val_str
.char_rep ();
255 return value_out
== 0 ? -1 : 0;
259 ACE_Naming_Context::unbind (const ACE_NS_WString
&name_in
)
261 ACE_TRACE ("ACE_Naming_Context::unbind");
262 return this->name_space_
->unbind (name_in
);
266 ACE_Naming_Context::unbind (const char *name_in
)
268 ACE_TRACE ("ACE_Naming_Context::unbind");
269 return this->unbind (ACE_NS_WString (name_in
));
273 ACE_Naming_Context::list_names (ACE_PWSTRING_SET
&set_out
,
274 const ACE_NS_WString
&pattern_in
)
276 ACE_TRACE ("ACE_Naming_Context::list_names");
277 return this->name_space_
->list_names (set_out
,
282 ACE_Naming_Context::list_names (ACE_PWSTRING_SET
&set_out
,
283 const char *pattern_in
)
285 ACE_TRACE ("ACE_Naming_Context::list_names");
286 return this->list_names (set_out
,
287 ACE_NS_WString (pattern_in
));
291 ACE_Naming_Context::list_values (ACE_PWSTRING_SET
&set_out
,
292 const ACE_NS_WString
&pattern_in
)
294 ACE_TRACE ("ACE_Naming_Context::list_values");
295 return this->name_space_
->list_values (set_out
,
300 ACE_Naming_Context::list_values (ACE_PWSTRING_SET
&set_out
,
301 const char *pattern_in
)
303 ACE_TRACE ("ACE_Naming_Context::list_values");
304 return this->list_values (set_out
,
305 ACE_NS_WString (pattern_in
));
309 ACE_Naming_Context::list_types (ACE_PWSTRING_SET
&set_out
,
310 const ACE_NS_WString
&pattern_in
)
312 ACE_TRACE ("ACE_Naming_Context::list_types");
313 return this->name_space_
->list_types (set_out
,
318 ACE_Naming_Context::list_types (ACE_PWSTRING_SET
&set_out
,
319 const char *pattern_in
)
321 ACE_TRACE ("ACE_Naming_Context::list_types");
322 return this->list_types (set_out
,
323 ACE_NS_WString (pattern_in
));
327 ACE_Naming_Context::list_name_entries (ACE_BINDING_SET
&set_out
,
328 const ACE_NS_WString
&pattern_in
)
330 ACE_TRACE ("ACE_Naming_Context::list_name_entries");
331 return this->name_space_
->list_name_entries (set_out
,
336 ACE_Naming_Context::list_name_entries (ACE_BINDING_SET
&set_out
,
337 const char *pattern_in
)
339 ACE_TRACE ("ACE_Naming_Context::list_name_entries");
340 return this->list_name_entries (set_out
,
341 ACE_NS_WString (pattern_in
));
345 ACE_Naming_Context::list_value_entries (ACE_BINDING_SET
&set_out
,
346 const ACE_NS_WString
&pattern_in
)
348 ACE_TRACE ("ACE_Naming_Context::list_value_entries");
349 return this->name_space_
->list_value_entries (set_out
,
354 ACE_Naming_Context::list_value_entries (ACE_BINDING_SET
&set_out
,
355 const char *pattern_in
)
357 ACE_TRACE ("ACE_Naming_Context::list_value_entries");
358 return this->list_value_entries (set_out
,
359 ACE_NS_WString (pattern_in
));
363 ACE_Naming_Context::list_type_entries (ACE_BINDING_SET
&set_out
,
364 const ACE_NS_WString
&pattern_in
)
366 ACE_TRACE ("ACE_Naming_Context::list_type_entries");
367 return this->name_space_
->list_type_entries (set_out
,
372 ACE_Naming_Context::list_type_entries (ACE_BINDING_SET
&set_out
,
373 const char *pattern_in
)
375 ACE_TRACE ("ACE_Naming_Context::list_type_entries");
376 return this->list_type_entries (set_out
,
377 ACE_NS_WString (pattern_in
));
380 ACE_Naming_Context::~ACE_Naming_Context ()
382 ACE_TRACE ("ACE_Naming_Context::~ACE_Naming_Context");
388 ACE_Naming_Context::dump ()
390 #if defined (ACE_HAS_DUMP)
391 ACE_TRACE ("ACE_Naming_Context::dump");
392 this->name_space_
->dump();
393 #endif /* ACE_HAS_DUMP */
397 ACE_Naming_Context::init (int argc
, ACE_TCHAR
*argv
[])
400 ACELIB_DEBUG ((LM_DEBUG
,
401 ACE_TEXT ("ACE_Naming_Context::init\n")));
402 this->name_options_
->parse_args (argc
, argv
);
403 return this->open (this->name_options_
->context ());
407 ACE_Naming_Context::fini ()
410 ACELIB_DEBUG ((LM_DEBUG
,
411 ACE_TEXT ("ACE_Naming_Context::fini\n")));
416 ACE_Name_Options::ACE_Name_Options ()
417 : debugging_ (false),
419 use_registry_ (false),
420 nameserver_port_ (ACE_DEFAULT_SERVER_PORT
),
421 nameserver_host_ (ACE_OS::strdup (ACE_DEFAULT_SERVER_HOST
)),
423 database_ (ACE_OS::strdup (ACE_DEFAULT_LOCALNAME
)),
424 base_address_ (ACE_DEFAULT_BASE_ADDR
)
426 ACE_TRACE ("ACE_Name_Options::ACE_Name_Options");
428 #if defined (ACE_DEFAULT_NAMESPACE_DIR)
429 this->namespace_dir_
= ACE_OS::strdup (ACE_DEFAULT_NAMESPACE_DIR
);
430 #else /* ACE_DEFAULT_NAMESPACE_DIR */
431 size_t pathsize
= (MAXPATHLEN
+ 1) * sizeof (ACE_TCHAR
);
432 #if defined (ACE_HAS_ALLOC_HOOKS)
433 this->namespace_dir_
= static_cast <ACE_TCHAR
*> (ACE_Allocator::instance()->malloc (pathsize
));
435 this->namespace_dir_
= static_cast <ACE_TCHAR
*> (ACE_OS::malloc (pathsize
));
436 #endif /* ACE_HAS_ALLOC_HOOKS */
438 if (ACE::get_temp_dir (this->namespace_dir_
, MAXPATHLEN
) == -1)
440 ACELIB_ERROR ((LM_ERROR
,
441 ACE_TEXT ("Temporary path too long, ")
442 ACE_TEXT ("defaulting to current directory\n")));
443 ACE_OS::strcpy (this->namespace_dir_
, ACE_TEXT ("."));
444 ACE_OS::strcat (this->namespace_dir_
, ACE_DIRECTORY_SEPARATOR_STR
);
446 #endif /* ACE_DEFAULT_NAMESPACE_DIR */
449 ACE_Name_Options::~ACE_Name_Options ()
451 ACE_TRACE ("ACE_Name_Options::~ACE_Name_Options");
453 #if defined (ACE_HAS_ALLOC_HOOKS)
454 ACE_Allocator::instance()->free ((void *) this->nameserver_host_
);
455 ACE_Allocator::instance()->free ((void *) this->namespace_dir_
);
456 ACE_Allocator::instance()->free ((void *) this->process_name_
);
457 ACE_Allocator::instance()->free ((void *) this->database_
);
459 ACE_OS::free ((void *) this->nameserver_host_
);
460 ACE_OS::free ((void *) this->namespace_dir_
);
461 ACE_OS::free ((void *) this->process_name_
);
462 ACE_OS::free ((void *) this->database_
);
463 #endif /* ACE_HAS_ALLOC_HOOKS */
466 ACE_ALLOC_HOOK_DEFINE(ACE_Name_Options
)
469 ACE_Name_Options::nameserver_port (int port
)
471 ACE_TRACE ("ACE_Name_Options::nameserver_port");
472 this->nameserver_port_
= port
;
476 ACE_Name_Options::nameserver_port ()
478 ACE_TRACE ("ACE_Name_Options::nameserver_port");
479 return this->nameserver_port_
;
483 ACE_Name_Options::namespace_dir (const ACE_TCHAR
*dir
)
485 ACE_TRACE ("ACE_Name_Options::namespace_dir");
486 #if defined (ACE_HAS_ALLOC_HOOKS)
487 ACE_Allocator::instance()->free ((void *) this->namespace_dir_
);
489 ACE_OS::free ((void *) this->namespace_dir_
);
490 #endif /* ACE_HAS_ALLOC_HOOKS */
491 this->namespace_dir_
= ACE_OS::strdup (dir
);
495 ACE_Name_Options::process_name (const ACE_TCHAR
*pname
)
497 ACE_TRACE ("ACE_Name_Options::process_name");
498 const ACE_TCHAR
*t
= ACE::basename (pname
, ACE_DIRECTORY_SEPARATOR_CHAR
);
499 #if defined (ACE_HAS_ALLOC_HOOKS)
500 ACE_Allocator::instance()->free ((void *) this->process_name_
);
502 ACE_OS::free ((void *) this->process_name_
);
503 #endif /* ACE_HAS_ALLOC_HOOKS */
504 this->process_name_
= ACE_OS::strdup (t
);
508 ACE_Name_Options::nameserver_host (const ACE_TCHAR
*host
)
510 ACE_TRACE ("ACE_Name_Options::nameserver_host");
511 #if defined (ACE_HAS_ALLOC_HOOKS)
512 ACE_Allocator::instance()->free ((void *) this->nameserver_host_
);
514 ACE_OS::free ((void *) this->nameserver_host_
);
515 #endif /* ACE_HAS_ALLOC_HOOKS */
516 this->nameserver_host_
= ACE_OS::strdup (host
);
520 ACE_Name_Options::nameserver_host ()
522 ACE_TRACE ("ACE_Name_Options::nameserver_host");
523 return this->nameserver_host_
;
527 ACE_Name_Options::database ()
529 ACE_TRACE ("ACE_Name_Options::database");
530 return this->database_
;
534 ACE_Name_Options::database (const ACE_TCHAR
*db
)
536 ACE_TRACE ("ACE_Name_Options::database");
537 #if defined (ACE_HAS_ALLOC_HOOKS)
538 ACE_Allocator::instance()->free ((void *) this->database_
);
540 ACE_OS::free ((void *) this->database_
);
541 #endif /* ACE_HAS_ALLOC_HOOKS */
542 this->database_
= ACE_OS::strdup (db
);
546 ACE_Name_Options::base_address ()
548 ACE_TRACE ("ACE_Name_Options::base_address");
549 return this->base_address_
;
553 ACE_Name_Options::base_address (char *base_address
)
555 ACE_TRACE ("ACE_Name_Options::base_address");
556 this->base_address_
= base_address
;
559 ACE_Naming_Context::Context_Scope_Type
560 ACE_Name_Options::context ()
562 ACE_TRACE ("ACE_Name_Options::context");
563 return this->context_
;
567 ACE_Name_Options::context (ACE_Naming_Context::Context_Scope_Type context
)
569 ACE_TRACE ("ACE_Name_Options::context");
570 this->context_
= context
;
574 ACE_Name_Options::parse_args (int argc
, ACE_TCHAR
*argv
[])
576 ACE_TRACE ("ACE_Name_Options::parse_args");
578 const ACE_TCHAR
* program_name
= 0;
580 // Argc can be 0 on some platforms like VxWorks.
582 program_name
= argv
[0];
584 ACE_LOG_MSG
->open (program_name
);
585 this->process_name (program_name
);
587 // Default is to use the PROC_LOCAL context...
588 this->context (ACE_Naming_Context::PROC_LOCAL
);
590 // Make the database name the same as the process name by default
591 // (note that this makes a copy of the process_name_ so that we can
592 // clean it up in the destructor).
593 this->database (this->process_name ());
595 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT ("b:c:dh:l:P:p:s:T:vr"));
597 for (int c
; (c
= get_opt ()) != -1; )
602 if (ACE_OS::strcmp (get_opt
.opt_arg (), ACE_TEXT ("PROC_LOCAL")) == 0)
603 this->context (ACE_Naming_Context::PROC_LOCAL
);
604 else if (ACE_OS::strcmp (get_opt
.opt_arg (), ACE_TEXT ("NODE_LOCAL")) == 0)
605 this->context (ACE_Naming_Context::NODE_LOCAL
);
606 else if (ACE_OS::strcmp (get_opt
.opt_arg (), ACE_TEXT ("NET_LOCAL")) == 0)
607 this->context (ACE_Naming_Context::NET_LOCAL
);
611 this->debugging_
= true;
614 this->use_registry_
= true;
617 this->nameserver_host (get_opt
.opt_arg ());
620 this->namespace_dir (get_opt
.opt_arg ());
623 this->process_name (get_opt
.opt_arg ());
626 this->nameserver_port (ACE_OS::atoi (get_opt
.opt_arg ()));
629 this->database (get_opt
.opt_arg ());
633 (static_cast<char *> (ACE_OS::atop (get_opt
.opt_arg ())));
636 #if defined (ACE_HAS_TRACE)
637 if (ACE_OS::strcasecmp (get_opt
.opt_arg (), ACE_TEXT ("ON")) == 0)
638 ACE_Trace::start_tracing ();
639 else if (ACE_OS::strcasecmp (get_opt
.opt_arg (), ACE_TEXT ("OFF")) == 0)
640 ACE_Trace::stop_tracing ();
641 #endif /* ACE_HAS_TRACE */
644 this->verbosity_
= true;
647 #ifndef ACE_LACKS_STDERR
648 ACE_OS::fprintf (stderr
, "%s\n"
649 "\t[-d] (enable debugging)\n"
650 "\t[-h nameserver host]\n"
651 "\t[-l namespace directory]\n"
652 "\t[-P processname]\n"
653 "\t[-p nameserver port]\n"
654 "\t[-s database name]\n"
655 "\t[-b base address]\n"
657 "\t[-r] (use Win32 Registry)\n",
658 ACE_TEXT_ALWAYS_CHAR (argv
[0]));
665 // The following Factory is used by the ACE_Service_Config and
666 // svc.conf file to dynamically initialize the state of the Name
669 ACE_FACTORY_DEFINE (ACE
, ACE_Naming_Context
)
670 ACE_STATIC_SVC_DEFINE (ACE_Naming_Context
,
671 ACE_TEXT ("ACE_Naming_Context"),
673 &ACE_SVC_NAME (ACE_Naming_Context
),
674 ACE_Service_Type::DELETE_THIS
|
675 ACE_Service_Type::DELETE_OBJ
,
677 ACE_STATIC_SVC_REQUIRE (ACE_Naming_Context
)
679 ACE_END_VERSIONED_NAMESPACE_DECL