Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / DevGuideExamples / Messaging / RelativeRoundtripTimeout / MessengerServer.cpp
blobe0c8900a624790f64708f5c7f0236d285815461b
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 ("MessengerServer.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;
34 int
35 ACE_TMAIN (int argc, ACE_TCHAR *argv [])
37 try {
38 // Initialize orb
39 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
41 if (parse_args (argc, argv) != 0)
42 return 1;
44 // Get reference to Root POA.
45 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
46 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in());
48 // Activate POA manager
49 PortableServer::POAManager_var mgr = poa->the_POAManager();
50 mgr->activate();
52 // Create an object
53 PortableServer::Servant_var<Messenger_i> messenger_servant =
54 new Messenger_i;
56 // Write its stringified reference to a file
57 PortableServer::ObjectId_var oid = poa->activate_object(messenger_servant.in());
58 obj = poa->id_to_reference(oid.in());
59 Messenger_var messenger = Messenger::_narrow(obj.in());
60 CORBA::String_var str = orb->object_to_string(messenger.in());
61 std::ofstream fout(ACE_TEXT_ALWAYS_CHAR(ior_output_file));
62 fout << str.in() << std::endl;
63 fout.close();
64 std::cout << "IOR written to file " << ACE_TEXT_ALWAYS_CHAR(ior_output_file) << std::endl;
66 // Accept requests
67 orb->run();
68 orb->destroy();
70 catch(const CORBA::Exception& ex) {
71 std::cerr << "CORBA::Exception " << ex << std::endl;
72 return 1;
75 return 0;