/[resiprocate]/main/resip/dum/EventDispatcher.hxx
ViewVC logotype

Contents of /main/resip/dum/EventDispatcher.hxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7081 - (show annotations) (download)
Tue Apr 17 14:46:38 2007 UTC (12 years, 7 months ago) by bcampen
File MIME type: text/plain
File size: 4402 byte(s)
Reverting rev 7077 until it can get some review.
1 #ifndef RESIP_EventDispatcher_hxx
2 #define RESIP_EventDispatcher_hxx
3
4 #include <vector>
5
6 #include "resip/dum/DumCommand.hxx"
7 #include "resip/dum/Postable.hxx"
8 #include "rutil/Mutex.hxx"
9 #include "rutil/Lock.hxx"
10
11 namespace resip
12 {
13
14 template<class E>
15 class EventDispatcher
16 {
17 public:
18 EventDispatcher()
19 {
20 }
21
22 bool dispatch(Message* msg)
23 {
24 Lock lock(mMutex);
25 bool ret = false;
26
27 E* event = dynamic_cast<E*>(msg);
28 if (event)
29 {
30 if(mListeners.size() > 0)
31 {
32 ret = true;
33 unsigned int counter = 1;
34 for (std::vector<Postable*>::iterator it = mListeners.begin(); it != mListeners.end(); ++it)
35 {
36 if (counter == mListeners.size())
37 {
38 (*it)->post(msg);
39 }
40 else
41 {
42 ++counter;
43 (*it)->post(msg->clone());
44 }
45 }
46 }
47 }
48
49 return ret;
50 }
51
52 void addListener(Postable* listener)
53 {
54 Lock lock(mMutex);
55 std::vector<Postable*>::iterator it = find(listener);
56 if (it == mListeners.end())
57 {
58 mListeners.push_back(listener);
59 }
60 }
61
62 void removeListener(Postable* listener)
63 {
64 Lock lock(mMutex);
65 std::vector<Postable*>::iterator it = find(listener);
66 if (it != mListeners.end())
67 {
68 mListeners.erase(it);
69 }
70 }
71
72 private:
73 std::vector<Postable*> mListeners;
74 Mutex mMutex;
75
76 std::vector<Postable*>::iterator find(Postable* listener)
77 {
78 std::vector<Postable*>::iterator it;
79 for (it = mListeners.begin(); it != mListeners.end(); ++it)
80 {
81 if (listener == *it)
82 {
83 break;
84 }
85 }
86 return it;
87 }
88 };
89
90 }
91
92
93 #endif
94
95
96 /* ====================================================================
97 * The Vovida Software License, Version 1.0
98 *
99 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
100 *
101 * Redistribution and use in source and binary forms, with or without
102 * modification, are permitted provided that the following conditions
103 * are met:
104 *
105 * 1. Redistributions of source code must retain the above copyright
106 * notice, this list of conditions and the following disclaimer.
107 *
108 * 2. Redistributions in binary form must reproduce the above copyright
109 * notice, this list of conditions and the following disclaimer in
110 * the documentation and/or other materials provided with the
111 * distribution.
112 *
113 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
114 * and "Vovida Open Communication Application Library (VOCAL)" must
115 * not be used to endorse or promote products derived from this
116 * software without prior written permission. For written
117 * permission, please contact vocal@vovida.org.
118 *
119 * 4. Products derived from this software may not be called "VOCAL", nor
120 * may "VOCAL" appear in their name, without prior written
121 * permission of Vovida Networks, Inc.
122 *
123 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
124 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
125 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
126 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
127 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
128 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
129 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
130 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
131 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
132 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
133 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
134 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
135 * DAMAGE.
136 *
137 * ====================================================================
138 *
139 * This software consists of voluntary contributions made by Vovida
140 * Networks, Inc. and many individuals on behalf of Vovida Networks,
141 * Inc. For more information on Vovida Networks, Inc., please see
142 * <http://www.vovida.org/>.
143 *
144 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27