1 | // [Licence]
|
---|
2 | // The Ariba-Underlay Copyright
|
---|
3 | //
|
---|
4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
5 | //
|
---|
6 | // Institute of Telematics
|
---|
7 | // UniversitÀt Karlsruhe (TH)
|
---|
8 | // Zirkel 2, 76128 Karlsruhe
|
---|
9 | // Germany
|
---|
10 | //
|
---|
11 | // Redistribution and use in source and binary forms, with or without
|
---|
12 | // modification, are permitted provided that the following conditions are
|
---|
13 | // met:
|
---|
14 | //
|
---|
15 | // 1. Redistributions of source code must retain the above copyright
|
---|
16 | // notice, this list of conditions and the following disclaimer.
|
---|
17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
18 | // notice, this list of conditions and the following disclaimer in the
|
---|
19 | // documentation and/or other materials provided with the distribution.
|
---|
20 | //
|
---|
21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
32 | //
|
---|
33 | // The views and conclusions contained in the software and documentation
|
---|
34 | // are those of the authors and should not be interpreted as representing
|
---|
35 | // official policies, either expressed or implied, of the Institute of
|
---|
36 | // Telematics.
|
---|
37 | // [Licence]
|
---|
38 |
|
---|
39 | #ifndef DEMULTIPLEXER_H__
|
---|
40 | #define DEMULTIPLEXER_H__
|
---|
41 |
|
---|
42 | #include <list>
|
---|
43 | #include <map>
|
---|
44 | #include <boost/thread/mutex.hpp>
|
---|
45 | #include <boost/foreach.hpp>
|
---|
46 | #include "ariba/utility/messages/Message.h"
|
---|
47 |
|
---|
48 | using std::list;
|
---|
49 | using std::map;
|
---|
50 | using std::pair;
|
---|
51 | using ariba::utility::Message;
|
---|
52 |
|
---|
53 | namespace ariba {
|
---|
54 | namespace utility {
|
---|
55 |
|
---|
56 | template<typename S, typename T>
|
---|
57 | class Demultiplexer
|
---|
58 | {
|
---|
59 | private:
|
---|
60 |
|
---|
61 | typedef map<S,T> SERVICE_LISTENER_MAP;
|
---|
62 | typedef pair<S,T> SERVICE_LISTENER_PAIR;
|
---|
63 | typedef typename SERVICE_LISTENER_MAP::iterator SERVICE_LISTENER_MAP_ITERATOR;
|
---|
64 | typedef typename SERVICE_LISTENER_MAP::const_iterator SERVICE_LISTENER_MAP_CITERATOR;
|
---|
65 |
|
---|
66 | typedef map<T,S> LISTENER_SERVICE_MAP;
|
---|
67 | typedef pair<T,S> LISTENER_SERVICE_PAIR;
|
---|
68 | typedef typename LISTENER_SERVICE_MAP::iterator LISTENER_SERVICE_MAP_ITERATOR;
|
---|
69 | typedef typename LISTENER_SERVICE_MAP::const_iterator LISTENER_SERVICE_MAP_CITERATOR;
|
---|
70 |
|
---|
71 | SERVICE_LISTENER_MAP mapServiceListener;
|
---|
72 | LISTENER_SERVICE_MAP mapListenerService;
|
---|
73 | boost::mutex mapMutex;
|
---|
74 |
|
---|
75 | public:
|
---|
76 |
|
---|
77 | Demultiplexer() {
|
---|
78 | }
|
---|
79 |
|
---|
80 | ~Demultiplexer() {
|
---|
81 | }
|
---|
82 |
|
---|
83 | void registerItem( S id, T listener ) {
|
---|
84 | boost::mutex::scoped_lock lock( mapMutex );
|
---|
85 | {
|
---|
86 | mapServiceListener.insert( SERVICE_LISTENER_PAIR( id, listener ) );
|
---|
87 | mapListenerService.insert( LISTENER_SERVICE_PAIR( listener, id ) );
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | void unregisterItem( S id) {
|
---|
92 | T listener = get( id );
|
---|
93 |
|
---|
94 | boost::mutex::scoped_lock lock( mapMutex );
|
---|
95 | {
|
---|
96 | mapServiceListener.erase( id );
|
---|
97 | mapListenerService.erase( listener );
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | void unregisterItem( T listener ) {
|
---|
102 | S id = get (listener);
|
---|
103 | unregisterItem( id );
|
---|
104 | }
|
---|
105 |
|
---|
106 | S get( T listener ) {
|
---|
107 | boost::mutex::scoped_lock lock( mapMutex );
|
---|
108 | {
|
---|
109 | LISTENER_SERVICE_MAP_CITERATOR it = mapListenerService.find( listener );
|
---|
110 | return it->second;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | T get( S id ) {
|
---|
115 | boost::mutex::scoped_lock lock( mapMutex );
|
---|
116 | {
|
---|
117 | SERVICE_LISTENER_MAP_CITERATOR it = mapServiceListener.find( id );
|
---|
118 |
|
---|
119 | if( it == mapServiceListener.end() ) return NULL;
|
---|
120 | else return it->second;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | bool contains( T listener ) {
|
---|
125 | boost::mutex::scoped_lock lock( mapMutex );
|
---|
126 | {
|
---|
127 | LISTENER_SERVICE_MAP_CITERATOR it = mapListenerService.find( listener );
|
---|
128 | return ( it != mapListenerService.end() );
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | bool contains( S id ) {
|
---|
133 | boost::mutex::scoped_lock lock( mapMutex );
|
---|
134 | {
|
---|
135 | SERVICE_LISTENER_MAP_CITERATOR it = mapServiceListener.find( id );
|
---|
136 | return ( it != mapServiceListener.end() );
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | typedef list<S> OneList;
|
---|
141 | typedef list<T> TwoList;
|
---|
142 |
|
---|
143 | OneList getOneList() const {
|
---|
144 | OneList ret;
|
---|
145 |
|
---|
146 | BOOST_FOREACH( SERVICE_LISTENER_PAIR i, mapServiceListener ){
|
---|
147 | ret.push_back( i.first );
|
---|
148 | }
|
---|
149 |
|
---|
150 | return ret;
|
---|
151 | }
|
---|
152 |
|
---|
153 | TwoList getTwoList() const {
|
---|
154 | TwoList ret;
|
---|
155 |
|
---|
156 | BOOST_FOREACH( SERVICE_LISTENER_PAIR i, mapServiceListener ){
|
---|
157 | ret.push_back( i.first );
|
---|
158 | }
|
---|
159 |
|
---|
160 | return ret;
|
---|
161 | }
|
---|
162 |
|
---|
163 | };
|
---|
164 |
|
---|
165 | }} // namespace ariba, common
|
---|
166 |
|
---|
167 | #endif // DEMULTIPLEXER_H__
|
---|