00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 Beesnest is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* DcFilecacher.h: interface for the CdcFilecacher class. */ 00021 00022 /** 00023 * This class caches files for the HTTP server. it stores the file name, data 00024 * and last modified time in a map. The class is thread safe, it built to be 00025 * accessed from multiple threads. 00026 * <p> 00027 * In addition there is a lower priority thread that deletes the last access 00028 * cached file, if there are too many files in the cache. In the server 00029 * configuration file <b>Cache-Limit</b> is the maximum number of files to 00030 * cache. If this number is set to 0 (or missing) this class will not do a 00031 * thing (cache or start the cleanup thread). 00032 * <p> 00033 * What make the File cacher efficient is the Copy On Write (or Reference 00034 * Counting) of the CdcBuffer class. So its not really copies the data each time 00035 * we transfer a buffer from object to object. 00036 * 00037 * @author Erez Bibi 00038 * @version 1.0 00039 */ 00040 00041 #ifndef __CDC_FILE_cacher 00042 #define __CDC_FILE_cacher 00043 00044 #include "DcGlobals.h" 00045 #include "DcBuffer.h" 00046 #include "DcParameters.h" 00047 #include "DcHttpTime.h" 00048 #include _GET_INCLUDE_PATH (DcMutex.h) 00049 #include _GET_INCLUDE_PATH (DcSemaphore.h) 00050 #include _GET_INCLUDE_PATH (DcThread.h) 00051 00052 00053 class CdcFileCacher 00054 { 00055 private: 00056 00057 /** 00058 * This is an inner class that represents a file to store. 00059 */ 00060 class CdcFile 00061 { 00062 public: 00063 /** The file data. */ 00064 CdcBuffer aData; 00065 /** The file last modified time and date. */ 00066 CdcHttpTime oModifiedTime; 00067 /** The name of the file. */ 00068 CdcString sFileName; 00069 /** A number that indicates when this file was last accessed, */ 00070 unsigned int nAccessNumber; 00071 /** A Mutex for the file (can be accessed just by one thread at a time. */ 00072 CdcMutex oMutex; 00073 00074 /** Constructor. */ 00075 CdcFile (const CdcString& name, const CdcHttpTime& time, 00076 const CdcBuffer& data, int number) 00077 : aData (data), oModifiedTime (time), sFileName (name), 00078 nAccessNumber (number) 00079 {}; 00080 }; 00081 00082 00083 /** An access counter for all the files. */ 00084 unsigned int nAccessCounter; 00085 00086 /** Maximum number of files to cache. */ 00087 int nCacheLimit; 00088 00089 typedef map <string, CdcFile*> t_file_map; 00090 00091 /** The map to store the files in. */ 00092 t_file_map oMap; 00093 00094 /** The files limiter thread object. */ 00095 CdcThread oThread; 00096 00097 /** A semaphore to start the thread loop, (checks the number of files). */ 00098 CdcSemaphore oStartThread; 00099 00100 /** Flag to the thread to exit. */ 00101 bool isLimitThreadGo; 00102 00103 public: 00104 00105 /** 00106 * Constructor - Don't do much. The work get done in Init. 00107 */ 00108 CdcFileCacher(); 00109 00110 /** 00111 * Destructor - Delete the map and all its content, and delete the limiter 00112 * thread. 00113 */ 00114 virtual ~CdcFileCacher(); 00115 00116 /** 00117 * Since this class will form just one static object, I will use Init to 00118 * initialize the object, instead of the constructor (that runs before main) 00119 * Init finds out how many files to cache, and if this is a positive number, 00120 * it starts the limiter thread. 00121 */ 00122 void Init (); 00123 00124 /** 00125 * Try to get a file from the cache, by name and date of last 00126 * modification. 00127 * <p> 00128 * @param name The name of the required file. 00129 * @param time The last modified time of the required file. 00130 * @param data Reference to the data of the file that will be returned from 00131 * the cache (if the file does not exist in the cache, this parameter will 00132 * not be changed). 00133 * @return True if the file is in the cache. 00134 */ 00135 bool GetFile (const CdcString& name, const CdcHttpTime& time, CdcBuffer& data); 00136 00137 /** 00138 * Try to put a file into the cache, by name and date of last modification. 00139 * <p> 00140 * @param name The name of the file to insert. 00141 * @param time The last modified time of the file to insert. 00142 * @param data Reference to the data of the file. 00143 * @return True if the file was not in the cache. 00144 */ 00145 bool PutFile (const CdcString& name, const CdcHttpTime& time, const CdcBuffer& data); 00146 00147 00148 /** 00149 * This function runs on the map, finds the last accessed file and deletes 00150 * it. if the file has been accessed since it was found, the function will 00151 * return without doing a thing. 00152 */ 00153 void DeleteLast (); 00154 00155 /** 00156 * Global function to the number of files limiter thread. 00157 */ 00158 friend int DcLimit (void* pParam); 00159 00160 }; 00161 00162 #endif /* __CDC_FILE_cacher */