Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / NotifyService / SupplierSideNC / MessengerServer.cpp
blob14e332e2672662b61e3d6683745d1a2bfed1cd6e
1 #include "orbsvcs/CosNamingC.h"
2 #include "Messenger_i.h"
3 #include <iostream>
4 #include <fstream>
5 #include "ace/Get_Opt.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT ("Messenger.ior");
9 int
10 parse_args (int argc, ACE_TCHAR *argv[])
12 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
13 int c;
15 while ((c = get_opts ()) != -1)
16 switch (c)
18 case 'o':
19 ior_output_file = get_opts.opt_arg ();
20 break;
22 case '?':
23 default:
26 // Indicates successful parsing of the command line
27 return 0;
30 int
31 ACE_TMAIN (int argc, ACE_TCHAR *argv [])
33 try
35 // Initialize orb
36 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
38 if (parse_args (argc, argv) != 0)
39 return 1;
41 // Find the Naming Service.
42 CORBA::Object_var rootObj = orb->resolve_initial_references("NameService");
43 CosNaming::NamingContext_var rootNC =
44 CosNaming::NamingContext::_narrow(rootObj.in());
46 // Get the Root POA.
47 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
48 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in());
50 // Activate POA manager
51 PortableServer::POAManager_var mgr = poa->the_POAManager();
52 mgr->activate();
54 // Create our Messenger servant.
55 PortableServer::Servant_var<Messenger_i> messenger_servant =
56 new Messenger_i(orb.in());
58 // Register it with the RootPOA.
59 PortableServer::ObjectId_var oid =
60 poa->activate_object( messenger_servant.in() );
61 CORBA::Object_var messenger_obj = poa->id_to_reference( oid.in() );
63 // Bind it in the Naming Service.
64 CosNaming::Name name;
65 name.length (1);
66 name[0].id = CORBA::string_dup("MessengerService");
67 rootNC->rebind(name, messenger_obj.in());
69 CORBA::String_var str = orb->object_to_string (messenger_obj.in());
70 std::ofstream iorFile (ACE_TEXT_ALWAYS_CHAR(ior_output_file));
71 iorFile << str.in () << std::endl;
72 iorFile.close ();
73 std::cout << "IOR written to file " << ior_output_file << std::endl;
75 // Accept requests
76 orb->run();
77 orb->destroy();
80 catch(const CORBA::Exception& ex) {
81 std::cerr << ex << std::endl;
82 return 1;
84 return 0;