diff options
author | Alexander Tkachev | 2016-05-13 17:23:28 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 1b89e25580c186fc2cc1821acee942e23fea3682 (patch) | |
tree | 62f05694534d5fa52793406f76643e5da472d106 | |
parent | b272bba7519951f38aa1aa34c70197c7be1b63fd (diff) | |
download | scummvm-rg350-1b89e25580c186fc2cc1821acee942e23fea3682.tar.gz scummvm-rg350-1b89e25580c186fc2cc1821acee942e23fea3682.tar.bz2 scummvm-rg350-1b89e25580c186fc2cc1821acee942e23fea3682.zip |
CLOUD: Add first Request
Just fooling around with the example Request, but the idea works well.
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 178 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 9 | ||||
-rw-r--r-- | backends/cloud/dropbox/finalcountdownrequest.h | 52 | ||||
-rw-r--r-- | backends/cloud/request.h | 53 | ||||
-rw-r--r-- | backends/cloud/storage.cpp | 28 | ||||
-rw-r--r-- | backends/cloud/storage.h | 14 |
6 files changed, 148 insertions, 186 deletions
diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index a8e3076aae..8a0772dc3f 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -21,187 +21,25 @@ */ #include "backends/cloud/dropbox/dropboxstorage.h" +#include "backends/cloud/dropbox/finalcountdownrequest.h" #include "common/debug.h" -#include "common/json.h" - -void example1(); -void example2(); -void example3(); namespace Cloud { namespace Dropbox { -void DropboxStorage::handler() { - if (_firstTime) { - _firstTime = false; - - example1(); - example2(); - example3(); - } else { } +static void finalCountdownCallback(void *ptr) { + warning("ladies and gentlemen, this is your callback speaking"); + warning("the returned pointer is %d", ptr); + warning("thank you for your attention"); } void DropboxStorage::listDirectory(Common::String path) { - setTimeout(1000000); //in one second + startTimer(1000000); //in one second } -void DropboxStorage::syncSaves() { - setTimeout(1000000); //in one second +void DropboxStorage::syncSaves() { + addRequest(new FinalCountdownRequest(finalCountdownCallback)); } } //end of namespace Dropbox } //end of namespace Cloud - -/// SimpleJSON examples: - -using Common::JSON; -using Common::JSONValue; -using Common::JSONArray; -using Common::JSONObject; - -// Just some sample JSON text, feel free to change but could break demo -const char *EXAMPLE = "\ -{ \ - \"string_name\" : \"string\tvalue and a \\\"quote\\\" and a unicode char \\u00BE and a c:\\\\path\\\\ or a \\/unix\\/path\\/ :D\", \ - \"bool_name\" : true, \ - \"bool_second\" : FaLsE, \ - \"null_name\" : nULl, \ - \"negative\" : -34.276, \ - \"sub_object\" : { \ - \"foo\" : \"abc\", \ - \"bar\" : 1.35e2, \ - \"blah\" : { \"a\" : \"A\", \"b\" : \"B\", \"c\" : \"C\" } \ - }, \ - \"array_letters\" : [ \"a\", \"b\", \"c\", [ 1, 2, 3 ] ] \ -} "; - -// Example 1 -void example1() { - // Parse example data - JSONValue *value = JSON::parse(EXAMPLE); - - // Did it go wrong? - if (value == NULL) { - debug("Example code failed to parse, did you change it?\r\n"); - } else { - // Retrieve the main object - JSONObject root; - if (value->isObject() == false) { - debug("The root element is not an object, did you change the example?\r\n"); - } else { - root = value->asObject(); - - // Retrieving a string - if (root.find("string_name") != root.end() && root["string_name"]->isString()) { - debug("string_name:\r\n"); - debug("------------\r\n"); - debug(root["string_name"]->asString().c_str()); - debug("\r\n\r\n"); - } - - // Retrieving a boolean - if (root.find("bool_second") != root.end() && root["bool_second"]->isBool()) { - debug("bool_second:\r\n"); - debug("------------\r\n"); - debug(root["bool_second"]->asBool() ? "it's true!" : "it's false!"); - debug("\r\n\r\n"); - } - - // Retrieving an array - if (root.find("array_letters") != root.end() && root["array_letters"]->isArray()) { - JSONArray array = root["array_letters"]->asArray(); - debug("array_letters:\r\n"); - debug("--------------\r\n"); - for (unsigned int i = 0; i < array.size(); i++) { - //wstringstream output; - debug("[%d] => %s\r\n", i, array[i]->stringify().c_str()); - } - debug("\r\n"); - } - - // Retrieving nested object - if (root.find("sub_object") != root.end() && root["sub_object"]->isObject()) { - debug("sub_object:\r\n"); - debug("-----------\r\n"); - debug(root["sub_object"]->stringify().c_str()); - debug("\r\n\r\n"); - } - } - - delete value; - } -} - -// Example 3 : compact vs. prettyprint -void example2() { - const char *EXAMPLE3 = - "{\ - \"SelectedTab\":\"Math\",\ - \"Widgets\":[\ - {\"WidgetPosition\":[0,369,800,582],\"WidgetIndex\":1,\"WidgetType\":\"WidgetCheckbox.1\"},\ - {\"WidgetPosition\":[235,453,283,489],\"IsWidgetVisible\":-1,\"Caption\":\"On\",\"EnableCaption\":-1,\"Name\":\"F2.View\",\"CaptionPosition\":2,\"ControlWidth\":25,\"ControlHeight\":36,\"Font\":0,\"Value\":\"Off\",\"WidgetIndex\":2,\"WidgetType\":\"WidgetCheckbox.1\"},\ - {\"WidgetPosition\":[235,494,283,530],\"IsWidgetVisible\":-1,\"Caption\":\"On\",\"EnableCaption\":-1,\"Name\":\"F3.View\",\"CaptionPosition\":2,\"ControlWidth\":25,\"ControlHeight\":36,\"Font\":0,\"Value\":\"Off\",\"WidgetIndex\":3,\"WidgetType\":\"WidgetCheckbox.1\"},\ - {\"WidgetPosition\":[235,536,283,572],\"IsWidgetVisible\":-1,\"Caption\":\"On\",\"EnableCaption\":-1,\"Name\":\"F4.View\",\"CaptionPosition\":2,\"ControlWidth\":25,\"ControlHeight\":36,\"Font\":0,\"Value\":\"Off\",\"WidgetIndex\":4,\"WidgetType\":\"WidgetCheckbox.1\"},\ - {\"WidgetPosition\":[287,417,400,439],\"IsWidgetVisible\":-1,\"Caption\":\"\",\"EnableCaption\":0,\"Name\":\"F1.Equation\",\"CaptionPosition\":1,\"ControlWidth\":113,\"ControlHeight\":22,\"Font\":0,\"AlignText\":0,\"EnableBorder\":0,\"CaptionOnly\":0,\"Value\":\"FFT(C1)\",\"WidgetIndex\":9,\"WidgetType\":\"WidgetStaticText.1\"},\ - {\"WidgetPosition\":[191,409,230,445],\"IsWidgetVisible\":0,\"Caption\":\"F1\",\"EnableCaption\":0,\"Name\":\"F1.MeasureOpGui\",\"CaptionPosition\":1,\"ControlWidth\":39,\"ControlHeight\":36,\"Font\":0,\"ButtonOnly\":-1,\"PickerTitle\":\"Select Measurement To Graph\",\"Value\":\"Amplitude\",\"WidgetIndex\":17,\"WidgetType\":\"WidgetProcessorCombobox.1\"},\ - {\"WidgetPosition\":[191,409,230,445],\"IsWidgetVisible\":-1,\"Caption\":\"F1\",\"EnableCaption\":0,\"Name\":\"F1.Operator1gui\",\"CaptionPosition\":1,\"ControlWidth\":39,\"ControlHeight\":36,\"Font\":0,\"ButtonOnly\":-1,\"PickerTitle\":\"Select Math Operator\",\"Value\":\"FFT\",\"WidgetIndex\":25,\"WidgetType\":\"WidgetProcessorCombobox.1\"},\ - {\"WidgetPosition\":[191,452,230,487],\"IsWidgetVisible\":-1,\"Caption\":\"F2\",\"EnableCaption\":0,\"Name\":\"F2.Operator1gui\",\"CaptionPosition\":1,\"ControlWidth\":39,\"ControlHeight\":36,\"Font\":0,\"ButtonOnly\":-1,\"PickerTitle\":\"Select Math Operator\",\"Value\":\"Zoom\",\"WidgetIndex\":26,\"WidgetType\":\"WidgetProcessorCombobox.1\"}\ - ]\ - }"; - - // Parse example data - JSONValue *value = JSON::parse(EXAMPLE3); - if (value) { - debug("-----------\r\n"); - debug(value->stringify().c_str()); - debug("\r\n"); - debug("-----------\r\n"); - debug(value->stringify(true).c_str()); - debug("\r\n"); - debug("-----------\r\n"); - } - - // Clean up - delete value; -} - -// Example 4 : List keys in an object. -void example3() { - // Parse the example. - JSONValue *main_object = JSON::parse(EXAMPLE); - if (main_object == NULL) { - debug("Example code failed to parse, did you change it?\r\n"); - } else if (!main_object->isObject()) { - debug("Example code is not an object, did you change it?\r\n"); - delete main_object; - } else { - // Print the main object. - debug("Main object:\r\n"); - debug(main_object->stringify(true).c_str()); - debug("-----------\r\n"); - - // Fetch the keys and print them out. - Common::Array<Common::String> keys = main_object->objectKeys(); - - Common::Array<Common::String>::iterator iter = keys.begin(); - while (iter != keys.end()) { - debug("Key: "); - debug((*iter).c_str()); - debug("\r\n"); - - // Get the key's value. - JSONValue *key_value = main_object->child((*iter).c_str()); - if (key_value) { - debug("Value: "); - debug(key_value->stringify().c_str()); - debug("\r\n"); - debug("-----------\r\n"); - } - - // Next key. - iter++; - } - - delete main_object; - } -} diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index 43ed9dcbee..f80854b987 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -27,14 +27,9 @@ namespace Cloud { namespace Dropbox { -class DropboxStorage: public Cloud::Storage { - bool _firstTime; - -protected: - virtual void handler(); - +class DropboxStorage: public Cloud::Storage { public: - DropboxStorage() : _firstTime(true) {}; + DropboxStorage() {}; virtual void listDirectory(Common::String path); virtual void syncSaves(); diff --git a/backends/cloud/dropbox/finalcountdownrequest.h b/backends/cloud/dropbox/finalcountdownrequest.h new file mode 100644 index 0000000000..34e8a807f9 --- /dev/null +++ b/backends/cloud/dropbox/finalcountdownrequest.h @@ -0,0 +1,52 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef BACKENDS_CLOUD_DROPBOX_FINALCOUNTDOWNREQUEST_H +#define BACKENDS_CLOUD_DROPBOX_FINALCOUNTDOWNREQUEST_H + +#include "backends/cloud/request.h" + +namespace Cloud { +namespace Dropbox { + +class FinalCountdownRequest : public Cloud::Request { + int _times; + +public: + FinalCountdownRequest(Callback cb) : Request(cb), _times(5) {}; + + virtual bool handle() { + if(--_times == 0) { + warning("It's the final countdown!"); + _callback(0); //meh, don't have anything for you, my caller + return true; + } + + warning("%d...", _times); + return false; + } +}; + +} +} //end of namespace Cloud::Dropbox + +#endif diff --git a/backends/cloud/request.h b/backends/cloud/request.h new file mode 100644 index 0000000000..ae85c234f2 --- /dev/null +++ b/backends/cloud/request.h @@ -0,0 +1,53 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef BACKENDS_CLOUD_REQUEST_H +#define BACKENDS_CLOUD_REQUEST_H + +namespace Cloud { + +class Request { +protected: + /** + * Callback, which should be called before Request returns true in handle(). + * That's the way Requests pass the result to the code which asked to create this request. + */ + + typedef void(*Callback)(void *result); + Callback _callback; + +public: + Request(Callback cb): _callback(cb) {}; + virtual ~Request() {}; + + /** + * Method, which does actual work. Depends on what this Request is doing. + * + * @return true if request's work is complete and it may be removed from Storage's list + */ + + virtual bool handle() = 0; +}; + +} //end of namespace Cloud + +#endif diff --git a/backends/cloud/storage.cpp b/backends/cloud/storage.cpp index ca6b9bf70e..597eb6bb13 100644 --- a/backends/cloud/storage.cpp +++ b/backends/cloud/storage.cpp @@ -31,19 +31,37 @@ static void cloudThread(void *thread) { cloudThread->handler(); } +Storage::Storage() : _timerStarted(false) {} + +void Storage::addRequest(Request *request) { + _requests.push_back(request); + if (!_timerStarted) startTimer(); +} + void Storage::handler() { - unsetTimeout(); + //TODO: lock mutex here (in case another handler() would be called before this one ends) + warning("handler's here"); + for (Common::Array<Request *>::iterator i = _requests.begin(); i != _requests.end();) { + if ((*i)->handle()) _requests.erase(i); + else ++i; + } + if (_requests.empty()) stopTimer(); + //TODO: unlock mutex here } -void Storage::setTimeout(int interval) { +void Storage::startTimer(int interval) { Common::TimerManager *manager = g_system->getTimerManager(); - if (!manager->installTimerProc(cloudThread, interval, this, "Cloud Thread")) - ; // warning("Failed to create cloud thread"); + if (manager->installTimerProc(cloudThread, interval, this, "Cloud Thread")) { + _timerStarted = true; + } else { + warning("Failed to create cloud thread"); + } } -void Storage::unsetTimeout() { +void Storage::stopTimer() { Common::TimerManager *manager = g_system->getTimerManager(); manager->removeTimerProc(cloudThread); + _timerStarted = false; } } //end of namespace Cloud diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index e71b8b7aa0..dcaa6af213 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -24,19 +24,25 @@ #define BACKENDS_CLOUD_STORAGE_H #include "common/str.h" +#include "common/array.h" +#include "backends/cloud/request.h" namespace Cloud { class Storage { friend void cloudThread(void *); //calls handler() + bool _timerStarted; protected: - virtual void handler(); - virtual void setTimeout(int interval); - virtual void unsetTimeout(); + Common::Array<Request *> _requests; + + virtual void addRequest(Request *request); //starts the timer if it's not started + virtual void handler(); + virtual void startTimer(int interval = 1000000); //1 second is the default interval + virtual void stopTimer(); public: - Storage() {}; + Storage(); virtual ~Storage() {}; /** |