diff options
author | Alexander Tkachev | 2016-06-16 15:19:13 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 5ac3adbd4fbf40c22fccab6ce8bf8dcab8f98eff (patch) | |
tree | 3b376dc1594ea7f4cddfaeedc6e7cacdc611021c | |
parent | 733d998e6a005af78b308817ea7326d1f5192069 (diff) | |
download | scummvm-rg350-5ac3adbd4fbf40c22fccab6ce8bf8dcab8f98eff.tar.gz scummvm-rg350-5ac3adbd4fbf40c22fccab6ce8bf8dcab8f98eff.tar.bz2 scummvm-rg350-5ac3adbd4fbf40c22fccab6ce8bf8dcab8f98eff.zip |
CLOUD: Add IndexPageHandler
This commit also adds LocalWebserver's stopOnIdle().
That means server is not stopped immediately, but only when all clients
are served.
-rw-r--r-- | backends/module.mk | 1 | ||||
-rw-r--r-- | backends/networking/sdl_net/indexpagehandler.cpp | 56 | ||||
-rw-r--r-- | backends/networking/sdl_net/indexpagehandler.h | 47 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.cpp | 23 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.h | 13 | ||||
-rw-r--r-- | gui/storagewizarddialog.cpp | 22 | ||||
-rw-r--r-- | gui/storagewizarddialog.h | 8 |
7 files changed, 164 insertions, 6 deletions
diff --git a/backends/module.mk b/backends/module.mk index 0f1eea0090..ecdca2e0a7 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -62,6 +62,7 @@ ifdef USE_SDL_NET MODULE_OBJS += \ networking/sdl_net/client.o \ networking/sdl_net/getclienthandler.o \ + networking/sdl_net/indexpagehandler.o \ networking/sdl_net/localwebserver.o endif diff --git a/backends/networking/sdl_net/indexpagehandler.cpp b/backends/networking/sdl_net/indexpagehandler.cpp new file mode 100644 index 0000000000..58c4761f59 --- /dev/null +++ b/backends/networking/sdl_net/indexpagehandler.cpp @@ -0,0 +1,56 @@ +/* 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. +* +*/ + +#include "backends/networking/sdl_net/indexpagehandler.h" +#include "backends/networking/sdl_net/localwebserver.h" +#include "gui/storagewizarddialog.h" + +namespace Networking { + +IndexPageHandler::IndexPageHandler(): CommandSender(nullptr) {} + +IndexPageHandler::~IndexPageHandler() { + LocalServer.removePathHandler("/"); +} + +void IndexPageHandler::handle(Client &client) { + Common::String code = client.queryParameter("code"); + + if (code == "") { + LocalWebserver::setClientGetHandler(client, "<html><head><title>ScummVM</title></head><body>This is a local webserver index page.</body></html>"); + return; + } + + _code = code; + sendCommand(GUI::kStorageCodePassedCmd, 0); + LocalWebserver::setClientGetHandler(client, "<html><head><title>ScummVM</title></head><body>ScummVM got the code and already connects to your cloud storage!</body></html>"); +} + +void IndexPageHandler::addPathHandler(LocalWebserver &server) { + // we can't use LocalServer yet, because IndexPageHandler is created while LocalWebserver is created + // (thus no _instance is available and it causes stack overflow) + server.addPathHandler("/", new Common::Callback<IndexPageHandler, Client &>(this, &IndexPageHandler::handle)); +} + +Common::String IndexPageHandler::code() { return _code; } + +} // End of namespace Networking diff --git a/backends/networking/sdl_net/indexpagehandler.h b/backends/networking/sdl_net/indexpagehandler.h new file mode 100644 index 0000000000..5a1d2d7cbc --- /dev/null +++ b/backends/networking/sdl_net/indexpagehandler.h @@ -0,0 +1,47 @@ +/* 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_NETWORKING_SDL_NET_INDEXPAGEHANDLER_H +#define BACKENDS_NETWORKING_SDL_NET_INDEXPAGEHANDLER_H + +#include "backends/networking/sdl_net/client.h" +#include "gui/object.h" + +namespace Networking { +class LocalWebserver; + +class IndexPageHandler: public GUI::CommandSender { + Common::String _code; + + void handle(Client &client); + +public: + IndexPageHandler(); + virtual ~IndexPageHandler(); + + void addPathHandler(LocalWebserver &server); + Common::String code(); +}; + +} // End of namespace Networking + +#endif diff --git a/backends/networking/sdl_net/localwebserver.cpp b/backends/networking/sdl_net/localwebserver.cpp index 689bab4cb9..971f00af2a 100644 --- a/backends/networking/sdl_net/localwebserver.cpp +++ b/backends/networking/sdl_net/localwebserver.cpp @@ -40,7 +40,9 @@ DECLARE_SINGLETON(Networking::LocalWebserver); namespace Networking { -LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), _clients(0) {} +LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), _stopOnIdle(false), _clients(0) { + _indexPageHandler.addPathHandler(*this); +} LocalWebserver::~LocalWebserver() { stop(); @@ -110,11 +112,20 @@ void LocalWebserver::stop() { } } +void LocalWebserver::stopOnIdle() { _stopOnIdle = true; } + void LocalWebserver::addPathHandler(Common::String path, ClientHandler handler) { if (_pathHandlers.contains(path)) warning("LocalWebserver::addPathHandler: path already had a handler"); _pathHandlers[path] = handler; } +void LocalWebserver::removePathHandler(Common::String path) { + if (!_pathHandlers.contains(path)) warning("LocalWebserver::removePathHandler: no handler known for this path"); + _pathHandlers.erase(path); +} + +IndexPageHandler &LocalWebserver::indexPageHandler() { return _indexPageHandler; } + void LocalWebserver::handle() { int numready = SDLNet_CheckSockets(_set, 0); if (numready == -1) { @@ -123,6 +134,16 @@ void LocalWebserver::handle() { for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) handleClient(i); + + if (_stopOnIdle) { + bool idle = true; + for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) + if (_client[i].state() != INVALID) { + idle = false; + break; + } + if (idle) stop(); + } } void LocalWebserver::handleClient(uint32 i) { diff --git a/backends/networking/sdl_net/localwebserver.h b/backends/networking/sdl_net/localwebserver.h index bda0bfd7c1..2ad83f7b7a 100644 --- a/backends/networking/sdl_net/localwebserver.h +++ b/backends/networking/sdl_net/localwebserver.h @@ -24,6 +24,7 @@ #define BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H #include "backends/networking/sdl_net/client.h" +#include "backends/networking/sdl_net/indexpagehandler.h" #include "common/callback.h" #include "common/hash-str.h" #include "common/singleton.h" @@ -48,23 +49,29 @@ class LocalWebserver : public Common::Singleton<LocalWebserver> { TCPsocket _serverSocket; Client _client[MAX_CONNECTIONS]; int _clients; - bool _timerStarted; + bool _timerStarted, _stopOnIdle; Common::HashMap<Common::String, ClientHandler> _pathHandlers; + IndexPageHandler _indexPageHandler; void startTimer(int interval = TIMER_INTERVAL); void stopTimer(); void handle(); void handleClient(uint32 i); void acceptClient(); - void setClientGetHandler(Client &client, Common::String response, long code = 200); - + public: LocalWebserver(); virtual ~LocalWebserver(); void start(); void stop(); + void stopOnIdle(); void addPathHandler(Common::String path, ClientHandler handler); + void removePathHandler(Common::String path); + + IndexPageHandler &indexPageHandler(); + + static void setClientGetHandler(Client &client, Common::String response, long code = 200); }; /** Shortcut for accessing the local webserver. */ diff --git a/gui/storagewizarddialog.cpp b/gui/storagewizarddialog.cpp index 58ca095148..c2759f2ae6 100644 --- a/gui/storagewizarddialog.cpp +++ b/gui/storagewizarddialog.cpp @@ -38,7 +38,8 @@ enum { kCodeBoxCmd = 'CdBx' }; -StorageWizardDialog::StorageWizardDialog(uint32 storageId): Dialog("GlobalOptions_Cloud_ConnectionWizard"), _storageId(storageId) { +StorageWizardDialog::StorageWizardDialog(uint32 storageId): + Dialog("GlobalOptions_Cloud_ConnectionWizard"), _storageId(storageId), _close(false) { _backgroundType = GUI::ThemeEngine::kDialogBackgroundPlain; Common::String headline = Common::String::format(_("%s Storage Connection Wizard"), CloudMan.listStorages()[_storageId].c_str()); @@ -70,12 +71,14 @@ void StorageWizardDialog::open() { Dialog::open(); #ifdef USE_SDL_NET LocalServer.start(); + LocalServer.indexPageHandler().setTarget(this); #endif } void StorageWizardDialog::close() { #ifdef USE_SDL_NET - LocalServer.stop(); + LocalServer.stopOnIdle(); + LocalServer.indexPageHandler().setTarget(nullptr); #endif Dialog::close(); } @@ -143,11 +146,26 @@ void StorageWizardDialog::handleCommand(CommandSender *sender, uint32 cmd, uint3 close(); break; } +#ifdef USE_SDL_NET + case kStorageCodePassedCmd: + CloudMan.connectStorage(_storageId, LocalServer.indexPageHandler().code()); + _close = true; + break; +#endif default: Dialog::handleCommand(sender, cmd, data); } } +void StorageWizardDialog::handleTickle() { + if (_close) { + setResult(1); + close(); + } + + Dialog::handleTickle(); +} + int StorageWizardDialog::decodeHashchar(char c) { const char HASHCHARS[65] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ?!"; for (uint32 i = 0; i < 64; ++i) diff --git a/gui/storagewizarddialog.h b/gui/storagewizarddialog.h index fa45f922d9..93df56d300 100644 --- a/gui/storagewizarddialog.h +++ b/gui/storagewizarddialog.h @@ -33,12 +33,19 @@ class EditTextWidget; class StaticTextWidget; class ButtonWidget; +#ifdef USE_SDL_NET +enum StorageWizardDialogCommands { + kStorageCodePassedCmd = 'SWDC' +}; +#endif + class StorageWizardDialog : public Dialog { static const uint32 CODE_FIELDS = 8; uint32 _storageId; EditTextWidget *_codeWidget[CODE_FIELDS]; StaticTextWidget *_messageWidget; ButtonWidget *_connectWidget; + bool _close; /** * Return the value corresponding to the given character. @@ -69,6 +76,7 @@ public: virtual void open(); virtual void close(); virtual void handleCommand(CommandSender *sender, uint32 cmd, uint32 data); + virtual void handleTickle(); }; } // End of namespace GUI |