ACE+TAO 开源编程笔记/创建事件提供者
外观
设计两个事件端中较容易的一端是提供者。在 TAO 1.5 中,有几个事件示例。我最喜欢的是 TAO 网站上的 COSEC,它类似于 TAO 开发者指南中的示例。不幸的是,在 TAO 网站上的示例中没有注释。以下是发送每 6 秒一个事件的简单事件提供者示例。为了实现这一点,设置了一个 ACE 计时器(带有关联的处理程序/回调),它用于在指定时间发送事件。最后,计时器被插入到一个 ACE 反应器中,并被告知以开环模式“运行”。
在阅读代码时,请注意 main 顶部连接到 orb、命名服务和事件服务的方式。然后,可以找到大量与创建到事件服务的连接相关的样板代码。此样板代码的最后一行确定事件通道将不会关心消费者断开连接。
/************************************************
* EventSource
*
* A test program for snding events over TAO's event service
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include "eventsourceI.h"
#include "eventsourceC.h"
#include "etimer.h"
#include <ace/Reactor.h>
#include <orbsvcs/CosNamingC.h>
#include <orbsvcs/CosEventCommS.h>
#include <orbsvcs/CosEventChannelAdminC.h>
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<CosEventChannelAdmin::EventChannel>(
//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;
}
#ifndef MYTIMER_H
#define MYTIMER_H
#include <ace/streams.h>
#include <orbsvcs/CosNamingC.h>
#include <orbsvcs/CosEventCommS.h>
#include <orbsvcs/CosEventChannelAdminC.h>
#include "eventsourceC.h"
/**
Simple ACE timer impl for polling the bill acceptor & handling events
@author Evan Carew <carew@pobox.com>
*/
class MyTimer: public ACE_Event_Handler{
public:
int handle_timeout(const ACE_Time_Value &, const void * = 0);
};
#endif
/************************************************
* EventSource
*
* A test program for snding events over TAO's event service
*/
#include "etimer.h"
#include "eventsourceC.h"
using namespace std;
using namespace CORBA;
extern CosEventChannelAdmin::ProxyPushConsumer_var consumer;
/*!
\fn MyTimer::handle_timeout(const ACE_Time_Value &, const void * = 0)
*/
int MyTimer::handle_timeout(const ACE_Time_Value ¤t_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;
}
struct accepted{
unsigned long bill;
unsigned long status;
unsigned long command;
};
-lTAO_CosNaming -lTAO_CosEvent -lTAO_PortableServer -lTAO_AnyTypeCode -lTAO -lACE