3 Event Service
3.1 Overview of the CosEvent Service
The Event service allows programmers to subscribe to information channels. Suppliers can generate events without knowing the consumer identities and the consumer can receive events without knowing the supplier identity. Both push and pull event delivery are supported. The Event service will queue information and processes.
The CORBA Event service provides a flexible model for asynchronous, decoupled communication between objects. This chapter outlines communication models and the roles and relationships of key components in the CosEvent service. It shows a simple example on use of this service.
3.2 Event Service Components
There are five components in the OMG CosEvent service architecture. These are described below:
Figure 1: Event service Components
- Suppliers and consumers: Consumers are the ultimate targets of events generated by suppliers. Consumers and suppliers can both play active and passive roles. There could be two types of consumers and suppliers: push or pull. A PushSupplier object can actively push an event to a passive PushConsumer object. Likewise, a PullSupplier object can passively wait for a PullConsumer object to actively pull an event from it.
- EventChannel: The central abstraction in the CosEvent service is the EventChannel which plays the role of a mediator between consumers and suppliers. Consumers and suppliers register their interest with the EventChannel. It can provide many-to-many communication. The channel consumes events from one or more suppliers, and supplies events to one or more consumers. An EventChannel can support consumers and suppliers using different communication models.
- ProxySuppliers and ProxyConsumers: ProxySuppliers act as middlemen between consumers and the EventChannel. A ProxySupplier is similar to a normal supplier, but includes an additional method for connecting a consumer to the ProxySupplier. Likewise, ProxyConsumers act as middlemen between suppliers and the EventChannel. A ProxyConsumer is similar to a normal consumer, but includes an additional method for connecting a supplier to the ProxyConsumer.
- Supplier and consumer administrations: Consumer administration acts as a factory for creating ProxySuppliers. Supplier administration acts as a factory for creating ProxyConsumers.
3.3 Event Service Communication Models
There are four general models of component collaboration in the OMG CosEvent service architecture. The following describes these models: (Please note that proxies are not shown in the diagrams for simplicity).
Figure 2: Event service Communication Models
- The Canonical Push Model: The Canonical push model shown in figure 2(A) allows the suppliers of events to initiate the transfer of event data to consumers. In this model, suppliers are active initiators and consumers are the passive targets of the requests. EventChannels play the role of
Notifier
. Thus, active suppliers use EventChannels to push data to passive consumers that have registered with the EventChannels.
- The Canonical Pull Model:The Canonical pull model shown in figure 2(B) allows consumers to request events from suppliers. In this model, Consumers are active initiators and suppliers are the passive targets of the pull requests. EventChannel plays the role of
Procurer
since it procures events on behalf of consumers. Thus, active consumers can explicitly pull data from passive suppliers via the EventChannels.
- The Hybrid Push/Pull Model: The push/pull model shown in figure 2(C) is a hybrid that allows consumers to request events queued at an EventChannel by suppliers. In this model, both suppliers and consumers are active initiators of the requests. EventChannels play the role of
Queue
. Thus, active consumers can explicitly pull data deposited by active suppliers via the EventChannels.
- The Hybrid Pull/Push Model: The pull/push model shown in figure 2(D) is another hybrid that allows the channel to pull events from suppliers and push them to consumers. In this model, suppliers are passive targets of pull requests and consumers are passive targets of pushes. EventChannels play the role of
Intelligent Agent
. Thus, active EventChannels can pull data from passive suppliers and push that data to passive consumers.
3.4 Creating an EventChannel
An EventChannel can be created by using the
EventChannelFactory
interface, which is implemented byOrberEventChannel_EventChannelFactory
.To start the factory server one needs to make a call to
corba:create/2
which could look like this:-module(event_channel_factory). -include_lib("orber/include/corba.hrl"). -include_lib("orber/COSS/CosNaming/CosNaming.hrl"). -include_lib("orber/COSS/CosNaming/lname.hrl"). -export([start/0]). start() -> ECFok = 'OrberEventChannel_EventChannelFactory':oe_create(), NS = corba:resolve_initial_references("Nameservice"), NC = lname_component:set_id(lname_component:create(), "EventChannelFactory"), N = lname:insert_component(lname:create(), 1, NC), 'CosNaming_NamingContext':bind(NS, N, ECFok).Now an EventChannelFactory is created and registered in the CosNaming service and could be found by consumers and suppliers.
3.5 Using the Event Service
This section shows an example of usage of the Event service in order to decouple communication between a measurements collector and a safety controller.
3.5.1 Using the Consumer interface for safety controller
The safety controller plays the role of a PushConsumer. It is interested in the data provided by the measurements collector, which plays the role of a PushSupplier. Safety controller is responsible for the action required in case some measurements exceed the safety limits.
First, the safety controller creates a PushConsumer itself, and then obtains an EventSupplier channel object reference using the
EventChannelFactory
, as follows:// The safety controller creates a PushConsumer object MyPushConsumer = my_push_consumer_srv:create(), // EventChannel created through EventChannelFactory // EventChannelFactory obtained from the CosNaming service (not shown) // EventChannel registered in the CosNaming service (not shown) EventChannel = 'OrberEventChannel_EventChannelFactory': create_event_channel(ECFactory),This code assumes that the
MyPushConsumer
supports thePushConsumer
interface and implements the appropriate safety controller logic.Note: If no support exists for the push consumer the process will crash.
Next, the safety controller connects itself to the
EventChannel
:// first step: obtain ConsumerAdmin object reference ConsumerAdmin = 'CosEventChannelAdmin_EventChannel' :for_consumers(EventChannel), // obtain ProxyPushSupplier from the ConsumerAdmin object PPhS = 'CosEventChannelAdmin_ConsumerAdmin' :obtain_push_supplier(ConsumerAdmin), // second step: connect our PushConsumer to the ProxyPushSupplier 'CosEventChannelAdmin_ProxyPushSupplier' :connect_push_consumer(PPhS, MyPushConsumer)When an event arrives in the EventChannel, it will invoke the
push
operation on the registeredPushConsumer
object reference.3.5.2 Using the supplier interface for measurements collector
Measurements collector sends data containing information about current measurement of the system to the EventChannel in order to keep safety controller informed of any changes.
As with the safety controller, the measurements collector needs an object reference to an
EventChannel
and to a PushSupplier to connect to the channel. This is accomplished as follows:// measurements collector creates a PushSupplier MyPushSupplier = my_push_supplier_srv:create(), // EventChannel obtained from the Naming service (not shown) EventChannel = //... // obtain SupplierAdmiin object reference SupplierAdmin = 'CosEventChannelAdmin_EventChannel':for_suppliers(EventChannel), // obtain ProxyPushConsumer from SupplierAdmin object PPhC = 'CosEventChannelAdmin_SupplierAdmin':obtain_push_consumer(SupplierAdmin), // connect our PushSupplier to the ProxyPushConsumer 'CosEventChannelAdmin_ProxyPushConsumer':connect_push_supplier(PPhC, MyPushSupplier),Once the consumer and the supplier registration code get executed, both the safety controller and the measurements collector are connected to the EventChannel. At this point, safety controller will automatically receive measurements data that are pushed by the measurements collector.
3.5.3 Exchanging and processing event data
The events exchanged between supplier and consumer must always be specified in OMG IDL so that they can be stored into an
any
type variable. Consider the following data example sent by the measurements controller:record(measurements, {temperature, pressure, water_level}).In order to push an event, the measurements collector must create and initialize this record, put it into
CORBA::any
, and callpush
on the EventChannel PushConsumer interface:// create some data EventRecord = #measurements{temperature = 150, pressure = 100, water_level = 200}, EventData = { measurements:tc(),EventRecord}, // push the event to consumer 'CosEventChannelAdmin_ProxyPushConsumer':push(PPhC, EventData),Once the EventChannel receives an event from the measurements collector, it pushes the event data to the consumer by invoking the
push
operation on registeredPushConsumer
object reference.The implementation of the safety controller consumer
push
could look like this:push(Data) -> { if Data#measurements.temperature > 300 -> // some logic to set alarm ; Data#measurements.water_level < 50 -> // some logic to get more water ; ......etc end. }