Last active
December 1, 2015 09:56
-
-
Save FeherMarcell/dcca842bb6d38cce8dc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DropboxConnector : public RESTConnector | |
{ | |
private: | |
bool cacheEnabled = false; | |
public: | |
// Reads and sets cache mode | |
bool isCacheEnabled() { return cacheEnabled; }; | |
void enableCacheMode() { cacheEnabled = true; }; | |
void disableCacheMode() { cacheEnabled = false; }; | |
/// Encoding-related inherited functions | |
/// (ErrorCallback added, otherwise no change) | |
void storeData(const ProcessedFile& file, std::vector<unsigned char> data, unsigned index, PacketSavedCallback callback, ErrorCallback onerror); | |
void onEncodingFinished(const ProcessedFile& encodedFile, const CodingMetadata& metadata); | |
/** | |
* Cache function which is invoked from 'storeData()' when cache mode is turned on (it's off by default). | |
* The implementation has to save the given data persistently and assign the given ProcessedFile object and index to it. | |
* In case of error, it must invoke the ErrorCallback and provide information about the nature of the problem. | |
* When finished successfully, the PacketSavedCallback must be invoked. | |
* The function receives a full copy of the data (rather than a reference to it) to favor asynchronous implementations. | |
* | |
* @param file | |
* The userfile (virtual path + filename) to be saved | |
* @param data | |
* Contents of the coded packet | |
* @param index | |
* Running index of the coded packet. Unique only for the file, not globally | |
* @param onSuccess | |
* Callback that has to be invoked by the implementation when it finished saving the packet | |
* @param onError | |
* Callback that has to be invoked by the implementation when saving was unsuccessful | |
*/ | |
std::function<void(const ProcessedFile& file, std::vector<unsigned char> data, unsigned index, PacketSavedCallback onSuccess, ErrorCallback onError)> cachePacket = | |
[](const ProcessedFile& fileToStore, std::vector<unsigned char> data, unsigned index, PacketSavedCallback onSuccess, ErrorCallback onError) | |
{ | |
// TODO Review and change this default implementation if needed | |
std::string localCacheDirectory = "C:\\ProgramData\\O&O\\CloudCuber\\cache\\upload\\"; | |
if (localCacheDirectory.empty()) | |
{ | |
onError(IStorage::BAD_REQUEST, "Packet caching enabled but cache directory is not set! Aborting storeData"); | |
return; | |
} | |
// Generate file name of the cache file | |
// std::string cacheFilename = GetCacheFileName(fileToStore, index); | |
std::string cacheFilename = fileToStore.getFilename() + "_" + std::to_string(index) + "_.bin"; | |
if (cacheFilename.empty()) { | |
onError(IStorage::BAD_REQUEST, "Packet caching enabled but the cached filename generator returns empty string! Aborting storeData"); | |
return; | |
} | |
// Persist the file synchronously | |
std::ofstream file; | |
try { | |
file.open(localCacheDirectory + cacheFilename, ios::binary); | |
file << std::string(data.begin(), data.end()); | |
if (file.fail() || file.bad()) { | |
onError(IStorage::ERROR_REASON::GENERIC_ERROR, "Failure during saving cache file. Are you sure that the directory '" + localCacheDirectory + "' exists?"); | |
return; | |
} | |
} | |
catch (std::exception &e) { | |
onError(IStorage::ERROR_REASON::GENERIC_ERROR, "Could not save cache file: " + std::string(e.what())); | |
} | |
try { | |
file.close(); | |
} | |
catch (exception &e) { | |
onError(IStorage::ERROR_REASON::GENERIC_ERROR, "Error during closing cache file: " + std::string(e.what())); | |
} | |
// All good, invoke success callback | |
onSuccess(); | |
}; | |
/** | |
* Function for caching metadata, called from 'onEncodingFinished()' when cache mode is turned on. | |
* The implementation may save the metadata locally. | |
* Currently there is not way to signal failure and no success callback either. The caller | |
* assumes that when the function returns, it successfully finished doing its business. | |
* (Callbacks will be introduced in the next version) | |
* Since this function is invoked from 'onEncodingFinished()', you can be sure that | |
* all 'storeData()' calls had finished already and 'cachePacket()' function was invoked | |
* and finished for every packet. | |
* It's a good idea to add the file to the file list and schedule uploading packets of the file | |
* to the cloud storage in this function. | |
* | |
* | |
* @param encodedFile | |
* The userfile (virtual path + filename) that is being encoded. | |
* @param metadata | |
* CodingMetadata that belongs to the userfile | |
*/ | |
std::function<void(const ProcessedFile& encodedFile, const CodingMetadata& metadata)> cacheMetadata = | |
[](const ProcessedFile& encodedFile, const CodingMetadata& metadata) | |
{ | |
// TODO Implement me | |
// If you want to cache the metadata persistently, use CodingMetadata::toBytes() for | |
// serialization (and CodingMetadata::fromBytes() for de-serialization) | |
// | |
// Serialization: std::vector<unsigned char> binaryMetadata = metadata.toBytes(); | |
// De-serialization: CodingMetadata metadata = CodingMetadata::fromBytes(binaryMetadata); | |
// TODO schedule upload of the file packets (?) | |
}; | |
/// Decoding-related inherited functions (ErrorCallback added) | |
bool onDecodingStart(const ProcessedFile& fileToDecode); | |
void getData(const ProcessedFile& fileToDecode, const std::list<unsigned>& packetIndices, PacketReceivedHandler packetReceivedHandler, ErrorCallback onError); | |
/** | |
* Funcion to be called from 'onDecodingStart()' when cache mode is turned on. | |
* It has to check whether cached packets of the given userfile are available or not. | |
* | |
* @param file | |
* The userfile which is decoded | |
* @returns | |
* True if the file's packet(s) are available in the cache, false otherwise | |
*/ | |
std::function<bool(const ProcessedFile& file)> isFileCached = [](const ProcessedFile& file) | |
{ | |
// TODO implement me | |
return false; | |
}; | |
/** | |
* Function to be called from 'getData()' when cache mode is turned on. | |
* Must load the requested packet from cache and pass it to the data callback. | |
* In case of an error, the ErrorCallback must be invoked. | |
* | |
* @param file | |
* The userfile which the requested packet belongs to | |
* @param packetIndex | |
* Index of the packet | |
* @param onData | |
* Packet data must be passed to this callback when loaded from cache | |
* @param onError | |
* Callback to be invoked upon error | |
*/ | |
std::function<void(const ProcessedFile& file, unsigned packetIndex, DataCallback onData, ErrorCallback onError)> loadCachedPacket = | |
[](const ProcessedFile& file, unsigned packetIndex, DataCallback onData, ErrorCallback onError) | |
{ | |
// TODO implement me | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment