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
[])
51 CORBA::ORB_var orb
= CORBA::ORB_init(argc
, argv
);
53 if (parse_args (argc
, argv
) != 0)
56 //Get reference to Root POA
57 CORBA::Object_var obj
= orb
->resolve_initial_references("RootPOA");
58 PortableServer::POA_var poa
= PortableServer::POA::_narrow(obj
.in());
60 // Activate POA Manager
61 PortableServer::POAManager_var mgr
= poa
->the_POAManager();
64 // Find the Naming Service
65 obj
= orb
->resolve_initial_references("NameService");
66 CosNaming::NamingContextExt_var root
=
67 CosNaming::NamingContextExt::_narrow(obj
.in());
68 if (CORBA::is_nil(root
.in())) {
69 std::cerr
<< "Nil Naming Context reference" << std::endl
;
73 // Bind a new context.
76 name
[0].id
= CORBA::string_dup( "root.esc-dot" );
77 name
[0].kind
= CORBA::string_dup( "kind1" );
80 obj
= root
->resolve(name
);
82 catch(const CosNaming::NamingContext::NotFound
&) {
83 CosNaming::NamingContext_var dummy
= root
->bind_new_context(name
);
87 name
[1].id
= CORBA::string_dup( "leaf/esc-slash" );
88 name
[1].kind
= CORBA::string_dup( "kind2" );
91 PortableServer::Servant_var
<Messenger_i
> servant
= new Messenger_i
;
92 PortableServer::ObjectId_var oid
= poa
->activate_object(servant
.in());
93 CORBA::Object_var messenger_obj
= poa
->id_to_reference(oid
.in());
94 root
->rebind(name
, messenger_obj
.in());
96 // Also try rebinding to a simple path.
97 CosNaming::Name_var simp_name
= root
->to_name("Simple");
99 obj
= root
->resolve(simp_name
.in());
101 catch(const CosNaming::NamingContext::NotFound
&) {
102 CosNaming::NamingContext_var dummy
=
103 root
->bind_new_context(simp_name
.in());
105 simp_name
= root
->to_name("Simple/Messenger");
106 root
->rebind(simp_name
.in(), messenger_obj
.in());
108 // Convert Name to String Name.
109 CORBA::String_var str_name
= root
->to_string(name
);
110 std::cout
<< "str_name: " << str_name
.in() << std::endl
;
111 CORBA::String_var str_simple
= root
->to_string(simp_name
.in());
112 std::cout
<< "simple: " << str_simple
.in() << std::endl
;
114 // Convert String Name to Name.
115 CosNaming::Name_var tname
= root
->to_name(str_name
.in());
117 std::cout
<< "converted back to a CosNaming::Name: " << std::endl
;
118 std::cout
<< " name[0] = " << (* tname
)[0].id
.in() << " , "
119 << (* tname
)[0].kind
.in() << std::endl
;
120 std::cout
<< " name[1] = " << (* tname
)[1].id
.in() << " , "
121 << (* tname
)[1].kind
.in() << std::endl
;
123 // Find the application object by resolve_str.
125 obj
= root
->resolve_str(str_name
.in());
127 catch(const CosNaming::NamingContext::NotFound
&) {
128 std::cerr
<<"Couldn't resolve the string name: " << str_name
<< std::endl
;
132 ACE_CString
base_address (":");
133 base_address
+= ACE_TEXT_ALWAYS_CHAR (hostname
);
135 base_address
+= ACE_TEXT_ALWAYS_CHAR (port
);
136 ACE_CString
addr ("");
137 addr
= base_address
+ "/key/str";
139 // Create an URL string for application object.
140 CORBA::String_var address
= CORBA::string_dup (addr
.c_str());
142 std::cout
<< "call to_url(\"" << address
.in() << "\"" << std::endl
;
143 std::cout
<< " ,\"" << str_simple
.in() << "\")"<< std::endl
;
145 CORBA::String_var url_string
= root
->to_url(address
.in(), str_simple
.in());
147 std::cout
<< "to_url result: " << url_string
.in() << std::endl
;
150 // Write NS url to a file to let client read NS URL to get
151 // NamingContext reference.
152 CORBA::String_var ns_addr
= CORBA::string_dup(base_address
.c_str());
154 std::cout
<< "call to_url(\"" <<ns_addr
.in() << "\",\""
155 << str_simple
.in() << "\")"<< std::endl
;
157 CORBA::String_var url
= root
->to_url(ns_addr
.in(), str_simple
.in());
158 std::cout
<< "to_url result: " << url
.in() << std::endl
;
161 std::ofstream
iorFile(ACE_TEXT_ALWAYS_CHAR (ior_output_file
));
162 iorFile
<< url
.in() << std::endl
;
165 std::cout
<< "Naming Service URL written to file " << ior_output_file
<< std::endl
;
171 catch(const CORBA::Exception
& ex
) {
172 std::cerr
<< "Caught a CORBA::Exception: " << ex
<< std::endl
;