Module: sems Branch: 1.5 Commit: d08a0c3a5399d4728c6444f110d8487586b7c621 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=d08a0c3a5399d4728c6444f110d8487586b7c621
Author: Raphael Coeffic <[email protected]> Committer: Raphael Coeffic <[email protected]> Date: Tue Nov 20 15:16:11 2012 +0100 removed obsolete UserTimer --- core/plug-in/session_timer/SessionTimer.cpp | 1 - core/plug-in/session_timer/UserTimer.cpp | 247 --------------------------- core/plug-in/session_timer/UserTimer.h | 101 ----------- 3 files changed, 0 insertions(+), 349 deletions(-) diff --git a/core/plug-in/session_timer/SessionTimer.cpp b/core/plug-in/session_timer/SessionTimer.cpp index 3ae08b4..92ba0ee 100644 --- a/core/plug-in/session_timer/SessionTimer.cpp +++ b/core/plug-in/session_timer/SessionTimer.cpp @@ -27,7 +27,6 @@ #include "SessionTimer.h" #include "AmUtils.h" -#include "UserTimer.h" #include "AmSipHeaders.h" EXPORT_SESSION_EVENT_HANDLER_FACTORY(SessionTimerFactory, MOD_NAME); diff --git a/core/plug-in/session_timer/UserTimer.cpp b/core/plug-in/session_timer/UserTimer.cpp deleted file mode 100644 index 46162d1..0000000 --- a/core/plug-in/session_timer/UserTimer.cpp +++ /dev/null @@ -1,247 +0,0 @@ - -#include "UserTimer.h" - -#include "sip/hash.h" - -#include <sys/time.h> -#include <unistd.h> -#include <math.h> - -#define SESSION_TIMER_GRANULARITY 100 // check every 100 millisec - -/** \brief component for providing user_timer DI interface */ -class UserTimerFactory: public AmDynInvokeFactory -{ -public: - UserTimerFactory(const string& name) - : AmDynInvokeFactory(name) {} - - AmDynInvoke* getInstance(){ - return UserTimer::instance(); - } - - int onLoad(){ -#ifdef SESSION_TIMER_THREAD - UserTimer::instance()->start(); -#endif - return 0; - } - -#ifdef SESSION_TIMER_THREAD - void onUnload() { - DBG("stopping userTimer thread\n"); - AmThreadWatcher::instance()->add(UserTimer::instance()); - UserTimer::instance()->_running = false; - } -#endif -}; - - -EXPORT_PLUGIN_CLASS_FACTORY(UserTimerFactory,"user_timer"); - -UserTimer::UserTimer() -{ -} - -UserTimer::~UserTimer() -{ -} - -UserTimer* UserTimer::_instance=0; - -UserTimer* UserTimer::instance() -{ - if(!_instance) - _instance = new UserTimer(); - return _instance; -} - -#ifdef SESSION_TIMER_THREAD -void UserTimer::run() { - _running = true; - while(_running){ - usleep(SESSION_TIMER_GRANULARITY * 1000); - checkTimers(); - } -} - -void UserTimer::on_stop() { -} -#endif // SESSION_TIMER_THREAD - -bool operator < (const AmTimer& l, const AmTimer& r) -{ - return timercmp(&l.time,&r.time,<); -} - -void UserTimer::checkTimers() { - vector<std::pair<string, int> > expired_timers; - - struct timeval cur_time; - gettimeofday(&cur_time,NULL); - - // run through all buckets - for (unsigned int bucket=0;bucket<TIMERS_LOCKSTRIPE_BUCKETS;bucket++) { - // get expired timers in bucket - timers_mut[bucket].lock(); - if (!timers[bucket].empty()) { - std::multiset<AmTimer>::iterator it = timers[bucket].begin(); - - while (timercmp(&it->time,&cur_time,<) - || timercmp(&it->time,&cur_time,==)) { - int id = it->id; - string session_id = it->session_id; - // erase - timers[bucket].erase(it); - expired_timers.push_back(make_pair(session_id, id)); - - if(timers[bucket].empty()) break; - it = timers[bucket].begin(); - } - } - timers_mut[bucket].unlock(); - } - - for (vector<std::pair<string, int> >::iterator e_it = - expired_timers.begin(); e_it != expired_timers.end(); e_it++) { - // 'fire' timer - if (!AmSessionContainer::instance()->postEvent(e_it->first, - new AmTimeoutEvent(e_it->second))) { - DBG("Timeout Event '%d' could not be posted, session '%s' does not exist any more.\n", - e_it->second, e_it->first.c_str()); - } - else { - DBG("Timeout Event '%d' posted to %s.\n", - e_it->second, e_it->first.c_str()); - } - } -} - -void UserTimer::setTimer(int id, int seconds, const string& session_id) { - struct timeval tval; - gettimeofday(&tval,NULL); - - tval.tv_sec += seconds; - setTimer(id, &tval, session_id); -} - -void UserTimer::setTimer(int id, double seconds, const string& session_id) { - struct timeval tval; - gettimeofday(&tval,NULL); - - struct timeval diff; - diff.tv_sec = trunc(seconds); - diff.tv_usec = 1000000.0*(double)seconds - 1000000.0*trunc(seconds); - timeradd(&tval, &diff, &tval); - - setTimer(id, &tval, session_id); -} - -void UserTimer::setTimer(int id, struct timeval* t, - const string& session_id) -{ - unsigned int bucket = hash(session_id); - - timers_mut[bucket].lock(); - - // erase old timer if exists - unsafe_removeTimer(id, session_id, bucket); - - // add new - timers[bucket].insert(AmTimer(id, session_id, t)); - - timers_mut[bucket].unlock(); -} - - -void UserTimer::removeTimer(int id, const string& session_id) { - unsigned int bucket = hash(session_id); - timers_mut[bucket].lock(); - unsafe_removeTimer(id, session_id, bucket); - timers_mut[bucket].unlock(); -} - - -unsigned int UserTimer::hash(const string& s1) -{ - return hashlittle(s1.c_str(),s1.length(),0) - & (TIMERS_LOCKSTRIPE_BUCKETS-1); -} - -void UserTimer::unsafe_removeTimer(int id, const string& session_id, unsigned int bucket) -{ - // erase old timer if exists - std::multiset<AmTimer>::iterator it = timers[bucket].begin(); - while (it != timers[bucket].end()) { - if ((it->id == id)&&(it->session_id == session_id)) { - timers[bucket].erase(it); - break; - } - it++; - } -} - -void UserTimer::removeTimers(const string& session_id) { - // DBG("removing timers for <%s>\n", session_id.c_str()); - unsigned int bucket = hash(session_id); - timers_mut[bucket].lock(); - std::multiset<AmTimer>::iterator it = timers[bucket].begin(); - while (it != timers[bucket].end()) { - if (it->session_id == session_id) { - std::multiset<AmTimer>::iterator d_it = it; - it++; - timers[bucket].erase(d_it); - // DBG(" o timer removed.\n"); - } else { - it++; - } - } - timers_mut[bucket].unlock(); -} - -void UserTimer::removeUserTimers(const string& session_id) { - // DBG("removing User timers for <%s>\n", session_id.c_str()); - unsigned int bucket = hash(session_id); - timers_mut[bucket].lock(); - std::multiset<AmTimer>::iterator it = timers[bucket].begin(); - while (it != timers[bucket].end()) { - if ((it->id > 0)&&(it->session_id == session_id)) { - std::multiset<AmTimer>::iterator d_it = it; - it++; - timers[bucket].erase(d_it); - // DBG(" o timer removed.\n"); - } else { - it++; - } - } - timers_mut[bucket].unlock(); -} - -void UserTimer::invoke(const string& method, const AmArg& args, AmArg& ret) -{ - if(method == "setTimer"){ - if (isArgInt(args.get(1))) { - setTimer(args.get(0).asInt(), - args.get(1).asInt(), - args.get(2).asCStr()); - } else if (isArgDouble(args.get(1))) { - setTimer(args.get(0).asInt(), - args.get(1).asDouble(), - args.get(2).asCStr()); - } else { - ERROR("unsupported timeout type in '%s'\n", AmArg::print(args).c_str()); - } - } - else if(method == "removeTimer"){ - removeTimer(args.get(0).asInt(), - args.get(1).asCStr()); - } - else if(method == "removeUserTimers"){ - removeUserTimers(args.get(0).asCStr()); - } - else if(method == "stop"){ - _running = false; - } - else - throw AmDynInvoke::NotImplemented(method); -} diff --git a/core/plug-in/session_timer/UserTimer.h b/core/plug-in/session_timer/UserTimer.h deleted file mode 100644 index 8f983d9..0000000 --- a/core/plug-in/session_timer/UserTimer.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Timer class with seconds granularity - */ -#ifndef AM_SESSION_TIMER_H -#define AM_SESSION_TIMER_H - -// if the CallTimer should run as separate thread, -// define this one, otherwise call -// checkTimers periodically -#define SESSION_TIMER_THREAD - -#define TIMEOUT_EVENT_ID 99 - -// 2^5 = 32 timer buckets - should alleviate any lock contention -#define TIMERS_LOCKSTRIPE_POWER 5 -#define TIMERS_LOCKSTRIPE_BUCKETS (1<<TIMERS_LOCKSTRIPE_POWER) - -#ifdef SESSION_TIMER_THREAD -#include "AmThread.h" -#endif -#include "AmSessionContainer.h" - -#include <set> - -/** - * \brief Timer struct containing the alarm time. - */ -struct AmTimer -{ - int id; - string session_id; - - struct timeval time; - - AmTimer(int id, const string& session_id, struct timeval* tval) - : id(id), session_id(session_id), time(*tval) {} -}; - - - -bool operator < (const AmTimer& l, const AmTimer& r); - -/** - * \brief user timer class. - * - * Implements a timer with session granularity. - * On timeout an AmTimeoutEvent with the ID is posted. - */ -class UserTimer: public AmDynInvoke -#ifdef SESSION_TIMER_THREAD -,public AmThread -#endif -{ - static UserTimer* _instance; - - std::multiset<AmTimer> timers[TIMERS_LOCKSTRIPE_BUCKETS]; - AmMutex timers_mut[TIMERS_LOCKSTRIPE_BUCKETS]; - - unsigned int hash(const string& s1); - - void unsafe_removeTimer(int id, const string& session_id, unsigned int bucket); - - public: - UserTimer(); - ~UserTimer(); - - static UserTimer* instance(); - - bool _running; - - /** set timer with ID id, fire after s seconds event in - session session_id */ - void setTimer(int id, int seconds, const string& session_id); - /** set timer with ID id, fire after s seconds event in - session session_id */ - void setTimer(int id, double seconds, const string& session_id); - - /** set timer with ID id, fire at time t event in session session_id */ - void setTimer(int id, struct timeval* t, const string& session_id); - - /** remove timer with ID id */ - void removeTimer(int id, const string& session_id); - /** remove all timers belonging to the session session_id */ - void removeTimers(const string& session_id); - /** remove all timers belonging to the session session_id with an ID > 0 */ - void removeUserTimers(const string& session_id); - - /** ifndef SESSION_TIMER_THREAD, this routine must be - * periodically called. */ - void checkTimers(); - -#ifdef SESSION_TIMER_THREAD - void run(); - void on_stop(); -#endif - - /** DI API */ - void invoke(const string& method, const AmArg& args, AmArg& ret); -}; - -#endif //AM_SESSION_TIMER_H _______________________________________________ Semsdev mailing list [email protected] http://lists.iptel.org/mailman/listinfo/semsdev
