Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / NamingService / Messenger / MessengerClient.cpp
blob085eacdf1192cfda1cb1637caaada88dc2139392
1 #include "MessengerC.h"
2 #include "orbsvcs/CosNamingC.h"
3 #include "ace/OS_NS_unistd.h"
4 #include <iostream>
5 int
6 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
8 try {
9 // Initialize orb
10 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
12 // Find the Naming Service
13 CORBA::Object_var naming_obj = orb->resolve_initial_references("NameService");
14 CosNaming::NamingContextExt_var root =
15 CosNaming::NamingContextExt::_narrow(naming_obj.in());
16 if (CORBA::is_nil(root.in())) {
17 std::cerr << "Nil Naming Context reference" << 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 = root->resolve_str("example/Messenger");
30 } catch (const CosNaming::NamingContext::NotFound&) {
31 // Sleep for a second and try again
32 ACE_OS::sleep(1);
36 // Narrow the Messenger object reference
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!");
45 // Send a message
46 messenger->send_message("TAO User", "TAO Test", message.inout());
48 std::cout << "Message was sent" << std::endl;
50 orb->destroy();
52 catch(const CORBA::Exception& ex) {
53 std::cerr << "Caught a CORBA exception: " << ex << std::endl;
54 return 1;
57 return 0;