VisiBroker for C++ Developer’s Guide : Event Queue

Event Queue
This section provides information about the Event Queue feature. This feature is provided for the server-side only.
A server can register listeners to the event queue based on event types that the server is interested and therefore can process those events when the server needs to do so.
Event types
Currently, connection event type is the only event type generated.
Connection events
There are two connection events that the VisiBroker ORB will generate and push to the registered connection event, as follows:
Connection established: indicates that a new client is connected to the server successfully.
Connection closed: indicates that an existing client is disconnected from the server.
Event listeners
A server implements and registers listeners with the VisiBroker ORB based on event types the server needs to process. The connection event listener is the only event listener supported.
IDL definition
The interface definitions are as follows:
module EventQueue {
// Connection event types
enum EventType {UNDEFINED, CONN_EVENT_TYPE};
// Peer (Client) connection info
struct ConnInfo {
string ipaddress; // in %d.%d.%d.%d format
long port;
long connID;
};
// Marker interface for all types of event listeners
local interface EventListener {};
typedef sequence<EventListener> EventListeners;
// connection event listener interface
local interface ConnEventListener : EventListener{
void conn_established(in ConnInfo info);
void conn_closed(in ConnInfo info);
};
// The EventQueue manager
local interface EventQueueManager : interceptor::InterceptorManager {
void register_listener(in EventListener listener, in EventType type);
void unregister_listener(in EventListener listener, in EventType type);
EventListeners get_listeners(in EventType type);
};
};
The details of the interface definitions are described in the following sections.
ConnInfo structure
The ConnInfo structure contains the following client connection information.
EventListener interface
The EventListener interface section is the marker interface for all types of event listeners.
ConnEventListeners interface
The ConnEventListeners interface defines the following operations.
The server-side application is responsible for the implementation of the ConnEventListener interface as well as the processing of the events being pushed into the listener.
EventQueueManager interface
The EventQueueManager interface is used as a handle by the server-side implementation for the registration of event listeners. This interface defines the following operations.
How to return the EventQueueManager
An EventQueueManager object is created upon ORB initialization. Server-side implementation returns the EventQueueManager object reference using the following code:
CORBA::Object *object =
orb->resolve_initial_references("VisiBrokerInterceptorControl");
interceptor::InterceptorManagerControl_var control =
interceptor::InterceptorManagerControl::_narrow(object);
interceptor::InterceptorManager_var manager =
control->get_manager("EventQueueManager");
EventQueue::EventQueueManager_var eq_mgr =
EventQueue::EventQueueManager::_narrow(manager);
Event Queue code samples
This section contains some code samples for registering EventListeners and implementing a connection EventListener.
Registering EventListeners
The SampleServerLoader class contains the init() method which is called by the ORB during initialization. The purpose of the ServerLoader is to register an EventListener by creating and registering it to the EventQueueManager.
#ifdef _VIS_STD
#include <iostream>
#else
#include <iostream.h>
#endif
#include "vinit.h"
#include "ConnEventListenerImpl.h"

USE_STD_NS

class SampleServerLoader : VISInit {
private:
short int _conn_event_interceptors_installed;
public:
SampleServerLoader(){
_conn_event_interceptors_installed = 0;
}

void ORB_init(int& argc, char* const* argv, CORBA::ORB_ptr orb) {
if( _conn_event_interceptors_installed) return;
cout << "Installing Connection event interceptors" << endl;
ConnEventListenerImpl *interceptor =
new ConnEventListenerImpl("ConnEventListener");
// Get the interceptor manager control
CORBA::Object *object =
orb->resolve_initial_references("VisiBrokerInterceptorControl");
interceptor::InterceptorManagerControl_var control =
interceptor::InterceptorManagerControl::_narrow(object);
// Get the POA manager
interceptor::InterceptorManager_var manager =
control->get_manager("EventQueueManager");
EventQueue::EventQueueManager_var eq_mgr =
EventQueue::EventQueueManager::_narrow(manager);
// Add POA interceptor to the list
eq_mgr->register_listener(
(EventQueue::ConnEventListener *)interceptor, EventQueue::CONN_EVENT_TYPE);
cout << "Event queue interceptors installed" << endl;
_conn_event_interceptors_installed = 1;
}
};
Implementing EventListeners
The ConnEventListenerImpl contains a connection event listener implementation sample. The ConnEventListener interface implements the conn_established and conn_closed operations at the server-side application. For more information, go to “ConnEventListeners interface”. The implementation enables the connection to idle for 30000 milliseconds while waiting for a request at the server-side. These operations are called when the connection is established by the client and when the connection is dropped, respectively.
#ifdef _VIS_STD
#include <iostream>
#else
#include <iostream.h>
#endif
#include "vextclosure.h"
#include "interceptor_c.hh"
#include "IOP_c.hh"
#include "EventQueue_c.hh"
#include "vutil.h"

// USE_STD_NS is a define setup by VisiBroker to use the std namespace
USE_STD_NS

//--------------------------------------------------------------------
// defines the server interceptor functionality
//--------------------------------------------------------------------
class ConnEventListenerImpl : EventQueue::ConnEventListener
{
private:
char * _id;
public:
ConnEventListenerImpl( const char* id) {
_id = new char[ strlen(id) + 1];
strcpy( _id,id);
}

~ConnEventListenerImpl() {
delete[] _id;
_id = NULL;
}

//--------------------------------------------------------------------
// This method gets called when a request arrives at the server end.
//--------------------------------------------------------------------

void conn_established(const EventQueue::ConnInfo& connInfo){
cout <<"Processing connection established from" <<endl;
cout << connInfo;
cout <<endl;
VISUtil::sleep(30000);
}
void conn_closed(const EventQueue::ConnInfo & connInfo) {
cout <<"Processing connection closed from " <<endl ;
cout <<connInfo ;
cout << endl;
VISUtil::sleep(30000);
}
};