Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / DevGuideExamples / PortableInterceptors / IOR / MessengerClient.cpp
blob3a60401f3499290ec5a0081fb84ba3d017ba4cc9
1 #include "MessengerC.h"
2 #include "ClientInitializer.h"
3 #include "tao/ORBInitializer_Registry.h"
4 // Ensure that the PI library is linked in when building statically
5 #include "tao/PI/PI.h"
6 #include <iostream>
7 #include "ace/Get_Opt.h"
9 const ACE_TCHAR *ior = ACE_TEXT ("file://Messenger.ior");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'k':
21 ior = get_opts.opt_arg ();
22 break;
24 case '?':
25 default:
26 ACE_ERROR_RETURN ((LM_ERROR,
27 "usage: %s "
28 "-k <ior> "
29 "\n",
30 argv [0]),
31 -1);
33 // Indicates successful parsing of the command line
34 return 0;
37 int
38 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
40 try
42 PortableInterceptor::ORBInitializer_var orb_initializer =
43 new ClientInitializer;
45 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
47 // Initialize orb
48 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
50 if (parse_args (argc, argv) != 0)
51 return 1;
53 // Destringify ior
54 CORBA::Object_var obj = orb->string_to_object( ior );
55 if( CORBA::is_nil( obj.in() ) ) {
56 std::cerr << "Nil Messenger reference" << std::endl;
57 return 1;
60 // Narrow
61 Messenger_var messenger = Messenger::_narrow( obj.in() );
62 if( CORBA::is_nil( messenger.in() ) ) {
63 std::cerr << "Argument is not a Messenger reference" << std::endl;
64 return 1;
67 // Obtain a reference to the CodecFactory.
68 CORBA::Object_var obj2 =
69 orb->resolve_initial_references ("CodecFactory");
71 IOP::CodecFactory_var codec_factory;
73 if(CORBA::is_nil(obj2.in()))
75 std::cerr << "Error: codec_factory" << std::endl;
76 ACE_OS::exit(1);
78 else
80 codec_factory = IOP::CodecFactory::_narrow (obj2.in ());
81 std::cout << "got codec factory" << std::endl;
84 // Set up a structure that contains information necessary to
85 // create a GIOP 1.2 CDR encapsulation Codec.
86 IOP::Encoding encoding;
87 encoding.format = IOP::ENCODING_CDR_ENCAPS;
88 encoding.major_version = 1;
89 encoding.minor_version = 2;
91 // Obtain the CDR encapsulation Codec.
92 IOP::Codec_var codec =
93 codec_factory->create_codec (encoding);
95 CORBA::Long uid = 64321;
97 CORBA::Any uid_as_any;
99 uid_as_any <<= uid;
100 CORBA::OctetSeq client_uid = *codec->encode (uid_as_any);
101 messenger->send_message( client_uid );
104 catch(const CORBA::Exception& ex)
106 std::cerr << "Exception in MessengerClient: " << ex << std::endl;
107 return 1;
110 std::cout << "message was sent" << std::endl;
111 return 0;