Sirikata
libcore/include/sirikata/core/transfer/CacheLayer.hpp
Go to the documentation of this file.
00001 /*  Sirikata Transfer -- Content Transfer management system
00002  *  CacheLayer.hpp
00003  *
00004  *  Copyright (c) 2008, Patrick Reiter Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 /*  Created on: Dec 31, 2008 */
00033 
00034 #ifndef SIRIKATA_CacheLayer_HPP__
00035 #define SIRIKATA_CacheLayer_HPP__
00036 
00037 #include "TransferData.hpp"
00038 #include "URI.hpp"
00039 #include "CachePolicy.hpp"
00040 
00041 
00042 namespace Sirikata {
00043 namespace Transfer {
00044 
00052 typedef std::tr1::function<void(const SparseData*)> TransferCallback;
00053 
00056 class CacheLayer : Noncopyable {
00057 private:
00058     CacheLayer *mRespondTo;
00059     CacheLayer *mNext;
00060 
00061     friend class CacheMap;
00062 
00063     inline void setResponder(CacheLayer *other) {
00064         mRespondTo = other;
00065     }
00066 
00067 protected:
00074     inline void populateParentCaches(const Fingerprint &fileId, const DenseDataPtr &data) {
00075         if (mRespondTo) {
00076             mRespondTo->populateCache(fileId, data);
00077         }
00078     }
00079 
00080     struct CacheEntry {
00081     };
00082 
00088     virtual void destroyCacheEntry(const Fingerprint &fileId, CacheEntry *cacheLayerData, cache_usize_type releaseSize) {
00089     }
00090 
00097     virtual void populateCache(const Fingerprint &fileId, const DenseDataPtr &data) {
00098         populateParentCaches(fileId, data);
00099     }
00100 
00101 public:
00102 
00103     virtual ~CacheLayer() {
00104         if (mNext) {
00105             mNext->setResponder(NULL);
00106         }
00107     }
00108 
00112     CacheLayer(CacheLayer *tryNext)
00113             : mRespondTo(NULL), mNext(tryNext) {
00114         if (tryNext) {
00115             tryNext->setResponder(this);
00116         }
00117     }
00118 
00119     /* UNSAFE (for testing only)!
00120        Ensure you set this back to NULL before deallocating either CacheLayer.
00121     */
00122     void setRespondTo(CacheLayer *newRespond) {
00123         mRespondTo = newRespond;
00124     }
00125 
00130     void setNext(CacheLayer *newNext) {
00131         if (mNext) {
00132             mNext->setResponder(NULL);
00133         }
00134         mNext = newNext;
00135         if (mNext) {
00136             mNext->setResponder(this);
00137         }
00138     }
00139 
00140     CacheLayer *getNext() const {
00141         return mNext;
00142     }
00143 
00144     CacheLayer *getResponder() const {
00145         return mRespondTo;
00146     }
00147 
00148     void addToCache(const Fingerprint &fileId, const DenseDataPtr &data) {
00149         if (mNext) {
00150             mNext->addToCache(fileId, data);
00151         } else {
00152             populateCache(fileId, data);
00153         }
00154     }
00155 
00164     virtual void purgeFromCache(const Fingerprint &fileId) {
00165         if (mNext) {
00166             mNext->purgeFromCache(fileId);
00167         }
00168     }
00169 
00179     virtual void getData(const Fingerprint &fileId, const Range &requestedRange,
00180             const TransferCallback&callback) {
00181         if (mNext) {
00182             mNext->getData(fileId, requestedRange, callback);
00183         } else {
00184             // need some way to signal error
00185             callback(NULL);
00186         }
00187     }
00188 
00189 };
00190 
00191 }
00192 }
00193 
00194 #endif /* SIRIKATA_CacheLayer_HPP__ */