Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / DevGuideExamples / Messaging / RelativeRoundtripTimeout / MessengerClient.cpp
blob66361fec627ec97a12c1373f53c362a61050eef5
1 #include "MessengerC.h"
2 #include "tao/TimeBaseC.h"
3 #include "tao/Messaging/Messaging.h"
4 #include <iostream>
5 #include "ace/Get_Opt.h"
7 const ACE_TCHAR *ior = ACE_TEXT ("file://MessengerServer.ior");
8 long msecs = 0; // the RelativeRoundtripTimeout value
10 int
11 parse_args (int argc, ACE_TCHAR *argv[])
13 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:t:"));
14 int c;
16 while ((c = get_opts ()) != -1)
17 switch (c)
19 case 'k':
20 ior = get_opts.opt_arg ();
21 break;
22 case 't':
23 msecs = ACE_OS::atoi(get_opts.opt_arg ());
24 break;
25 case '?':
26 default:
27 ACE_ERROR_RETURN ((LM_ERROR,
28 "usage: %s "
29 "-k <ior> "
30 "-t <msec> "
31 "\n",
32 argv [0]),
33 -1);
35 // Indicates successful parsing of the command line
36 return 0;
39 int ACE_TMAIN(int argc, ACE_TCHAR * argv[])
41 try {
42 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
44 if (parse_args (argc, argv) != 0)
45 return 1;
47 CORBA::Object_var obj = orb->string_to_object(ior);
48 if (CORBA::is_nil(obj.in())) {
49 std::cerr << "Nil Messenger reference" << std::endl;
50 return 1;
52 Messenger_var messenger = Messenger::_narrow(obj.in());
53 ACE_ASSERT(! CORBA::is_nil(messenger.in()));
55 obj = orb->resolve_initial_references("ORBPolicyManager");
56 CORBA::PolicyManager_var policy_manager = CORBA::PolicyManager::_narrow(obj.in());
57 ACE_ASSERT(! CORBA::is_nil(policy_manager.in()));
58 obj = orb->resolve_initial_references("PolicyCurrent");
59 CORBA::PolicyCurrent_var policy_current = CORBA::PolicyCurrent::_narrow (obj.in());
60 ACE_ASSERT(! CORBA::is_nil(policy_current.in()));
62 // TimeT is in 10ths of a microsecond (10000 == 1.0e-3 * 1.0e7).
63 if (msecs <= 0) {
64 std::cout << "Enter relative roundtrip timeout in milliseconds -->";
65 std::cin >> msecs;
67 TimeBase::TimeT relative_rt_timeout = msecs * 10000;
69 CORBA::Any relative_rt_timeout_as_any;
70 relative_rt_timeout_as_any <<= relative_rt_timeout;
71 CORBA::PolicyList policy_list(1);
72 policy_list.length(1);
73 policy_list[0] =
74 orb->create_policy(Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
75 relative_rt_timeout_as_any);
77 // We demonstrate setting the policy at each of three levels. In practice,
78 // you would only need to set the policy for one of these.
79 // Set timeouts for the whole orb.
80 policy_manager->set_policy_overrides(policy_list, CORBA::ADD_OVERRIDE);
81 // Set timeouts for the current thread.
82 policy_current->set_policy_overrides(policy_list, CORBA::ADD_OVERRIDE);
83 // Set timeouts for the messenger object.
84 obj = messenger->_set_policy_overrides(policy_list, CORBA::SET_OVERRIDE);
85 messenger = Messenger::_narrow(obj.in());
86 ACE_ASSERT(! CORBA::is_nil(messenger.in()));
88 policy_list[0]->destroy();
89 policy_list[0] = CORBA::Policy::_nil();
91 CORBA::String_var message = CORBA::string_dup("Hello!");
93 try {
94 messenger->send_message ("TAO User", "TAO Test", message.inout());
95 std::cerr << "ERROR: Message did not timeout." << std::endl;
96 } catch(const CORBA::TIMEOUT&) {
97 std::cout << "Message Timed out as expected." << std::endl;
100 catch(const CORBA::Exception& ex) {
101 std::cerr << "Caught a CORBA exception: " << ex << std::endl;
102 return 1;
105 return 0;