00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __CODEX_QUORUM_RESPONSETRACKER_H__
00022 #define __CODEX_QUORUM_RESPONSETRACKER_H__
00023
00024 #include "ResponseInfo.h"
00025
00026 namespace CODEX_Quorum
00027 {
00032 template <size_t _Tn>
00033 struct memless : public binary_function<const void*,const void*,bool>
00034 {
00036 bool operator()(const void* __x, const void* __y) const {
00037 return memcmp(__x,__y,_Tn) < 0; }
00038 };
00039
00046 class ResponseTracker
00047 {
00048 public :
00050 virtual ~ResponseTracker() {}
00051
00057 virtual void insert(unsigned char* key,ResponseInfo* m) = 0;
00058
00067 virtual bool remove(const unsigned char* key, bool del=false) = 0;
00068
00080 virtual bool check(const unsigned char* data,
00081 int length,
00082 unsigned int server)
00083 = 0;
00084
00089 virtual ResponseInfo* operator()(const unsigned char* msgID) = 0;
00090
00097 virtual unsigned char* extractKey(const unsigned char* data) = 0;
00098 };
00099
00108 template< size_t ST, size_t MT >
00109 class ConcreteResponseTracker : public ResponseTracker
00110 {
00111 public :
00113 typedef map< const unsigned char*, ResponseInfo*, memless< MT > >
00114 MessageResponseMap;
00116 typedef typename MessageResponseMap::iterator MessageResponseMapItr;
00117
00122 ConcreteResponseTracker( unsigned char* self ) :
00123 m_self( self )
00124 {
00125 }
00126
00128 virtual ~ConcreteResponseTracker() {
00129 delete [] m_self;
00130 while( m_map.size() > 0 )
00131 {
00132 MessageResponseMapItr item = m_map.begin();
00133 m_map.erase(item);
00134 delete [] item->first;
00135 delete item->second;
00136 }
00137 }
00138
00139 void insert(unsigned char* key,ResponseInfo* m) {
00140 m_map.insert(MessageResponseMap::value_type(key,m));
00141 }
00142
00143 bool remove(const unsigned char* key, bool del=false) {
00144 MessageResponseMapItr item = m_map.find(key);
00145 if ( m_map.end() == item )
00146 {
00147 return false;
00148 }
00149 m_map.erase(item);
00150
00151
00152 delete [] item->first;
00153 if ( del )
00154 {
00155 delete item->second;
00156 }
00157 return true;
00158 }
00159
00160 bool check( const unsigned char* data,
00161 int length,
00162 unsigned int server ) {
00163
00164 if ( 0 != memcmp(data,m_self,ST) )
00165 {
00166 return false;
00167 }
00168
00169 MessageResponseMapItr item = m_map.find(data+ST);
00170 if ( item == m_map.end() )
00171 {
00172 return false;
00173 }
00174
00175 Message* pMsg = new Message();
00176 pMsg->fill( data+ST+MT, length-ST-MT );
00177 item->second->add(server,pMsg);
00178 return true;
00179 }
00180
00181 ResponseInfo* operator()(const unsigned char* msgID) {
00182 MessageResponseMapItr item = m_map.find(msgID);
00183 if ( item == m_map.end() )
00184 {
00185 return 0;
00186 }
00187 return item->second;
00188 }
00189
00190 unsigned char* extractKey(const unsigned char* data) {
00191 unsigned char* retVal = new unsigned char[MT];
00192 memcpy(retVal,data+ST,MT);
00193 return retVal;
00194 }
00195
00196 private :
00197 unsigned char* m_self;
00198 MessageResponseMap m_map;
00199 };
00200
00201 }
00202
00203 #endif