ACE+TAO Opensource Programming Notes/Create an event supplier

Discussion
The easiest of the two event sides to design is the supplier. In TAO 1.5 there are a few event examples. My favorite is the COSEC on the TAO site, which is similar to the example in the TAO Developer's Guide example. Unfortunately, in the examples on the TAO site, there are no comments. What follows is an example of a simple event supplier which sends an event every 6 seconds. To accomplish this, an ACE timer is set up (with associated handler/callback) which serves to send the event at the appointed time. Finally, the timer is inserted into an ACE reactor and told to "run" in open loop mode.

When reading the code, note at the top of main that connections are made to the orb, the naming service, and the event service. Then, quite a bit of boilerplate code related to creating connections to the event service can be found. The last line of this boilerplate code determines that the event channel will not care that consumers are disconnecting.

main.cpp
/************************************************ * EventSource * * A test program for snding events over TAO's event service */


 * 1) ifdef HAVE_CONFIG_H
 * 2) include 
 * 3) endif


 * 1) include
 * 2) include
 * 3) include "eventsourceI.h"
 * 4) include "eventsourceC.h"
 * 5) include "etimer.h"
 * 6) include 
 * 7) include 
 * 8) include 
 * 9) include 

using namespace std; using namespace CORBA; CosEventChannelAdmin::ProxyPushConsumer_var consumer;

int main(int argc, char *argv[]) { // Initialize the ORB. ORB_var orb = ORB_init(argc, argv, "whatever"); Object_var poa_object = orb->resolve_initial_references ("RootPOA"); //Now we need a reference to the naming service Object_var naming_context_object = orb->resolve_initial_references ("NameService"); CosNaming::NamingContext_var naming_context = CosNaming::NamingContext::_narrow (naming_context_object.in ); CosNaming::Name ec_name; ec_name.length(1); ec_name[0].id = string_dup("CosEventService"); //TAO Specific

// Resolve the binding to the event channel object reference. CosEventChannelAdmin::EventChannel_var channel = CosEventChannelAdmin::EventChannel::_narrow(naming_context->resolve(ec_name)); //resolve_name(     //naming_context, ec_name); // Get the admin object to the event channel CosEventChannelAdmin::SupplierAdmin_var supplier_admin = channel->for_suppliers; // Obtain a ProxyPushConsumer from the SupplierAdmin. consumer = supplier_admin->obtain_push_consumer; // Invoke the connect_push_supplier operation, passing // a nil PushSupplier reference to it. CosEventComm::PushSupplier_var nil_supplier = CosEventComm::PushSupplier::_nil; consumer->connect_push_supplier(nil_supplier); MyTimer *mt = new MyTimer; ACE_Time_Value initialDelay (3); ACE_Time_Value interval (6); ACE_Reactor reactor; reactor.schedule_timer (mt, 0, initialDelay, interval); reactor.run_reactor_event_loop; while(1){ cout << "In while loop." << endl; sleep(100); } orb->destroy ; return EXIT_SUCCESS; }

etimer.h
/** Simple ACE timer impl for polling the bill acceptor & handling events
 * 1) ifndef MYTIMER_H
 * 2) define MYTIMER_H
 * 3) include 
 * 4) include 
 * 5) include 
 * 6) include 
 * 7) include "eventsourceC.h"

@author Evan Carew  */ class MyTimer: public ACE_Event_Handler{ public: int handle_timeout(const ACE_Time_Value &, const void * = 0); };


 * 1) endif

etimer.cpp
/************************************************ * EventSource * * A test program for snding events over TAO's event service */ using namespace std; using namespace CORBA;
 * 1) include "etimer.h"
 * 2) include "eventsourceC.h"

extern CosEventChannelAdmin::ProxyPushConsumer_var consumer;

/*!   \fn MyTimer::handle_timeout(const ACE_Time_Value &, const void * = 0) */ int MyTimer::handle_timeout(const ACE_Time_Value &current_time, const void *nuts) { //create an event accepted accept_event; accept_event.bill = 1; accept_event.status = 1; accept_event.command = 0;

//Insert the event data into an any. Any any; any <<= accept_event; //Now, push the event to the proxy (consumer) consumer->push(any); cout << "Pushing event." << endl; }

eventsource.idl
struct accepted{ unsigned long bill; unsigned long status; unsigned long command; };

Link Line Details
-lTAO_CosNaming -lTAO_CosEvent -lTAO_PortableServer -lTAO_AnyTypeCode -lTAO -lACE