00001 #ifndef __SUT_CACHE_H__ 00002 #define __SUT_CACHE_H__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d S u t C a c h e . h h */ 00006 /* */ 00007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* Produced by Gerri Ganis for CERN */ 00009 /* */ 00010 /* This file is part of the XRootD software suite. */ 00011 /* */ 00012 /* XRootD is free software: you can redistribute it and/or modify it under */ 00013 /* the terms of the GNU Lesser General Public License as published by the */ 00014 /* Free Software Foundation, either version 3 of the License, or (at your */ 00015 /* option) any later version. */ 00016 /* */ 00017 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00018 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00019 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00020 /* License for more details. */ 00021 /* */ 00022 /* You should have received a copy of the GNU Lesser General Public License */ 00023 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00024 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00025 /* */ 00026 /* The copyright holder's institutional names and contributor's names may not */ 00027 /* be used to endorse or promote products derived from this software without */ 00028 /* specific prior written permission of the institution or contributor. */ 00029 /******************************************************************************/ 00030 00031 #include "XProtocol/XPtypes.hh" 00032 #include "XrdSut/XrdSutPFEntry.hh" 00033 #include "XrdOuc/XrdOucHash.hh" 00034 #include "XrdOuc/XrdOucString.hh" 00035 #include "XrdSys/XrdSysPthread.hh" 00036 00037 /******************************************************************************/ 00038 /* */ 00039 /* For caching temporary information during the authentication handshake */ 00040 /* */ 00041 /******************************************************************************/ 00042 00043 class XrdSutPFCacheRef 00044 { 00045 public: 00046 00047 inline void Lock(XrdSysMutex *Mutex) 00048 {if (mtx) {if (mtx != Mutex) mtx->UnLock(); 00049 else return; 00050 } 00051 Mutex->Lock(); 00052 mtx = Mutex; 00053 }; 00054 00055 inline void Set(XrdSysMutex *Mutex) 00056 {if (mtx) {if (mtx != Mutex) mtx->UnLock(); 00057 else return; 00058 } 00059 mtx = Mutex; 00060 }; 00061 00062 inline void UnLock() {if (mtx) {mtx->UnLock(); mtx = 0;}} 00063 00064 XrdSutPFCacheRef() : mtx(0) {} 00065 00066 ~XrdSutPFCacheRef() {if (mtx) UnLock();} 00067 protected: 00068 XrdSysMutex *mtx; 00069 }; 00070 00071 class XrdSutPFCache 00072 { 00073 private: 00074 XrdSysRWLock rwlock; // Access synchronizator 00075 int cachesz; // Number of entries allocated 00076 int cachemx; // Largest Index of allocated entries 00077 XrdSutPFEntry **cachent; // Pointers to filled entries 00078 kXR_int32 utime; // time at which was last updated 00079 int lifetime; // lifetime (in secs) of the cache info 00080 XrdOucHash<kXR_int32> hashtable; // Reflects the file index structure 00081 kXR_int32 htmtime; // time at which hash table was last rebuild 00082 XrdOucString pfile; // file name (if loaded from file) 00083 bool isinit; // true if already initialized 00084 00085 XrdSutPFEntry *Get(const char *ID, bool *wild); 00086 bool Delete(XrdSutPFEntry *pfEnt); 00087 00088 static const int maxTries = 100; // Max time to try getting a lock 00089 static const int retryMSW = 300; // Milliseconds to wait to get lock 00090 00091 public: 00092 XrdSutPFCache() { cachemx = -1; cachesz = 0; cachent = 0; lifetime = 300; 00093 utime = -1; htmtime = -1; pfile = ""; isinit = 0; } 00094 virtual ~XrdSutPFCache(); 00095 00096 // Status 00097 int Entries() const { return (cachemx+1); } 00098 bool Empty() const { return (cachemx == -1); } 00099 00100 // Initialization methods 00101 int Init(int capacity = 100, bool lock = 1); 00102 int Reset(int newsz = -1, bool lock = 1); 00103 int Load(const char *pfname); // build cache of a pwd file 00104 int Flush(const char *pfname = 0); // flush content to pwd file 00105 int Refresh(); // refresh content from source file 00106 int Rehash(bool force = 0, bool lock = 1); // (re)build hash table 00107 void SetLifetime(int lifet = 300) { lifetime = lifet; } 00108 00109 // Cache management 00110 XrdSutPFEntry *Get(int i) const { return (i<=cachemx) ? cachent[i] : 00111 (XrdSutPFEntry *)0; } 00112 XrdSutPFEntry *Get(XrdSutPFCacheRef &urRef, const char *ID, bool *wild = 0); 00113 XrdSutPFEntry *Add(XrdSutPFCacheRef &urRef, const char *ID, bool force = 0); 00114 bool Remove(const char *ID, int opt = 1); 00115 int Trim(int lifet = 0); 00116 00117 // For debug purposes 00118 void Dump(const char *msg= 0); 00119 }; 00120 00121 #endif 00122