Previous Table of Contents Next


22.11.4 Client-Side C++ Example of the Polling Model


   22.11.4.1 C++ Example of Generated Poller

   The typed Poller valuetype class implementation is provided by the messaging-aware ORB. The generated C++ class has the following declaration:

   // Generated file: stockmgr_c.hh (Filename is non-normative)class AMI_StockManagerPoller : public Messaging::Poller{public:virtual void get_stock_exchange_name(

   CORBA::ULong timeout,CORBA::String_out ami_return_val);

   virtual void set_stock_exchange_name(CORBA::ULong timeout);

   virtual void add_stock(CORBA::ULong timeout,CORBA::Boolean_out ami_return_val);

   virtual void edit_stock(CORBA::ULong timeout);

   virtual void remove_stock(CORBA::ULong timeout,CORBA::Double_out quote);

   virtual void find_closest_symbol(CORBA::ULong timeout,CORBA::Boolean_out ami_return_val,CORBA::String_out symbol);

   virtual void get_quote(CORBA::ULong timeout,

   CORBA::Double_out ami_return_val);

   virtual AMI_StockManagerHandler_ptr associated_handler();virtual void associated_handler(AMI_StockManagerHandler_ptr _val);};

   22.11.4.2 C++ Example of Polling Client Program

   The following example client program demonstrates the use of the Polling model. The bulk of the program is exactly the same as the program demonstrated in Section 22.11.3.3, “C++ Example of Callback Client Program,? on page 22-36. Each invocation uses the polling “sendp_? in this program and the returned Pollers are then sequentially called to obtain the results. The following notes apply to this sample program:

   // polling_client_main.cpp#include <stockmgr_c.hh> // include filename is non-normativeint main(int argc, char ** argv){// Initialize the ORBCORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

   // Initializing objRef for StockManager -- assumes IOR is passed// on command-lineCORBA::Object_var obj = orb->string_to_object(argv[1]);StockManager_var stockMgr = StockManager::_narrow(obj);

   // Obtain the ORB’s PolicyManagerCORBA::Object_var orbQosObj =orb->resolve_initial_references("ORBPolicyManager");CORBA::PolicyManager_var orbQos =CORBA::PolicyManager::_narrow(orbQosObj);

   // Create and apply an ORB-wide Routed Delivery QoSCORBA::Any routing_val;Messaging::RoutingTypeRange routing;routing.min = Messaging::FORWARD;routing.max = Messaging::STORE_AND_FORWARD;routing_val <<= routing;CORBA::PolicyList orb_pols(1);orb_pols.length(1);orb_pols[(CORBA::ULong) 0] =

   orb->create_policy(Messaging::ROUTING_POLICY_TYPE, routing_val);orbQos->set_policy_overrides(orb_pols, CORBA::ADD_OVERRIDE);

   // Create and apply an object-reference-specific Priority QoSCORBA::Any priority_val;Messaging::PriorityRange priority;priority.min = 5;

   priority.max = 15;priority_val <<= priority;CORBA::PolicyList obj_pols(1);obj_pols.length(1);obj_pols[(CORBA::ULong) 0] =

   orb->create_policy(Messaging::REQUEST_PRIORITY_POLICY_TYPE,priority_val);stockMgr = stockMgr->set_policy_overrides(obj_pols);

   // At this point QoS has been set and a protocol selected.

   // Make each invocation and store the returned PollersAMI_StockManagerPoller* pollers[6];

   // Async Attributespollers[0] = stockMgr->sendp_set_stock_exchange_name("NSDQ");pollers[1] = stockMgr->sendp_get_stock_exchange_name();

   // Async Operationspollers[2] = stockMgr->sendp_add_stock("ACME", 100.5);pollers[3] = stockMgr->sendp_edit_stock("ACME", 150.4);

   // Notice no out param is passed.pollers[4] = stockMgr->sendp_remove_stock("ABC");pollers[5] = stockMgr->sendp_find_closest_symbol("ACMA");

   // Now obtain each resultCORBA::ULong max_timeout = (CORBA::ULong) -1;pollers[0]->set_stock_exchange_name(max_timeout);cout << "Setting stock exchange name succeeded" << endl;

   CORBA::String_var exchange_name;

   pollers[1]->get_stock_exchange_name(max_timeout,exchange_name.out());

   cout << "Obtained stock exchange name [" << exchange_name << "]" << endl;

   CORBA::Boolean stock_added;

   pollers[2]->add_stock(max_timeout,stock_added);

   if (stock_added)cout << "Stock added successfully" << endl;elsecout << "Stock not added" << endl;

   try {pollers[3]->edit_stock(max_timeout);cout << "Edited stock successfully" << endl;

   }catch (const CORBA::Exception& e) {cout << "Edit stock failure [" << e << "]" << endl;}

   try {CORBA::Double quote;pollers[4]->remove_stock(

   max_timeout,quote);cout << "Removed stock successfully with quote [" << quote << "]"

   << endl;}catch (const CORBA::Exception& e) {

   cout << "Remove stock failure [" << e << "]" << endl;}

   CORBA::Boolean closest_found;CORBA::String_var symbol;pollers[5]->find_closest_symbol(

   max_timeout,closest_found, symbol.out());if (closest_found)cout << "Found closest symbol [" << symbol << "]" << endl;

   cout << "Exiting Polling Client" << endl;return 0;}

   22.11.4.3 C++ Example of Using PollableSet in a Client Program

   The following example client program demonstrates the use of the PollableSet and wait for multiple requests to finish. The program would be exactly the same as that of the previous section, as far as the comment “// Now obtain each result?.

   In this example, after the PollableSet::poll indicates that a particular Poller has finished, the code makes the call on the type-specific poller in a non-blocking manner and doesn’t bother checking for completion in the return value. Checking isn’t necessary when only a single client is using the Poller, but it is the safe practice if multiple clients are waiting.

   // Obtain results in any order. First set up

   // the PollableSet.CORBA::PollableSet_var poll_set = pollers[0]->create_pollable_set();

   CORBA::Pollable_var pollables[6];

   for (int i=0; i<6, i++) {pollables[i] = pollers[i]._this();poll_set->add_pollable(pollables[i]);

   }

   // repeat until all completions have been receivedCORBA::ULong max_timeout = (CORBA::ULong) -1;while (poll_set->number_left() > 0) {

   // wait for a completion

   CORBA::Pollable_ptr pollable = poll_set->poll(max_timeout);// the returned Pollable is ready to return its replyfor (int j=0; j < 6; j++) {

   if (pollables[j]->is_equivalent(pollable)) break;}

   switch(j) {

   case 0:pollers[0]->set_stock_exchange_name(0UL);cout << "Setting stock exchange name succeeded"

   << endl;break;

   case 1:CORBA::String_var exchange_name;pollers[1]->get_stock_exchange_name(0UL, exchange_name.out());cout << "Obtained stock exchange name ["

   << exchange_name << "]" << endl;break;

   case 2:CORBA::Boolean stock_added;pollers[2]->add_stock(0UL, stock_added);if (stock_added)

   cout << "Stock added successfully" << endl;elsecout << "Stock not added" << endl;break;

   case 3:

   try {pollers[3]->edit_stock(0UL);cout << "Edited stock successfully" << endl;

   }catch (const CORBA::Exception& e) {cout << "Edit stock failure [" << e << "]"

   << endl;}break;

   case 4:

   try {CORBA::Double quote;pollers[4]->remove_stock(0UL, quote);cout << "Removed stock successfully with quote ["

   << quote << "]" << endl;}catch (const CORBA::Exception& e) {

   cout << "Remove stock failure [" << e << "]"

   << endl;}break;

   case 5:CORBA::Boolean closest_found;CORBA::String_var symbol;pollers[5]->find_closest_symbol(0UL, closest_found,

   symbol.out());

   if (closest_found)cout << "Found closest symbol [" << symbol<< "]" << endl;break;

   }}cout << "All replies received. Exiting Polling Client"

   << endl;return 0;}