From f4547f44df32ce1f49a6a36df083e7703751adcd Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Thu, 26 May 2016 19:02:55 +0600 Subject: CLOUD: Add RequestIdPair struct Can be used with Callback (means it's still type safe). It's used to pass not only Request id to user's callback, but also a value user wanted. void *data field is removed from RequestInfo. --- backends/cloud/downloadrequest.cpp | 6 ++-- .../cloud/dropbox/dropboxlistdirectoryrequest.cpp | 11 ++++---- .../cloud/dropbox/dropboxlistdirectoryrequest.h | 2 +- backends/cloud/dropbox/dropboxstorage.cpp | 26 +++++++++--------- backends/cloud/dropbox/dropboxstorage.h | 4 +-- backends/cloud/onedrive/onedrivestorage.cpp | 32 +++++++++++----------- backends/cloud/onedrive/onedrivestorage.h | 8 +++--- backends/cloud/storage.h | 14 +++++++--- backends/networking/curl/connectionmanager.h | 7 ++--- backends/networking/curl/curljsonrequest.cpp | 4 +-- backends/networking/curl/curljsonrequest.h | 2 +- backends/networking/curl/curlrequest.cpp | 2 +- backends/networking/curl/curlrequest.h | 2 +- backends/networking/curl/request.h | 14 ++++++++-- 14 files changed, 75 insertions(+), 59 deletions(-) (limited to 'backends') diff --git a/backends/cloud/downloadrequest.cpp b/backends/cloud/downloadrequest.cpp index a96c298fe8..6f777b7cb1 100644 --- a/backends/cloud/downloadrequest.cpp +++ b/backends/cloud/downloadrequest.cpp @@ -57,7 +57,7 @@ bool DownloadRequest::handle() { if (_localFile->write(buf, readBytes) != readBytes) { warning("DownloadRequest: unable to write all received bytes into output file"); ConnMan.getRequestInfo(_id).state = Networking::FINISHED; - if (_boolCallback) (*_boolCallback)(false); + if (_boolCallback) (*_boolCallback)(Storage::RequestBoolPair(_id, false)); return true; } @@ -68,7 +68,7 @@ bool DownloadRequest::handle() { } ConnMan.getRequestInfo(_id).state = Networking::FINISHED; - if (_boolCallback) (*_boolCallback)(_remoteFileStream->httpResponseCode() == 200); + if (_boolCallback) (*_boolCallback)(Storage::RequestBoolPair(_id, _remoteFileStream->httpResponseCode() == 200)); _localFile->close(); //yes, I know it's closed automatically in ~DumpFile() return true; @@ -82,7 +82,7 @@ void DownloadRequest::restart() { //thus, it can't restart it warning("DownloadRequest: cannot be restarted"); ConnMan.getRequestInfo(_id).state = Networking::FINISHED; - if (_boolCallback) (*_boolCallback)(false); + if (_boolCallback) (*_boolCallback)(Storage::RequestBoolPair(_id, false)); //TODO: fix that } diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp index 3dada884a0..be9304081e 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp @@ -25,6 +25,7 @@ #include "backends/networking/curl/connectionmanager.h" #include "backends/networking/curl/curljsonrequest.h" #include "common/json.h" +#include "backends/cloud/storage.h" namespace Cloud { namespace Dropbox { @@ -39,7 +40,7 @@ void DropboxListDirectoryRequest::startupWork() { _files.clear(); _complete = false; - Common::BaseCallback<> *innerCallback = new Common::Callback(this, &DropboxListDirectoryRequest::responseCallback); + Networking::DataCallback innerCallback = new Common::Callback(this, &DropboxListDirectoryRequest::responseCallback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/2/files/list_folder"); request->addHeader("Authorization: Bearer " + _token); request->addHeader("Content-Type: application/json"); @@ -57,8 +58,8 @@ void DropboxListDirectoryRequest::startupWork() { } -void DropboxListDirectoryRequest::responseCallback(void *jsonPtr) { - Common::JSONValue *json = (Common::JSONValue *)jsonPtr; +void DropboxListDirectoryRequest::responseCallback(Networking::RequestDataPair pair) { + Common::JSONValue *json = (Common::JSONValue *)pair.value; if (json) { Common::JSONObject response = json->asObject(); @@ -88,7 +89,7 @@ void DropboxListDirectoryRequest::responseCallback(void *jsonPtr) { bool hasMore = response.getVal("has_more")->asBool(); if (hasMore) { - Common::BaseCallback<> *innerCallback = new Common::Callback(this, &DropboxListDirectoryRequest::responseCallback); + Networking::DataCallback innerCallback = new Common::Callback(this, &DropboxListDirectoryRequest::responseCallback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/2/files/list_folder/continue"); request->addHeader("Authorization: Bearer " + _token); request->addHeader("Content-Type: application/json"); @@ -114,7 +115,7 @@ void DropboxListDirectoryRequest::responseCallback(void *jsonPtr) { bool DropboxListDirectoryRequest::handle() { if (_complete && _filesCallback) { ConnMan.getRequestInfo(_id).state = Networking::FINISHED; - if (_filesCallback) (*_filesCallback)(_files); + if (_filesCallback) (*_filesCallback)(Storage::RequestFileArrayPair(_id, _files)); } return _complete; diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h index 36070a2a32..58f3dc6113 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h @@ -40,7 +40,7 @@ class DropboxListDirectoryRequest: public Networking::Request { Common::Array _files; int32 _requestId; - void responseCallback(void *jsonPtr); + void responseCallback(Networking::RequestDataPair pair); void startupWork(); public: diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 9acbfc0428..02b033fced 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -38,8 +38,8 @@ 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 saveAccessTokenCallback(void *ptr) { - Common::JSONValue *json = (Common::JSONValue *)ptr; +static void saveAccessTokenCallback(Networking::RequestDataPair pair) { + Common::JSONValue *json = (Common::JSONValue *)pair.value; if (json) { debug("saveAccessTokenCallback:"); debug("%s", json->stringify(true).c_str()); @@ -105,7 +105,7 @@ int32 DropboxStorage::download(Common::String remotePath, Common::String localPa Common::DumpFile *f = new Common::DumpFile(); if (!f->open(localPath, true)) { warning("DropboxStorage: unable to open file to download into"); - if (callback) (*callback)(false); + if (callback) (*callback)(RequestBoolPair(-1, false)); delete f; return -1; } @@ -121,7 +121,7 @@ int32 DropboxStorage::syncSaves(BoolCallback callback) { } int32 DropboxStorage::info(StorageInfoCallback outerCallback) { - Common::BaseCallback<> *innerCallback = new Common::CallbackBridge(this, &DropboxStorage::infoInnerCallback, outerCallback); + Networking::DataCallback 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); return ConnMan.addRequest(request); @@ -131,8 +131,8 @@ int32 DropboxStorage::info(StorageInfoCallback outerCallback) { //and then calls the outerCallback (which wants to receive StorageInfo, not void *) } -void DropboxStorage::infoInnerCallback(StorageInfoCallback outerCallback, void *jsonPointer) { - Common::JSONValue *json = (Common::JSONValue *)jsonPointer; +void DropboxStorage::infoInnerCallback(StorageInfoCallback outerCallback, Networking::RequestDataPair pair) { + Common::JSONValue *json = (Common::JSONValue *)pair.value; if (!json) { warning("NULL passed instead of JSON"); delete outerCallback; @@ -148,19 +148,19 @@ void DropboxStorage::infoInnerCallback(StorageInfoCallback outerCallback, void * Common::JSONObject quota = info.getVal("quota_info")->asObject(); uint32 quotaNormal = quota.getVal("normal")->asNumber(); uint32 quotaShared = quota.getVal("shared")->asNumber(); - uint32 quotaAllocated = quota.getVal("quota")->asNumber(); - (*outerCallback)(StorageInfo(uid, name, email, quotaNormal+quotaShared, quotaAllocated)); + uint32 quotaAllocated = quota.getVal("quota")->asNumber(); + (*outerCallback)(RequestStorageInfoPair(-1, StorageInfo(uid, name, email, quotaNormal+quotaShared, quotaAllocated))); delete outerCallback; } delete json; } -void DropboxStorage::infoMethodCallback(StorageInfo storageInfo) { +void DropboxStorage::infoMethodCallback(RequestStorageInfoPair pair) { debug("\nStorage info:"); - debug("User name: %s", storageInfo.name().c_str()); - debug("Email: %s", storageInfo.email().c_str()); - debug("Disk usage: %u/%u", storageInfo.used(), storageInfo.available()); + debug("User name: %s", pair.value.name().c_str()); + debug("Email: %s", pair.value.email().c_str()); + debug("Disk usage: %u/%u", pair.value.used(), pair.value.available()); } DropboxStorage *DropboxStorage::loadFromConfig(Common::String keyPrefix) { @@ -214,7 +214,7 @@ void DropboxStorage::authThroughConsole() { } void DropboxStorage::getAccessToken(Common::String code) { - Common::BaseCallback<> *callback = new Common::GlobalFunctionCallback(saveAccessTokenCallback); + Networking::DataCallback 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 6abd3d1d94..4fe6109c17 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -40,7 +40,7 @@ class DropboxStorage: public Cloud::Storage { static void getAccessToken(Common::String code); /** Constructs StorageInfo based on JSON response from cloud. */ - void infoInnerCallback(StorageInfoCallback outerCallback, void *json); + void infoInnerCallback(StorageInfoCallback outerCallback, Networking::RequestDataPair json); void printFiles(Common::Array files); @@ -91,7 +91,7 @@ public: virtual int32 info(StorageInfoCallback callback); /** This method is passed into info(). (Temporary) */ - void infoMethodCallback(StorageInfo storageInfo); + void infoMethodCallback(RequestStorageInfoPair pair); /** Returns whether saves sync process is running. */ virtual bool isSyncing() { return false; } //TODO diff --git a/backends/cloud/onedrive/onedrivestorage.cpp b/backends/cloud/onedrive/onedrivestorage.cpp index b0690be5b0..36b3e26f1b 100644 --- a/backends/cloud/onedrive/onedrivestorage.cpp +++ b/backends/cloud/onedrive/onedrivestorage.cpp @@ -42,7 +42,7 @@ OneDriveStorage::OneDriveStorage(Common::String accessToken, Common::String user _token(accessToken), _uid(userId), _refreshToken(refreshToken) {} OneDriveStorage::OneDriveStorage(Common::String code) { - getAccessToken(new Common::Callback(this, &OneDriveStorage::codeFlowComplete), code); + getAccessToken(new Common::Callback(this, &OneDriveStorage::codeFlowComplete), code); } OneDriveStorage::~OneDriveStorage() {} @@ -52,11 +52,11 @@ void OneDriveStorage::getAccessToken(BoolCallback callback, Common::String code) if (!codeFlow && _refreshToken == "") { warning("OneDriveStorage: no refresh token available to get new access token."); - if (callback) (*callback)(false); + if (callback) (*callback)(RequestBoolPair(-1, false)); return; } - Common::BaseCallback<> *innerCallback = new Common::CallbackBridge(this, &OneDriveStorage::tokenRefreshed, callback); + Networking::DataCallback innerCallback = new Common::CallbackBridge(this, &OneDriveStorage::tokenRefreshed, callback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://login.live.com/oauth20_token.srf"); if (codeFlow) { request->addPostField("code=" + code); @@ -71,11 +71,11 @@ void OneDriveStorage::getAccessToken(BoolCallback callback, Common::String code) ConnMan.addRequest(request); } -void OneDriveStorage::tokenRefreshed(BoolCallback callback, void *jsonPointer) { - Common::JSONValue *json = (Common::JSONValue *)jsonPointer; +void OneDriveStorage::tokenRefreshed(BoolCallback callback, Networking::RequestDataPair pair) { + Common::JSONValue *json = (Common::JSONValue *)pair.value; if (!json) { warning("OneDriveStorage: got NULL instead of JSON"); - if (callback) (*callback)(false); + if (callback) (*callback)(RequestBoolPair(-1, false)); return; } @@ -83,19 +83,19 @@ void OneDriveStorage::tokenRefreshed(BoolCallback callback, void *jsonPointer) { if (!result.contains("access_token") || !result.contains("user_id") || !result.contains("refresh_token")) { warning("Bad response, no token or user_id passed"); debug("%s", json->stringify().c_str()); - if (callback) (*callback)(false); + if (callback) (*callback)(RequestBoolPair(-1, false)); } else { _token = result.getVal("access_token")->asString(); _uid = result.getVal("user_id")->asString(); _refreshToken = result.getVal("refresh_token")->asString(); g_system->getCloudManager()->save(); //ask CloudManager to save our new refreshToken - if (callback) (*callback)(true); + if (callback) (*callback)(RequestBoolPair(-1, true)); } delete json; } -void OneDriveStorage::codeFlowComplete(bool success) { - if (!success) { +void OneDriveStorage::codeFlowComplete(RequestBoolPair pair) { + if (!pair.value) { warning("OneDriveStorage: failed to get access token through code flow"); return; } @@ -113,12 +113,12 @@ void OneDriveStorage::saveConfig(Common::String keyPrefix) { ConfMan.set(keyPrefix + "refresh_token", _refreshToken, "cloud"); } -void OneDriveStorage::printJsonTokenReceived(bool success) { - if (success) syncSaves(0); //try again +void OneDriveStorage::printJsonTokenReceived(RequestBoolPair pair) { + if (pair.value) syncSaves(0); //try again } -void OneDriveStorage::printJson(void *jsonPointer) { - Common::JSONValue *json = (Common::JSONValue *)jsonPointer; +void OneDriveStorage::printJson(Networking::RequestDataPair pair) { + Common::JSONValue *json = (Common::JSONValue *)pair.value; if (!json) { warning("printJson: NULL"); return; @@ -128,7 +128,7 @@ void OneDriveStorage::printJson(void *jsonPointer) { if (result.contains("error")) { //Common::JSONObject error = result.getVal("error")->asObject(); debug("bad token, trying again..."); - getAccessToken(new Common::Callback(this, &OneDriveStorage::printJsonTokenReceived)); + getAccessToken(new Common::Callback(this, &OneDriveStorage::printJsonTokenReceived)); delete json; return; } @@ -139,7 +139,7 @@ void OneDriveStorage::printJson(void *jsonPointer) { int32 OneDriveStorage::syncSaves(BoolCallback callback) { //this is not the real syncSaves() implementation - Common::BaseCallback<> *innerCallback = new Common::Callback(this, &OneDriveStorage::printJson); + Networking::DataCallback innerCallback = new Common::Callback(this, &OneDriveStorage::printJson); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.onedrive.com/v1.0/drives/"); request->addHeader("Authorization: bearer " + _token); return ConnMan.addRequest(request); diff --git a/backends/cloud/onedrive/onedrivestorage.h b/backends/cloud/onedrive/onedrivestorage.h index 4141771f65..3c92880750 100644 --- a/backends/cloud/onedrive/onedrivestorage.h +++ b/backends/cloud/onedrive/onedrivestorage.h @@ -49,11 +49,11 @@ class OneDriveStorage: public Cloud::Storage { * continue your work when new token is available. */ void getAccessToken(BoolCallback callback, Common::String code = ""); - void tokenRefreshed(BoolCallback callback, void *jsonPointer); - void codeFlowComplete(bool success); + void tokenRefreshed(BoolCallback callback, Networking::RequestDataPair pair); + void codeFlowComplete(RequestBoolPair pair); - void printJson(void *jsonPointer); - void printJsonTokenReceived(bool success); + void printJson(Networking::RequestDataPair pair); + void printJsonTokenReceived(RequestBoolPair pair); public: virtual ~OneDriveStorage(); diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 394fc2c22d..325d57d02c 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -30,15 +30,21 @@ #include "backends/cloud/storagefile.h" #include "backends/cloud/storageinfo.h" #include "backends/networking/curl/networkreadstream.h" +#include namespace Cloud { class Storage { public: - typedef Common::BaseCallback< Common::Array > *FileArrayCallback; - typedef Common::BaseCallback *ReadStreamCallback; - typedef Common::BaseCallback *StorageInfoCallback; - typedef Common::BaseCallback *BoolCallback; + typedef Networking::RequestIdPair&> RequestFileArrayPair; + typedef Networking::RequestIdPair RequestReadStreamPair; + typedef Networking::RequestIdPair RequestStorageInfoPair; + typedef Networking::RequestIdPair RequestBoolPair; + + typedef Common::BaseCallback *FileArrayCallback; + typedef Common::BaseCallback *ReadStreamCallback; + typedef Common::BaseCallback *StorageInfoCallback; + typedef Common::BaseCallback *BoolCallback; Storage() {} virtual ~Storage() {} diff --git a/backends/networking/curl/connectionmanager.h b/backends/networking/curl/connectionmanager.h index 9ae52b3eeb..15327a28b2 100644 --- a/backends/networking/curl/connectionmanager.h +++ b/backends/networking/curl/connectionmanager.h @@ -46,12 +46,11 @@ enum RequestState { struct RequestInfo { int32 id; Request *request; - RequestState state; - void *data; + RequestState state; uint32 retryInSeconds; - RequestInfo() : id(-1), request(0), state(FINISHED), data(0), retryInSeconds(0) {} - RequestInfo(int32 rqId, Request *rq) : id(rqId), request(rq), state(PROCESSING), data(0), retryInSeconds(0) {} + RequestInfo() : id(-1), request(0), state(FINISHED), retryInSeconds(0) {} + RequestInfo(int32 rqId, Request *rq) : id(rqId), request(rq), state(PROCESSING), retryInSeconds(0) {} }; class ConnectionManager : public Common::Singleton { diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp index 21c0a0f644..11eeb2904a 100644 --- a/backends/networking/curl/curljsonrequest.cpp +++ b/backends/networking/curl/curljsonrequest.cpp @@ -31,7 +31,7 @@ namespace Networking { -CurlJsonRequest::CurlJsonRequest(Common::BaseCallback<> *cb, const char *url): +CurlJsonRequest::CurlJsonRequest(DataCallback cb, const char *url): CurlRequest(cb, url), _contentsStream(DisposeAfterUse::YES) {} CurlJsonRequest::~CurlJsonRequest() {} @@ -75,7 +75,7 @@ bool CurlJsonRequest::handle() { if (_stream->httpResponseCode() != 200) debug("%s", contents); Common::JSONValue *json = Common::JSON::parse(contents); - (*_callback)(json); //potential memory leak, free it in your callbacks! + (*_callback)(RequestDataPair(_id, json)); //potential memory leak, free it in your callbacks! } return true; } diff --git a/backends/networking/curl/curljsonrequest.h b/backends/networking/curl/curljsonrequest.h index cfb82e97e3..9b23cd79f4 100644 --- a/backends/networking/curl/curljsonrequest.h +++ b/backends/networking/curl/curljsonrequest.h @@ -37,7 +37,7 @@ class CurlJsonRequest: public CurlRequest { char *getPreparedContents(); public: - CurlJsonRequest(Common::BaseCallback<> *cb, const char *url); + CurlJsonRequest(DataCallback cb, const char *url); //TODO: use some Callback already virtual ~CurlJsonRequest(); virtual bool handle(); diff --git a/backends/networking/curl/curlrequest.cpp b/backends/networking/curl/curlrequest.cpp index e1c8f2b18c..6f5c612bdf 100644 --- a/backends/networking/curl/curlrequest.cpp +++ b/backends/networking/curl/curlrequest.cpp @@ -30,7 +30,7 @@ namespace Networking { -CurlRequest::CurlRequest(Common::BaseCallback<> *cb, const char *url): +CurlRequest::CurlRequest(DataCallback cb, const char *url): Request(cb), _url(url), _stream(0), _headersList(0) {} CurlRequest::~CurlRequest() { diff --git a/backends/networking/curl/curlrequest.h b/backends/networking/curl/curlrequest.h index ec1a9e33c6..c7f07fcb18 100644 --- a/backends/networking/curl/curlrequest.h +++ b/backends/networking/curl/curlrequest.h @@ -40,7 +40,7 @@ protected: Common::String _postFields; public: - CurlRequest(Common::BaseCallback<> *cb, const char *url); + CurlRequest(DataCallback cb, const char *url); virtual ~CurlRequest(); virtual bool handle(); diff --git a/backends/networking/curl/request.h b/backends/networking/curl/request.h index 136f007920..f2c2f1f247 100644 --- a/backends/networking/curl/request.h +++ b/backends/networking/curl/request.h @@ -28,6 +28,16 @@ namespace Networking { +template struct RequestIdPair { + int32 id; + T value; + + RequestIdPair(int32 rid, T v) : id(rid), value(v) {} +}; + +typedef RequestIdPair RequestDataPair; +typedef Common::BaseCallback *DataCallback; + class Request { protected: /** @@ -35,12 +45,12 @@ protected: * That's the way Requests pass the result to the code which asked to create this request. */ - Common::BaseCallback<> *_callback; + DataCallback _callback; int32 _id; public: - Request(Common::BaseCallback<> *cb): _callback(cb), _id(-1) {} + Request(DataCallback cb): _callback(cb), _id(-1) {} virtual ~Request() { delete _callback; } /** -- cgit v1.2.3