#pragma once #include #include "Tree/Service.hpp" #include "DataModel/ContentProvider.hpp" #include "Utility/ContentProviderJob.hpp" #include "Utility/ControlledLRUCache.hpp" #include #include typedef boost::unordered_set Set; namespace Aya { extern const char* const sCacheableContentProvider; class CacheableContentProvider : public DescribedNonCreatable , public Service , public HeartbeatInstance { typedef DescribedNonCreatable Super; protected: class CachedItem { public: AsyncHttpQueue::RequestResult requestResult; shared_ptr data; public: CachedItem(shared_ptr data = shared_ptr(), AsyncHttpQueue::RequestResult result = AsyncHttpQueue::Waiting) : data(data) , requestResult(result) { } ~CachedItem() { data.reset(); } friend class CacheableContentProvider; }; shared_ptr contentJob; Aya::atomic pendingRequests; bool immediateMode; boost::scoped_ptr>> lruCache; boost::mutex failedCacheMutex; Set failedCache; public: CacheableContentProvider(CacheSizeEnforceMethod enforceMethod, unsigned long size); virtual ~CacheableContentProvider(); void setCacheSize(int size); void setImmediateMode(); bool isRequestQueueEmpty() { return pendingRequests == 0; } bool clearContent(); bool hasContent(const ContentId& id); boost::shared_ptr requestContent(const ContentId& id, float priority, bool markUsed, AsyncHttpQueue::RequestResult& result); boost::shared_ptr blockingRequestContent(const ContentId& id, bool markUsed); boost::shared_ptr fetchContent(const ContentId& id); bool isAssetFailed(const ContentId& id); // HeartbeatInstance /*override*/ void onHeartbeat(const Heartbeat& event); /*override*/ void onServiceProvider(ServiceProvider* oldProvider, ServiceProvider* newProvider); protected: static void LoadContentCallbackHelper(boost::weak_ptr cacheableContentProvider, AsyncHttpQueue::RequestResult result, std::istream* filestream, shared_ptr data, std::string id); void LoadContentCallback(AsyncHttpQueue::RequestResult result, std::istream* filestream, shared_ptr data, std::string id); static TaskScheduler::StepResult ProcessTaskHelper( boost::weak_ptr weakCcp, const std::string& id, shared_ptr data); virtual TaskScheduler::StepResult ProcessTask(const std::string& id, shared_ptr data) = 0; static void ErrorTaskHelper(boost::weak_ptr weakCcp, const std::string& id); virtual void ErrorTask(const std::string& id); bool isAssetContent(ContentId id); void markContentFailed(const std::string& id); AsyncHttpQueue::RequestResult getContentStatus(const std::string& id); virtual void updateContent(const std::string& id, boost::shared_ptr cachedItem); }; } // namespace Aya