diff --git a/program/Build/RaspberryCloud.sdf b/program/Build/RaspberryCloud.sdf index 4f9c5f3fccc5d753f47837fe77623c5b5edbfd87..fc689ef9c000ed498bd0eccd96149902c6b599d0 100644 Binary files a/program/Build/RaspberryCloud.sdf and b/program/Build/RaspberryCloud.sdf differ diff --git a/program/Build/RaspberryCloud.v12.suo b/program/Build/RaspberryCloud.v12.suo index 0e72be7db554e35abd8b10faa06020a369326f72..96f8b4f1153a378a17093a8ad958187a533169a2 100644 Binary files a/program/Build/RaspberryCloud.v12.suo and b/program/Build/RaspberryCloud.v12.suo differ diff --git a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj index 26fc1aa682480dc35924317fe93786e45acf6e07..cdcaf8ea04be812327e1dbe219a391e6d4813211 100644 --- a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj +++ b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj @@ -89,6 +89,11 @@ <ItemGroup> <ClCompile Include="..\..\Source\app\Application.cpp" /> <ClCompile Include="..\..\Source\app\UI.cpp" /> + <ClCompile Include="..\..\Source\cache\BasicCache.cpp" /> + <ClCompile Include="..\..\Source\cache\FIFOCache.cpp" /> + <ClCompile Include="..\..\Source\cache\LFUCache.cpp" /> + <ClCompile Include="..\..\Source\cache\LRUCache.cpp" /> + <ClCompile Include="..\..\Source\cache\NoCache.cpp" /> <ClCompile Include="..\..\Source\cloud\CloudAccessLayer.cpp" /> <ClCompile Include="..\..\Source\cloud\CloudAdapter.cpp" /> <ClCompile Include="..\..\Source\cloud\CloudManager.cpp" /> @@ -100,7 +105,6 @@ <ClCompile Include="..\..\Source\dataAccess\CloudFileSystem.cpp" /> <ClCompile Include="..\..\Source\dataAccess\ComputeOnClient.cpp" /> <ClCompile Include="..\..\Source\dataAccess\ComputeOnPi.cpp" /> - <ClCompile Include="..\..\Source\dataAccess\ConcreteCache.cpp" /> <ClCompile Include="..\..\Source\dataAccess\Decoder.cpp" /> <ClCompile Include="..\..\Source\dataAccess\Distributor.cpp" /> <ClCompile Include="..\..\Source\dataAccess\Encoder.cpp" /> @@ -119,6 +123,13 @@ <ItemGroup> <ClInclude Include="..\..\Source\app\Application.h" /> <ClInclude Include="..\..\Source\app\UI.h" /> + <ClInclude Include="..\..\Source\cache\BasicCache.h" /> + <ClInclude Include="..\..\Source\cache\Cache.h" /> + <ClInclude Include="..\..\Source\cache\CacheFile.h" /> + <ClInclude Include="..\..\Source\cache\FIFOCache.h" /> + <ClInclude Include="..\..\Source\cache\LFUCache.h" /> + <ClInclude Include="..\..\Source\cache\LRUCache.h" /> + <ClInclude Include="..\..\Source\cache\NoCache.h" /> <ClInclude Include="..\..\Source\cloud\CloudAccessLayer.h" /> <ClInclude Include="..\..\Source\cloud\CloudAdapter.h" /> <ClInclude Include="..\..\Source\cloud\CloudException.h" /> @@ -127,12 +138,10 @@ <ClInclude Include="..\..\Source\cloud\ExampleCloudAdapter.h" /> <ClInclude Include="..\..\Source\cloud\FTPAdapter.h" /> <ClInclude Include="..\..\Source\cloud\LocalCloudAdapter.h" /> - <ClInclude Include="..\..\Source\dataAccess\Cache.h" /> <ClInclude Include="..\..\Source\dataAccess\CloudFileList.h" /> <ClInclude Include="..\..\Source\dataAccess\CloudFileSystem.h" /> <ClInclude Include="..\..\Source\dataAccess\ComputeOnClient.h" /> <ClInclude Include="..\..\Source\dataAccess\ComputeOnPi.h" /> - <ClInclude Include="..\..\Source\dataAccess\ConcreteCache.h" /> <ClInclude Include="..\..\Source\dataAccess\Decoder.h" /> <ClInclude Include="..\..\Source\dataAccess\Distributor.h" /> <ClInclude Include="..\..\Source\dataAccess\Encoder.h" /> diff --git a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters index 96ef00de5d090fd98671506cd3d9f86887d7ccea..c5fe3838bc9c9303a9218b94e89444897835c2bb 100644 --- a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters +++ b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters @@ -55,6 +55,12 @@ <Filter Include="Header Files\cloud\adapter"> <UniqueIdentifier>{4bd543fd-eb05-47c9-a515-f7f94b0e9009}</UniqueIdentifier> </Filter> + <Filter Include="Header Files\cache"> + <UniqueIdentifier>{3bc6f97a-3e60-44c4-8fcb-6d0700e45c04}</UniqueIdentifier> + </Filter> + <Filter Include="Source Files\cache"> + <UniqueIdentifier>{7a804dde-3827-48f5-97f3-de7ea5499492}</UniqueIdentifier> + </Filter> </ItemGroup> <ItemGroup> <ClCompile Include="..\..\Source\app\Application.cpp"> @@ -78,9 +84,6 @@ <ClCompile Include="..\..\Source\dataAccess\ComputeOnPi.cpp"> <Filter>Source Files\dataAccess</Filter> </ClCompile> - <ClCompile Include="..\..\Source\dataAccess\ConcreteCache.cpp"> - <Filter>Source Files\dataAccess</Filter> - </ClCompile> <ClCompile Include="..\..\Source\dataAccess\Decoder.cpp"> <Filter>Source Files\dataAccess</Filter> </ClCompile> @@ -141,6 +144,21 @@ <ClCompile Include="..\..\Source\fileModel\ReturnableFile.cpp"> <Filter>Source Files\fileModel</Filter> </ClCompile> + <ClCompile Include="..\..\Source\cache\NoCache.cpp"> + <Filter>Source Files\cache</Filter> + </ClCompile> + <ClCompile Include="..\..\Source\cache\LRUCache.cpp"> + <Filter>Source Files\cache</Filter> + </ClCompile> + <ClCompile Include="..\..\Source\cache\LFUCache.cpp"> + <Filter>Source Files\cache</Filter> + </ClCompile> + <ClCompile Include="..\..\Source\cache\FIFOCache.cpp"> + <Filter>Source Files\cache</Filter> + </ClCompile> + <ClCompile Include="..\..\Source\cache\BasicCache.cpp"> + <Filter>Source Files\cache</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\..\Source\app\UI.h"> @@ -152,9 +170,6 @@ <ClInclude Include="..\..\Source\cloud\CloudManager.h"> <Filter>Header Files\cloud</Filter> </ClInclude> - <ClInclude Include="..\..\Source\dataAccess\Cache.h"> - <Filter>Header Files\dataAccess</Filter> - </ClInclude> <ClInclude Include="..\..\Source\dataAccess\CloudFileSystem.h"> <Filter>Header Files\dataAccess</Filter> </ClInclude> @@ -164,9 +179,6 @@ <ClInclude Include="..\..\Source\dataAccess\ComputeOnPi.h"> <Filter>Header Files\dataAccess</Filter> </ClInclude> - <ClInclude Include="..\..\Source\dataAccess\ConcreteCache.h"> - <Filter>Header Files\dataAccess</Filter> - </ClInclude> <ClInclude Include="..\..\Source\dataAccess\Decoder.h"> <Filter>Header Files\dataAccess</Filter> </ClInclude> @@ -242,5 +254,26 @@ <ClInclude Include="..\..\Source\fileModel\ReturnableFile.h"> <Filter>Header Files\fileModel</Filter> </ClInclude> + <ClInclude Include="..\..\Source\cache\NoCache.h"> + <Filter>Header Files\cache</Filter> + </ClInclude> + <ClInclude Include="..\..\Source\cache\LRUCache.h"> + <Filter>Header Files\cache</Filter> + </ClInclude> + <ClInclude Include="..\..\Source\cache\LFUCache.h"> + <Filter>Header Files\cache</Filter> + </ClInclude> + <ClInclude Include="..\..\Source\cache\FIFOCache.h"> + <Filter>Header Files\cache</Filter> + </ClInclude> + <ClInclude Include="..\..\Source\cache\Cache.h"> + <Filter>Header Files\cache</Filter> + </ClInclude> + <ClInclude Include="..\..\Source\cache\BasicCache.h"> + <Filter>Header Files\cache</Filter> + </ClInclude> + <ClInclude Include="..\..\Source\cache\CacheFile.h"> + <Filter>Header Files\cache</Filter> + </ClInclude> </ItemGroup> </Project> \ No newline at end of file diff --git a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.user b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.user index 64a3621a437c792daa9e8ebbf84ed009acb26092..ff5921cf0d2fd9ab81317dc308955058e0042125 100644 --- a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.user +++ b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.user @@ -1,7 +1,8 @@ <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> - <LocalDebuggerCommandArguments>-dc:\Work\raspberrycloud\program\data\ -cc:\Work\raspberrycloud\program\cloud\ -fc:\Work\raspberrycloud\program\temp\</LocalDebuggerCommandArguments> + <LocalDebuggerCommandArguments> + </LocalDebuggerCommandArguments> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> </PropertyGroup> </Project> \ No newline at end of file diff --git a/program/Source/app/Application.cpp b/program/Source/app/Application.cpp index d65e9ac2e5cd21574c9dddd3967aadb7a8f37b75..1e93aae58cad4606ef8a941a0af2476a0dcb44dc 100644 --- a/program/Source/app/Application.cpp +++ b/program/Source/app/Application.cpp @@ -4,6 +4,10 @@ #include "../cloud/LocalCloudAdapter.h" #include "../cloud/DropboxAdapter.h" #include "../cloud/FTPAdapter.h" +#include "../cache/NoCache.h" +#include "../cache/FIFOCache.h" +#include "../cache/LRUCache.h" +#include "../cache/LFUCache.h" using namespace std; @@ -40,6 +44,7 @@ int main(int argc, char **argv) { string dataFolder = "C:\\Users\\krisz\\raspberrycloud\\skeleton\\Program\\data\\"; string fragFolder = "C:\\Users\\krisz\\raspberrycloud\\skeleton\\Program\\temp\\"; string cloudFolder = "C:\\Users\\krisz\\raspberrycloud\\skeleton\\Program\\cloud\\"; + string cacheFolder = "C:\\Users\\krisz\\raspberrycloud\\skeleton\\Program\\cache\\"; for (int i = 1; i < argc; i++) { @@ -60,6 +65,41 @@ int main(int argc, char **argv) { application.cfs.getEncoder()->setFolders(dataFolder,fragFolder); application.cfs.getDecoder()->setFolders(dataFolder,fragFolder); application.cfs.setFolders(fragFolder); + + bool waitAnswer = true; + Cache *cacheToSetup; + while (waitAnswer){ + cout << "Please select caching method (1: no, 2: FIFO, 3: LRU, 4: LFU)" << endl; + int answer = 0; + cin >> answer; + + int cacheSize = 3; + + switch (answer){ + case 1: + cacheToSetup = new NoCache(); + waitAnswer = false; + break; + case 2: + cacheToSetup = new FIFOCache(cacheSize); + waitAnswer = false; + break; + case 3: + cacheToSetup = new LRUCache(cacheSize); + waitAnswer = false; + break; + case 4: + cacheToSetup = new LFUCache(cacheSize); + waitAnswer = false; + break; + default: + break; + } + } + + application.cfs.setCache(cacheToSetup); + application.cfs.getCache()->setFolders(cacheFolder, dataFolder); + application.cfs.getCache()->readCache(); application.hnc.setDataFolder(dataFolder); @@ -105,8 +145,7 @@ int main(int argc, char **argv) { application.ui.setComputeOnPi(false); break; } - } - + } cout << "To list avaliable orders enter \"help\"." << endl; diff --git a/program/Source/cache/Cache.h b/program/Source/cache/Cache.h new file mode 100644 index 0000000000000000000000000000000000000000..a02c9c9a4d27e4653978ebf24ec3b6cdfd0a7983 --- /dev/null +++ b/program/Source/cache/Cache.h @@ -0,0 +1,45 @@ +#ifndef _CACHE_H +#define _CACHE_H + +#include <boost/filesystem.hpp> +#include "CacheFile.h" +#include "../fileModel/ReturnableFile.h" +#include <fstream> +#include <sstream> + + +using namespace boost::filesystem; + +/** + * Cache class + * provides interface to the caching functionality + */ +class Cache { +protected: + int maxSize; + int realSize; + int baseValue; + bool readAble; + CacheFile* files; + std::string sourceFolder; + std::string cacheFolder; +public: + void setFolders(std::string _cacheFolder, std::string _sourceFolder){ + cacheFolder = _cacheFolder; + sourceFolder = _sourceFolder; + } + + /** + * Tries to get a file from the cache + */ + virtual ReturnableFile getFile(std::string localFileID) = 0; + + /** + * Adds a file to the cache + */ + virtual bool addFile(std::string localFileID) = 0; + + virtual void readCache() = 0; +}; + +#endif //_CACHE_H \ No newline at end of file diff --git a/program/Source/cache/CacheFile.h b/program/Source/cache/CacheFile.h new file mode 100644 index 0000000000000000000000000000000000000000..7534058508907162baf71f845952a942d8c9fd8c --- /dev/null +++ b/program/Source/cache/CacheFile.h @@ -0,0 +1,22 @@ +#ifndef _CACHEFILE_H +#define _CACHEFILE_H + +#include <string> + +using namespace std; + +class CacheFile +{ +private: + string fileId; +public: + int used; + + CacheFile() {} + CacheFile(string fileId, int used) : fileId(fileId), used(used) {} + + string getFileId() { return fileId; } + void setFileId(string _fileId) { fileId = _fileId; } +}; + +#endif \ No newline at end of file diff --git a/program/Source/cache/FIFOCache.cpp b/program/Source/cache/FIFOCache.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fed0106e17c93f4df19dd6c4a01aab77aa88faf --- /dev/null +++ b/program/Source/cache/FIFOCache.cpp @@ -0,0 +1,145 @@ +#include "FIFOCache.h" +#include "../logger/Logger.h" + +using namespace std; +using namespace boost::filesystem; + +/** + * FIFOCache implementation + */ +FIFOCache::FIFOCache(int _size) +{ + maxSize = _size; + realSize = 0; + baseValue = 0; + readAble = true; + files = new CacheFile[maxSize]; + for (int i = 0; i < maxSize; i++){ + files[i] = CacheFile("", baseValue); + } +} + +FIFOCache::~FIFOCache() +{ + delete[] files; +} + +ReturnableFile FIFOCache::getFile(std::string localFileID) { + LOG_ENTER_EXIT; + ReturnableFile fileFromCache(localFileID); + + //copy file from cache to source + ifstream src(cacheFolder + localFileID, ios::binary); + if (src) { + ofstream dst(sourceFolder + localFileID, ios::binary); + if (dst) { + dst << src.rdbuf(); + dst.close(); + fileFromCache.setValid(true); + } + else { + fileFromCache.setErrorMessage("Cache output could not be initialized."); + } + src.close(); + } + else { + fileFromCache.setErrorMessage("Cache source could not be initialized."); + } + + return fileFromCache; +} + +bool FIFOCache::addFile(std::string localFileID) { + LOG_ENTER_EXIT; + bool successful = true; + if (!checkFile(localFileID)){ + successful = false; + path cacheLocation = cacheFolder + localFileID; + path cacheParentDir = cacheLocation.parent_path(); + + if (!exists(cacheParentDir)) + { + create_directories(cacheParentDir); + } + + //copy file to cache + ifstream src(sourceFolder + localFileID, ios::binary); + if (src) { + ofstream dst(cacheFolder + localFileID, ios::binary); + if (dst) { + if (realSize == maxSize) + { + // tele van, ez�rt az utols�t kit�r�lj�k + string spath = cacheFolder + files[maxSize - 1].getFileId(); + const char* path = spath.c_str(); + int res = remove(path); + + for (int idx = realSize - 1; idx >= 0; idx--) + { + if ((idx + 1) < maxSize) + { + files[idx + 1] = files[idx]; + } + } + files[0] = CacheFile(localFileID, 0); + } + else + { + for (int idx = realSize - 1; idx >= 0; idx--) + { + if ((idx + 1) < maxSize) + { + files[idx + 1] = files[idx]; + } + } + files[0] = CacheFile(localFileID, 0); + realSize++; + } + + dst << src.rdbuf(); + dst.close(); + successful = true; + + cout << toString(); + } + src.close(); + } + } + return successful; +} + +bool FIFOCache::checkFile(string id){ + for (int idx = 0; idx < realSize; idx++) { + if (files[idx].getFileId() == id) { + return true; + } + } + return false; +} + +string FIFOCache::toString() { + ostringstream ss; + ss << "rs:" << realSize << " data: "; + for (int i = 0; i < realSize; i++) { + ss << files[i].getFileId() << " "; + } + ss << endl; + return ss.str(); +} + +void FIFOCache::readCache() { + if (readAble) { + path p = cacheFolder; + if (exists(p) && is_directory(p)) + { + vector<path> v; + copy(directory_iterator(p), directory_iterator(), back_inserter(v)); + for (unsigned int i = 0; i < v.size(); i++) + { + path p = v.at(i).filename(); + files[i] = CacheFile(p.string(), baseValue); + realSize++; + } + } + } +} \ No newline at end of file diff --git a/program/Source/cache/FIFOCache.h b/program/Source/cache/FIFOCache.h new file mode 100644 index 0000000000000000000000000000000000000000..f92f4a46693df9ef3307a91bca14799312ed714d --- /dev/null +++ b/program/Source/cache/FIFOCache.h @@ -0,0 +1,34 @@ +/** + * Project Untitled + */ + + +#ifndef _FIFOCACHE_H +#define _FIFOCACHE_H + +#include "Cache.h" + +/** + * FIFOCache class + * implenents Cache interface + */ +class FIFOCache : public Cache { +public: + FIFOCache(int size); + ~FIFOCache(); + + /** + * Tries to get a file from the cache + */ + ReturnableFile getFile(std::string localFileID); + + /** + * Adds a file to the cache + */ + bool addFile(std::string localFileID); + bool checkFile(std::string id); + string toString(); + void readCache(); +}; + +#endif //_FIFOCACHE_H \ No newline at end of file diff --git a/program/Source/cache/LFUCache.cpp b/program/Source/cache/LFUCache.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef223028adc6fb266269614165cbdac5b5c79b80 --- /dev/null +++ b/program/Source/cache/LFUCache.cpp @@ -0,0 +1,150 @@ +#include "LFUCache.h" +#include "../logger/Logger.h" + +using namespace std; +using namespace boost::filesystem; + +/** + * LFUCache implementation + */ + +LFUCache::LFUCache(int _size) +{ + maxSize = _size; + realSize = 0; + baseValue = -1; + readAble = true; + files = new CacheFile[maxSize]; + + for (int i = 0; i < maxSize; i++){ + files[i] = CacheFile("", baseValue); + } +} + +LFUCache::~LFUCache() +{ + delete[] files; +} + +ReturnableFile LFUCache::getFile(std::string localFileID) { + LOG_ENTER_EXIT; + ReturnableFile fileFromCache(localFileID); + + //copy file from cache to source + ifstream src(cacheFolder + localFileID, ios::binary); + if (src) { + ofstream dst(sourceFolder + localFileID, ios::binary); + if (dst) { + dst << src.rdbuf(); + dst.close(); + fileFromCache.setValid(true); + + for (int idx = 0; idx < maxSize; idx++) { + if (files[idx].getFileId() == localFileID) { + files[idx].used++; + break; + } + } + } + else { + fileFromCache.setErrorMessage("Cache output could not be initialized."); + } + src.close(); + } + else { + fileFromCache.setErrorMessage("Cache source could not be initialized."); + } + + return fileFromCache; +} + +bool LFUCache::addFile(std::string localFileID) { + LOG_ENTER_EXIT; + bool successful = true; + if (!checkFile(localFileID)){ + successful = false; + path cacheLocation = cacheFolder + localFileID; + path cacheParentDir = cacheLocation.parent_path(); + + if (!exists(cacheParentDir)) + { + create_directories(cacheParentDir); + } + + //copy file to cache + ifstream src(sourceFolder + localFileID, ios::binary); + if (src) { + ofstream dst(cacheFolder + localFileID, ios::binary); + if (dst) { + int minAge = 2147483647; + for (int i = 0; i < minAge; i++) + { + if (files[i].used < minAge) { + minAge = files[i].used; + } + } + + for (int i = 0; i < maxSize; i++) { + if (files[i].used == minAge) { + // tele van, ez�rt az utols�t kit�r�lj�k + string spath = cacheFolder + files[i].getFileId(); + const char* path = spath.c_str(); + int res = remove(path); + + files[i] = CacheFile(localFileID, 0); + break; + } + } + + dst << src.rdbuf(); + dst.close(); + successful = true; + + cout << toString(); + } + src.close(); + } + } + return successful; +} + +bool LFUCache::checkFile(string id){ + for (int idx = 0; idx < maxSize; idx++) { + if (files[idx].getFileId() == id) { + return true; + } + } + return false; +} + +string LFUCache::toString() { + ostringstream ss; + ss << "Files: "; + for (int i = 0; i < maxSize; i++) { + ss << files[i].getFileId() << " "; + } + ss << " Used: "; + for (int i = 0; i < maxSize; i++) { + ss << files[i].used << " "; + } + ss << endl; + return ss.str(); +} + +void LFUCache::readCache() { + if (readAble) { + path p = cacheFolder; + if (exists(p) && is_directory(p)) + { + vector<path> v; + copy(directory_iterator(p), directory_iterator(), back_inserter(v)); + for (unsigned int i = 0; i < v.size(); i++) + { + path p = v.at(i).filename(); + files[i] = CacheFile(p.string(), 0); + realSize++; + } + } + } + cout << toString(); +} \ No newline at end of file diff --git a/program/Source/cache/LFUCache.h b/program/Source/cache/LFUCache.h new file mode 100644 index 0000000000000000000000000000000000000000..ae3256090bf57154bf1f0c590088c6c0fc56df5c --- /dev/null +++ b/program/Source/cache/LFUCache.h @@ -0,0 +1,34 @@ +/** + * Project Untitled + */ + + +#ifndef _LFUCACHE_H +#define _LFUCACHE_H + +#include "Cache.h" + +/** + * LFUCache class + * implenents Cache interface + */ +class LFUCache : public Cache { +public: + LFUCache(int size); + ~LFUCache(); + + /** + * Tries to get a file from the cache + */ + ReturnableFile getFile(std::string localFileID); + + /** + * Adds a file to the cache + */ + bool addFile(std::string localFileID); + string toString(); + bool checkFile(std::string id); + void readCache(); +}; + +#endif //_LFUCACHE_H \ No newline at end of file diff --git a/program/Source/cache/LRUCache.cpp b/program/Source/cache/LRUCache.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1ba7a9a58125b59d8e84c9ac58e6589ff4132cb --- /dev/null +++ b/program/Source/cache/LRUCache.cpp @@ -0,0 +1,151 @@ +#include "LRUCache.h" +#include "../logger/Logger.h" + +using namespace std; +using namespace boost::filesystem; + +/** + * LRUCache implementation + */ + +LRUCache::LRUCache(int _size) +{ + maxSize = _size; + realSize = 0; + baseValue = 1; + readAble = true; + files = new CacheFile[maxSize]; + + for (int i = 0; i < maxSize; i++){ + files[i] = CacheFile("", baseValue); + } +} + +LRUCache::~LRUCache() +{ + delete[] files; +} + +ReturnableFile LRUCache::getFile(std::string localFileID) { + LOG_ENTER_EXIT; + ReturnableFile fileFromCache(localFileID); + + //copy file from cache to source + ifstream src(cacheFolder + localFileID, ios::binary); + if (src) { + ofstream dst(sourceFolder + localFileID, ios::binary); + if (dst) { + dst << src.rdbuf(); + dst.close(); + fileFromCache.setValid(true); + + for (int idx = 0; idx < maxSize; idx++) { + if (files[idx].getFileId() == localFileID) { + for (int i = 0; i < maxSize; i++){ + files[i].used++; + } + files[idx].used = 0; + } + } + } + else { + fileFromCache.setErrorMessage("Cache output could not be initialized."); + } + src.close(); + } + else { + fileFromCache.setErrorMessage("Cache source could not be initialized."); + } + + return fileFromCache; +} + +bool LRUCache::addFile(std::string localFileID) { + LOG_ENTER_EXIT; + bool successful = true; + if (!checkFile(localFileID)){ + successful = false; + path cacheLocation = cacheFolder + localFileID; + path cacheParentDir = cacheLocation.parent_path(); + + if (!exists(cacheParentDir)) + { + create_directories(cacheParentDir); + } + + //copy file to cache + ifstream src(sourceFolder + localFileID, ios::binary); + if (src) { + ofstream dst(cacheFolder + localFileID, ios::binary); + if (dst) { + int maxAge = 0; + for (int i = 0; i < maxSize; i++) + { + if (files[i].used > maxAge) { + maxAge = files[i].used; + } + } + + for (int i = 0; i < maxSize; i++) { + if (files[i].used == maxAge) { + // tele van, ez�rt az utols�t kit�r�lj�k + string spath = cacheFolder + files[i].getFileId(); + const char* path = spath.c_str(); + int res = remove(path); + + files[i] = CacheFile(localFileID, 0); + break; + } + } + + dst << src.rdbuf(); + dst.close(); + successful = true; + + cout << toString(); + } + src.close(); + } + } + return successful; +} + +bool LRUCache::checkFile(string id){ + for (int idx = 0; idx < maxSize; idx++) { + if (files[idx].getFileId() == id) { + return true; + } + } + return false; +} + +string LRUCache::toString() { + ostringstream ss; + ss << "Files: "; + for (int i = 0; i < maxSize; i++) { + ss << files[i].getFileId() << " "; + } + ss << " Used: "; + for (int i = 0; i < maxSize; i++) { + ss << files[i].used << " "; + } + ss << endl; + return ss.str(); +} + +void LRUCache::readCache() { + if (readAble) { + path p = cacheFolder; + if (exists(p) && is_directory(p)) + { + vector<path> v; + copy(directory_iterator(p), directory_iterator(), back_inserter(v)); + for (unsigned int i = 0; i < v.size(); i++) + { + path p = v.at(i).filename(); + files[i] = CacheFile(p.string(), 0); + realSize++; + } + } + } +} \ No newline at end of file diff --git a/program/Source/cache/LRUCache.h b/program/Source/cache/LRUCache.h new file mode 100644 index 0000000000000000000000000000000000000000..62525aac02a03c2230356df72ae6b744ef789289 --- /dev/null +++ b/program/Source/cache/LRUCache.h @@ -0,0 +1,34 @@ +/** + * Project Untitled + */ + + +#ifndef _LRUCACHE_H +#define _LRUCACHE_H + +#include "Cache.h" + +/** + * LRUCache class + * implenents Cache interface + */ +class LRUCache : public Cache { +public: + LRUCache(int size); + ~LRUCache(); + + /** + * Tries to get a file from the cache + */ + ReturnableFile getFile(std::string localFileID); + + /** + * Adds a file to the cache + */ + bool addFile(std::string localFileID); + string toString(); + bool checkFile(std::string id); + void readCache(); +}; + +#endif //_LRUCACHE_H \ No newline at end of file diff --git a/program/Source/cache/NoCache.cpp b/program/Source/cache/NoCache.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38c31a07b7c6f281ea8c01505d21f008bd8ef41d --- /dev/null +++ b/program/Source/cache/NoCache.cpp @@ -0,0 +1,21 @@ +#include "NoCache.h" +#include "../logger/Logger.h" + +/** + * NoCache implementation + */ + +NoCache::NoCache() { + readAble = false; +} + +ReturnableFile NoCache::getFile(std::string localFileID) { + LOG_ENTER_EXIT; + ReturnableFile fileFromCache(localFileID); + return fileFromCache; +} + +bool NoCache::addFile(std::string localFileID) { + LOG_ENTER_EXIT; + return true; +} \ No newline at end of file diff --git a/program/Source/dataAccess/ConcreteCache.h b/program/Source/cache/NoCache.h similarity index 64% rename from program/Source/dataAccess/ConcreteCache.h rename to program/Source/cache/NoCache.h index 44bc7357388eb2554a99230ede8b626b5c97fb67..a1de2ee5a4838c7674c239c25c9ee2c8838dae0c 100644 --- a/program/Source/dataAccess/ConcreteCache.h +++ b/program/Source/cache/NoCache.h @@ -3,17 +3,18 @@ */ -#ifndef _CONCRETECACHE_H -#define _CONCRETECACHE_H +#ifndef _NOCACHE_H +#define _NOCACHE_H #include "Cache.h" /** - * ConcreteCache class + * NoCache class * implenents Cache interface */ -class ConcreteCache: public Cache { -public: +class NoCache : public Cache { +public: + NoCache(); /** * Tries to get a file from the cache */ @@ -23,6 +24,8 @@ public: * Adds a file to the cache */ bool addFile(std::string localFileID); + + void readCache() {} }; -#endif //_CONCRETECACHE_H \ No newline at end of file +#endif //_NOCACHE_H \ No newline at end of file diff --git a/program/Source/dataAccess/Cache.h b/program/Source/dataAccess/Cache.h deleted file mode 100644 index 9b035e5abf22c4e81e804b96c9b8f36a7bca799a..0000000000000000000000000000000000000000 --- a/program/Source/dataAccess/Cache.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _CACHE_H -#define _CACHE_H - -#include "../fileModel/ReturnableFile.h" - -/** - * Cache class - * provides interface to the caching functionality - */ -class Cache { -public: - /** - * Tries to get a file from the cache - */ - virtual ReturnableFile getFile(std::string localFileID) = 0; - - /** - * Adds a file to the cache - */ - virtual bool addFile(std::string localFileID) = 0; -}; - -#endif //_CACHE_H \ No newline at end of file diff --git a/program/Source/dataAccess/CloudFileSystem.cpp b/program/Source/dataAccess/CloudFileSystem.cpp index 3cb0f235dda38c75171b0cbafbfe2a53c5c4cdb2..039bd58b98b43bdfc91d959dbbf1ace45f8064e4 100644 --- a/program/Source/dataAccess/CloudFileSystem.cpp +++ b/program/Source/dataAccess/CloudFileSystem.cpp @@ -1,6 +1,7 @@ #include "../app/Application.h" #include "ComputeOnPi.h" #include "ComputeOnClient.h" +#include "../cache/NoCache.h" using namespace std; @@ -14,7 +15,7 @@ CloudFileSystem::CloudFileSystem(Application &application) : homeNetworkBehaviour(new ComputeOnClient(*this)), application(application), distributor(application.cm), - cache(new ConcreteCache()) + cache(new NoCache()) { cfl.setPersPath("store.dat"); cfl.load(); @@ -41,6 +42,11 @@ void CloudFileSystem::setFolders(string _fragFolder){ fragFolder = _fragFolder; } +void CloudFileSystem::setCache(Cache* c){ + delete cache; + cache = c; +} + string CloudFileSystem::fragFolder = ""; // Not working as we planned, maybe should be rethinked diff --git a/program/Source/dataAccess/CloudFileSystem.h b/program/Source/dataAccess/CloudFileSystem.h index 7e71b176f4afdf32cc1fc6f6910a1c23d151b51d..a1fbfc99d66852b48ae5998eb04b29b6d11dadad 100644 --- a/program/Source/dataAccess/CloudFileSystem.h +++ b/program/Source/dataAccess/CloudFileSystem.h @@ -4,7 +4,6 @@ #include <string> #include <set> #include <fstream> -#include "ConcreteCache.h" #include "CloudFileSystem.h" #include "../logger/Logger.h" #include "../cloud/CloudAccessLayer.h" @@ -13,7 +12,7 @@ #include "Distributor.h" #include "../cloud/CloudAccessLayer.h" #include "../networking/HomeNetworkCommunications.h" -#include "Cache.h" +#include "../cache/Cache.h" #include "CloudFileList.h" #include "../dataAccess/Encoder.h" #include "../dataAccess/Decoder.h" @@ -67,6 +66,8 @@ public: void setFolders(std::string _fragFolder); + void setCache(Cache* c); + /** * Setter to the setComputePi variable * Sets the right HomeNetworkBehaviour diff --git a/program/Source/dataAccess/ComputeOnClient.cpp b/program/Source/dataAccess/ComputeOnClient.cpp index d3a904c896070e19565b8b90179d0cdd7a51813d..00c130a091eacdc2185f2ecbfc6ce14e21c0c2ab 100644 --- a/program/Source/dataAccess/ComputeOnClient.cpp +++ b/program/Source/dataAccess/ComputeOnClient.cpp @@ -4,7 +4,7 @@ #include "Encoder.h" #include "Distributor.h" #include "../cloud/CloudAccessLayer.h" -#include "Cache.h" +#include "../cache/Cache.h" using namespace fileModel; using namespace std; @@ -17,8 +17,9 @@ bool ComputeOnClient::addFile(string localFileID, string destinationFileID) { LOG_ENTER_EXIT; bool success; - //TODO: well, I guess it needs a parameter... sooner or later. - cloudFileSystem.getCache()->addFile(localFileID); + if (!cloudFileSystem.getCache()->addFile(localFileID)){ + return false; + } auto fileDescriptor = Encoder::encode(localFileID, destinationFileID); diff --git a/program/Source/dataAccess/ConcreteCache.cpp b/program/Source/dataAccess/ConcreteCache.cpp deleted file mode 100644 index a84ef938d596c13d1a7572a3ba31b80cd4883076..0000000000000000000000000000000000000000 --- a/program/Source/dataAccess/ConcreteCache.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "ConcreteCache.h" -#include "../logger/Logger.h" - -/** - * ConcreteCache implementation - */ - -ReturnableFile ConcreteCache::getFile(std::string localFileID) { - LOG_ENTER_EXIT; - ReturnableFile fileFromCache(localFileID); - return fileFromCache; -} - -bool ConcreteCache::addFile(std::string localFileID) { - LOG_ENTER_EXIT; - return false; -} \ No newline at end of file