From e384281aacb0567fe4b260cea99d95b76f422866 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kecsk=C3=A9s=20Kriszti=C3=A1n?= <kk1205@sch.bme.hu>
Date: Tue, 24 Nov 2015 21:07:04 +0100
Subject: [PATCH] Getfile methods no longer return erronous strings+

---
 program/Source/app/Application.cpp            |   9 +-
 program/Source/app/UI.cpp                     |   2 +-
 program/Source/app/UI.h                       |   3 +-
 program/Source/dataAccess/Cache.h             |   6 +-
 program/Source/dataAccess/CloudFileSystem.cpp |   6 +-
 program/Source/dataAccess/CloudFileSystem.h   |   2 +-
 program/Source/dataAccess/ComputeOnClient.cpp |  36 +++---
 program/Source/dataAccess/ComputeOnClient.h   |   2 +-
 program/Source/dataAccess/ComputeOnPi.cpp     |  13 ++-
 program/Source/dataAccess/ComputeOnPi.h       |   2 +-
 program/Source/dataAccess/ConcreteCache.cpp   |   7 +-
 program/Source/dataAccess/ConcreteCache.h     |  10 +-
 program/Source/dataAccess/Decoder.cpp         | 110 +++++++++---------
 program/Source/dataAccess/Decoder.h           |   3 +-
 .../Source/dataAccess/HomeNetworkBehaviour.h  |   3 +-
 program/Source/fileModel/ReturnableFile.cpp   |  15 +++
 program/Source/fileModel/ReturnableFile.h     |  24 ++++
 17 files changed, 159 insertions(+), 94 deletions(-)
 create mode 100644 program/Source/fileModel/ReturnableFile.cpp
 create mode 100644 program/Source/fileModel/ReturnableFile.h

