Module: sems
Branch: master
Commit: b43816f3a84935f090cefa65511886f66b297cd7
URL:    
http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=b43816f3a84935f090cefa65511886f66b297cd7

Author: Stefan Sayer <[email protected]>
Committer: Stefan Sayer <[email protected]>
Date:   Fri Nov  4 12:26:59 2011 +0100

milliseconds precision for setting user timers

Conflicts:

        core/AmSession.cpp

---

 core/AmSession.cpp                       |   14 +++++++++++---
 core/AmSession.h                         |    4 ++--
 core/plug-in/session_timer/UserTimer.cpp |   27 ++++++++++++++++++++++++---
 core/plug-in/session_timer/UserTimer.h   |    6 +++++-
 4 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/core/AmSession.cpp b/core/AmSession.cpp
index 888ce6f..708504e 100644
--- a/core/AmSession.cpp
+++ b/core/AmSession.cpp
@@ -1258,17 +1258,25 @@ bool AmSession::timersSupported() {
   return NULL != AmPlugIn::instance()->getFactory4Di("user_timer") ;
 }
 
-bool AmSession::setTimer(int timer_id, unsigned int timeout) {
+bool AmSession::setTimer(int timer_id, double timeout) {
+  if (timeout <= 0.005) {
+    DBG("setting timer %d with immediate timeout - posting Event\n", timer_id);
+    AmPluginEvent* ev = new AmPluginEvent("timer_timeout");
+    ev->data.push(timer_id);
+    postEvent(ev);
+    return true;
+  }
+
   if (NULL == user_timer_ref)
     getUserTimerInstance();
 
   if (NULL == user_timer_ref)
     return false;
 
-  DBG("setting timer %d with timeout %u\n", timer_id, timeout);
+  DBG("setting timer %d with timeout %f\n", timer_id, timeout);
   AmArg di_args,ret;
   di_args.push((int)timer_id);
-  di_args.push((int)timeout);           // in seconds
+  di_args.push((double)timeout);           // in seconds
   di_args.push(getLocalTag().c_str());
   user_timer_ref->invoke("setTimer", di_args, ret);
 
diff --git a/core/AmSession.h b/core/AmSession.h
index cb18516..02122d4 100644
--- a/core/AmSession.h
+++ b/core/AmSession.h
@@ -484,10 +484,10 @@ public:
   /**
      set a Timer
      @param timer_id the ID of the timer (<0 for system timers)
-     @param timeout timeout in seconds
+     @param timeout timeout in seconds (fractal value allowed)
      @return true on success
   */
-  virtual bool setTimer(int timer_id, unsigned int timeout);
+  virtual bool setTimer(int timer_id, double timeout);
 
   /**
      remove a Timer
diff --git a/core/plug-in/session_timer/UserTimer.cpp 
b/core/plug-in/session_timer/UserTimer.cpp
index b144176..f0a3164 100644
--- a/core/plug-in/session_timer/UserTimer.cpp
+++ b/core/plug-in/session_timer/UserTimer.cpp
@@ -5,6 +5,7 @@
 
 #include <sys/time.h>
 #include <unistd.h>
+#include <math.h>
 
 #define SESSION_TIMER_GRANULARITY 100 // check every 100 millisec
 
@@ -131,6 +132,18 @@ void UserTimer::setTimer(int id, int seconds, const 
string& session_id) {
   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) 
 {
@@ -214,9 +227,17 @@ void UserTimer::removeUserTimers(const string& session_id) 
{
 void UserTimer::invoke(const string& method, const AmArg& args, AmArg& ret)
 {
   if(method == "setTimer"){
-    setTimer(args.get(0).asInt(),
-            args.get(1).asInt(),
-            args.get(2).asCStr());
+    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(),
diff --git a/core/plug-in/session_timer/UserTimer.h 
b/core/plug-in/session_timer/UserTimer.h
index 984b319..770d9f0 100644
--- a/core/plug-in/session_timer/UserTimer.h
+++ b/core/plug-in/session_timer/UserTimer.h
@@ -83,9 +83,13 @@ class UserTimer: public AmDynInvoke
 
   bool _running;
 
-  /** set timer with ID id, fire after s seconds event in 
+  /** 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);
 

_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to