Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / NamingService / Naming_Context_Ext / MessengerServer.cpp
blob9436c6ad8e6c30ef3583f504c4e31f09b2c65154
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[])
50 try {
51 // Initialize orb
52 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
54 if (parse_args (argc, argv) != 0)
55 return 1;
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();
63 mgr->activate();
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;
71 return 1;
74 // Bind a new context.
75 CosNaming::Name name;
76 name.length( 1 );
77 name[0].id = CORBA::string_dup( "root.esc-dot" );
78 name[0].kind = CORBA::string_dup( "kind1" );
80 try {
81 obj = root->resolve(name);
83 catch(const CosNaming::NamingContext::NotFound&) {
84 CosNaming::NamingContext_var dummy = root->bind_new_context(name);
87 name.length( 2 );
88 name[1].id = CORBA::string_dup( "leaf/esc-slash" );
89 name[1].kind = CORBA::string_dup( "kind2" );
91 // Create an object
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");
99 try {
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.
125 try {
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;
130 return 1;
133 ACE_CString base_address (":");
134 base_address += ACE_TEXT_ALWAYS_CHAR (hostname);
135 base_address += ":";
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;
164 iorFile.close();
166 std::cout << "Naming Service URL written to file " << ior_output_file << std::endl;
168 // Accept requests
169 orb->run();
170 orb->destroy();
172 catch(const CORBA::Exception& ex) {
173 std::cerr << "Caught a CORBA::Exception: " << ex << std::endl;
174 return 1;
177 return 0;