1 /*
2  * Copyright (C) 2011 Stefan Sayer
3  *
4  * This file is part of SEMS, a free SIP media server.
5  *
6  * SEMS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version. This program is released under
10  * the GPL with the additional exemption that compiling, linking,
11  * and/or using OpenSSL is allowed.
12  *
13  * For a license to use the SEMS software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * SEMS is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27 
28 #ifndef _AmAppTimer_h_
29 #define _AmAppTimer_h_
30 
31 #include "sip/wheeltimer.h"
32 #include <string>
33 using std::string;
34 
35 #include <map>
36 #include <set>
37 
38 #define TICKS_PER_SEC (1000000 / TIMER_RESOLUTION)
39 
40 class app_timer;
41 class direct_app_timer;
42 
43 class DirectAppTimer
44 {
45 public:
~DirectAppTimer()46   virtual ~DirectAppTimer() {}
47   virtual void fire()=0;
48 };
49 
50 class _AmAppTimer
51   : public _wheeltimer
52 {
53   typedef std::map<int, app_timer*>   AppTimers;
54   typedef std::map<string, AppTimers> TimerQueues;
55   typedef std::map<DirectAppTimer*,direct_app_timer*> DirectTimers;
56 
57   AmMutex user_timers_mut;
58   TimerQueues user_timers;
59 
60   AmMutex direct_timers_mut;
61   DirectTimers direct_timers;
62 
63   /** creates timer object and inserts it into our container */
64   app_timer* create_timer(const string& q_id, int id, unsigned int expires);
65   /** erases timer - does not delete timer object @return timer object pointer, if found */
66   app_timer* erase_timer(const string& q_id, int id);
67 
68   /* callback used by app_timer */
69   void app_timer_cb(app_timer* at);
70   friend class app_timer;
71 
72   /* callback used by direct_app_timer */
73   void direct_app_timer_cb(direct_app_timer* t);
74   friend class direct_app_timer;
75 
76  public:
77   _AmAppTimer();
78   ~_AmAppTimer();
79 
80   /** set a timer for event queue eventqueue_name with id timer_id and timeout (s) */
81   void setTimer(const string& eventqueue_name, int timer_id, double timeout);
82   /** remove timer for event queue eventqueue_name with id timer_id */
83   void removeTimer(const string& eventqueue_name, int timer_id);
84   /** remove all timers for event queue eventqueue_name */
85   void removeTimers(const string& eventqueue_name);
86 
87   /* set a timer which directly calls your handler */
88   void setTimer(DirectAppTimer* t, double timeout);
89   /* remove a timer which directly calls your handler */
90   void removeTimer(DirectAppTimer* t);
91 
92   /* ONLY use this from inside the timer handler of a direct timer */
93   void setTimer_unsafe(DirectAppTimer* t, double timeout);
94   /* ONLY use this from inside the timer handler of a direct timer */
95   void removeTimer_unsafe(DirectAppTimer* t);
96 };
97 
98 typedef singleton<_AmAppTimer> AmAppTimer;
99 
100 #endif
101