source: source/ariba/utility/misc/Demultiplexer.hpp@ 9633

Last change on this file since 9633 was 3705, checked in by Christoph Mayer, 15 years ago

-einige fixed bzgl. link management, fehlerhafte serialisierer, etc.

File size: 5.3 KB
Line 
1// [License]
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// [License]
38
39#ifndef DEMULTIPLEXER_H__
40#define DEMULTIPLEXER_H__
41
42#include <list>
43#include <iostream>
44#include <map>
45#include <boost/thread/mutex.hpp>
46#include <boost/foreach.hpp>
47#include "ariba/utility/messages/Message.h"
48
49using std::cout;
50using std::list;
51using std::map;
52using std::pair;
53using ariba::utility::Message;
54
55namespace ariba {
56namespace utility {
57
58template<typename S, typename T>
59class Demultiplexer
60{
61private:
62
63 typedef map<S,T> SERVICE_LISTENER_MAP;
64 typedef pair<S,T> SERVICE_LISTENER_PAIR;
65 typedef typename SERVICE_LISTENER_MAP::iterator SERVICE_LISTENER_MAP_ITERATOR;
66 typedef typename SERVICE_LISTENER_MAP::const_iterator SERVICE_LISTENER_MAP_CITERATOR;
67
68 typedef map<T,S> LISTENER_SERVICE_MAP;
69 typedef pair<T,S> LISTENER_SERVICE_PAIR;
70 typedef typename LISTENER_SERVICE_MAP::iterator LISTENER_SERVICE_MAP_ITERATOR;
71 typedef typename LISTENER_SERVICE_MAP::const_iterator LISTENER_SERVICE_MAP_CITERATOR;
72
73 SERVICE_LISTENER_MAP mapServiceListener;
74 LISTENER_SERVICE_MAP mapListenerService;
75 boost::mutex mapMutex;
76
77 void debugprint() {
78 cout << "-------------start--------" << std::endl;
79 {
80 LISTENER_SERVICE_MAP_CITERATOR i = mapListenerService.begin();
81 LISTENER_SERVICE_MAP_CITERATOR iend = mapListenerService.end();
82
83 for( ; i != iend; i++ )
84 cout << "xxx" << i->first.toString() << " -> " << i->second << std::endl;
85 }
86 cout << "-----------------------" << std::endl;
87 {
88 SERVICE_LISTENER_MAP_CITERATOR i = mapServiceListener.begin();
89 SERVICE_LISTENER_MAP_CITERATOR iend = mapServiceListener.end();
90
91 for( ; i != iend; i++ )
92 cout << "xxx" << i->first << " -> " << i->second.toString() << std::endl;
93 }
94 cout << "-------------end---------" << std::endl;
95 }
96
97public:
98
99 Demultiplexer() {
100 }
101
102 ~Demultiplexer() {
103 }
104
105 void registerItem( S id, T listener ) {
106 boost::mutex::scoped_lock lock( mapMutex );
107
108 mapServiceListener.insert( SERVICE_LISTENER_PAIR( id, listener ) );
109 mapListenerService.insert( LISTENER_SERVICE_PAIR( listener, id ) );
110 }
111
112 void unregisterItem( S id ) {
113 T listener = get( id );
114
115 {
116 boost::mutex::scoped_lock lock( mapMutex );
117 mapServiceListener.erase( id );
118 mapListenerService.erase( listener );
119 }
120 }
121
122 void unregisterItem( T listener ) {
123 S id = get( listener );
124 unregisterItem( id );
125 }
126
127 S get( T listener ) {
128 boost::mutex::scoped_lock lock( mapMutex );
129
130 LISTENER_SERVICE_MAP_CITERATOR it = mapListenerService.find( listener );
131 return it->second;
132 }
133
134 T get( S id ) {
135 boost::mutex::scoped_lock lock( mapMutex );
136
137 SERVICE_LISTENER_MAP_CITERATOR it = mapServiceListener.find( id );
138 if( it == mapServiceListener.end() ) return NULL;
139 else return it->second;
140 }
141
142 bool contains( T listener ) {
143 boost::mutex::scoped_lock lock( mapMutex );
144
145 LISTENER_SERVICE_MAP_CITERATOR it = mapListenerService.find( listener );
146 return ( it != mapListenerService.end() );
147 }
148
149 bool contains( S id ) {
150 boost::mutex::scoped_lock lock( mapMutex );
151
152 SERVICE_LISTENER_MAP_CITERATOR it = mapServiceListener.find( id );
153 return ( it != mapServiceListener.end() );
154 }
155
156 typedef list<S> OneList;
157 typedef list<T> TwoList;
158
159 OneList getOneList() {
160 boost::mutex::scoped_lock lock( mapMutex );
161 OneList ret;
162
163 BOOST_FOREACH( SERVICE_LISTENER_PAIR i, mapServiceListener ){
164 ret.push_back( i.first );
165 }
166
167 return ret;
168 }
169
170 TwoList getTwoList() {
171 boost::mutex::scoped_lock lock( mapMutex );
172 TwoList ret;
173
174 BOOST_FOREACH( SERVICE_LISTENER_PAIR i, mapServiceListener ){
175 ret.push_back( i.first );
176 }
177
178 return ret;
179 }
180
181};
182
183}} // namespace ariba, common
184
185#endif // DEMULTIPLEXER_H__
Note: See TracBrowser for help on using the repository browser.