Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ResponseTracker.h

00001 /*
00002  * Copyright 2003 Michael A. Marsh, Cornell University. All rights reserved.
00003  * This software is released under the modified BSD license.
00004  * See the file LICENSE in the top-level directory for details.
00005  */
00006 //
00007 // $Id: ResponseTracker.h,v 1.4 2005/01/21 19:44:17 mmarsh Exp $
00008 //
00009 // $Log: ResponseTracker.h,v $
00010 // Revision 1.4  2005/01/21 19:44:17  mmarsh
00011 // Updated for compatibility with Doxygen 1.4.1
00012 //
00013 // Revision 1.3  2004/05/19 15:56:56  mmarsh
00014 // *** empty log message ***
00015 //
00016 // Revision 1.2  2003/11/04 22:17:23  mmarsh
00017 // General code cleanup.
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             // We always need to delete the key, since we have the only
00151             // handle to it.
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             // Is this a response to a request from this server?
00164             if ( 0 != memcmp(data,m_self,ST) )
00165             {
00166                return false;
00167             }
00168             // Has the request been registered to collect responses?
00169             MessageResponseMapItr item = m_map.find(data+ST);
00170             if ( item == m_map.end() )
00171             {
00172                return false;
00173             }
00174             // Pass the message along to the ResponseInfo object.
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 /* __CODEX_QUORUM_RESPONSETRACKER_H__ */

Generated on Fri May 6 17:41:02 2005 for COrnell Data EXchange (CODEX) by  doxygen 1.4.1