1 #include "Messenger_i.h"
2 #include "orbsvcs/CosNamingC.h"
5 #include "ace/Get_Opt.h"
7 const ACE_TCHAR
*ior_output_file
= ACE_TEXT ("url.ior");
8 const ACE_TCHAR
*hostname
= ACE_TEXT ("localhost");
9 const ACE_TCHAR
*port
= ACE_TEXT ("2809");
12 parse_args (int argc
, ACE_TCHAR
*argv
[])
14 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:h:p:"));
17 while ((c
= get_opts ()) != -1)
21 ior_output_file
= get_opts
.opt_arg ();
25 hostname
= get_opts
.opt_arg ();
29 port
= get_opts
.opt_arg ();
34 ACE_ERROR_RETURN ((LM_ERROR
,
43 // Indicates successful parsing of the command line
47 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
52 CORBA::ORB_var orb
= CORBA::ORB_init(argc
, argv
);
54 if (parse_args (argc
, argv
) != 0)
57 //Get reference to Root POA
58 CORBA::Object_var obj
= orb
->resolve_initial_references("RootPOA");
59 PortableServer::POA_var poa
= PortableServer::POA::_narrow(obj
.in());
61 // Activate POA Manager
62 PortableServer::POAManager_var mgr
= poa
->the_POAManager();
65 // Find the Naming Service
66 obj
= orb
->resolve_initial_references("NameService");
67 CosNaming::NamingContextExt_var root
=
68 CosNaming::NamingContextExt::_narrow(obj
.in());
69 if (CORBA::is_nil(root
.in())) {
70 std::cerr
<< "Nil Naming Context reference" << std::endl
;
74 // Bind a new context.
77 name
[0].id
= CORBA::string_dup( "root.esc-dot" );
78 name
[0].kind
= CORBA::string_dup( "kind1" );
81 obj
= root
->resolve(name
);
83 catch(const CosNaming::NamingContext::NotFound
&) {
84 CosNaming::NamingContext_var dummy
= root
->bind_new_context(name
);
88 name
[1].id
= CORBA::string_dup( "leaf/esc-slash" );
89 name
[1].kind
= CORBA::string_dup( "kind2" );
92 PortableServer::Servant_var
<Messenger_i
> servant
= new Messenger_i
;
93 PortableServer::ObjectId_var oid
= poa
->activate_object(servant
.in());
94 CORBA::Object_var messenger_obj
= poa
->id_to_reference(oid
.in());
95 root
->rebind(name
, messenger_obj
.in());
97 // Also try rebinding to a simple path.
98 CosNaming::Name_var simp_name
= root
->to_name("Simple");
100 obj
= root
->resolve(simp_name
.in());
102 catch(const CosNaming::NamingContext::NotFound
&) {
103 CosNaming::NamingContext_var dummy
=
104 root
->bind_new_context(simp_name
.in());
106 simp_name
= root
->to_name("Simple/Messenger");
107 root
->rebind(simp_name
.in(), messenger_obj
.in());
109 // Convert Name to String Name.
110 CORBA::String_var str_name
= root
->to_string(name
);
111 std::cout
<< "str_name: " << str_name
.in() << std::endl
;
112 CORBA::String_var str_simple
= root
->to_string(simp_name
.in());
113 std::cout
<< "simple: " << str_simple
.in() << std::endl
;
115 // Convert String Name to Name.
116 CosNaming::Name_var tname
= root
->to_name(str_name
.in());
118 std::cout
<< "converted back to a CosNaming::Name: " << std::endl
;
119 std::cout
<< " name[0] = " << (* tname
)[0].id
.in() << " , "
120 << (* tname
)[0].kind
.in() << std::endl
;
121 std::cout
<< " name[1] = " << (* tname
)[1].id
.in() << " , "
122 << (* tname
)[1].kind
.in() << std::endl
;
124 // Find the application object by resolve_str.
126 obj
= root
->resolve_str(str_name
.in());
128 catch(const CosNaming::NamingContext::NotFound
&) {
129 std::cerr
<<"Couldn't resolve the string name: " << str_name
<< std::endl
;
133 ACE_CString
base_address (":");
134 base_address
+= ACE_TEXT_ALWAYS_CHAR (hostname
);
136 base_address
+= ACE_TEXT_ALWAYS_CHAR (port
);
137 ACE_CString
addr ("");
138 addr
= base_address
+ "/key/str";
140 // Create an URL string for application object.
141 CORBA::String_var address
= CORBA::string_dup (addr
.c_str());
143 std::cout
<< "call to_url(\"" << address
.in() << "\"" << std::endl
;
144 std::cout
<< " ,\"" << str_simple
.in() << "\")"<< std::endl
;
146 CORBA::String_var url_string
= root
->to_url(address
.in(), str_simple
.in());
148 std::cout
<< "to_url result: " << url_string
.in() << std::endl
;
151 // Write NS url to a file to let client read NS URL to get
152 // NamingContext reference.
153 CORBA::String_var ns_addr
= CORBA::string_dup(base_address
.c_str());
155 std::cout
<< "call to_url(\"" <<ns_addr
.in() << "\",\""
156 << str_simple
.in() << "\")"<< std::endl
;
158 CORBA::String_var url
= root
->to_url(ns_addr
.in(), str_simple
.in());
159 std::cout
<< "to_url result: " << url
.in() << std::endl
;
162 std::ofstream
iorFile(ACE_TEXT_ALWAYS_CHAR (ior_output_file
));
163 iorFile
<< url
.in() << std::endl
;
166 std::cout
<< "Naming Service URL written to file " << ior_output_file
<< std::endl
;
172 catch(const CORBA::Exception
& ex
) {
173 std::cerr
<< "Caught a CORBA::Exception: " << ex
<< std::endl
;