Homepage Demos Overview Downloads Tutorials Reference
Credits

TimeOutTrans.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_TimeOutTrans_h_
00003 #define INCLUDED_TimeOutTrans_h_
00004 
00005 #include "Behaviors/Transition.h"
00006 #include "Events/EventRouter.h"
00007 
00008 //! causes a transition after a specified amount of time has passed
00009 /*! If any event parameters are specified, this transition will listen
00010  *  for matching events, and if any are received, it will reset the
00011  *  timer */
00012 class TimeOutTrans : public Transition {
00013 public:
00014   //! constructor, specify delay in milliseconds
00015   TimeOutTrans(StateNode* destination, unsigned int delay)
00016     : Transition("TimeOutTrans","TimeOutTrans",destination), d(delay),
00017       eventargcount(0), egid(EventBase::unknownEGID), esid(0), etid(EventBase::statusETID) {}
00018 
00019   //! constructor, specify delay in milliseconds, if any events matching given parameters are received, the timer will be reset
00020   TimeOutTrans(StateNode* destination, unsigned int delay, EventBase::EventGeneratorID_t gid)
00021     : Transition("TimeOutTrans","TimeOutTrans",destination), d(delay),
00022       eventargcount(1), egid(gid), esid(0), etid(EventBase::statusETID) {}
00023 
00024   //! constructor, specify delay in milliseconds, if any events matching given parameters are received, the timer will be reset
00025   TimeOutTrans(StateNode* destination, unsigned int delay, EventBase::EventGeneratorID_t gid, unsigned int sid)
00026     : Transition("TimeOutTrans","TimeOutTrans",destination), d(delay),
00027       eventargcount(2), egid(gid), esid(sid), etid(EventBase::statusETID) {}
00028 
00029   //! constructor, specify delay in milliseconds, if any events matching given parameters are received, the timer will be reset
00030   TimeOutTrans(StateNode* destination, unsigned int delay, EventBase::EventGeneratorID_t gid, unsigned int sid, EventBase::EventTypeID_t tid)
00031     : Transition("TimeOutTrans","TimeOutTrans",destination), d(delay),
00032       eventargcount(3), egid(gid), esid(sid), etid(tid) {}
00033 
00034   //!starts timer
00035   virtual void DoStart() {
00036     Transition::DoStart();
00037     switch (eventargcount) {
00038     case 1: erouter->addListener(this,egid); break;
00039     case 2: erouter->addListener(this,egid,esid); break;
00040     case 3: erouter->addListener(this,egid,esid,etid); break;
00041     };
00042     resetTimer(); 
00043   }
00044 
00045   //!stops timer
00046   virtual void DoStop() {
00047     erouter->removeListener(this);
00048     Transition::DoStop();
00049   }
00050   
00051   //!resets timer
00052   void resetTimer() {
00053     // std::cout << "Reset @ " << get_time() << " stop @ " << get_time()+d << ' ' << this << std::endl;
00054     erouter->addTimer(this,0,d,false);
00055   }
00056 
00057   //!if we receive the timer event, fire()
00058   virtual void processEvent(const EventBase& e) {
00059     // std::cout << "Timeout @ " << get_time() << " from " << event.getName() << ' ' << this << std::endl;
00060     if(e.getGeneratorID()==EventBase::timerEGID)
00061       fire();
00062     else
00063       resetTimer();
00064   }
00065 
00066 protected:
00067   //! constructor, specify delay in milliseconds - use assignment in your subclass's constructor if you want set #egid,#esid,#etid (don't forget #eventargcount!)
00068   TimeOutTrans(const std::string& classname, const std::string& instancename, StateNode* destination, unsigned int delay)
00069     : Transition(classname,instancename,destination), d(delay),
00070       eventargcount(0), egid(EventBase::unknownEGID), esid(0), etid(EventBase::statusETID) {}
00071 
00072   //!amount to delay (in milliseconds) before transition
00073   unsigned int d;
00074 
00075   //!level of specificity of events to listen for
00076   unsigned int eventargcount;
00077 
00078   EventBase::EventGeneratorID_t egid; //!< the event generator to listen for
00079   unsigned int esid; //!< the source to listen for
00080   EventBase::EventTypeID_t etid; //!< the type to listen for
00081 
00082 };
00083 
00084 /*! @file
00085  * @brief Defines TimeOutTrans, which causes a transition after a specified amount of time has passed
00086  * @author ejt (Creator)
00087  *
00088  * $Author: ejt $
00089  * $Name: tekkotsu-2_2_2 $
00090  * $Revision: 1.15 $
00091  * $State: Exp $
00092  * $Date: 2004/12/04 00:11:19 $
00093  */
00094 
00095 #endif

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:15 2005 by Doxygen 1.4.0