CLOUD: Add Requests id

(Upgrading ConnectionManager step by step.)
This commit is contained in:
Alexander Tkachev 2016-05-26 17:12:40 +06:00
parent 17fc40a944
commit eda575a660
8 changed files with 68 additions and 48 deletions

View file

@ -84,8 +84,8 @@ void DropboxStorage::printFiles(Common::Array<StorageFile> files) {
debug("\t%s", files[i].name().c_str()); debug("\t%s", files[i].name().c_str());
} }
void DropboxStorage::listDirectory(Common::String path, FileArrayCallback outerCallback, bool recursive) { int32 DropboxStorage::listDirectory(Common::String path, FileArrayCallback outerCallback, bool recursive) {
ConnMan.addRequest(new DropboxListDirectoryRequest(_token, path, outerCallback, recursive)); return ConnMan.addRequest(new DropboxListDirectoryRequest(_token, path, outerCallback, recursive));
} }
Networking::NetworkReadStream *DropboxStorage::streamFile(Common::String path) { Networking::NetworkReadStream *DropboxStorage::streamFile(Common::String path) {
@ -101,30 +101,30 @@ Networking::NetworkReadStream *DropboxStorage::streamFile(Common::String path) {
return request->execute(); return request->execute();
} }
void DropboxStorage::download(Common::String remotePath, Common::String localPath, BoolCallback callback) { int32 DropboxStorage::download(Common::String remotePath, Common::String localPath, BoolCallback callback) {
Common::DumpFile *f = new Common::DumpFile(); Common::DumpFile *f = new Common::DumpFile();
if (!f->open(localPath, true)) { if (!f->open(localPath, true)) {
warning("DropboxStorage: unable to open file to download into"); warning("DropboxStorage: unable to open file to download into");
if (callback) (*callback)(false); if (callback) (*callback)(false);
delete f; delete f;
return; return -1;
} }
ConnMan.addRequest(new DownloadRequest(callback, streamFile(remotePath), f)); return ConnMan.addRequest(new DownloadRequest(callback, streamFile(remotePath), f));
} }
void DropboxStorage::syncSaves(BoolCallback callback) { int32 DropboxStorage::syncSaves(BoolCallback callback) {
//this is not the real syncSaves() implementation //this is not the real syncSaves() implementation
//"" is root in Dropbox, not "/" //"" is root in Dropbox, not "/"
//this must create all these directories: //this must create all these directories:
download("/remote/test.jpg", "local/a/b/c/d/test.jpg", 0); return download("/remote/test.jpg", "local/a/b/c/d/test.jpg", 0);
} }
void DropboxStorage::info(StorageInfoCallback outerCallback) { int32 DropboxStorage::info(StorageInfoCallback outerCallback) {
Common::BaseCallback<> *innerCallback = new Common::CallbackBridge<DropboxStorage, StorageInfo>(this, &DropboxStorage::infoInnerCallback, outerCallback); Common::BaseCallback<> *innerCallback = new Common::CallbackBridge<DropboxStorage, StorageInfo>(this, &DropboxStorage::infoInnerCallback, outerCallback);
Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/1/account/info"); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/1/account/info");
request->addHeader("Authorization: Bearer " + _token); request->addHeader("Authorization: Bearer " + _token);
ConnMan.addRequest(request); return ConnMan.addRequest(request);
//that callback bridge wraps the outerCallback (passed in arguments from user) into innerCallback //that callback bridge wraps the outerCallback (passed in arguments from user) into innerCallback
//so, when CurlJsonRequest is finished, it calls the innerCallback //so, when CurlJsonRequest is finished, it calls the innerCallback
//innerCallback (which is DropboxStorage::infoInnerCallback in this case) processes the void *ptr //innerCallback (which is DropboxStorage::infoInnerCallback in this case) processes the void *ptr

View file

@ -64,31 +64,31 @@ public:
/** Public Cloud API comes down there. */ /** Public Cloud API comes down there. */
/** Returns Common::Array<StorageFile>. */ /** Returns Common::Array<StorageFile>. */
virtual void listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false); virtual int32 listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false);
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) {} //TODO virtual int32 upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) { return -1; } //TODO
/** Returns pointer to Networking::NetworkReadStream. */ /** Returns pointer to Networking::NetworkReadStream. */
virtual Networking::NetworkReadStream *streamFile(Common::String path); virtual Networking::NetworkReadStream *streamFile(Common::String path);
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void download(Common::String remotePath, Common::String localPath, BoolCallback callback); virtual int32 download(Common::String remotePath, Common::String localPath, BoolCallback callback);
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void remove(Common::String path, BoolCallback callback) {} //TODO virtual int32 remove(Common::String path, BoolCallback callback) { return -1; } //TODO
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void syncSaves(BoolCallback callback); virtual int32 syncSaves(BoolCallback callback);
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void createDirectory(Common::String path, BoolCallback callback) {} //TODO virtual int32 createDirectory(Common::String path, BoolCallback callback) { return -1; } //TODO
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void touch(Common::String path, BoolCallback callback) {} //TODO virtual int32 touch(Common::String path, BoolCallback callback) { return -1; } //TODO
/** Returns the StorageInfo struct. */ /** Returns the StorageInfo struct. */
virtual void info(StorageInfoCallback callback); virtual int32 info(StorageInfoCallback callback);
/** This method is passed into info(). (Temporary) */ /** This method is passed into info(). (Temporary) */
void infoMethodCallback(StorageInfo storageInfo); void infoMethodCallback(StorageInfo storageInfo);

