Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / DevGuideExamples / SmartProxies / MessengerClient.cpp
blob722500386c6615b83755e4d05a55b59924ad0bbf
1 #include "MessengerC.h"
2 #include "Smart_Messenger_Proxy.h"
3 #include <iostream>
4 #include "ace/Get_Opt.h"
6 const ACE_TCHAR *ior = ACE_TEXT ("file://Messenger.ior");
8 int
9 parse_args (int argc, ACE_TCHAR *argv[])
11 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
12 int c;
14 while ((c = get_opts ()) != -1)
15 switch (c)
17 case 'k':
18 ior = get_opts.opt_arg ();
19 break;
21 case '?':
22 default:
23 ACE_ERROR_RETURN ((LM_ERROR,
24 "usage: %s "
25 "-k <ior> "
26 "\n",
27 argv [0]),
28 -1);
30 // Indicates successful parsing of the command line
31 return 0;
34 int ACE_TMAIN(int argc, ACE_TCHAR * argv[])
36 try {
37 // Initialize the ORB
38 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
40 if (parse_args (argc, argv) != 0)
41 return 1;
43 // Create a smart proxy factory. It will register itself with the
44 // smart proxy factory adapter so it can be used to create
45 // Messenger proxies. It must be created on the heap, but is
46 // otherwise unused here.
47 new Smart_Messenger_Proxy_Factory(orb.in());
49 // Convert the contents of the Messenger.ior file to an object reference.
50 CORBA::Object_var obj = orb->string_to_object(ior);
51 if (CORBA::is_nil(obj.in())) {
52 std::cerr << "Nil Messenger reference" << std::endl;
53 return 1;
56 // Narrow the object reference to a Messenger object reference.
57 Messenger_var messenger = Messenger::_narrow(obj.in());
58 if (CORBA::is_nil(messenger.in())) {
59 std::cerr << "Not a Messenger object reference" << std::endl;
60 return 1;
63 // Create a message and send it to the Messenger.
64 CORBA::String_var message = CORBA::string_dup("Hello!");
65 messenger->send_message ("TAO User", "TAO Test", message.inout());
66 std::cout << "Message was sent" << std::endl;
68 // Release resources.
69 orb->destroy();
71 catch(const CORBA::Exception& ex) {
72 std::cerr << "Caught a CORBA exception: " << ex << std::endl;
73 return 1;
75 catch(...) {
76 std::cerr << "Caught an unknown exception type" << std::endl;
77 return 1;
80 return 0;