Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / DevGuideExamples / Messaging / RelativeRoundtripTimeout / MessengerClient.cpp
bloba47d0443e66da02dd61634ef73186408263aed9f
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 {
43 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
45 if (parse_args (argc, argv) != 0)
46 return 1;
48 CORBA::Object_var obj = orb->string_to_object(ior);
49 if (CORBA::is_nil(obj.in())) {
50 std::cerr << "Nil Messenger reference" << std::endl;
51 return 1;
53 Messenger_var messenger = Messenger::_narrow(obj.in());
54 ACE_ASSERT(! CORBA::is_nil(messenger.in()));
56 obj = orb->resolve_initial_references("ORBPolicyManager");
57 CORBA::PolicyManager_var policy_manager = CORBA::PolicyManager::_narrow(obj.in());
58 ACE_ASSERT(! CORBA::is_nil(policy_manager.in()));
59 obj = orb->resolve_initial_references("PolicyCurrent");
60 CORBA::PolicyCurrent_var policy_current = CORBA::PolicyCurrent::_narrow (obj.in());
61 ACE_ASSERT(! CORBA::is_nil(policy_current.in()));
63 // TimeT is in 10ths of a microsecond (10000 == 1.0e-3 * 1.0e7).
64 if (msecs <= 0) {
65 std::cout << "Enter relative roundtrip timeout in milliseconds -->";
66 std::cin >> msecs;
68 TimeBase::TimeT relative_rt_timeout = msecs * 10000;
70 CORBA::Any relative_rt_timeout_as_any;
71 relative_rt_timeout_as_any <<= relative_rt_timeout;
72 CORBA::PolicyList policy_list(1);
73 policy_list.length(1);
74 policy_list[0] =
75 orb->create_policy(Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
76 relative_rt_timeout_as_any);
78 // We demonstrate setting the policy at each of three levels. In practice,
79 // you would only need to set the policy for one of these.
80 // Set timeouts for the whole orb.
81 policy_manager->set_policy_overrides(policy_list, CORBA::ADD_OVERRIDE);
82 // Set timeouts for the current thread.
83 policy_current->set_policy_overrides(policy_list, CORBA::ADD_OVERRIDE);
84 // Set timeouts for the messenger object.
85 obj = messenger->_set_policy_overrides(policy_list, CORBA::SET_OVERRIDE);
86 messenger = Messenger::_narrow(obj.in());
87 ACE_ASSERT(! CORBA::is_nil(messenger.in()));
89 policy_list[0]->destroy();
90 policy_list[0] = CORBA::Policy::_nil();
92 CORBA::String_var message = CORBA::string_dup("Hello!");
94 try {
95 messenger->send_message ("TAO User", "TAO Test", message.inout());
96 std::cerr << "ERROR: Message did not timeout." << std::endl;
97 } catch(const CORBA::TIMEOUT&) {
98 std::cout << "Message Timed out as expected." << std::endl;
101 catch(const CORBA::Exception& ex) {
102 std::cerr << "Caught a CORBA exception: " << ex << std::endl;
103 return 1;
106 return 0;