00001 /* 00002 * Timer.h 00003 * 00004 * Copyright 2003, MobileSpear Inc. (www.mobilespear.com). All rights reserved. 00005 * Copyright 2003, David Resnick. All rights reserved. 00006 * 00007 * See the file doc\license.txt for the terms of usage and distribution. 00008 */ 00009 00010 #ifndef _BOGOTEL_TIMER_H 00011 #define _BOGOTEL_TIMER_H 00012 00013 #include <string> 00014 #include <list> 00015 #include <set> 00016 #include <vector> 00017 00018 #include <boost/thread/mutex.hpp> 00019 #include <boost/thread/condition.hpp> 00020 #include <boost/thread/thread.hpp> 00021 00022 #include <gclib.h> 00023 00024 #include <bogotel/BgtErrors.h> 00025 00026 #include <bogotel/STLhelp.h> 00027 #include <bogotel/timertarget.h> 00028 00029 namespace bogotel { 00030 00031 class CDevBase; 00032 00033 class timer_info { 00034 00035 public: 00036 00037 CTimerTarget* m_pTarget; // Device requesting timer. 00038 int m_count; // Number of periodic timers before timer request expires. 00039 int m_iTimerType; 00040 // ID used to detect if the state of the target has changed since 00041 // the timer was set 00042 int m_iTargetStateId; 00043 00044 long m_lTargetId; 00045 00046 /* 00047 * Default constructor 00048 */ 00049 timer_info(int count, CTimerTarget* pTarget, int timerType = 0, int targetStateId = 0); 00050 00051 /* 00052 * Overload the GreaterThan, LessThan, and EqualTo Operators 00053 * so this structure can be sorted in an STL list. 00054 */ 00055 bool operator < (const timer_info & rhs) { 00056 00057 return (this->m_count < rhs.m_count); 00058 } 00059 00060 bool operator > (const timer_info & rhs) { 00061 00062 return (this->m_count > rhs.m_count); 00063 } 00064 00065 bool operator == (const timer_info & rhs) { 00066 if (this->m_iTimerType != rhs.m_iTimerType) 00067 return false; 00068 if (this->m_count != rhs.m_count) 00069 return false; 00070 if (this->m_lTargetId != rhs.m_lTargetId) 00071 return false; 00072 if (this->m_iTargetStateId != rhs.m_iTargetStateId) 00073 return false; 00074 if (this->m_pTarget != rhs.m_pTarget) 00075 return false; 00076 00077 return true; 00078 } 00079 00080 timer_info* operator = (timer_info & rhs) { 00081 00082 this->m_pTarget = rhs.m_pTarget; 00083 this->m_count = rhs.m_count; 00084 this->m_iTimerType = rhs.m_iTimerType; 00085 this->m_iTargetStateId = rhs.m_iTargetStateId; 00086 this->m_lTargetId = rhs.m_lTargetId; 00087 00088 return(this); 00089 } 00090 00091 std::string toString(); 00092 }; 00093 00094 typedef std::list<timer_info*> LST_TIMER; 00095 typedef std::vector<CTimerTarget*> VEC_TIMERTARGET; 00096 typedef std::set<long> SET_LONG; 00097 00098 // This class implements a delayed queue. 00099 // Devices (both signal and voice) use it to be informed of 00100 // termination of processes they have started, such as 00101 // timeout for making a call or playback of a WAV finishing. 00102 // Has a worker thread. 00103 class CTimer 00104 { 00105 00106 public: 00107 00111 CTimer(int period = 100); 00112 virtual ~CTimer(); 00113 00114 void run(); 00115 00116 void add(long lDelay, CTimerTarget *pTarget, int timerType = 0, int stateId = 0); 00117 00118 static void do_thread(void* param); 00119 00120 int registerTarget(CTimerTarget *pTarget); 00121 void requestTargetDeletion(CTimerTarget *pTarget); 00122 00123 private: 00124 00125 void checkForExpirations(); 00126 void handleDefunctTargets(); 00127 bool targetIsActive(long lTargetId); 00128 00129 boost::thread* m_pThrd; 00130 boost::mutex m_mtxTimerInfoList; 00131 boost::mutex m_mtxActiveTimerSet; 00132 boost::mutex m_mtxDefunctVec; 00133 boost::mutex m_mtxStop; 00134 boost::condition m_condStop; 00135 bool m_bStop; 00136 00137 int m_period; // Period Timer Length. 00138 00139 // List of Devices using this timer with time in ticks as key 00140 LST_TIMER m_lstTimer; 00141 // active timer targets 00142 SET_LONG m_setActive; 00143 // timer targets to be removed from the active set 00144 VEC_TIMERTARGET m_vecDefunct; 00145 }; 00146 00147 } 00148 00149 #endif // !defined(_BOGOTEL_TIMER_H)