Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / DevGuideExamples / Messaging / AMIcallback / MessengerServer.cpp
blobbecda190ed24851b9c0d21fabc11b2519adf51fe
1 #include "Messenger_i.h"
2 #include <fstream>
3 #include <iostream>
4 #include <fstream>
5 #include "ace/Get_Opt.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT ("MessengerServer.ior");
8 unsigned int seconds_to_wait = 0;
9 CORBA::Boolean servant_throws_exception = false;
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:e:t:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'o':
21 ior_output_file = get_opts.opt_arg ();
22 break;
23 case 'e':
24 servant_throws_exception = true;
25 std::cout << "Messenger_i::send_message() will throw an exception." << std::endl;
26 break;
27 case 't':
28 seconds_to_wait = ACE_OS::atoi(get_opts.opt_arg ());
29 std::cout << "Messenger_i::send_message() will wait "
30 << seconds_to_wait << " seconds" << std::endl;
31 break;
32 case '?':
33 default:
34 ACE_ERROR_RETURN ((LM_ERROR,
35 "usage: %s"
36 " -o <iorfile>"
37 " -e"
38 " -t <seconds_to_wait>"
39 "\n",
40 argv [0]),
41 -1);
44 // Indicates successful parsing of the command line
45 return 0;
48 int
49 ACE_TMAIN (int argc, ACE_TCHAR *argv [])
51 try {
52 // Initialize orb
53 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
55 if (parse_args (argc, argv) != 0)
56 return 1;
58 // Get reference to Root POA.
59 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
60 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in());
62 // Activate POA manager
63 PortableServer::POAManager_var mgr = poa->the_POAManager();
64 mgr->activate();
66 // Create an object
67 PortableServer::Servant_var<Messenger_i> servant = new
68 Messenger_i(seconds_to_wait, servant_throws_exception);
70 // Write its stringified reference to stdout
71 PortableServer::ObjectId_var oid = poa->activate_object(servant.in());
72 obj = poa->id_to_reference(oid.in());
73 Messenger_var messenger = Messenger::_narrow(obj.in());
74 CORBA::String_var str = orb->object_to_string(messenger.in());
75 std::ofstream fout(ACE_TEXT_ALWAYS_CHAR(ior_output_file));
76 fout << str.in() << std::endl;
77 fout.close();
78 std::cout << "IOR written to file " << ACE_TEXT_ALWAYS_CHAR(ior_output_file) << std::endl;
80 // Accept requests
81 orb->run();
82 orb->destroy();
84 catch(const CORBA::Exception& ex) {
85 std::cerr << "Caught a CORBA::Exception: " << ex << std::endl;
86 return 1;
89 return 0;