00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __CODEX_QUORUM_RESPONSETRACKER_H__
00019 #define __CODEX_QUORUM_RESPONSETRACKER_H__
00020
00021 #include "ResponseInfo.h"
00022
00023 namespace CODEX_Quorum
00024 {
00029 template <size_t _Tn>
00030 struct memless : public binary_function<const void*,const void*,bool>
00031 {
00033 bool operator()(const void* __x, const void* __y) const {
00034 return memcmp(__x,__y,_Tn) < 0; }
00035 };
00036
00043 class ResponseTracker
00044 {
00045 public :
00047 virtual ~ResponseTracker() {}
00048
00054 virtual void insert(unsigned char* key,ResponseInfo* m) = 0;
00055
00064 virtual bool remove(const unsigned char* key, bool del=false) = 0;
00065
00077 virtual bool check(const unsigned char* data,
00078 int length,
00079 unsigned int server)
00080 = 0;
00081
00086 virtual ResponseInfo* operator()(const unsigned char* msgID) = 0;
00087
00094 virtual unsigned char* extractKey(const unsigned char* data) = 0;
00095 };
00096
00105 template< size_t ST, size_t MT >
00106 class ConcreteResponseTracker : public ResponseTracker
00107 {
00108 public :
00110 typedef map< const unsigned char*, ResponseInfo*, memless< MT > >
00111 MessageResponseMap;
00113 typedef typename MessageResponseMap::iterator MessageResponseMapItr;
00114
00116 ConcreteResponseTracker( unsigned char* self ) :
00117 m_self( self )
00118 {
00119 }
00120
00122 virtual ~ConcreteResponseTracker() {
00123 delete [] m_self;
00124 while( m_map.size() > 0 )
00125 {
00126 MessageResponseMapItr item = m_map.begin();
00127 m_map.erase(item);
00128 delete [] item->first;
00129 delete item->second;
00130 }
00131 }
00132
00133 void insert(unsigned char* key,ResponseInfo* m) {
00134 m_map.insert(MessageResponseMap::value_type(key,m));
00135 }
00136
00137 bool remove(const unsigned char* key, bool del=false) {
00138 MessageResponseMapItr item = m_map.find(key);
00139 if ( m_map.end() == item )
00140 {
00141 return false;
00142 }
00143 m_map.erase(item);
00144
00145
00146 delete [] item->first;
00147 if ( del )
00148 {
00149 delete item->second;
00150 }
00151 return true;
00152 }
00153
00154 bool check( const unsigned char* data,
00155 int length,
00156 unsigned int server ) {
00157
00158 if ( 0 != memcmp(data,m_self,ST) )
00159 {
00160 return false;
00161 }
00162
00163 MessageResponseMapItr item = m_map.find(data+ST);
00164 if ( item == m_map.end() )
00165 {
00166 return false;
00167 }
00168
00169 Message* pMsg = new Message();
00170 pMsg->fill( data+ST+MT, length-ST-MT );
00171 item->second->add(server,pMsg);
00172 return true;
00173 }
00174
00175 ResponseInfo* operator()(const unsigned char* msgID) {
00176 MessageResponseMapItr item = m_map.find(msgID);
00177 if ( item == m_map.end() )
00178 {
00179 return 0;
00180 }
00181 return item->second;
00182 }
00183
00184 unsigned char* extractKey(const unsigned char* data) {
00185 unsigned char* retVal = new unsigned char[MT];
00186 memcpy(retVal,data+ST,MT);
00187 return retVal;
00188 }
00189
00190 private :
00191 unsigned char* m_self;
00192 MessageResponseMap m_map;
00193 };
00194
00195 }
00196
00197 #endif