Homepage Demos Overview Downloads Tutorials Reference
Credits

EventRouter.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_EventRouter_h
00003 #define INCLUDED_EventRouter_h
00004 
00005 #include <vector>
00006 #include <list>
00007 #include <map>
00008 #include <algorithm>
00009 #include "EventListener.h"
00010 #include "EventTrapper.h"
00011 #include "Shared/get_time.h"
00012 #include "Shared/debuget.h"
00013 #include <iostream>
00014 
00015 //! This class will handle distribution of events as well as management of timers
00016 /*! Classes must inherit from EventListener and/or EventTrapper in order to
00017  *  receive events.
00018  *
00019  *  Use the global @c ::erouter EventRouter to post and subscribe to
00020  *  events, except if you are posting from within a MotionCommand, in
00021  *  which case you should use MotionCommand::postEvent() so that it
00022  *  will automatically be sent from the Motion process to the Main
00023  *  process.
00024  *
00025  *  When multiple listeners are subscribed, the order in which an event is
00026  *  distributed among them looks like this:
00027  *  -# "Specific" listeners: any listener which specifies a particular source id.
00028  *     (It doesn't matter if they specify a type id or not.)
00029  *    - older listeners get events before younger listeners
00030  *  -# "General" listeners: those that subscribe to an entire generator
00031  *    - older listeners get events before younger listeners
00032  *
00033  *  ...but if you're relying on that ordering, there should be a cleaner
00034  *  way to do whatever you're doing.
00035  *
00036  *  If one behaviors unsubscribes another one during a processEvent(),
00037  *  that behavior may still get the "current" event before the
00038  *  unsubscription takes place.  This is not a prescribed behavior,
00039  *  and also should not be relied on one way or the other.
00040  *
00041  *  Timer events are only sent to the generator which requested them.  So if 
00042  *  EventListener @e A requests a timer with ID 0 at two second intervals,
00043  *  and EventListener @e B requests a timer with ID 0 at three second intervals,
00044  *  each will still only receive the timers they requested - no cross talk.
00045  *  The timer generator is unique in this regard, which is why it is built in
00046  *  as an integral component of the EventRouter.  All other events are broadcast.
00047  *
00048  *  If an EventListener/EventTrapper subscribes to the same event source multiple
00049  *  times, it will receive multiple copies of the event.  However, the first call
00050  *  to removeListener for a source will remove all subscriptions to that source.\n
00051  *  Example: EventListener @e A subscribes to (buttonEGID,*,*), and twice to
00052  *  (buttonEGID,0,*).
00053  *    - If button 0 is pressed, @e A will get three copies of the event.
00054  *    - If button 1 is pressed, @e A will get one copy.
00055  *    - If removeListener(&A,buttonEGID) is called, the (buttonEGID,*,*) is
00056  *      removed, <em>as well as</em> both of (buttonEGID,0,*).
00057  *    - If removeListener(&A,buttonEGID,0) is called, both of (buttonEGID,0,*)
00058  *      are removed, but (buttonEGID,*,*) would be untouched.
00059  *
00060  *  The buffered event distribution has not been tested thoroughly,
00061  *  and should be considered deprecated.
00062  *
00063  *  @see EventBase::EventGeneratorID_t for a complete listing of all generators,
00064  *  as well as instructions on how to add new generators.
00065  */
00066 class EventRouter : public EventListener {
00067  public:
00068   EventRouter(); //!< Constructs the router, #buffertime defaults to 1
00069   virtual ~EventRouter() { reset(); removeAllTimers(); } //!< just calls reset and removeAllTimers()
00070   void reset() { listeners.clear(); trappers.clear(); removeAllTimers(); } //!< erases all listeners, trappers and timers, resets EventRouter
00071   
00072   void setBufferTime(unsigned int t) { buffertime=t; } /*!< @brief sets the time to wait between buffer clears, see EventRouter::buffertime. @param t time to wait between buffer clears @see buffertime */
00073   unsigned int getBufferTime() { return buffertime; } /*!< @brief returns the time to wait between buffer clears, see EventRouter::buffertime. @return time to wait between buffer clears @see buffertime */
00074 
00075   //!@name Posting/Processing Events
00076   /*!@brief recommended to create and post an event using current buffer setting */
00077   void postEvent(EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid, unsigned int dur) { if(buffertime>0) events.push_back(new EventBase(egid,sid,etid,dur)); else processEvent(EventBase(egid,sid,etid,dur)); }
00078   void postEvent(EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid, unsigned int dur, const std::string& n, float m) { if(buffertime>0) events.push_back(new EventBase(egid,sid,etid,dur,n,m)); else processEvent(EventBase(egid,sid,etid,dur,n,m)); }
00079   void postEvent(EventBase* e) { if(buffertime>0) events.push_back(e); else { processEvent(*e); delete e; } }
00080 
00081   //! determines if timers need to be posted, and posts them if so.
00082   /*! Call this often to ensure accurate timers.  Also, will clear event buffer before sending
00083       timer events in order to ensure correct ordering of event reception */
00084   void processTimers();
00085   void processEventBuffer(); //!< clears the event buffer, deletes events as it does so.
00086   void processEvent(const EventBase& e); //!< forces unbuffered - sends event *now*.  Will clear the buffer first if needed to ensure proper event ordering
00087   //@}
00088 
00089   //!@name Listener Detection
00090   /*!@brief <b>counts both listeners and trappers</b>, so stuff can tell if it even needs to bother generating an event...*/
00091   /* ... if a tree falls in a forest, and there's no one around to see it, does it make a sound?\n
00092      ... if Vision sees a ball in an image, and there's no listeners, does it make an event? ;) */
00093   bool hasListeners(EventBase::EventGeneratorID_t egid) { return trappers.hasMapping(egid) || listeners.hasMapping(egid); }
00094   bool hasListeners(EventBase::EventGeneratorID_t egid, unsigned int sid) { return trappers.hasMapping(egid,sid) || listeners.hasMapping(egid,sid); }
00095   bool hasListeners(EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid) { return trappers.hasMapping(egid,sid,etid) || listeners.hasMapping(egid,sid,etid); }
00096   bool hasListeners(const EventBase& e) { return hasListeners(e.getGeneratorID(),e.getSourceID(),e.getTypeID()); }
00097   //@}
00098 
00099   //!@name Timer Management
00100   void addTimer(EventListener* el, unsigned int sid, unsigned int delay, bool repeat=true); //!< adds a timer if it doesn't exist, or resets the timer if it already exists.
00101   void addTimer(EventListener* el, const EventBase& e, bool repeat=true) { addTimer(el,e.getSourceID(),e.getDuration(),repeat); } //!< calls the other addTimer() with the event's source id and duration, doesn't check to see if the generator is timerEGID
00102   void removeTimer(EventListener* el); //!< clears all pending timers for listener @a el
00103   void removeTimer(EventListener* el, unsigned int sid); //!< clears any pending timers with source id @a sid for listener @a el
00104   void removeAllTimers(); //!< clears all timers for all listeners
00105   //@}
00106 
00107   //!@name Listener Management
00108   //! Adds a listener for all events from a given event generator
00109   void addListener(EventListener* el, EventBase::EventGeneratorID_t egid);
00110   void addListener(EventListener* el, EventBase::EventGeneratorID_t egid, unsigned int sid); //!< Adds a listener for all types from a specific source and generator
00111   void addListener(EventListener* el, EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid); //!< Adds a listener for a specific source id and type from a given event generator
00112   //! Adds a listener for a specific source id and type from a given event generator, adding a Timer event will invoke addTimer(@a el, @a e.getSourceID(), @a e.getDuration(), @c true)
00113   void addListener(EventListener* el, const EventBase& e); 
00114 
00115   //! stops sending ALL events to the listener, including timers
00116   void removeListener(EventListener* el); 
00117   //! stops sending specified events from the generator to the listener.
00118   void removeListener(EventListener* el, EventBase::EventGeneratorID_t egid);
00119   void removeListener(EventListener* el, EventBase::EventGeneratorID_t egid, unsigned int sid); //!< stops sending specified events from the generator to the listener.
00120   void removeListener(EventListener* el, EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid); //!< stops sending specified events from the generator to the listener.
00121   //! stops sending specified events from the generator to the listener.  If a timer is passed it will invoke removeTimer(@a el, @a e.getSourceID())
00122   void removeListener(EventListener* el, const EventBase& e);
00123 
00124   /*! @deprecated use removeListener(EventListener* el) instead
00125    *  @brief deprecated, you should call removeListener(EventListener* el) instead */
00126   void forgetListener(EventListener* el) __attribute__((deprecated));
00127   //@}
00128 
00129   //!@name Trapper Management
00130   //! Adds a trapper for a specific source id and type from a given event generator
00131   /*! Note that since timers are not broadcast, they cannot be trapped.  Only the EventListener which requested the timer will receive that timer. */
00132   void addTrapper(EventTrapper* el, const EventBase& e);
00133   void addTrapper(EventTrapper* el, EventBase::EventGeneratorID_t egid); //!< Adds a trapper for all events from a given event generator
00134   void addTrapper(EventTrapper* el, EventBase::EventGeneratorID_t egid, unsigned int sid);  //!< Adds a trapper for all types from a specific source and generator
00135   void addTrapper(EventTrapper* el, EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid); //!< Adds a trapper for a specific source id and type from a given event generator
00136 
00137   void addTrapper(EventTrapper* el); //!< adds a trapper for ALL events
00138 
00139   //! stops sending specified events from the generator to the trapper.
00140   void removeTrapper(EventTrapper* el, const EventBase& e);
00141   void removeTrapper(EventTrapper* el, EventBase::EventGeneratorID_t egid); //!< stops sending specified events from the generator to the trapper.
00142   void removeTrapper(EventTrapper* el, EventBase::EventGeneratorID_t egid, unsigned int sid); //!< stops sending specified events from the generator to the trapper.
00143   void removeTrapper(EventTrapper* el, EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid); //!< stops sending specified events from the generator to the trapper.
00144 
00145   void removeTrapper(EventTrapper* el); //!< stops sending ALL events to the trapper
00146   //@}
00147 
00148  protected:
00149   void doSendBuffer(); //!< does the work of clearing the buffer (calls doSendEvent() )
00150   
00151   //! does the work of sending an event
00152   /*! Be aware this is an O(n^2) where n is the number of listeners for a particular event.
00153    *  This is because after each call to processEvent, the list of listeners could have changed
00154    *  so each listener much be verified before it is sent an event.  New listeners won't get the
00155    *  current event, but neither should listeners which have just be removed */
00156   void doSendEvent(const EventBase& e); 
00157 
00158   //! Contains all the information needed to maintain a timer by the EventRouter
00159   struct TimerEntry {
00160     //! constructors an entry using the given value for next - useful for with TimerEntryPtrCmp
00161     explicit TimerEntry(unsigned int nxt) : el(NULL), sid(0), delay(0), next(nxt), repeat(false) {}
00162     //! constructs with the given values, sets next field automatically; see next
00163     TimerEntry(EventListener* e, unsigned int s, unsigned int d, bool r) : el(e), sid(s), delay(d), next(get_time()+delay), repeat(r) {}
00164     //! just does the default, i'm just being explicit since there's a pointer (no deep copy!)
00165     TimerEntry(const TimerEntry& t) : el(t.el), sid(t.sid), delay(t.delay), next(t.next), repeat(t.repeat) {}
00166     //! just does the default, i'm just being explicit since there's a pointer (no deep copy!)
00167     TimerEntry operator=(const TimerEntry& t) { el=t.el; sid=t.sid; delay=t.delay; next=t.next; repeat=t.repeat; return *this; }
00168     //! will reset timer
00169     /*! @param d the time from now when the timer should go off (in milliseconds)
00170      *  @param r true if the timer should automatically repeat */
00171     void Set(unsigned int d, bool r) { delay=d; repeat=r; next=get_time()+delay; }
00172     EventListener* el;  //!< the listener to fire at
00173     unsigned int sid;   //!< the source id to fire with
00174     unsigned int delay; //!< the delay until firing
00175     unsigned int next;  //!< the time at which this timer will go off next
00176     bool repeat;        //!< if true, will reset after firing, else will be deleted
00177   };
00178   /*! @brief Used by STL to sort the timer list in order of activation time
00179    *  @see EventRouter::timers */
00180   class TimerEntryPtrCmp {
00181   public:
00182     //! Used by STL to sort the timer list in order of activation time; see timers
00183     /*! Since we remove NULLs before sorting, shouldn't need to check here (and I want to know if i'm wrong)
00184      *  @return (a->next<b->next) */
00185     bool operator()(const TimerEntry* const a, const TimerEntry* const b) const { return (a->next<b->next); }
00186   };
00187   typedef std::vector<TimerEntry*>::iterator timer_it_t; //!< makes code more readable
00188   std::vector<TimerEntry*> timers;         //!< the list of timer entries being maintained, kept sorted by time they go active
00189 
00190   //! just for debugging
00191   void chkTimers() {
00192     unsigned int last=0;
00193     for(timer_it_t it=timers.begin(); it!=timers.end(); it++) {
00194       if(last>(*it)->next) {
00195         dispTimers();
00196         return;
00197       }
00198       last=(*it)->next;
00199     }
00200   }
00201   //! just for debugging
00202   void dispTimers() {
00203     std::cout << "out of order timers " << get_time() << " :\t";
00204     unsigned int last=0;
00205     for(timer_it_t it=timers.begin(); it!=timers.end(); it++) {
00206       if(last>(*it)->next)
00207         std::cout << "##";
00208       std::cout << (last=(*it)->next) << '\t';
00209     }
00210     std::cout << std::endl;
00211   }
00212 
00213   std::vector<EventBase*> events;     //!< used to store buffered events
00214   bool doSendBufferLock;              //!< in case of recursive calls to processEventBuffer()/doSendBuffer()
00215   unsigned int lastBufClear;          //!< time of last event buffer clear
00216   unsigned int buffertime;            //!< The time between clearings of the buffer
00217                                       /*!< <li>0 will not use the buffer, events are routed upon posting
00218                                        *   <li>1 will clear the buffer at next call to processTimers() or processEventBuffer()
00219                                        *   <li>a larger value will cause a delay of that number of milliseconds since the last clearing */
00220   
00221   //! Does the actual storage of the mapping between EventBase's and the EventListeners/EventTrappers who should receive them
00222   /*! Actually only stores void*'s, so it's more general than just Listeners or Trappers */
00223   class EventMapper {
00224   public:
00225     //! constructor
00226     EventMapper();
00227 
00228     void addMapping(void* el, EventBase::EventGeneratorID_t egid) { allevents[egid].push_back(el); } //!< Adds a listener for all events from a given event generator
00229     void addMapping(void* el, EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid); //!< Adds a listener for a specific source id and type from a given event generator
00230 
00231     //! Removes a listener for all events from a given event generator, returns true if something was actually removed
00232     /*! Doesn't necessarily remove the vector or mapping if this was the last listener, use clean() to do that */
00233     bool removeMapping(void* el, EventBase::EventGeneratorID_t egid); 
00234 
00235     //! Removes a listener for a specific source id and type from a given event generator, returns true if something was actually removed
00236     /*! Doesn't necessarily remove the vector or mapping if this was the last listener, use clean() to do that */
00237     bool removeMapping(void* el, EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid);
00238 
00239     void clean(); //!<removes empty data structures for all event generators
00240     void clean(EventBase::EventGeneratorID_t egid); //!< removes empty data structures associated with a single event generator
00241     void clear(); //!<Resets the mapping
00242 
00243     //@{
00244     //! so stuff can tell if it even needs to bother generating an event...
00245     /*! ... if a tree falls in a forest, and there's no one around to see it, does it make a sound?\n
00246         ... if Vision sees a ball in an image, and there's no listeners, does it make an event? ;) \n
00247         @return true if it has any listeners, false otherwise */
00248     bool hasMapping(EventBase::EventGeneratorID_t egid);
00249     bool hasMapping(EventBase::EventGeneratorID_t egid, unsigned int sid);
00250     bool hasMapping(EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid);
00251     //@}
00252 
00253     //! builds a list of all listeners which should receive the event, templated to typecast the pointers for you
00254     /*! @param e the key event
00255      *  @param listeners upon return, the resulting list of listeners @a e maps to\n
00256      *  @a listeners is not cleared prior to building, new listeners are pushed on end\n
00257      *  Results are in the order: all specific matches first, all generator listeners second, in order they were added to the EventMapper.*/
00258     template<class T>
00259     void getMapping(const EventBase& e, std::vector<T*>& listeners);
00260 
00261     //! Used to make sure that the specified listener exists for the given event
00262     /*! This is needed because after we call processEvent on a lister, we can't assume
00263      *  that no other listeners have been modified - one listener could cause another
00264      *  to turn off.  If that has happened, we shouldn't send the event, even if it
00265      *  was in the queue originally. */
00266     bool verifyMapping(void * listener, const EventBase& e) { return verifyMapping(listener,e.getGeneratorID(),e.getSourceID(),e.getTypeID()); }
00267     //! Used to make sure that the specified listener exists for the given event
00268     /*! This is needed because after we call processEvent on a lister, we can't assume
00269      *  that no other listeners have been modified - one listener could cause another
00270      *  to turn off.  If that has happened, we shouldn't send the event, even if it
00271      *  was in the queue originally. */
00272     bool verifyMapping(void * listener, EventBase::EventGeneratorID_t egid, unsigned int sid, EventBase::EventTypeID_t etid);
00273 
00274   protected:
00275     //! a mapping from source IDs (unsigned ints), each to a vector of pointers to listeners
00276     /*! main use in filteredevents @see filteredevents */
00277     typedef std::map<unsigned int,std::vector<void*>,std::less<unsigned int> > SIDtoListenerVectorMap_t;
00278     
00279     //! an array of vectors of pointers to listeners... in other words, a vector of listener pointers for each generator
00280     std::vector<void*> allevents[EventBase::numEGIDs];
00281     //! not for the faint of heart: a matrix of mappings to vectors of pointers to listeners
00282     SIDtoListenerVectorMap_t* filteredevents[EventBase::numEGIDs][EventBase::numETIDs];
00283 
00284   private:
00285     EventMapper(const EventMapper&);           //!< this shouldn't be called...
00286     EventMapper operator=(const EventMapper&); //!< this shouldn't be called...
00287   };
00288 
00289   EventMapper trappers;  //!< A mapping of which EventTrapper's should get a chance to trap the event
00290   EventMapper listeners; //!< A mapping of which EventListener's should receive events
00291 
00292 };
00293 
00294 //! a global router for cross communication, probably the most common usage, although perhaps there may be times you'd rather have multiple event routers for smaller sections
00295 extern EventRouter * erouter;
00296 
00297 /*! @file
00298  * @brief Describes EventRouter class, for distribution and trapping of events to listeners
00299  * @author ejt (Creator)
00300  *
00301  * $Author: ejt $
00302  * $Name: tekkotsu-2_2 $
00303  * $Revision: 1.16 $
00304  * $State: Exp $
00305  * $Date: 2004/10/07 22:14:17 $
00306  */
00307 
00308 #endif

Tekkotsu v2.2
Generated Tue Oct 19 14:19:14 2004 by Doxygen 1.3.9.1