View file

@ -137,12 +137,12 @@ void OneDriveStorage::printJson(void *jsonPointer) {
delete json; delete json;
} }
void OneDriveStorage::syncSaves(BoolCallback callback) { int32 OneDriveStorage::syncSaves(BoolCallback callback) {
//this is not the real syncSaves() implementation //this is not the real syncSaves() implementation
Common::BaseCallback<> *innerCallback = new Common::Callback<OneDriveStorage>(this, &OneDriveStorage::printJson); Common::BaseCallback<> *innerCallback = new Common::Callback<OneDriveStorage>(this, &OneDriveStorage::printJson);
Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.onedrive.com/v1.0/drives/"); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.onedrive.com/v1.0/drives/");
request->addHeader("Authorization: bearer " + _token); request->addHeader("Authorization: bearer " + _token);
ConnMan.addRequest(request); return ConnMan.addRequest(request);
} }
OneDriveStorage *OneDriveStorage::loadFromConfig(Common::String keyPrefix) { OneDriveStorage *OneDriveStorage::loadFromConfig(Common::String keyPrefix) {

View file

@ -74,31 +74,31 @@ public:
/** Public Cloud API comes down there. */ /** Public Cloud API comes down there. */
/** Returns Common::Array<StorageFile>. */ /** Returns Common::Array<StorageFile>. */
virtual void listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false) {} //TODO virtual int32 listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false) { return -1; } //TODO
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) {} //TODO virtual int32 upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) { return -1; } //TODO
/** Returns pointer to Networking::NetworkReadStream. */ /** Returns pointer to Networking::NetworkReadStream. */
virtual Networking::NetworkReadStream *streamFile(Common::String path) { return 0; } //TODO virtual Networking::NetworkReadStream *streamFile(Common::String path) { return 0; } //TODO
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void download(Common::String remotePath, Common::String localPath, BoolCallback callback) {} //TODO virtual int32 download(Common::String remotePath, Common::String localPath, BoolCallback callback) { return -1; } //TODO
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void remove(Common::String path, BoolCallback callback) {} //TODO virtual int32 remove(Common::String path, BoolCallback callback) { return -1; } //TODO
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void syncSaves(BoolCallback callback); virtual int32 syncSaves(BoolCallback callback);
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void createDirectory(Common::String path, BoolCallback callback) {} //TODO virtual int32 createDirectory(Common::String path, BoolCallback callback) { return -1; } //TODO
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void touch(Common::String path, BoolCallback callback) {} //TODO virtual int32 touch(Common::String path, BoolCallback callback) { return -1; } //TODO
/** Returns the StorageInfo struct. */ /** Returns the StorageInfo struct. */
virtual void info(StorageInfoCallback callback) {} //TODO virtual int32 info(StorageInfoCallback callback) { return -1; } //TODO
/** Returns whether saves sync process is running. */ /** Returns whether saves sync process is running. */
virtual bool isSyncing() { return false; } //TODO virtual bool isSyncing() { return false; } //TODO

View file

@ -57,34 +57,40 @@ public:
virtual void saveConfig(Common::String keyPrefix) = 0; virtual void saveConfig(Common::String keyPrefix) = 0;
/** Public Cloud API comes down there. */ /**
* Public Cloud API comes down there.
*
* All Cloud API methods return int32 request id, which might be used to access
* request through ConnectionManager. All methods also accept a callback, which
* would be called, when request is complete.
*/
/** Returns Common::Array<StorageFile>. */ /** Returns Common::Array<StorageFile>. */
virtual void listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false) = 0; virtual int32 listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false) = 0;
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) = 0; virtual int32 upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) = 0;
/** Returns pointer to Networking::NetworkReadStream. */ /** Returns pointer to Networking::NetworkReadStream. */
virtual Networking::NetworkReadStream *streamFile(Common::String path) = 0; virtual Networking::NetworkReadStream *streamFile(Common::String path) = 0; //TODO: return int32 somehow
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void download(Common::String remotePath, Common::String localPath, BoolCallback callback) = 0; virtual int32 download(Common::String remotePath, Common::String localPath, BoolCallback callback) = 0;
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void remove(Common::String path, BoolCallback callback) = 0; virtual int32 remove(Common::String path, BoolCallback callback) = 0;
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void syncSaves(BoolCallback callback) = 0; virtual int32 syncSaves(BoolCallback callback) = 0;
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void createDirectory(Common::String path, BoolCallback callback) = 0; virtual int32 createDirectory(Common::String path, BoolCallback callback) = 0;
/** Calls the callback when finished. */ /** Calls the callback when finished. */
virtual void touch(Common::String path, BoolCallback callback) = 0; virtual int32 touch(Common::String path, BoolCallback callback) = 0;
/** Returns the StorageInfo struct. */ /** Returns the StorageInfo struct. */
virtual void info(StorageInfoCallback callback) = 0; virtual int32 info(StorageInfoCallback callback) = 0;
/** Returns whether saves sync process is running. */ /** Returns whether saves sync process is running. */
virtual bool isSyncing() = 0; virtual bool isSyncing() = 0;

