Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / DevGuideExamples / Messaging / AMIcallback / MessengerClient.cpp
blob400e8f9419936e534d0b885cbff26aba05e352db
1 #include "MessengerC.h"
2 #include "MessengerHandler.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/OS_NS_sys_time.h"
7 #include <iostream>
8 #include "ace/Get_Opt.h"
10 const ACE_TCHAR *ior = ACE_TEXT ("file://MessengerServer.ior");
11 bool automated = false;
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:a:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'k':
23 ior = get_opts.opt_arg ();
24 break;
25 case 'a':
26 automated = !(ACE_OS::atoi(get_opts.opt_arg ()) == 0);
27 break;
28 case '?':
29 default:
30 ACE_ERROR_RETURN ((LM_ERROR,
31 "usage: %s"
32 " -k <ior>"
33 " -a <automated>"
34 "\n",
35 argv [0]),
36 -1);
38 // Indicates successful parsing of the command line
39 return 0;
42 int
43 ACE_TMAIN (int argc, ACE_TCHAR *argv [])
45 try {
46 // Initialize orb
47 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
49 if (parse_args (argc, argv) != 0)
50 return 1;
52 CORBA::Object_var obj = orb->string_to_object(ior);
53 if (CORBA::is_nil(obj.in())) {
54 std::cerr << "Nil Messenger reference" << std::endl;
55 return 1;
58 // Narrow
59 Messenger_var messenger = Messenger::_narrow(obj.in());
60 if (CORBA::is_nil(messenger.in())) {
61 std::cerr << "Argument is not a Messenger reference" << std::endl;
62 return 1;
65 // Get reference to Root POA.
66 obj = orb->resolve_initial_references("RootPOA");
67 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in());
69 // Activate POA manager
70 PortableServer::POAManager_var mgr = poa->the_POAManager();
71 mgr->activate();
73 // Register an AMI handler for the Messenger interface
74 PortableServer::Servant_var<MessengerHandler> servant =
75 new MessengerHandler(orb.in());
76 PortableServer::ObjectId_var oid = poa->activate_object(servant.in());
77 obj = poa->id_to_reference(oid.in());
78 AMI_MessengerHandler_var handler = AMI_MessengerHandler::_narrow(obj.in());
80 CORBA::String_var user = CORBA::string_alloc(81);
81 CORBA::String_var subject = CORBA::string_alloc(81);
82 CORBA::String_var message = CORBA::string_alloc(81);
84 if (! automated) {
85 std::cout << "Enter user name -->";
86 std::cin.getline(user, 81);
88 std::cout << "Enter subject -->";
89 std::cin.getline(subject, 81);
91 std::cout << "Enter message -->";
92 std::cin.getline(message, 81);
93 } else {
94 user = CORBA::string_dup("TestUser");
95 subject = CORBA::string_dup("TestSubject");
96 message = CORBA::string_dup("Have a nice day.");
99 // Record the time the request was made.
100 ACE_Time_Value time_sent = ACE_OS::gettimeofday();
102 messenger->sendc_send_message(handler.in(), user.in(), subject.in(), message.in());
104 // Do some work to prove that we can send the message asynchronously, do some work,
105 // then come back later and retrieve the results.
106 for (int i = 0; i < 10; ++i) {
107 ACE_OS::printf(".");
108 ACE_OS::sleep(ACE_Time_Value(0, 10 * 1000));
111 // Our simple servant will exit as soon as it receives the results.
112 orb->run();
114 if (servant->message_was_sent())
116 // Note : We can't use the
117 ACE_Time_Value delay = ACE_OS::gettimeofday() - time_sent;
118 std::cout << std::endl << "Reply Delay = " << delay.msec() << "ms" << std::endl;
121 orb->destroy();
123 catch(const CORBA::Exception& ex) {
124 std::cerr << "Caught a CORBA::Exception: " << ex << std::endl;
125 return 1;
128 return 0;