Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / ImplRepo / Basic / MessengerServer.cpp
blobc3b588acd63ac5b699a609978b498606b3752f14
1 #include "Messenger_i.h"
2 #include <iostream>
3 #include <fstream>
4 #include "ace/Get_Opt.h"
6 const ACE_TCHAR *ior_output_file = ACE_TEXT ("messenger.ior");
8 int
9 parse_args (int argc, ACE_TCHAR *argv[])
11 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
12 int c;
14 while ((c = get_opts ()) != -1)
15 switch (c)
17 case 'o':
18 ior_output_file = get_opts.opt_arg ();
19 break;
21 case '?':
22 default:
23 ACE_ERROR_RETURN ((LM_ERROR,
24 "usage: %s "
25 "-o <iorfile>"
26 "\n",
27 argv [0]),
28 -1);
30 // Indicates successful parsing of the command line
31 return 0;
35 PortableServer::POA_ptr
36 createPersistentPOA(PortableServer::POA_ptr root_poa,
37 const char* poa_name)
39 CORBA::PolicyList policies;
40 policies.length(2);
42 policies[0] = root_poa->create_lifespan_policy(PortableServer::PERSISTENT);
43 policies[1] = root_poa->create_id_assignment_policy(PortableServer::USER_ID);
45 PortableServer::POAManager_var mgr = root_poa->the_POAManager();
46 PortableServer::POA_var poa =
47 root_poa->create_POA(poa_name, mgr.in(), policies);
49 policies[0]->destroy();
50 policies[1]->destroy();
52 return poa._retn();
55 int
56 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
58 try {
59 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
61 if (parse_args (argc, argv) != 0)
62 return 1;
64 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
65 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj.in());
67 PortableServer::POAManager_var mgr = root_poa->the_POAManager();
69 const char* poa_name = "MessengerService";
71 PortableServer::POA_var poa = createPersistentPOA(root_poa.in(), poa_name);
73 PortableServer::Servant_var<Messenger_i> servant = new Messenger_i;
75 PortableServer::ObjectId_var object_id =
76 PortableServer::string_to_ObjectId("object");
78 poa->activate_object_with_id(object_id.in(), servant.in());
80 obj = poa->id_to_reference(object_id.in());
81 CORBA::String_var ior = orb->object_to_string(obj.in());
83 // If the ior_output_file exists, output the ior to it
84 if (ior_output_file != 0)
86 FILE *output_file= ACE_OS::fopen (ACE_TEXT_ALWAYS_CHAR(ior_output_file), "w");
87 if (output_file == 0)
88 ACE_ERROR_RETURN ((LM_ERROR,
89 "Cannot open output file for writing IOR: %s",
90 ior_output_file),
91 1);
92 ACE_OS::fprintf (output_file, "%s", ior.in ());
93 ACE_OS::fclose (output_file);
96 mgr->activate();
98 std::cout << "Messenger server ready." << std::endl;
100 orb->run();
102 std::cout << "Messenger server shutting down." << std::endl;
104 root_poa->destroy(1,1);
105 orb->destroy();
107 return 0;
108 } catch(const CORBA::Exception& ex) {
109 std::cerr << "Server main() Caught Exception" << ex << std::endl;
111 return 1;