Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / NamingService / Naming_Context_Ext / MessengerServer.cpp
blobe474955373f230886d17da9693478fd2e8f580d7
1 #include "Messenger_i.h"
2 #include "orbsvcs/CosNamingC.h"
3 #include <iostream>
4 #include <fstream>
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");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:h:p:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'o':
21 ior_output_file = get_opts.opt_arg ();
22 break;
24 case 'h':
25 hostname = get_opts.opt_arg ();
26 break;
28 case 'p':
29 port = get_opts.opt_arg ();
30 break;
32 case '?':
33 default:
34 ACE_ERROR_RETURN ((LM_ERROR,
35 "usage: %s "
36 "-o <iorfile> "
37 "-h <host> "
38 "-p <port>"
39 "\n",
40 argv [0]),
41 -1);
43 // Indicates successful parsing of the command line
44 return 0;
47 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
49 try {
50 // Initialize orb
51 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
53 if (parse_args (argc, argv) != 0)
54 return 1;
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();
62 mgr->activate();
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;
70 return 1;
73 // Bind a new context.
74 CosNaming::Name name;
75 name.length( 1 );
76 name[0].id = CORBA::string_dup( "root.esc-dot" );
77 name[0].kind = CORBA::string_dup( "kind1" );
79 try {
80 obj = root->resolve(name);
82 catch(const CosNaming::NamingContext::NotFound&) {
83 CosNaming::NamingContext_var dummy = root->bind_new_context(name);
86 name.length( 2 );
87 name[1].id = CORBA::string_dup( "leaf/esc-slash" );
88 name[1].kind = CORBA::string_dup( "kind2" );
90 // Create an object
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");
98 try {
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.
124 try {
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;
129 return 1;
132 ACE_CString base_address (":");
133 base_address += ACE_TEXT_ALWAYS_CHAR (hostname);
134 base_address += ":";
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;
163 iorFile.close();
165 std::cout << "Naming Service URL written to file " << ior_output_file << std::endl;
167 // Accept requests
168 orb->run();
169 orb->destroy();
171 catch(const CORBA::Exception& ex) {
172 std::cerr << "Caught a CORBA::Exception: " << ex << std::endl;
173 return 1;
176 return 0;