diff --git a/program/Source/app/Application.cpp b/program/Source/app/Application.cpp
index 711abfb8..0dc6cc9f 100644
--- a/program/Source/app/Application.cpp
+++ b/program/Source/app/Application.cpp
@@ -131,9 +131,12 @@ int main(int argc, char **argv) {
 			string fileid;
 			cin >> fileid;
 			FileDescriptor fileDesc = FileDescriptor(fileid);
-			string returned = application.ui.getFile(fileDesc);
-			cout << returned << endl;
-			
+			ReturnableFile returned = application.ui.getFile(fileDesc);
+			if (returned.isValid()) {
+				cout << "Successfully downloaded: " << returned.getLocalFileId() << endl;
+			} else {
+				cout << "ERROR with getting: " << returned.getLocalFileId() << endl << returned.getErrorMessage() << endl;
+			}			
 		}
 		else if (command == "getFileList"){
 			list<FileDescriptor*>* fileTree = application.ui.getFileTree();
diff --git a/program/Source/app/UI.cpp b/program/Source/app/UI.cpp
index 4919b1df..a1e25b57 100644
--- a/program/Source/app/UI.cpp
+++ b/program/Source/app/UI.cpp
@@ -31,7 +31,7 @@ bool UI::createDirectory(string directoryID) {
 }
 
 
-string UI::getFile(FileDescriptor fileDescriptor) {
+ReturnableFile UI::getFile(FileDescriptor fileDescriptor) {
 	LOG_ENTER_EXIT;
 	return app->cfs.getFile(fileDescriptor);
 }
diff --git a/program/Source/app/UI.h b/program/Source/app/UI.h
index 253276a8..411522e1 100644
--- a/program/Source/app/UI.h
+++ b/program/Source/app/UI.h
@@ -4,6 +4,7 @@
 #include <string>
 #include <list>
 #include "../fileModel/FileDescriptor.h"
+#include "../fileModel/ReturnableFile.h"
 
 class Application;
 
@@ -53,7 +54,7 @@ public:
 	 * @param fileDescriptor descriptor of the file to get
 	 * @return path to retrieved file
 	 */
-	std::string getFile(FileDescriptor fileDescriptor);
+	ReturnableFile getFile(FileDescriptor fileDescriptor);
 	
 	/**
 	 * It starts the process of deleting a given file from the system
diff --git a/program/Source/dataAccess/Cache.h b/program/Source/dataAccess/Cache.h
index 2b1fac7d..9b035e5a 100644
--- a/program/Source/dataAccess/Cache.h
+++ b/program/Source/dataAccess/Cache.h
@@ -1,6 +1,8 @@
 #ifndef _CACHE_H
 #define _CACHE_H
 
+#include "../fileModel/ReturnableFile.h"
+
 /**
  * Cache class
  * provides interface to the caching functionality
@@ -10,12 +12,12 @@ public:
 	/**
 	 * Tries to get a file from the cache
 	 */
-	virtual void getFile() = 0;
+	virtual ReturnableFile getFile(std::string localFileID) = 0;
 	
 	/**
 	 * Adds a file to the cache
 	 */
-	virtual void addFile() = 0;
+	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 59c3b8f9..0d0dd131 100644
--- a/program/Source/dataAccess/CloudFileSystem.cpp
+++ b/program/Source/dataAccess/CloudFileSystem.cpp
@@ -83,11 +83,13 @@ bool CloudFileSystem::addFile(string localFileID, string destinationFileID) {
 	
 }
 
-string CloudFileSystem::getFile(FileDescriptor fileDescriptor) {
+ReturnableFile CloudFileSystem::getFile(FileDescriptor fileDescriptor) {
 	LOG_ENTER_EXIT;
 		
 	if (homeNetworkBehaviour == nullptr) {
-		return "No HomeNetworkBehaviour specified";
+		ReturnableFile resultFile(fileDescriptor.getFileID());
+		resultFile.setErrorMessage("No HomeNetworkBehaviour specified");
+		return resultFile;
 	}
 
 	return homeNetworkBehaviour->getFile(fileDescriptor);
diff --git a/program/Source/dataAccess/CloudFileSystem.h b/program/Source/dataAccess/CloudFileSystem.h
index be828a81..43d11851 100644
--- a/program/Source/dataAccess/CloudFileSystem.h
+++ b/program/Source/dataAccess/CloudFileSystem.h
@@ -112,7 +112,7 @@ public:
 	 * Returns with "" if none is set
 	 * @param fileDescriptor descriptor of the file to be retrieved
 	 */
-	std::string getFile(FileDescriptor fileDescriptor);
+	ReturnableFile getFile(FileDescriptor fileDescriptor);
 	
 	/**
 	 * Creates a directory on the cloud
diff --git a/program/Source/dataAccess/ComputeOnClient.cpp b/program/Source/dataAccess/ComputeOnClient.cpp
index 6cbb7d12..d3a904c8 100644
--- a/program/Source/dataAccess/ComputeOnClient.cpp
+++ b/program/Source/dataAccess/ComputeOnClient.cpp
@@ -18,7 +18,7 @@ bool ComputeOnClient::addFile(string localFileID, string destinationFileID) {
 	bool success;
 
 	//TODO: well, I guess it needs a parameter... sooner or later.
-	cloudFileSystem.getCache()->addFile();
+	cloudFileSystem.getCache()->addFile(localFileID);
 
 	auto fileDescriptor = Encoder::encode(localFileID, destinationFileID);
 
@@ -38,25 +38,29 @@ bool ComputeOnClient::addFile(string localFileID, string destinationFileID) {
 	return success;
 }
 
-string ComputeOnClient::getFile(FileDescriptor fileDescriptor) {
+ReturnableFile ComputeOnClient::getFile(FileDescriptor fileDescriptor) {
 	LOG_ENTER_EXIT;
 
-	//TODO: check if anything is returned... where does it even return anything?
-	cloudFileSystem.getCache()->getFile();
+	ReturnableFile resultFile = cloudFileSystem.getCache()->getFile(fileDescriptor.getFileID());
 
-	if (cloudFileSystem.findByID(&fileDescriptor) == nullptr){
-		return "Clouds doesn't contain the file.";
-	}
-	FileDescriptor* fileToDownload = cloudFileSystem.findByID(&fileDescriptor);
+	if (!resultFile.isValid()) {
+		if (cloudFileSystem.findByID(&fileDescriptor) == nullptr){
+			resultFile.setErrorMessage("Clouds doesn't contain the file.");
+		}
+		else {
+			FileDescriptor* fileToDownload = cloudFileSystem.findByID(&fileDescriptor);
 
-	if (!cloudFileSystem.isLocked(*fileToDownload))
-	{
-		cloudFileSystem.lock(*fileToDownload);
-		FileDescriptor* downloaded = cloudFileSystem.getCloudAccessLayer()->download(*fileToDownload);
-		cloudFileSystem.unlock(*fileToDownload);
+			if (!cloudFileSystem.isLocked(*fileToDownload))
+			{
+				cloudFileSystem.lock(*fileToDownload);
+				FileDescriptor* downloaded = cloudFileSystem.getCloudAccessLayer()->download(*fileToDownload);
+				cloudFileSystem.unlock(*fileToDownload);
 
-		return Decoder::decode(fileToDownload);
+				return Decoder::decode(fileToDownload);
+			}
+			resultFile.setErrorMessage("File is locked");
+		}
 	}
-	
-	return "File is locked";
+
+	return resultFile;
 }
\ No newline at end of file
diff --git a/program/Source/dataAccess/ComputeOnClient.h b/program/Source/dataAccess/ComputeOnClient.h
index a45d527e..03c36365 100644
--- a/program/Source/dataAccess/ComputeOnClient.h
+++ b/program/Source/dataAccess/ComputeOnClient.h
@@ -34,7 +34,7 @@ public:
 	 * @param fileDescriptor the descriptor of the file to be retireved
 	 * @return path of the file retrieved
 	 */
-	std::string getFile(FileDescriptor fileDescriptor) override;
+	ReturnableFile getFile(FileDescriptor fileDescriptor) override;
 };
 
 #endif //_COMPUTEONCLIENT_H
\ No newline at end of file
diff --git a/program/Source/dataAccess/ComputeOnPi.cpp b/program/Source/dataAccess/ComputeOnPi.cpp
index e5f54a2b..e58b1936 100644
--- a/program/Source/dataAccess/ComputeOnPi.cpp
+++ b/program/Source/dataAccess/ComputeOnPi.cpp
@@ -10,7 +10,16 @@ bool ComputeOnPi::addFile(string localFileID, string destinationFileID) {
 	return cloudFileSystem.getHomeNetworkCommunications()->addFileRemote(localFileID);
 }
 
-string ComputeOnPi::getFile(FileDescriptor fileDescriptor) {
+ReturnableFile ComputeOnPi::getFile(FileDescriptor fileDescriptor) {
 	//TODO: change HomeNetworkCommunications' getFileRemote's attribute's type to fileDescriptor
-	return cloudFileSystem.getHomeNetworkCommunications()->getFileRemote(fileDescriptor.getFileID()) ? "File downloaded successfully" : "Could't download file";
+	ReturnableFile resultFile(fileDescriptor.getFileID());
+
+	if (cloudFileSystem.getHomeNetworkCommunications()->getFileRemote(fileDescriptor.getFileID())){
+		resultFile.setValid(true);
+	}
+	else {
+		resultFile.setErrorMessage("Could't download file");
+	}
+
+	return resultFile;
 }
\ No newline at end of file
diff --git a/program/Source/dataAccess/ComputeOnPi.h b/program/Source/dataAccess/ComputeOnPi.h
index 59f1830b..06c25a06 100644
--- a/program/Source/dataAccess/ComputeOnPi.h
+++ b/program/Source/dataAccess/ComputeOnPi.h
@@ -31,7 +31,7 @@ public:
 	 * @param fileDescriptor the descriptor of the file to be retireved
 	 * @return path of the file retrieved
 	 */
-	std::string getFile(FileDescriptor fileDescriptor);
+	ReturnableFile getFile(FileDescriptor fileDescriptor);
 };
 
 #endif //_COMPUTEONPI_H
\ No newline at end of file
diff --git a/program/Source/dataAccess/ConcreteCache.cpp b/program/Source/dataAccess/ConcreteCache.cpp
index 145ab5fd..a84ef938 100644
--- a/program/Source/dataAccess/ConcreteCache.cpp
+++ b/program/Source/dataAccess/ConcreteCache.cpp
@@ -5,10 +5,13 @@
  * ConcreteCache implementation
  */
 
-void ConcreteCache::getFile() {
+ReturnableFile ConcreteCache::getFile(std::string localFileID) {
 	LOG_ENTER_EXIT;
+	ReturnableFile fileFromCache(localFileID);
+	return fileFromCache;
 }
 
-void ConcreteCache::addFile() {
+bool ConcreteCache::addFile(std::string localFileID) {
 	LOG_ENTER_EXIT;
+	return false;
 }
\ No newline at end of file
diff --git a/program/Source/dataAccess/ConcreteCache.h b/program/Source/dataAccess/ConcreteCache.h
index b13ddb34..44bc7357 100644
--- a/program/Source/dataAccess/ConcreteCache.h
+++ b/program/Source/dataAccess/ConcreteCache.h
@@ -17,12 +17,12 @@ public:
 	/**
 	 * Tries to get a file from the cache
 	 */
-	void getFile();
-	
+	ReturnableFile getFile(std::string localFileID);
+
 	/**
-	 * Adds a file to the cache
-	 */
-	void addFile();
+	* Adds a file to the cache
+	*/
+	bool addFile(std::string localFileID);
 };
 
 #endif //_CONCRETECACHE_H
\ No newline at end of file
diff --git a/program/Source/dataAccess/Decoder.cpp b/program/Source/dataAccess/Decoder.cpp
index 3dffc8e6..62912bea 100644
--- a/program/Source/dataAccess/Decoder.cpp
+++ b/program/Source/dataAccess/Decoder.cpp
@@ -22,78 +22,78 @@ void Decoder::setFolders(string _dataFolder, string _fragFolder){
 string Decoder::dataFolder = "";
 string Decoder::fragFolder = "";
 
-string Decoder::decode(FileDescriptor* fileDescriptor)
+ReturnableFile Decoder::decode(FileDescriptor* fileDescriptor)
 {
+	ReturnableFile resultFile(fileDescriptor->getFileID());
+
 	if (dataFolder == "" || fragFolder == "") {
-		cout << "Error: folders not set." << endl;
-		return NULL;
-	}
+		resultFile.setErrorMessage("Decoder Error: folders not set.");
+	} 
+	else if (fileDescriptor == nullptr)	{
+		resultFile.setErrorMessage("unable to decode");
+	} else {
+		auto fragments = fileDescriptor->getFragments();
+		if (fragments.size() > 0)
+		{
+			uint32_t max_symbols = 10;
+			uint32_t max_symbol_size = fileDescriptor->getSize() / 10 + 1;
 
-	LOG_ENTER_EXIT;
-	if (fileDescriptor == nullptr)
-	{
-		return "unable to decode";
-	}
+			using file_decoder = kodo::object::file_decoder<
+				kodo::shallow_full_rlnc_decoder<fifi::binary>>;
 
-	auto fragments = fileDescriptor->getFragments();
-	if (fragments.size() > 0)
-	{
-		uint32_t max_symbols = 10;
-		uint32_t max_symbol_size = fileDescriptor->getSize() / 10 + 1;
+			file_decoder::factory decoder_factory(max_symbols, max_symbol_size);
 
-		using file_decoder = kodo::object::file_decoder<
-			kodo::shallow_full_rlnc_decoder<fifi::binary>>;
+			decoder_factory.set_filename(dataFolder + fileDescriptor->getFileID());
+			decoder_factory.set_file_size(fileDescriptor->getSize());
 
-		file_decoder::factory decoder_factory(max_symbols, max_symbol_size);
+			auto decoder = decoder_factory.build();
 
-		decoder_factory.set_filename(dataFolder + fileDescriptor->getFileID());
-		decoder_factory.set_file_size(fileDescriptor->getSize());
+			for (uint32_t i = 0; i < decoder->blocks(); i++)
+			{
+				auto d = decoder->build(i);
 
-		auto decoder = decoder_factory.build();
+				for (int fragNum = 0; fragNum < fragments.size() && !d->is_complete(); fragNum++)
+				{
+					vector<uint8_t> payload;
 
-		for (uint32_t i = 0; i < decoder->blocks(); i++)
-		{
-			auto d = decoder->build(i);
+					ifstream fragFile(
+						fragFolder + fragments[fragNum].getCloudFileID(),
+						ios::in | ios::binary);
 
-			for (int fragNum = 0; fragNum < fragments.size() && !d->is_complete(); fragNum++)
-			{
-				vector<uint8_t> payload;
+					if (!fragFile)
+					{
+						resultFile.setErrorMessage(fragFolder + fragments[fragNum].getCloudFileID().c_str() + ": No such file exists.");
+					}
 
-				ifstream fragFile(
-					fragFolder + fragments[fragNum].getCloudFileID(),
-					ios::in | ios::binary);
+					copy(istreambuf_iterator<char>(fragFile),
+							  istreambuf_iterator<char>(),
+							  back_inserter(payload));
+					fragFile.close();
+					payload.shrink_to_fit();
 
-				if (!fragFile)
-				{
-					throw CloudException("No such file exists");
+					d->decode(payload.data());
 				}
 
-				copy(istreambuf_iterator<char>(fragFile),
-				          istreambuf_iterator<char>(),
-				          back_inserter(payload));
-				fragFile.close();
-				payload.shrink_to_fit();
-
-				d->decode(payload.data());
-			}
+				// Not removed in previous loop, because that doesn't necessarily 
+				// iterate through every fragment
+				for (Fragment fragment : fragments)
+				{
+					remove((fragFolder + fragment.getCloudFileID()).c_str());
+				}
 
-			// Not removed in previous loop, because that doesn't necessarily 
-			// iterate through every fragment
-			for (Fragment fragment : fragments)
-			{
-				remove((fragFolder + fragment.getCloudFileID()).c_str());
+				if (!d->is_complete())
+				{
+					resultFile.setErrorMessage("Error while assembling file");
+				}
 			}
 
-			if (!d->is_complete())
-			{
-				throw CloudException("Error while assembling file");
-			}
+			resultFile.setValid(true);
+		}
+		else
+		{
+			resultFile.setErrorMessage("No fragments found to decode");
 		}
-
-		return "Successfully decoded " + fileDescriptor->getFileID();
-	}
-	else
-	{
-		return "No fragments found to decode";
 	}
+
+	return resultFile;
 }
\ No newline at end of file
diff --git a/program/Source/dataAccess/Decoder.h b/program/Source/dataAccess/Decoder.h
index 712a72ac..d4ec1107 100644
--- a/program/Source/dataAccess/Decoder.h
+++ b/program/Source/dataAccess/Decoder.h
@@ -2,6 +2,7 @@
 #define _DECODER_H
 #include <string>
 #include "../fileModel/FileDescriptor.h"
+#include "../fileModel/ReturnableFile.h"
 
 /**
  * Decoder class
@@ -18,7 +19,7 @@ public:
 	 * decodes the fragments to a file 
 	 * @param fileDescriptor descriptor of the file to decode
 	 */
-	static std::string decode(FileDescriptor* fileDescriptor);
+	static ReturnableFile decode(FileDescriptor* fileDescriptor);
 };
 
 #endif //_DECODER_H
\ No newline at end of file
diff --git a/program/Source/dataAccess/HomeNetworkBehaviour.h b/program/Source/dataAccess/HomeNetworkBehaviour.h
index 5801b998..60b8cf69 100644
--- a/program/Source/dataAccess/HomeNetworkBehaviour.h
+++ b/program/Source/dataAccess/HomeNetworkBehaviour.h
@@ -3,6 +3,7 @@
 
 #include <string>
 #include "../fileModel/FileDescriptor.h"
+#include "../fileModel/ReturnableFile.h"
 
 /**
  * HomeNetworkBehaviour class
@@ -26,7 +27,7 @@ public:
 	 * @param fileDescriptor the descriptor of the file to be retireved
 	 * @return path of the file retrieved
 	 */
-	virtual std::string getFile(FileDescriptor fileDescriptor) = 0;
+	virtual ReturnableFile getFile(FileDescriptor fileDescriptor) = 0;
 };
 
 #endif //_HOMENETWORKBEHAVIOUR_H
\ No newline at end of file
diff --git a/program/Source/fileModel/ReturnableFile.cpp b/program/Source/fileModel/ReturnableFile.cpp
new file mode 100644
index 00000000..492867f6
--- /dev/null
+++ b/program/Source/fileModel/ReturnableFile.cpp
@@ -0,0 +1,15 @@
+#include "ReturnableFile.h"
+
+using namespace std;
+
+ReturnableFile::ReturnableFile(string _localFileId)
+{
+	localFileId = _localFileId;
+	valid = false;
+	errorMessage = "";
+}
+
+
+ReturnableFile::~ReturnableFile()
+{
+}
diff --git a/program/Source/fileModel/ReturnableFile.h b/program/Source/fileModel/ReturnableFile.h
new file mode 100644
index 00000000..a6774c5b
--- /dev/null
+++ b/program/Source/fileModel/ReturnableFile.h
@@ -0,0 +1,24 @@
+#ifndef _RETURNABLEFILE_H
+#define _RETURNABLEFILE_H
+
+#include <string>
+
+class ReturnableFile
+{
+	std::string localFileId;
+	bool valid;
+	std::string errorMessage;
+public:
+	ReturnableFile(std::string _localFileId);
+	~ReturnableFile();
+
+	std::string getLocalFileId() { return localFileId; }
+	bool isValid() { return valid; }
+	std::string getErrorMessage() { return errorMessage; }
+
+	void setLocalFileId(std::string _localFileId) { localFileId = _localFileId; }
+	void setValid(bool _valid) { valid = _valid; }
+	void setErrorMessage(std::string _errorMessage) { errorMessage = _errorMessage; }
+};
+
+#endif
\ No newline at end of file
-- 
GitLab