1 /*
2  * Copyright (C) 2008 Raphael Coeffic
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 #ifndef _AMEVENTDISPATCHER_h_
28 #define _AMEVENTDISPATCHER_h_
29 
30 #include "AmEventQueue.h"
31 #include "AmSipMsg.h"
32 #include <map>
33 
34 #define EVENT_DISPATCHER_POWER   10
35 #define EVENT_DISPATCHER_BUCKETS (1<<EVENT_DISPATCHER_POWER)
36 
37 class AmEventDispatcher
38 {
39 public:
40 
41     struct QueueEntry {
42       AmEventQueueInterface* q;
43       string                 id;
44 
QueueEntryQueueEntry45       QueueEntry()
46 	: q(NULL), id() {}
47 
QueueEntryQueueEntry48       QueueEntry(AmEventQueueInterface* q)
49         : q(q), id() {}
50 
QueueEntryQueueEntry51       QueueEntry(AmEventQueueInterface* q, string id)
52 	: q(q), id(id){}
53     };
54 
55     typedef std::map<string, QueueEntry> EvQueueMap;
56     typedef EvQueueMap::iterator         EvQueueMapIter;
57 
58     typedef std::map<string,string>  Dictionnary;
59     typedef Dictionnary::iterator    DictIter;
60 
61 
62 private:
63 
64     static AmEventDispatcher *_instance;
65 
66     /**
67      * Container for active sessions
68      * local tag -> event queue
69      */
70     EvQueueMap queues[EVENT_DISPATCHER_BUCKETS];
71 
72     // mutex for "queues"
73     AmMutex queues_mut[EVENT_DISPATCHER_BUCKETS];
74 
75     /**
76      * Call ID + remote tag + via_branch -> local tag
77      *  (needed for CANCELs)
78      *  (UAS sessions only)
79      */
80     Dictionnary id_lookup[EVENT_DISPATCHER_BUCKETS];
81     // mutex for "id_lookup"
82     AmMutex id_lookup_mut[EVENT_DISPATCHER_BUCKETS];
83 
84     unsigned int hash(const string& s1);
85     unsigned int hash(const string& s1, const string s2);
86 public:
87 
88     static AmEventDispatcher* instance();
89     static void dispose();
90 
91     bool postSipRequest(const AmSipRequest& req);
92 
93     bool post(const string& local_tag, AmEvent* ev);
94     bool post(const string& callid,
95 	      const string& remote_tag,
96 	      const string& via_branch,
97 	      AmEvent* ev);
98 
99     /* send event to all event queues. Note: event instances will be cloned */
100     bool broadcast(AmEvent* ev);
101 
102     bool addEventQueue(const string& local_tag,
103 		       AmEventQueueInterface* q);
104 
105     bool addEventQueue(const string& local_tag,
106 		       AmEventQueueInterface* q,
107 		       const string& callid,
108 		       const string& remote_tag,
109 		       const string& via_branch);
110 
111     AmEventQueueInterface* delEventQueue(const string& local_tag);
112 
113     bool empty();
114 
115     void dump();
116 };
117 
118 #endif
119