1 // AMI pollable set echo example. Use as a client to echo/eg2_impl
2 //
3 // Usage: echo_pollable_set <object reference>
4 //
5 
6 #include <echo_ami.hh>
7 
8 #ifdef HAVE_STD
9 #  include <iostream>
10 #  include <fstream>
11    using namespace std;
12 #else
13 #  include <iostream.h>
14 #endif
15 
16 //////////////////////////////////////////////////////////////////////
17 
main(int argc,char ** argv)18 int main(int argc, char** argv)
19 {
20   try {
21     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
22 
23     if (argc != 2) {
24       cerr << "usage: echo_pollable_set <object reference>" << endl;
25       return 1;
26     }
27 
28     // Get reference to Echo object
29     CORBA::Object_var obj = orb->string_to_object(argv[1]);
30     Echo_var echoref = Echo::_narrow(obj);
31 
32     if (CORBA::is_nil(echoref)) {
33       cerr << "Can't narrow reference to type Echo (or it was nil)." << endl;
34       return 1;
35     }
36 
37     // Make some asynchronous calls
38     AMI_EchoPoller_var poller1 = echoref->sendp_echoString("Hello async 1!");
39     AMI_EchoPoller_var poller2 = echoref->sendp_echoString("Hello async 2!");
40 
41     // Create PollableSet containing both pollers
42     CORBA::PollableSet_var pset = poller1->create_pollable_set();
43     pset->add_pollable(poller2);
44 
45     try {
46       while (1) {
47         cout << pset->number_left() << " pollers left" << endl;
48 
49         CORBA::Pollable_var pollable = pset->get_ready_pollable(2000);
50         AMI_EchoPoller*     poller   = AMI_EchoPoller::_downcast(pollable);
51 
52         CORBA::String_var result;
53         poller->echoString(0, result.out());
54         cout << "The call returned: " << (const char*)result << endl;
55       }
56     }
57     catch (CORBA::PollableSet::NoPossiblePollable&) {
58       cout << "No possible pollable." << endl;
59     }
60     catch (CORBA::TIMEOUT&) {
61       cout << "Timeout" << endl;
62     }
63 
64     orb->destroy();
65   }
66   catch(CORBA::TRANSIENT&) {
67     cerr << "Caught system exception TRANSIENT -- unable to contact the "
68          << "server." << endl;
69   }
70   catch(CORBA::SystemException& ex) {
71     cerr << "Caught a CORBA::" << ex._name() << endl;
72   }
73   catch(CORBA::Exception& ex) {
74     cerr << "Caught CORBA::Exception: " << ex._name() << endl;
75   }
76   return 0;
77 }
78