00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef DEMULTIPLEXER_H__
00040 #define DEMULTIPLEXER_H__
00041
00042 #include <list>
00043 #include <iostream>
00044 #include <map>
00045 #include <boost/thread/mutex.hpp>
00046 #include <boost/foreach.hpp>
00047 #include "ariba/utility/messages/Message.h"
00048
00049 using std::cout;
00050 using std::list;
00051 using std::map;
00052 using std::pair;
00053 using ariba::utility::Message;
00054
00055 namespace ariba {
00056 namespace utility {
00057
00058 template<typename S, typename T>
00059 class Demultiplexer
00060 {
00061 private:
00062
00063 typedef map<S,T> SERVICE_LISTENER_MAP;
00064 typedef pair<S,T> SERVICE_LISTENER_PAIR;
00065 typedef typename SERVICE_LISTENER_MAP::iterator SERVICE_LISTENER_MAP_ITERATOR;
00066 typedef typename SERVICE_LISTENER_MAP::const_iterator SERVICE_LISTENER_MAP_CITERATOR;
00067
00068 typedef map<T,S> LISTENER_SERVICE_MAP;
00069 typedef pair<T,S> LISTENER_SERVICE_PAIR;
00070 typedef typename LISTENER_SERVICE_MAP::iterator LISTENER_SERVICE_MAP_ITERATOR;
00071 typedef typename LISTENER_SERVICE_MAP::const_iterator LISTENER_SERVICE_MAP_CITERATOR;
00072
00073 SERVICE_LISTENER_MAP mapServiceListener;
00074 LISTENER_SERVICE_MAP mapListenerService;
00075 boost::mutex mapMutex;
00076
00077 void debugprint() {
00078 cout << "-------------start--------" << std::endl;
00079 {
00080 LISTENER_SERVICE_MAP_CITERATOR i = mapListenerService.begin();
00081 LISTENER_SERVICE_MAP_CITERATOR iend = mapListenerService.end();
00082
00083 for( ; i != iend; i++ )
00084 cout << "xxx" << i->first.toString() << " -> " << i->second << std::endl;
00085 }
00086 cout << "-----------------------" << std::endl;
00087 {
00088 SERVICE_LISTENER_MAP_CITERATOR i = mapServiceListener.begin();
00089 SERVICE_LISTENER_MAP_CITERATOR iend = mapServiceListener.end();
00090
00091 for( ; i != iend; i++ )
00092 cout << "xxx" << i->first << " -> " << i->second.toString() << std::endl;
00093 }
00094 cout << "-------------end---------" << std::endl;
00095 }
00096
00097 public:
00098
00099 Demultiplexer() {
00100 }
00101
00102 ~Demultiplexer() {
00103 }
00104
00105 void registerItem( S id, T listener ) {
00106 boost::mutex::scoped_lock lock( mapMutex );
00107
00108 mapServiceListener.insert( SERVICE_LISTENER_PAIR( id, listener ) );
00109 mapListenerService.insert( LISTENER_SERVICE_PAIR( listener, id ) );
00110 }
00111
00112 void unregisterItem( S id ) {
00113 T listener = get( id );
00114
00115 {
00116 boost::mutex::scoped_lock lock( mapMutex );
00117 mapServiceListener.erase( id );
00118 mapListenerService.erase( listener );
00119 }
00120 }
00121
00122 void unregisterItem( T listener ) {
00123 S id = get( listener );
00124 unregisterItem( id );
00125 }
00126
00127 S get( T listener ) {
00128 boost::mutex::scoped_lock lock( mapMutex );
00129
00130 LISTENER_SERVICE_MAP_CITERATOR it = mapListenerService.find( listener );
00131 return it->second;
00132 }
00133
00134 T get( S id ) {
00135 boost::mutex::scoped_lock lock( mapMutex );
00136
00137 SERVICE_LISTENER_MAP_CITERATOR it = mapServiceListener.find( id );
00138 if( it == mapServiceListener.end() ) return NULL;
00139 else return it->second;
00140 }
00141
00142 bool contains( T listener ) {
00143 boost::mutex::scoped_lock lock( mapMutex );
00144
00145 LISTENER_SERVICE_MAP_CITERATOR it = mapListenerService.find( listener );
00146 return ( it != mapListenerService.end() );
00147 }
00148
00149 bool contains( S id ) {
00150 boost::mutex::scoped_lock lock( mapMutex );
00151
00152 SERVICE_LISTENER_MAP_CITERATOR it = mapServiceListener.find( id );
00153 return ( it != mapServiceListener.end() );
00154 }
00155
00156 typedef list<S> OneList;
00157 typedef list<T> TwoList;
00158
00159 OneList getOneList() {
00160 boost::mutex::scoped_lock lock( mapMutex );
00161 OneList ret;
00162
00163 BOOST_FOREACH( SERVICE_LISTENER_PAIR i, mapServiceListener ){
00164 ret.push_back( i.first );
00165 }
00166
00167 return ret;
00168 }
00169
00170 TwoList getTwoList() {
00171 boost::mutex::scoped_lock lock( mapMutex );
00172 TwoList ret;
00173
00174 BOOST_FOREACH( SERVICE_LISTENER_PAIR i, mapServiceListener ){
00175 ret.push_back( i.first );
00176 }
00177
00178 return ret;
00179 }
00180
00181 };
00182
00183 }}
00184
00185 #endif // DEMULTIPLEXER_H__