EventListenersの登録およびコネクションEventListenerのインプリメントについて,C++とJavaのコードサンプルを示します。
SampleServerLoaderクラスには,初期化時にORBが呼び出すORB_init()メソッド(C++)またはinit()メソッド(Java)があります。ServerLoaderの目的は,EventListenerを作成して,それをEventQueueManagerに登録することです。
#ifdef _VIS_STD
#include <iostream>
#else
#include <iostream.h>
#endif
#include "vinit.h"
#include "ConnEventListenerImpl.h"
USE_STD_NS
class SampleServerLoader :public 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;
}
};
import com.inprise.vbroker.EventQueue.*;
import com.inprise.vbroker.interceptor.*;
import com.inprise.vbroker.PortableServerExt.*;
public class SampleServerLoader implements ServiceLoader
{
public void init(org.omg.CORBA.ORB orb)
{
try {
InterceptorManagerControl control =
InterceptorManagerControlHelper.narrow(
orb.resolve_initial_references
("VisiBrokerInterceptorControl"));
EventQueueManager queue_manager =
(EventQueueManager)control.get_manager("EventQueue");
queue_manager.register_listener((EventListener)new
ConnEventListenerImpl(),EventType.CONN_EVENT_TYPE);
}
catch(Exception e){
e.printStackTrace();
throw new org.omg.CORBA.INITIALIZE(e.toString());
}
System.out.println("=========>SampleServerLoader:
ConnEventListener registered");
}
public void init_complete(org.omg.CORBA.ORB orb){
}
public void shutdown(org.omg.CORBA.ORB orb){
}
}
ConnEventListenerImplには,コネクションイベントリスナーのインプリメンテーションサンプルがあります。ConnEventListenerインタフェースは,サーバ側アプリケーションでconn_establishedおよびconn_closedオペレーションをインプリメントします。インプリメンテーションによってコネクションは,サーバ側でリクエストを待っている間,30,000ミリ秒間アイドル状態にできます。このようなオペレーションは,クライアントがコネクションを設定した場合と,コネクションが切断された場合にそれぞれ呼び出されます。
#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 :public EventQueue::ConnEventListener
{
private:
char * _id;
public:
ConnEventListenerImpl( const char* id){
_id =new char [ strlen((id) + 1 ];
strcpy( _id,id);
}
virtual~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);
}
};
import com.inprise.vbroker.EventQueue.*;
import org.omg.CORBA.LocalObject;
public class ConnEventListenerImpl extends LocalObject implements
ConnEventListener {
public void conn_established(ConnInfo info){
System.out.println("Received conn_established:address ="+
info.ipaddress + "port ="+info.port +
" connID = " + info.connID);
System.out.println("Processing the event ...");
try {
Thread.sleep(30000);
}catch (Exception e){e.printStackTrace();}
}
public void conn_closed(ConnInfo info) {
System.out.println("Received conn_closed:address = " +
info.ipaddress+"port ="+info.port +
" connID = " + info.connID);
}
}