Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / DevGuideExamples / PortableInterceptors / IOR / MessengerServer.cpp
blob32624452d6e9dc03032e6b64e4af65a201b357b4
1 #include "Messenger_i.h"
2 #include "MessengerC.h"
3 #include "ServerInitializer.h"
4 #include "tao/ORBInitializer_Registry.h"
5 // Ensure that the PI_Server library is linked in when building statically
6 #include "tao/PI_Server/PI_Server.h"
7 #include <iostream>
8 #include <fstream>
9 #include "ace/Get_Opt.h"
11 const ACE_TCHAR *ior_output_file = ACE_TEXT ("Messenger.ior");
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'o':
23 ior_output_file = get_opts.opt_arg ();
24 break;
26 case '?':
27 default:
28 ACE_ERROR_RETURN ((LM_ERROR,
29 "usage: %s "
30 "-o <iorfile>"
31 "\n",
32 argv [0]),
33 -1);
35 // Indicates successful parsing of the command line
36 return 0;
39 int
40 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
42 try {
43 PortableInterceptor::ORBInitializer_var orb_initializer =
44 new ServerInitializer;
46 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
48 // Initialize orb
49 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
51 if (parse_args (argc, argv) != 0)
52 return 1;
54 //Get reference to Root POA
55 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
56 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
58 // Activate POA Manager
59 PortableServer::POAManager_var mgr = poa->the_POAManager();
60 mgr->activate();
62 // Create an object
63 PortableServer::Servant_var<Messenger_i> messenger_servant = new Messenger_i;
65 // Register the servant with the RootPOA, obtain its object
66 // reference, stringify it, and write it to a file.
67 PortableServer::ObjectId_var oid =
68 poa->activate_object( messenger_servant.in() );
69 CORBA::Object_var messenger_obj = poa->id_to_reference( oid.in() );
70 CORBA::String_var str = orb->object_to_string( messenger_obj.in() );
71 std::ofstream iorFile( ACE_TEXT_ALWAYS_CHAR(ior_output_file) );
72 iorFile << str.in() << std::endl;
73 iorFile.close();
74 std::cout << "IOR written to file " << ACE_TEXT_ALWAYS_CHAR(ior_output_file) << std::endl;
76 // Accept requests
77 orb->run();
78 orb->destroy();
81 catch(const CORBA::Exception& ex)
83 std::cerr << "Exception in MessengerServer: " << ex << std::endl;
84 return 1;
87 return 0;