Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / DevGuideExamples / LocalObjects / ServantLocator / MessengerServer.cpp
blobb15baad88cbe3fd7c6e180dfa5a6385daa5744c5
1 #include "MessengerLocator_i.h"
2 #include "MessengerC.h"
4 #include "tao/AnyTypeCode/TypeCode.h"
5 #include <iostream>
6 #include <fstream>
7 #include "ace/Get_Opt.h"
9 const ACE_TCHAR *ior_output_file = ACE_TEXT ("Messenger.ior");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
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 '?':
25 default:
26 ACE_ERROR_RETURN ((LM_ERROR,
27 "usage: %s "
28 "-o <iorfile>"
29 "\n",
30 argv [0]),
31 -1);
33 // Indicates successful parsing of the command line
34 return 0;
37 int ACE_TMAIN (int argc, ACE_TCHAR *argv [])
39 try {
40 // Initialize the ORB
41 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
43 if (parse_args (argc, argv) != 0)
44 return 1;
46 // Get a reference to the POA
47 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
48 PortableServer::POA_var rootPOA = PortableServer::POA::_narrow (obj.in());
50 // Active the POA Manager
51 PortableServer::POAManager_var mgr = rootPOA->the_POAManager();
52 mgr->activate();
54 // Create the policies and assign them for the child POA
55 CORBA::PolicyList policies(3);
56 policies.length(3);
58 policies[0] = rootPOA->create_id_assignment_policy(PortableServer::USER_ID);
59 policies[1] = rootPOA->create_request_processing_policy(PortableServer::USE_SERVANT_MANAGER);
60 policies[2] = rootPOA->create_servant_retention_policy(PortableServer::NON_RETAIN);
62 // Create the POA with these policies
63 PortableServer::POA_var childPOA = rootPOA->create_POA("childPOA", mgr.in(), policies);
65 // Destroy the policy objects
66 for (CORBA::ULong i = 0; i != policies.length(); ++i) {
67 policies[i]->destroy();
70 // Create our Messenger's ServantLocator.
71 PortableServer::ServantLocator_var locator = new Messenger_Locator_i;
73 // Set the Servant Manager with the childPOA.
74 childPOA->set_servant_manager(locator.in());
76 // Get the object id for the user-created ID in the childPOA.
77 PortableServer::ObjectId_var child_oid = PortableServer::string_to_ObjectId("Messenger");
79 // Create the object without creating a servant.
80 CORBA::Object_var messenger_obj =
81 childPOA->create_reference_with_id(child_oid.in(), ::_tc_Messenger->id());
83 // Put the object reference into an IOR string
84 CORBA::String_var str = orb->object_to_string(messenger_obj.in());
86 // Write the IOR string to a file
87 std::ofstream iorFile(ACE_TEXT_ALWAYS_CHAR(ior_output_file)); // Throws exception if there's a problem.
88 iorFile << str.in();
89 iorFile.close();
91 std::cout << "IOR written to the file " << ACE_TEXT_ALWAYS_CHAR(ior_output_file) << std::endl;
93 // Accept requests from clients.
94 orb->run();
96 // Release resources
97 rootPOA->destroy(true,true);
98 orb->destroy();
100 catch(const CORBA::Exception& ex) {
101 std::cerr << "Server Caught a CORBA::Exception: " << ex << std::endl;
102 return 1;
105 return 0;