Main Page   Class Hierarchy   Compound List   File List   Compound Members  

tdisp.h

00001 /*
00002  * Copyright (c) 1991-2006 TIBCO Software Inc.
00003  * All rights reserved.
00004  * For more information, please contact:
00005  * TIBCO Software Inc., Palo Alto, California, USA
00006  *
00007  * $Id: //dev/packages/tpsi/cpsi/dev/68/src/rtworks/tdisp.h#1 $
00008  */
00009 
00010 #ifndef _T_DISPATCHER_HH
00011 #define _T_DISPATCHER_HH
00012 
00013 #include <rtworks/ipc.h>
00014 #include <rtworks/tex.h>
00015 #include <rtworks/tevent.h>
00016 #include <rtworks/tsrv.h>
00017 #include <rtworks/tmsg.h>
00018 namespace SmartSockets {
00019 
00026   class T_IPCX_API TipcDispatcherException : public TipcException {
00027 
00028   public:
00032     TipcDispatcherException ()
00033     {
00034     }
00035 
00036     TipcDispatcherException (int4 errorNum)
00037     :TipcException(errorNum)
00038     {
00039     }
00040 
00044     virtual ~TipcDispatcherException () throw()
00045     {
00046     }
00047   };
00048 
00049   class T_IPCX_API TipcDispatcher;
00050   class T_IPCX_API TipcEvent;
00051 
00052 
00056   class T_IPCX_API TipcDispatcherTraverser {
00057   public:
00058 
00065     typedef bool (TipcDispatcherTraverser::* Handler)
00066     (
00067     TipcDispatcher& disp,
00068     const TipcEvent* event
00069     ) ;
00070 
00071 
00072 
00076     TipcDispatcherTraverser();
00077 
00082     void setHandler(Handler cbMethod);
00083 
00087     Handler getHandler();
00088 
00089 
00093     virtual ~TipcDispatcherTraverser() throw();
00094 
00095 
00102     virtual bool onTraverse (TipcDispatcher& disp, const TipcEvent* event);
00103 
00104     static  T_PTR T_ENTRY traverseFuncDelegator
00105     (
00106     T_IPC_DISPATCHER disp,
00107     T_IPC_EVENT  event,
00108     T_PTR arg
00109     );
00110 
00111   private:
00112 
00113     Handler _cbMethod;
00114 
00115   };
00116 
00161   class T_IPCX_API TipcDispatcher {
00162   private:
00163     bool _isDetached;
00164     bool _destroyFlag;
00165     T_IPC_DISPATCHER _dispatcher;
00166 
00167   public:
00177     TipcDispatcher (T_IPC_DISPATCHER dispatcher,
00178                     bool destroyFlag = false,
00179                     bool isDetached = false)
00180     throw (TipcException)
00181     :_dispatcher (dispatcher),
00182     _isDetached(isDetached),
00183     _destroyFlag(destroyFlag)
00184     {
00185       if (_dispatcher == NULL) {
00186         throw TipcDispatcherException(T_ERR_NULL_PTR);
00187       }
00188     }
00189 
00199     TipcDispatcher (bool isDetached = false)
00200     throw (TipcException)
00201     : _isDetached(isDetached)
00202     , _destroyFlag(true)
00203     {
00204       if (_isDetached) {
00205         if (!TipcThreadEnabled()) {
00206           throw (TipcDispatcherException(T_ERR_VAL_INVALID));
00207         }
00208         _dispatcher = TipcDispatcherCreateDetached();
00209         if (NULL == _dispatcher) {
00210           throw TipcDispatcherException();
00211         }
00212       }
00213       else {
00214         _dispatcher = TipcDispatcherCreate();
00215         if (NULL == _dispatcher) {
00216           throw TipcDispatcherException();
00217         }
00218       }
00219     }
00220 
00225     virtual ~TipcDispatcher () throw ()
00226     {
00227       if (_destroyFlag) {
00228         TipcDispatcherDestroy(_dispatcher);
00229       }
00230     }
00231 
00235     operator T_IPC_DISPATCHER ()
00236     const throw ()
00237     {
00238       return _dispatcher;
00239     }
00240 
00246     bool dispatch(real8 timeout = T_TIMEOUT_FOREVER)
00247     throw (TipcException);
00248 
00255     bool mainLoop(real8 timeout = T_TIMEOUT_FOREVER)
00256     throw (TipcException);
00257 
00258 
00266     void eventTraverse
00267     (
00268     TipcDispatcherTraverser* travObj
00269     ) throw (TipcException);
00270 
00271 
00288     MessageEvent* createMsgTypeEvent
00289     (
00290     const TipcSrv& srv,
00291     const TipcMt& mt,
00292     MessageEventHandler* handlerObj
00293     ) throw (TipcException)
00294     {
00295       MessageEventHandler::Handler hndlrMethod =
00296       &MessageEventHandler::onMessage;
00297       MessageEvent* event = new MessageEvent(handlerObj,hndlrMethod);
00298 
00299       if (!event)
00300         throw TipcDispatcherException(T_ERR_NOMEM);
00301 
00302       T_IPC_EVENT cEvent = TipcEventCreateMsgType(_dispatcher,
00303                                                   (T_IPC_SRV) srv,
00304                                                   (T_IPC_MT) mt,
00305                                                   &TipcDispatcher::messageEventDelegator,
00306                                                   event);
00307       if (!cEvent)
00308         throw TipcDispatcherException();
00309 
00310       event->setEvent(cEvent);
00311 
00312       return event;
00313 
00314     }
00315 
00316 #if 0
00317 
00332     MessageEvent* createMsgDefaultEvent
00333     (
00334     const TipcSrv& srv,
00335     MessageEventHandler* handlerObj,
00336     MessageEventHandler::Handler handlerMethod = &MessageEventHandler::onMessage
00337     ) throw (TipcException)
00338     {
00339       MessageEvent* event = new MessageEvent(handlerObj,handlerMethod);
00340 
00341       if (!event)
00342         throw TipcDispatcherException(T_ERR_NOMEM);
00343 
00344       T_IPC_EVENT cEvent = TipcEventCreateMsgDefault(_dispatcher,
00345                                                      (T_IPC_SRV) srv,
00346                                                      messageEventDelegator,
00347                                                      event);
00348       if (!cEvent)
00349         throw TipcDispatcherException();
00350 
00351       event->setEvent(cEvent);
00352 
00353       return event;
00354 
00355     }
00356 #endif
00357 
00371     MessageEvent* createMsgSubjEvent
00372     (
00373     const TipcSrv& srv,
00374     const char* subject,
00375     MessageEventHandler* handlerObj
00376     ) throw (TipcException)
00377     {
00378       MessageEventHandler::Handler handlerMethod =
00379       &MessageEventHandler::onMessage;
00380       MessageEvent* event = new MessageEvent(handlerObj,handlerMethod);
00381 
00382       if (!event)
00383         throw TipcDispatcherException(T_ERR_NOMEM);
00384 
00385       T_IPC_EVENT cEvent = TipcEventCreateMsg(_dispatcher,
00386                                               (T_IPC_SRV) srv,
00387                                               const_cast <char*>(subject),
00388                                               messageEventDelegator,
00389                                               event);
00390       if (!cEvent)
00391         throw TipcDispatcherException();
00392 
00393       event->setEvent(cEvent);
00394 
00395       return event;
00396 
00397     }
00398 
00399 
00410     TimerEvent* createTimerEvent
00411     (
00412     real8 interval,
00413     TimerEventHandler* handlerObj
00414     ) throw (TipcException)
00415     {
00416       TimerEventHandler::Handler handlerMethod = &TimerEventHandler::onTimer;
00417       TimerEvent* event = new TimerEvent(handlerObj,handlerMethod);
00418 
00419       if (!event)
00420         throw TipcDispatcherException(T_ERR_NOMEM);
00421 
00422       T_IPC_EVENT cEvent = TipcEventCreateTimer(_dispatcher,
00423                                                 interval,
00424                                                 timerEventDelegator,
00425                                                 event);
00426       if (!cEvent)
00427         throw TipcDispatcherException();
00428 
00429       event->setEvent(cEvent);
00430 
00431       return event;
00432     }
00433 
00450     ConnEvent* createConnEvent
00451     (
00452     const TipcConn& connection,
00453     T_IO_CHECK_MODE checkMode,
00454     ConnEventHandler* handlerObj
00455     ) throw (TipcException)
00456     {
00457       ConnEventHandler::Handler handlerMethod =
00458       &ConnEventHandler::onConnEvent;
00459       ConnEvent* event = new ConnEvent(handlerObj,handlerMethod);
00460 
00461       if (!event)
00462         throw TipcDispatcherException(T_ERR_NOMEM);
00463 
00464       T_IPC_EVENT cEvent = TipcEventCreateConn(_dispatcher,
00465                                                (T_IPC_CONN)connection,
00466                                                checkMode,
00467                                                &TipcDispatcher::connEventDelegator,
00468                                                event);
00469       if (!cEvent)
00470         throw TipcDispatcherException();
00471 
00472       event->setEvent(cEvent);
00473 
00474       return event;
00475     }
00476 
00493     SocketEvent* createSocketEvent
00494     (
00495     sock socketFd,
00496     T_IO_CHECK_MODE checkMode,
00497     SocketEventHandler* handlerObj
00498     ) throw (TipcException)
00499     {
00500       SocketEventHandler::Handler handlerMethod =
00501       &SocketEventHandler::onSocket;
00502       SocketEvent* event = new SocketEvent(handlerObj,handlerMethod);
00503 
00504       if (!event)
00505         throw TipcDispatcherException(T_ERR_NOMEM);
00506 
00507       T_IPC_EVENT cEvent = TipcEventCreateSocket(_dispatcher,
00508                                                  socketFd,
00509                                                  checkMode,
00510                                                  &TipcDispatcher::socketEventDelegator,
00511                                                  event);
00512       if (!cEvent)
00513         throw TipcDispatcherException();
00514 
00515       event->setEvent(cEvent);
00516 
00517       return event;
00518     }
00519 
00530     UserEvent* createUserEvent
00531     (
00532     void* data,
00533     UserEventHandler* handlerObj
00534     ) throw (TipcException)
00535     {
00536       UserEventHandler::Handler handlerMethod= &UserEventHandler::onUserEvent;
00537       UserEvent* event = new UserEvent(handlerObj,handlerMethod);
00538 
00539       if (!event)
00540         throw TipcDispatcherException(T_ERR_NOMEM);
00541 
00542       T_IPC_EVENT cEvent = TipcEventCreate(_dispatcher,
00543                                            data,
00544                                            &TipcDispatcher::userEventDelegator,
00545                                            event);
00546       if (!cEvent)
00547         throw TipcDispatcherException();
00548 
00549       event->setEvent(cEvent);
00550 
00551       return event;
00552     }
00553 
00554 
00558     void destroyEvent (TipcEvent* event)
00559     throw (TipcException)
00560     {
00561       try {
00562         event->destroy();
00563 
00564         event = NULL;
00565       }
00566       catch (Exception& e) {
00567         throw TipcDispatcherException(e.getErrNum());
00568       }
00569 
00570     }
00571 
00572 
00584     void addInboundConnection(const TipcSrv& srv)
00585     throw (TipcException)
00586     {
00587       T_IPC_DISPATCHER disp;
00588       if (!TipcSrvConnGetDispatcher((T_IPC_SRV)srv,&disp)) {
00589         throw TipcDispatcherException();
00590       }
00591       if (disp != NULL) {
00592         throw(TipcDispatcherException(T_ERR_ALREADY_EXISTS));
00593       }
00594       if (!TipcSrvConnSetDispatcher((T_IPC_SRV)srv,_dispatcher)) {
00595         throw TipcDispatcherException();
00596       }
00597     }
00598 
00606     void removeInboundConnection(const TipcSrv& srv)
00607     throw (TipcException)
00608     {
00609       T_IPC_DISPATCHER disp;
00610       if (!TipcSrvConnGetDispatcher((T_IPC_SRV)srv,&disp)) {
00611         if (disp != _dispatcher) {
00612           throw(TipcDispatcherException(T_ERR_DOESNT_EXIST));
00613         }
00614       }
00615       if (!TipcSrvConnSetDispatcher((T_IPC_SRV)srv,NULL)) {
00616         throw TipcDispatcherException();
00617       }
00618     }
00619 
00620   private:
00621 
00622     static void T_ENTRY messageEventDelegator (T_IPC_EVENT eventParam,
00623                                                T_IPC_EVENT_DATA eventData,
00624                                                T_PTR arg)
00625     {
00626       MessageEvent* event = (MessageEvent*) arg;
00627       TipcMsg msg(eventData->msg);
00628 
00629       ((event->_handlerObj)->*(event->_handlerMethod)) (*event,msg);
00630 
00631     }
00632 
00633 
00634     static void T_ENTRY socketEventDelegator (T_IPC_EVENT eventParam,
00635                                               T_IPC_EVENT_DATA eventData,
00636                                               T_PTR arg)
00637     {
00638       SocketEvent* event = (SocketEvent*) arg;
00639       sock socketFd = eventData->socket;
00640       T_IO_CHECK_MODE checkMode = eventData->check_mode;
00641 
00642       ((event->_handlerObj)->*(event->_handlerMethod)) (*event,
00643                                                         socketFd,
00644                                                         checkMode);
00645 
00646     }
00647 
00648 
00649     static void T_ENTRY userEventDelegator (T_IPC_EVENT eventParam,
00650                                             T_IPC_EVENT_DATA eventData,
00651                                             T_PTR arg)
00652     {
00653       UserEvent* event = (UserEvent*) arg;
00654       void* data = eventData->data;
00655 
00656       ((event->_handlerObj)->*(event->_handlerMethod)) (*event,
00657                                                         data);
00658 
00659     }
00660 
00661 
00662 
00663     static void T_ENTRY connEventDelegator (T_IPC_EVENT eventParam,
00664                                             T_IPC_EVENT_DATA eventData,
00665                                             T_PTR arg)
00666     {
00667       ConnEvent* event = (ConnEvent*) arg;
00668       TipcConn conn (eventData->conn,false);
00669       T_IO_CHECK_MODE checkMode = eventData->check_mode;
00670 
00671       ((event->_handlerObj)->*(event->_handlerMethod)) (*event,
00672                                                         conn,
00673                                                         checkMode);
00674 
00675     }
00676 
00677 
00678 
00679     static void T_ENTRY timerEventDelegator (T_IPC_EVENT eventParam,
00680                                              T_IPC_EVENT_DATA eventData,
00681                                              T_PTR arg)
00682     {
00683       TimerEvent* event = (TimerEvent*) arg;
00684       real8 interval = eventData->interval;
00685 
00686       ((event->_handlerObj)->*(event->_handlerMethod)) (*event,
00687                                                         interval);
00688 
00689     }
00690 
00691   };
00692 } // namespace SmartSockets
00693 
00694 
00695 #endif //_T_DISPATCHER_HH
00696 

Generated on Fri Jul 14 15:05:54 2006 by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001