int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { try { // Initialize orb CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); if (parse_args (argc, argv) != 0) return 1; //Get reference to Root POA CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in()); // Activate POA Manager PortableServer::POAManager_var mgr = poa->the_POAManager(); mgr->activate(); // Find the Naming Service obj = orb->resolve_initial_references("NameService"); CosNaming::NamingContextExt_var root = CosNaming::NamingContextExt::_narrow(obj.in()); if (CORBA::is_nil(root.in())) { std::cerr << "Nil Naming Context reference" << std::endl; return 1; } // Bind a new context. CosNaming::Name name; name.length( 1 ); name[0].id = CORBA::string_dup( "root.esc-dot" ); name[0].kind = CORBA::string_dup( "kind1" ); try { obj = root->resolve(name); } catch(const CosNaming::NamingContext::NotFound&) { CosNaming::NamingContext_var dummy = root->bind_new_context(name); } name.length( 2 ); name[1].id = CORBA::string_dup( "leaf/esc-slash" ); name[1].kind = CORBA::string_dup( "kind2" ); // Create an object PortableServer::Servant_var<Messenger_i> servant = new Messenger_i; PortableServer::ObjectId_var oid = poa->activate_object(servant.in()); CORBA::Object_var messenger_obj = poa->id_to_reference(oid.in()); root->rebind(name, messenger_obj.in()); // Also try rebinding to a simple path. CosNaming::Name_var simp_name = root->to_name("Simple"); try { obj = root->resolve(simp_name.in()); } catch(const CosNaming::NamingContext::NotFound&) { CosNaming::NamingContext_var dummy = root->bind_new_context(simp_name.in()); } simp_name = root->to_name("Simple/Messenger"); root->rebind(simp_name.in(), messenger_obj.in()); // Convert Name to String Name. CORBA::String_var str_name = root->to_string(name); std::cout << "str_name: " << str_name.in() << std::endl; CORBA::String_var str_simple = root->to_string(simp_name.in()); std::cout << "simple: " << str_simple.in() << std::endl; // Convert String Name to Name. CosNaming::Name_var tname = root->to_name(str_name.in()); std::cout << "converted back to a CosNaming::Name: " << std::endl; std::cout << " name[0] = " << (* tname)[0].id.in() << " , " << (* tname)[0].kind.in() << std::endl; std::cout << " name[1] = " << (* tname)[1].id.in() << " , " << (* tname)[1].kind.in() << std::endl; // Find the application object by resolve_str. try { obj = root->resolve_str(str_name.in()); } catch(const CosNaming::NamingContext::NotFound&) { std::cerr<<"Couldn't resolve the string name: " << str_name << std::endl; return 1; } ACE_CString base_address (":"); base_address += ACE_TEXT_ALWAYS_CHAR (hostname); base_address += ":"; base_address += ACE_TEXT_ALWAYS_CHAR (port); ACE_CString addr (""); addr = base_address + "/key/str"; // Create an URL string for application object. CORBA::String_var address = CORBA::string_dup (addr.c_str()); std::cout << "call to_url(\"" << address.in() << "\"" << std::endl; std::cout << " ,\"" << str_simple.in() << "\")"<< std::endl; CORBA::String_var url_string = root->to_url(address.in(), str_simple.in()); std::cout << "to_url result: " << url_string.in() << std::endl; // Write NS url to a file to let client read NS URL to get // NamingContext reference. CORBA::String_var ns_addr = CORBA::string_dup(base_address.c_str()); std::cout << "call to_url(\"" <<ns_addr.in() << "\",\"" << str_simple.in() << "\")"<< std::endl; CORBA::String_var url = root->to_url(ns_addr.in(), str_simple.in()); std::cout << "to_url result: " << url.in() << std::endl; std::ofstream iorFile(ACE_TEXT_ALWAYS_CHAR (ior_output_file)); iorFile << url.in() << std::endl; iorFile.close(); std::cout << "Naming Service URL written to file " << ior_output_file << std::endl; // Accept requests orb->run(); orb->destroy(); } catch(const CORBA::Exception& ex) { std::cerr << "Caught a CORBA::Exception: " << ex << std::endl; return 1; } return 0; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { try { // Initialize orb CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); // Find the Naming Service CORBA::Object_var naming_obj = orb->resolve_initial_references("NameService"); CosNaming::NamingContextExt_var root = CosNaming::NamingContextExt::_narrow(naming_obj.in()); if (CORBA::is_nil(root.in())) { std::cerr << "Nil Naming Context reference" << std::endl; return 1; } // Resolve the Messenger object CosNaming::Name name; name.length( 2 ); name[0].id = CORBA::string_dup( "example" ); name[1].id = CORBA::string_dup( "Messenger" ); CORBA::Object_var obj = CORBA::Object::_nil(); while (CORBA::is_nil(obj.in())) { try { obj = root->resolve_str("example/Messenger"); } catch (const CosNaming::NamingContext::NotFound&) { // Sleep for a second and try again ACE_OS::sleep(1); } } // Narrow the Messenger object reference Messenger_var messenger = Messenger::_narrow(obj.in()); if (CORBA::is_nil(messenger.in())) { std::cerr << "Not a Messenger reference" << std::endl; return 1; } CORBA::String_var message = CORBA::string_dup("Hello!"); // Send a message messenger->send_message("TAO User", "TAO Test", message.inout()); std::cout << "Message was sent" << std::endl; orb->destroy(); } catch(const CORBA::Exception& ex) { std::cerr << "Caught a CORBA exception: " << ex << std::endl; return 1; } return 0; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { try { // Initialize orb CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); if (parse_args (argc, argv) != 0) return 1; // Find the Naming Service & the Message Server name in it CORBA::Object_var obj = orb->string_to_object(ior_input_file); // Narrow Messenger_var messenger = Messenger::_narrow(obj.in()); if (CORBA::is_nil(messenger.in())) { std::cerr << "Not a Messenger reference" << std::endl; return 1; } CORBA::String_var message = CORBA::string_dup("Hello!"); messenger->send_message( "TAO User", "TAO Test", message.inout()); std::cout << "Message was sent" << std::endl; std::cout << "Now try the same thing with the simple name." << std::endl; obj = orb->resolve_initial_references("NameService"); CosNaming::NamingContextExt_var root = CosNaming::NamingContextExt::_narrow(obj.in()); obj = root->resolve_str("Simple/Messenger"); messenger = Messenger::_narrow(obj.in()); messenger->send_message( "ACE User", "TAO Test", message.inout()); std::cout << "Message was sent" << std::endl; } catch(const CORBA::Exception& ex) { std::cerr << "Caught a CORBA::Exception: " << ex << std::endl; return 1; } return 0; }
int main(int argc, char **argv) { // Removes ORB related arguments CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); // Program options std::string register_servant; std::string proxy_reference; _po::options_description _desc("Proxy Server options"); _desc.add_options() ("help,h", "produce help message") ("register-servant,r", _po::value< std::string >(®ister_servant), "register the servant into the name service") ("proxy-reference,p", _po::value< std::string >(&proxy_reference), "forward received calls to another servant"); _po::variables_map _vm; _po::store(_po::parse_command_line(argc, argv, _desc), _vm); _po::notify(_vm); if (_vm.count("help")) { std::cout << _desc << std::endl; return 0; } bool reference_is_ns_entry = false; // TODO // Name Service CosNaming::NamingContextExt_var nc; if (reference_is_ns_entry || !register_servant.empty()) { try { CORBA::Object_var ncObj = orb->resolve_initial_references("NameService"); nc = CosNaming::NamingContextExt::_narrow(ncObj); } catch(...) { } if (CORBA::is_nil(nc)) std::cerr << "Name service unavailable!" << std::endl; } // Proxy reference prueba::Iface_var reference; if (!proxy_reference.empty()) { CORBA::Object_var refObj; if (reference_is_ns_entry && !CORBA::is_nil(nc)) refObj = nc->resolve_str(proxy_reference.c_str()); else refObj = orb->string_to_object(proxy_reference.c_str()); reference = prueba::Iface::_narrow(refObj); } // Servant prueba_Iface_impl _impl(reference.in()); CORBA::Object_var rootPOAObj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(rootPOAObj.in()); PortableServer::POAManager_var manager = rootPOA->the_POAManager(); PortableServer::ObjectId_var myObjID = rootPOA->activate_object(&_impl); CORBA::Object_var obj = rootPOA->servant_to_reference(&_impl); // Displaying reference CORBA::String_var ref = orb->object_to_string(obj); std::cout << ref << std::endl; // Registring servant CosNaming::Name_var name; if (!register_servant.empty() && !CORBA::is_nil(nc)) { name = nc->to_name(register_servant.c_str()); nc->rebind(name, obj); } /*PROTECTED REGION ID(prueba_Iface_impl_server::___main) ENABLED START*/ /*PROTECTED REGION END*/ // Running manager->activate(); orb->run(); // Unbinding servant if (!CORBA::is_nil(nc) && name) nc->unbind(name); return 0; }