View file

@ -48,9 +48,12 @@ void ConnectionManager::registerEasyHandle(CURL *easy) {
curl_multi_add_handle(_multi, easy); curl_multi_add_handle(_multi, easy);
} }
void ConnectionManager::addRequest(Request *request) { int32 ConnectionManager::addRequest(Request *request) {
_requests.push_back(request); int32 newId = _nextId++;
_requests[newId] = request;
request->setId(newId);
if (!_timerStarted) startTimer(); if (!_timerStarted) startTimer();
return newId;
} }
//private goes here: //private goes here:
@ -84,10 +87,13 @@ void ConnectionManager::handle() {
void ConnectionManager::interateRequests() { void ConnectionManager::interateRequests() {
//call handle() of all running requests (so they can do their work) //call handle() of all running requests (so they can do their work)
debug("handling %d request(s)", _requests.size()); debug("handling %d request(s)", _requests.size());
for (Common::Array<Request *>::iterator i = _requests.begin(); i != _requests.end();) { for (Common::HashMap<int32, Request *>::iterator i = _requests.begin(); i != _requests.end();) {
if ((*i)->handle()) { Request *request = i->_value;
delete (*i); if (request && request->handle()) {
_requests.erase(i); delete request;
//_requests.erase(i);
_requests[i->_key] = 0;
++i; //that's temporary
} else ++i; } else ++i;
} }
if (_requests.empty()) stopTimer(); if (_requests.empty()) stopTimer();

View file

@ -26,7 +26,7 @@
#include "backends/networking/curl/request.h" #include "backends/networking/curl/request.h"
#include "common/str.h" #include "common/str.h"
#include "common/singleton.h" #include "common/singleton.h"
#include "common/array.h" #include "common/hashmap.h"
typedef void CURL; typedef void CURL;
typedef void CURLM; typedef void CURLM;
@ -41,7 +41,8 @@ class ConnectionManager : public Common::Singleton<ConnectionManager> {
CURLM *_multi; CURLM *_multi;
bool _timerStarted; bool _timerStarted;
Common::Array<Request *> _requests; Common::HashMap<int32, Request *> _requests;
int32 _nextId;
void startTimer(int interval = 1000000); //1 second is the default interval void startTimer(int interval = 1000000); //1 second is the default interval
void stopTimer(); void stopTimer();
@ -66,8 +67,10 @@ public:
* Requests until they return true. * Requests until they return true.
* *
* @note This method starts the timer if it's not started yet. * @note This method starts the timer if it's not started yet.
*
* @return generated Request's id, which might be used to get its status
*/ */
void addRequest(Request *request); int32 addRequest(Request *request);
}; };
/** Shortcut for accessing the connection manager. */ /** Shortcut for accessing the connection manager. */

View file

@ -24,6 +24,7 @@
#define BACKENDS_NETWORKING_CURL_REQUEST_H #define BACKENDS_NETWORKING_CURL_REQUEST_H
#include "common/callback.h" #include "common/callback.h"
#include "common/scummsys.h"
namespace Networking { namespace Networking {
@ -36,9 +37,11 @@ protected:
Common::BaseCallback<> *_callback; Common::BaseCallback<> *_callback;
int32 _id;
public: public:
Request(Common::BaseCallback<> *cb): _callback(cb) {}; Request(Common::BaseCallback<> *cb): _callback(cb), _id(-1) {}
virtual ~Request() { delete _callback; }; virtual ~Request() { delete _callback; }
/** /**
* Method, which does actual work. Depends on what this Request is doing. * Method, which does actual work. Depends on what this Request is doing.
@ -47,6 +50,8 @@ public:
*/ */
virtual bool handle() = 0; virtual bool handle() = 0;
void setId(int32 id) { _id = id; }
}; };
} //end of namespace Cloud } //end of namespace Cloud