From 9e531e3ce7f5b3a1cc87b43beb6f72911cb41bdd Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Sat, 21 May 2016 23:21:42 +0600 Subject: CLOUD: Polish Callbacks Cleaned up all example code and old callbacks. New Callback classes are introduced in "common/callback.h" and documented. --- backends/cloud/dropbox/dropboxstorage.cpp | 113 ++++++++---------------------- backends/cloud/dropbox/dropboxstorage.h | 31 ++++---- backends/cloud/manager.cpp | 2 +- backends/cloud/manager.h | 2 +- backends/cloud/storage.h | 26 +++---- 5 files changed, 57 insertions(+), 117 deletions(-) (limited to 'backends/cloud') diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 04e9eeef0b..964b95bb09 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -35,17 +35,6 @@ namespace Dropbox { Common::String DropboxStorage::KEY; //can't use ConfMan there yet, loading it on instance creation/auth Common::String DropboxStorage::SECRET; //TODO: hide these secrets somehow -static void printJsonCallback(Networking::Request* rq, void *ptr) { - Common::JSONValue *json = (Common::JSONValue *)ptr; - if (json) { - debug("printJsonCallback:"); - debug("%s", json->stringify(true).c_str()); - delete json; - } else { - debug("printJsonCallback: got NULL instead of JSON!"); - } -} - static void saveAccessTokenCallback(void *ptr) { Common::JSONValue *json = (Common::JSONValue *)ptr; if (json) { @@ -69,53 +58,6 @@ static void saveAccessTokenCallback(void *ptr) { } } -void infoCallback(Networking::Request* request, void *jsonPointer) { - if (!request) { - warning("infoCallback: got NULL instead of Request"); - - Common::JSONValue *json = (Common::JSONValue *)jsonPointer; - if (json) delete json; //yeah I know we can delete NULL safely - return; - } - - Storage::InfoCallback callback = (Storage::InfoCallback)request->pointer(); - - Common::JSONValue *json = (Common::JSONValue *)jsonPointer; - if (json) { - //Common::JSONObject result = json->asObject(); - if (callback) { - callback(StorageInfo(json->stringify())); - } - delete json; - } else { - warning("infoCallback: got NULL instead of JSON!"); - } -} - -void info2Callback(Networking::Request* request, void *jsonPointer) { - if (!request) { - warning("infoCallback: got NULL instead of Request"); - - Common::JSONValue *json = (Common::JSONValue *)jsonPointer; - if (json) delete json; //yeah I know we can delete NULL safely - return; - } - - Common::BaseCallback *callback = (Common::BaseCallback *)request->pointer(); - - Common::JSONValue *json = (Common::JSONValue *)jsonPointer; - if (json) { - //Common::JSONObject result = json->asObject(); - if (callback) { - (*callback)(new StorageInfo(json->stringify())); - delete callback; - } - delete json; - } else { - warning("infoCallback: got NULL instead of JSON!"); - } -} - DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) { curl_global_init(CURL_GLOBAL_ALL); } @@ -124,41 +66,42 @@ DropboxStorage::~DropboxStorage() { curl_global_cleanup(); } -void syncSavesInfoCallback(StorageInfo info) { - debug("info: %s", info.info().c_str()); -} - -void DropboxStorage::infoMethodCallback(void *storageInfo) { - StorageInfo *info = (StorageInfo *)storageInfo; - debug("info: %s", info->info().c_str()); +void DropboxStorage::syncSaves(Common::BaseCallback *callback) { + //this is not the real syncSaves() implementation + info(new Common::Callback(this, &DropboxStorage::infoMethodCallback)); + //that line meant the following: + //"please, do the info API request and, when it's finished, call the infoMethodCallback() of me" } -void DropboxStorage::syncSaves(OperationCallback callback) { - //this is not the real syncSaves() implementation - info2(new Common::Callback(this, &DropboxStorage::infoMethodCallback)); -} - -void DropboxStorage::info(InfoCallback callback) { - /* - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(infoCallback, "https://api.dropboxapi.com/1/account/info"); +void DropboxStorage::info(Common::BaseCallback *outerCallback) { + Common::BaseCallback<> *innerCallback = new Common::CallbackBridge(this, &DropboxStorage::infoInnerCallback, outerCallback); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/1/account/info"); request->addHeader("Authorization: Bearer " + _token); ConnMan.addRequest(request); - - request->setPointer(callback); - */ + //that callback bridge wraps the outerCallback (passed in arguments from user) into innerCallback + //so, when CurlJsonRequest is finished, it calls the innerCallback + //innerCallback (which is DropboxStorage::infoInnerCallback in this case) processes the void *ptr + //and then calls the outerCallback (which wants to receive StorageInfo, not void *) } -void DropboxStorage::info2BridgeCallback(Common::BaseCallback *outerCallback, void *ptr) { - //no NULL checks, delete and such yet +void DropboxStorage::infoInnerCallback(Common::BaseCallback *outerCallback, void *ptr) { Common::JSONValue *json = (Common::JSONValue *)ptr; - (*outerCallback)(new StorageInfo(json->stringify())); + if (!json) { + warning("NULL passed instead of JSON"); + delete outerCallback; + return; + } + + if (outerCallback) { + (*outerCallback)(StorageInfo(json->stringify())); + delete outerCallback; + } + + delete json; } -void DropboxStorage::info2(Common::BaseCallback *outerCallback) { - Common::BaseCallback *innerCallback = new Common::CallbackBridge(this, &DropboxStorage::info2BridgeCallback, outerCallback); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/1/account/info"); - request->addHeader("Authorization: Bearer " + _token); - ConnMan.addRequest(request); +void DropboxStorage::infoMethodCallback(StorageInfo storageInfo) { + debug("info: %s", storageInfo.info().c_str()); } DropboxStorage *DropboxStorage::loadFromConfig() { @@ -212,7 +155,7 @@ void DropboxStorage::authThroughConsole() { } void DropboxStorage::getAccessToken(Common::String code) { - Common::BaseCallback *callback = new Common::GlobalFunctionCallback(saveAccessTokenCallback); + Common::BaseCallback<> *callback = new Common::GlobalFunctionCallback(saveAccessTokenCallback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, "https://api.dropboxapi.com/1/oauth2/token"); request->addPostField("code=" + code); request->addPostField("grant_type=authorization_code"); diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index efd0eea257..8cc9312a87 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -24,7 +24,6 @@ #define BACKENDS_CLOUD_DROPBOX_STORAGE_H #include "backends/cloud/storage.h" -#include "backends/cloud/manager.h" #include "common/callback.h" namespace Cloud { @@ -40,36 +39,38 @@ class DropboxStorage: public Cloud::Storage { static void getAccessToken(Common::String code); - void infoMethodCallback(void *serviceInfoPtr); - public: virtual ~DropboxStorage(); - /** Returns pointer to Common::Array. */ - virtual void listDirectory(Common::String path, ListDirectoryCallback callback) {} //TODO + /** Returns pointer to Common::Array. */ + virtual void listDirectory(Common::String path, Common::BaseCallback< Common::Array > *callback) {} //TODO /** Calls the callback when finished. */ - virtual void upload(Common::String path, Common::ReadStream* contents, OperationCallback callback) {} //TODO + virtual void upload(Common::String path, Common::ReadStream* contents, Common::BaseCallback *callback) {} //TODO /** Returns pointer to Common::ReadStream. */ - virtual void download(Common::String path, DownloadCallback callback) {} //TODO + virtual void download(Common::String path, Common::BaseCallback *callback) {} //TODO /** Calls the callback when finished. */ - virtual void remove(Common::String path, OperationCallback callback) {} //TODO + virtual void remove(Common::String path, Common::BaseCallback *callback) {} //TODO /** Calls the callback when finished. */ - virtual void syncSaves(OperationCallback callback); + virtual void syncSaves(Common::BaseCallback *callback); /** Calls the callback when finished. */ - virtual void createDirectory(Common::String path, OperationCallback callback) {} //TODO + virtual void createDirectory(Common::String path, Common::BaseCallback *callback) {} //TODO /** Calls the callback when finished. */ - virtual void touch(Common::String path, OperationCallback callback) {} //TODO + virtual void touch(Common::String path, Common::BaseCallback *callback) {} //TODO + + /** Returns pointer to the StorageInfo struct. */ + virtual void info(Common::BaseCallback *callback); + + /** This is what is called by CurlJsonRequest. */ + void infoInnerCallback(Common::BaseCallback *outerCallback, void *ptr); - /** Returns pointer to the ServiceInfo struct. */ - virtual void info(InfoCallback callback); - void info2(Common::BaseCallback *outerCallback); - void info2BridgeCallback(Common::BaseCallback *outerCallback, void *ptr); + /** This is what is called by infoInnerCallback() (it's its outer callback). */ + void infoMethodCallback(StorageInfo storageInfo); /** Returns whether saves sync process is running. */ virtual bool isSyncing() { return false; } //TODO diff --git a/backends/cloud/manager.cpp b/backends/cloud/manager.cpp index fc271485bf..a9a2b8a232 100644 --- a/backends/cloud/manager.cpp +++ b/backends/cloud/manager.cpp @@ -46,7 +46,7 @@ Storage* Manager::getCurrentStorage() { return _currentStorage; } -void Manager::syncSaves(Storage::OperationCallback callback) { +void Manager::syncSaves(Common::BaseCallback *callback) { Storage* storage = getCurrentStorage(); if (storage) storage->syncSaves(callback); } diff --git a/backends/cloud/manager.h b/backends/cloud/manager.h index 47109cc146..c247132707 100644 --- a/backends/cloud/manager.h +++ b/backends/cloud/manager.h @@ -38,7 +38,7 @@ public: virtual void init(); virtual Storage* getCurrentStorage(); - virtual void syncSaves(Storage::OperationCallback callback); + virtual void syncSaves(Common::BaseCallback *callback); }; } //end of namespace Cloud diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 4fb06f6a9d..f2079eb919 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -26,6 +26,7 @@ #include "common/array.h" #include "common/stream.h" #include "common/str.h" +#include "common/callback.h" namespace Cloud { @@ -70,37 +71,32 @@ public: class Storage { public: - typedef void(*ListDirectoryCallback)(Common::Array& result); - typedef void(*DownloadCallback)(Common::ReadStream* result); - typedef void(*InfoCallback)(StorageInfo result); - typedef void(*OperationCallback)(bool successed); - Storage() {} virtual ~Storage() {} - /** Returns pointer to Common::Array. */ - virtual void listDirectory(Common::String path, ListDirectoryCallback callback) = 0; + /** Returns pointer to Common::Array. */ + virtual void listDirectory(Common::String path, Common::BaseCallback< Common::Array > *callback) = 0; /** Calls the callback when finished. */ - virtual void upload(Common::String path, Common::ReadStream* contents, OperationCallback callback) = 0; + virtual void upload(Common::String path, Common::ReadStream* contents, Common::BaseCallback *callback) = 0; /** Returns pointer to Common::ReadStream. */ - virtual void download(Common::String path, DownloadCallback callback) = 0; + virtual void download(Common::String path, Common::BaseCallback *callback) = 0; /** Calls the callback when finished. */ - virtual void remove(Common::String path, OperationCallback callback) = 0; + virtual void remove(Common::String path, Common::BaseCallback *callback) = 0; /** Calls the callback when finished. */ - virtual void syncSaves(OperationCallback callback) = 0; + virtual void syncSaves(Common::BaseCallback *callback) = 0; /** Calls the callback when finished. */ - virtual void createDirectory(Common::String path, OperationCallback callback) = 0; + virtual void createDirectory(Common::String path, Common::BaseCallback *callback) = 0; /** Calls the callback when finished. */ - virtual void touch(Common::String path, OperationCallback callback) = 0; + virtual void touch(Common::String path, Common::BaseCallback *callback) = 0; - /** Returns pointer to the ServiceInfo struct. */ - virtual void info(InfoCallback callback) = 0; + /** Returns pointer to the StorageInfo struct. */ + virtual void info(Common::BaseCallback *callback) = 0; /** Returns whether saves sync process is running. */ virtual bool isSyncing() = 0; -- cgit v1.2.3