CLOUD: Refactor ConnectionManager/Requests system

ConnectionManager now storages Request * (not generates ids for it),
Requests have control on their RequestState, RequestIdPair is now called
Response and storages Request * with some response together.

All related classes are changed to use it in more clean and
understandable way.

Request, RequestState and Response are carefully commented/documented.
This commit is contained in:
Alexander Tkachev 2016-05-27 15:21:06 +06:00
parent 83b349a033
commit 98150beb38
20 changed files with 325 additions and 239 deletions

View file

@ -32,7 +32,7 @@ namespace Dropbox {
DropboxListDirectoryRequest::DropboxListDirectoryRequest(Common::String token, Common::String path, Storage::FileArrayCallback cb, bool recursive):
Networking::Request(0), _requestedPath(path), _requestedRecursive(recursive), _filesCallback(cb),
_token(token), _complete(false), _requestId(-1) {
_token(token), _complete(false), _innerRequest(nullptr) {
startupWork();
}
@ -40,7 +40,7 @@ void DropboxListDirectoryRequest::startupWork() {
_files.clear();
_complete = false;
Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::RequestJsonPair>(this, &DropboxListDirectoryRequest::responseCallback);
Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::JsonResponse>(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");
@ -54,11 +54,11 @@ void DropboxListDirectoryRequest::startupWork() {
Common::JSONValue value(jsonRequestParameters);
request->addPostField(Common::JSON::stringify(&value));
_requestId = ConnMan.addRequest(request);
_innerRequest = ConnMan.addRequest(request);
}
void DropboxListDirectoryRequest::responseCallback(Networking::RequestJsonPair pair) {
void DropboxListDirectoryRequest::responseCallback(Networking::JsonResponse pair) {
Common::JSONValue *json = pair.value;
if (json) {
Common::JSONObject response = json->asObject();
@ -89,7 +89,7 @@ void DropboxListDirectoryRequest::responseCallback(Networking::RequestJsonPair p
bool hasMore = response.getVal("has_more")->asBool();
if (hasMore) {
Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::RequestJsonPair>(this, &DropboxListDirectoryRequest::responseCallback);
Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::JsonResponse>(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");
@ -113,22 +113,28 @@ void DropboxListDirectoryRequest::responseCallback(Networking::RequestJsonPair p
}
void DropboxListDirectoryRequest::handle() {
if (_complete) {
ConnMan.getRequestInfo(_id).state = Networking::FINISHED;
if (_filesCallback) (*_filesCallback)(Storage::RequestFileArrayPair(_id, _files));
}
if (_complete) finishFiles(_files);
}
void DropboxListDirectoryRequest::restart() {
if (_requestId != -1) {
Networking::RequestInfo &info = ConnMan.getRequestInfo(_requestId);
if (_innerRequest) {
//TODO: I'm really not sure some CurlRequest would handle this (it must stop corresponding CURL transfer)
info.state = Networking::FINISHED; //may be CANCELED or INTERRUPTED or something?
_requestId = -1;
_innerRequest->finish(); //may be CANCELED or INTERRUPTED or something?
_innerRequest = nullptr;
}
startupWork();
}
void DropboxListDirectoryRequest::finish() {
Common::Array<StorageFile> files;
finishFiles(files);
}
void DropboxListDirectoryRequest::finishFiles(Common::Array<StorageFile> &files) {
Request::finish();
if (_filesCallback) (*_filesCallback)(Storage::FileArrayResponse(this, files));
}
} //end of namespace Dropbox
} //end of namespace Cloud