1 #include "MessengerC.h"
2 #include "MessengerHandler.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/OS_NS_sys_time.h"
8 #include "ace/Get_Opt.h"
10 const ACE_TCHAR
*ior
= ACE_TEXT ("file://MessengerServer.ior");
11 bool automated
= false;
14 parse_args (int argc
, ACE_TCHAR
*argv
[])
16 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("k:a:"));
19 while ((c
= get_opts ()) != -1)
23 ior
= get_opts
.opt_arg ();
26 automated
= !(ACE_OS::atoi(get_opts
.opt_arg ()) == 0);
30 ACE_ERROR_RETURN ((LM_ERROR
,
38 // Indicates successful parsing of the command line
43 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
47 CORBA::ORB_var orb
= CORBA::ORB_init(argc
, argv
);
49 if (parse_args (argc
, argv
) != 0)
52 CORBA::Object_var obj
= orb
->string_to_object(ior
);
53 if (CORBA::is_nil(obj
.in())) {
54 std::cerr
<< "Nil Messenger reference" << std::endl
;
59 Messenger_var messenger
= Messenger::_narrow(obj
.in());
60 if (CORBA::is_nil(messenger
.in())) {
61 std::cerr
<< "Argument is not a Messenger reference" << std::endl
;
65 // Get reference to Root POA.
66 obj
= orb
->resolve_initial_references("RootPOA");
67 PortableServer::POA_var poa
= PortableServer::POA::_narrow(obj
.in());
69 // Activate POA manager
70 PortableServer::POAManager_var mgr
= poa
->the_POAManager();
73 // Register an AMI handler for the Messenger interface
74 PortableServer::Servant_var
<MessengerHandler
> servant
=
75 new MessengerHandler(orb
.in());
76 PortableServer::ObjectId_var oid
= poa
->activate_object(servant
.in());
77 obj
= poa
->id_to_reference(oid
.in());
78 AMI_MessengerHandler_var handler
= AMI_MessengerHandler::_narrow(obj
.in());
80 CORBA::String_var user
= CORBA::string_alloc(81);
81 CORBA::String_var subject
= CORBA::string_alloc(81);
82 CORBA::String_var message
= CORBA::string_alloc(81);
85 std::cout
<< "Enter user name -->";
86 std::cin
.getline(user
, 81);
88 std::cout
<< "Enter subject -->";
89 std::cin
.getline(subject
, 81);
91 std::cout
<< "Enter message -->";
92 std::cin
.getline(message
, 81);
94 user
= CORBA::string_dup("TestUser");
95 subject
= CORBA::string_dup("TestSubject");
96 message
= CORBA::string_dup("Have a nice day.");
99 // Record the time the request was made.
100 ACE_Time_Value time_sent
= ACE_OS::gettimeofday();
102 messenger
->sendc_send_message(handler
.in(), user
.in(), subject
.in(), message
.in());
104 // Do some work to prove that we can send the message asynchronously, do some work,
105 // then come back later and retrieve the results.
106 for (int i
= 0; i
< 10; ++i
) {
108 ACE_OS::sleep(ACE_Time_Value(0, 10 * 1000));
111 // Our simple servant will exit as soon as it receives the results.
114 if (servant
->message_was_sent())
116 // Note : We can't use the
117 ACE_Time_Value delay
= ACE_OS::gettimeofday() - time_sent
;
118 std::cout
<< std::endl
<< "Reply Delay = " << delay
.msec() << "ms" << std::endl
;
123 catch(const CORBA::Exception
& ex
) {
124 std::cerr
<< "Caught a CORBA::Exception: " << ex
<< std::endl
;