Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / DevGuideExamples / PortableInterceptors / PICurrent / MessengerServer.cpp
blobff6ef3abad83e3fa0c81a75f13575986ca82e621
1 #include "Messenger_i.h"
2 #include "MessengerS.h"
3 #include "ServerInitializer.h"
5 #include "tao/ORBInitializer_Registry.h"
6 // Ensure that the PI_Server library is linked in when building statically
7 #include "tao/PI_Server/PI_Server.h"
8 #include <iostream>
9 #include <fstream>
10 #include <fstream>
11 #include "ace/Get_Opt.h"
13 const ACE_TCHAR *ior_output_file = ACE_TEXT ("Messenger.ior");
15 int
16 parse_args (int argc, ACE_TCHAR *argv[])
18 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
19 int c;
21 while ((c = get_opts ()) != -1)
22 switch (c)
24 case 'o':
25 ior_output_file = get_opts.opt_arg ();
26 break;
28 case '?':
29 default:
30 ACE_ERROR_RETURN ((LM_ERROR,
31 "usage: %s "
32 "-o <iorfile>"
33 "\n",
34 argv [0]),
35 -1);
37 // Indicates successful parsing of the command line
38 return 0;
41 int
42 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
44 try
46 PortableInterceptor::ORBInitializer_var orb_initializer =
47 new ServerInitializer;
49 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
51 CORBA::ORB_var orb =
52 CORBA::ORB_init (argc, argv, "Server ORB");
54 if (parse_args (argc, argv) != 0)
55 return 1;
57 //Get reference to Root POA
58 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
59 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
61 // Activate POA Manager
62 PortableServer::POAManager_var mgr = poa->the_POAManager();
63 mgr->activate();
65 // Create an object
66 PortableServer::Servant_var<Messenger_i> messenger_servant = new Messenger_i;
68 PortableServer::ObjectId_var oid =
69 poa->activate_object( messenger_servant.in() );
70 CORBA::Object_var messenger_obj = poa->id_to_reference( oid.in() );
72 CORBA::String_var str = orb->object_to_string( messenger_obj.in() );
73 std::ofstream iorFile ( ACE_TEXT_ALWAYS_CHAR(ior_output_file) );
74 iorFile << str.in() << std::endl;
75 iorFile.close();
76 std::cout << std::endl << "IOR written to file " << ACE_TEXT_ALWAYS_CHAR(ior_output_file) << std::endl;
78 // Accept requests
79 orb->run();
80 orb->destroy();
83 catch(const CORBA::Exception& ex)
85 std::cerr << "server Caught CORBA exception: " << ex << std::endl;
86 return 1;
89 return 0;