Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / DevGuideExamples / RTCORBA / MessengerClient.cpp
blobfe22d886bafda4c880b51510a21eae2e08fb0400
1 #include "MessengerC.h"
2 #include "common.h"
3 #include <iostream>
4 #include "tao/RTCORBA/RTCORBA.h"
5 #include "ace/Get_Opt.h"
7 const ACE_TCHAR *ior_file = ACE_TEXT ("file://Messenger.ior");
9 int
10 parse_args (int argc, ACE_TCHAR *argv[])
12 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
13 int c;
15 while ((c = get_opts ()) != -1)
16 switch (c)
18 case 'k':
19 ior_file = get_opts.opt_arg ();
20 break;
22 case '?':
23 default:
24 ACE_ERROR_RETURN ((LM_ERROR,
25 "usage: %s "
26 "-k <ior> "
27 "\n",
28 argv [0]),
29 -1);
31 // Indicates successful parsing of the command line
32 return 0;
36 int
37 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
39 try {
40 // Initialize orb
41 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
43 if (parse_args (argc, argv) != 0)
44 return 1;
46 // Get the RTORB
47 CORBA::Object_var obj = orb->resolve_initial_references("RTORB");
48 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (obj.in());
50 // PolicyCurrent.
51 obj = orb->resolve_initial_references("PolicyCurrent");
52 CORBA::PolicyCurrent_var policy_current =
53 CORBA::PolicyCurrent::_narrow(obj.in());
54 if (CORBA::is_nil(policy_current.in())) {
55 std::cerr << "Unable to narrow the PolicyCurrent" << std::endl;
56 return 1;
59 // Destringify ior
60 obj = orb->string_to_object(ior_file);
61 if( CORBA::is_nil( obj.in() ) ) {
62 std::cerr << "Nil Messenger reference" << std::endl;
63 return 1;
66 // Narrow
67 Messenger_var messenger = Messenger::_narrow( obj.in() );
68 if( CORBA::is_nil( messenger.in() ) ) {
69 std::cerr << "Argument is not a Messenger reference" << std::endl;
70 return 1;
73 // Set the Private Connection Policy
74 CORBA::PolicyList policy_list(1);
75 policy_list.length (1);
76 policy_list[0] = rt_orb->create_private_connection_policy();
77 policy_current->set_policy_overrides (policy_list,
78 CORBA::SET_OVERRIDE);
80 // Get the RTCurrent.
81 obj = orb->resolve_initial_references ("RTCurrent");
82 RTCORBA::Current_var current =
83 RTCORBA::Current::_narrow(obj.in ());
85 // Change to a priority that matches the server
86 current->the_priority(0);
88 // Explicitly bind a connection to the server
89 CORBA::PolicyList_var inconsistent_policies;
90 CORBA::Boolean status =
91 messenger->_validate_connection(inconsistent_policies.out());
92 if (!status) {
93 std::cerr << "Unable to explicitly bind to the server" << std::endl;
94 return 1;
97 static const CORBA::Short increment = get_increment();
98 for(CORBA::ULong i = 0; i < get_total_lanes(); i++) {
99 // Set the priority to one that matches one of the lanes
100 CORBA::Short priority = i * increment;
101 current->the_priority(priority);
103 // Send the message
104 CORBA::String_var message = CORBA::string_dup( "Hello!" );
105 messenger->send_message( "TAO User", "TAO Test", message.inout() );
108 catch(const CORBA::Exception& ex) {
109 std::cerr << "MessengerClient caught CORBA exception: " << ex << std::endl;
110 return 1;
112 catch(...) {
113 std::cerr << "MessengerClient exception" << std::endl;
114 return 1;
117 std::cout << "messages were sent" << std::endl;
118 return 0;