Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / NamingService / Naming_Client / MessengerClient.cpp
blobe5e55c56cfaf98306b072e1db9994e45eb2d6a47
1 #include "MessengerC.h"
2 #include "orbsvcs/CosNamingC.h"
3 #include "orbsvcs/Naming/Naming_Client.h"
4 #include "ace/OS_NS_unistd.h"
5 #include <iostream>
6 int
7 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
9 try {
10 // Initialize orb
11 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
13 // Find the Naming Service
14 TAO_Naming_Client naming_client;
16 if (naming_client.init (orb.in ()) != 0) {
17 std::cerr << "Could not initialize naming client." << std::endl;
18 return 1;
21 // Resolve the Messenger object
22 CosNaming::Name name;
23 name.length( 2 );
24 name[0].id = CORBA::string_dup( "example" );
25 name[1].id = CORBA::string_dup( "Messenger" );
26 CORBA::Object_var obj = CORBA::Object::_nil();
27 while (CORBA::is_nil(obj.in())) {
28 try {
29 obj = naming_client->resolve(name);
30 } catch (const CosNaming::NamingContext::NotFound&) {
31 // Sleep for a second and try again
32 ACE_OS::sleep(1);
36 // Narrow
37 Messenger_var messenger = Messenger::_narrow(obj.in());
38 if (CORBA::is_nil(messenger.in())) {
39 std::cerr << "Not a Messenger reference" << std::endl;
40 return 1;
43 CORBA::String_var message = CORBA::string_dup( "Hello!" );
44 messenger->send_message( "TAO User", "TAO Test", message.inout() );
46 std::cout << "Message was sent" << std::endl;
48 catch(const CORBA::Exception& ex) {
49 std::cerr << "Caught a CORBA::Exception: " << ex << std::endl;
50 return 1;
53 return 0;