diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 9298cde0a13..2db915de382 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -22,15 +22,23 @@ #define FORBIDDEN_SYMBOL_ALLOW_ALL #include "backends/cloud/dropbox/dropboxstorage.h" -#include "backends/cloud/dropbox/curlrequest.h" +#include "backends/networking/curl/curljsonrequest.h" #include "common/debug.h" +#include "common/json.h" #include namespace Cloud { namespace Dropbox { -static void curlCallback(void *ptr) { - debug("--- curl request is complete ---"); +static void curlJsonCallback(void *ptr) { + Common::JSONValue *json = (Common::JSONValue *)ptr; + if (json) { + debug("curlJsonCallback:"); + debug("%s", json->stringify(true).c_str()); + delete json; + } else { + debug("curlJsonCallback: got NULL instead of JSON!"); + } } DropboxStorage::DropboxStorage() { @@ -45,9 +53,9 @@ void DropboxStorage::listDirectory(Common::String path) { startTimer(1000000); //in one second } -void DropboxStorage::syncSaves() { - addRequest(new CurlRequest(curlCallback, "tkachov.ru")); - addRequest(new CurlRequest(curlCallback, "scummvm.org")); +void DropboxStorage::syncSaves() { + //not so Dropbox, just testing JSON requesting & parsing: + addRequest(new Networking::CurlJsonRequest(curlJsonCallback, "https://api.vk.com/method/users.get?v=5.50&user_ids=205387401")); } } //end of namespace Dropbox diff --git a/backends/module.mk b/backends/module.mk index 0be8ef90a5b..72183f9563e 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -23,14 +23,14 @@ ifdef USE_CLOUD MODULE_OBJS += \ cloud/manager.o \ cloud/storage.o \ - cloud/dropbox/dropboxstorage.o \ - cloud/dropbox/curlrequest.o + cloud/dropbox/dropboxstorage.o endif ifdef USE_LIBCURL MODULE_OBJS += \ networking/curl/connectionmanager.o \ - networking/curl/networkreadstream.o + networking/curl/networkreadstream.o \ + networking/curl/curljsonrequest.o endif ifdef USE_ELF_LOADER diff --git a/backends/networking/curl/connectionmanager.cpp b/backends/networking/curl/connectionmanager.cpp index b7b7112c7f1..ceff8b62a43 100644 --- a/backends/networking/curl/connectionmanager.cpp +++ b/backends/networking/curl/connectionmanager.cpp @@ -46,25 +46,25 @@ NetworkReadStream *ConnectionManager::makeRequest(const char *url) { } void ConnectionManager::handle() { - int U; - curl_multi_perform(_multi, &U); + int transfersRunning; + curl_multi_perform(_multi, &transfersRunning); - int Q; + int messagesInQueue; CURLMsg *curlMsg; - while ((curlMsg = curl_multi_info_read(_multi, &Q))) { + while ((curlMsg = curl_multi_info_read(_multi, &messagesInQueue))) { + CURL *easyHandle = curlMsg->easy_handle; + + NetworkReadStream *stream; + curl_easy_getinfo(easyHandle, CURLINFO_PRIVATE, &stream); + if (stream) stream->done(); //I'm not sure it's OK to notify "done()" on failure + if (curlMsg->msg == CURLMSG_DONE) { - CURL *e = curlMsg->easy_handle; - - NetworkReadStream *stream; - curl_easy_getinfo(e, CURLINFO_PRIVATE, &stream); - if (stream) stream->done(); - - debug("ConnectionManager: SUCCESS (%d - %s)", curlMsg->data.result, curl_easy_strerror(curlMsg->data.result)); - curl_multi_remove_handle(_multi, e); + debug("ConnectionManager: SUCCESS (%d - %s)", curlMsg->data.result, curl_easy_strerror(curlMsg->data.result)); } else { - debug("ConnectionManager: FAILURE (CURLMsg (%d))", curlMsg->msg); - //TODO: notify stream on this case also + debug("ConnectionManager: FAILURE (CURLMsg (%d))", curlMsg->msg); } + + curl_multi_remove_handle(_multi, easyHandle); } } diff --git a/backends/cloud/dropbox/curlrequest.cpp b/backends/networking/curl/curljsonrequest.cpp similarity index 57% rename from backends/cloud/dropbox/curlrequest.cpp rename to backends/networking/curl/curljsonrequest.cpp index ecc868cb511..acd9675aed4 100644 --- a/backends/cloud/dropbox/curlrequest.cpp +++ b/backends/networking/curl/curljsonrequest.cpp @@ -22,37 +22,38 @@ #define FORBIDDEN_SYMBOL_ALLOW_ALL -#include "backends/cloud/dropbox/curlrequest.h" +#include "backends/networking/curl/curljsonrequest.h" #include "backends/networking/curl/networkreadstream.h" #include "common/debug.h" +#include "common/json.h" #include -namespace Cloud { -namespace Dropbox { +namespace Networking { -CurlRequest::CurlRequest(Callback cb, const char *url) : Request(cb), _firstTime(true), _stream(0) { +CurlJsonRequest::CurlJsonRequest(Callback cb, const char *url) : Request(cb), _stream(0) { _url = url; } -CurlRequest::~CurlRequest() { +CurlJsonRequest::~CurlJsonRequest() { if (_stream) delete _stream; } -bool CurlRequest::handle(Networking::ConnectionManager &manager) { - if (_firstTime) { - _stream = manager.makeRequest(_url); - _firstTime = false; - } +bool CurlJsonRequest::handle(ConnectionManager &manager) { + if (!_stream) _stream = manager.makeRequest(_url); if (_stream) { - const int kBufSize = 10000; + const int kBufSize = 16*1024; char buf[kBufSize+1]; uint32 readBytes = _stream->read(buf, kBufSize); - debug("%d", readBytes); - //if(readBytes != 0) debug("%s", buf); - - if(_stream->eos()) { - _callback(0); + if (readBytes != 0) _contents += Common::String(buf, readBytes); + if (_stream->eos()) { + //TODO: check that stream's CURL easy handle's HTTP response code is 200 OK + debug("CurlJsonRequest: contents:\n%s", _contents.c_str()); + if (_callback) { + Common::JSONValue *json = Common::JSON::parse(_contents.c_str()); //TODO: somehow fix JSON to work with UTF-8 + debug("CurlJsonRequest: JSONValue pointer = %p", json); + _callback(json); //potential memory leak, free it in your callbacks! + } return true; } } @@ -60,5 +61,4 @@ bool CurlRequest::handle(Networking::ConnectionManager &manager) { return false; } -} //end of namespace Dropbox -} //end of namespace Cloud +} //end of namespace Networking diff --git a/backends/cloud/dropbox/curlrequest.h b/backends/networking/curl/curljsonrequest.h similarity index 71% rename from backends/cloud/dropbox/curlrequest.h rename to backends/networking/curl/curljsonrequest.h index 3d5d4adb72c..73e0144c64c 100644 --- a/backends/cloud/dropbox/curlrequest.h +++ b/backends/networking/curl/curljsonrequest.h @@ -20,31 +20,27 @@ * */ -#ifndef BACKENDS_CLOUD_DROPBOX_CURLREQUEST_H -#define BACKENDS_CLOUD_DROPBOX_CURLREQUEST_H +#ifndef BACKENDS_NETWORKING_CURL_CURLJSONREQUEST_H +#define BACKENDS_NETWORKING_CURL_CURLJSONREQUEST_H #include "backends/cloud/request.h" namespace Networking { + class NetworkReadStream; -} -namespace Cloud { -namespace Dropbox { - -class CurlRequest : public Cloud::Request { - bool _firstTime; +class CurlJsonRequest : public Cloud::Request { const char *_url; - Networking::NetworkReadStream *_stream; + NetworkReadStream *_stream; + Common::String _contents; public: - CurlRequest(Callback cb, const char *url); - virtual ~CurlRequest(); + CurlJsonRequest(Callback cb, const char *url); + virtual ~CurlJsonRequest(); - virtual bool handle(Networking::ConnectionManager &manager); + virtual bool handle(ConnectionManager &manager); }; -} //end of namespace Dropbox -} //end of namespace Cloud +} //end of namespace Networking #endif diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp index f07f568828f..acb4d999c32 100644 --- a/backends/platform/sdl/sdl.cpp +++ b/backends/platform/sdl/sdl.cpp @@ -160,8 +160,8 @@ void OSystem_SDL::init() { #endif #if defined(USE_CLOUD) - if (_cloudThread == 0) - _cloudThread = new Cloud::Manager(); + if (_cloudManager == 0) + _cloudManager = new Cloud::Manager(); #endif } diff --git a/common/system.h b/common/system.h index a3092e794ff..815fa9d19df 100644 --- a/common/system.h +++ b/common/system.h @@ -185,7 +185,7 @@ protected: * * @note _cloudThread is deleted by the OSystem destructor. */ - Common::CloudManager *_cloudThread; + Common::CloudManager *_cloudManager; #endif /** @@ -1134,7 +1134,7 @@ public: * @return the CloudManager for the current architecture */ virtual Common::CloudManager *getCloudManager() { - return _cloudThread; + return _cloudManager; } #endif