CLOUD: Add IndexPageHandler

This commit also adds LocalWebserver's stopOnIdle().
That means server is not stopped immediately, but only when all clients
are served.
This commit is contained in:
Alexander Tkachev 2016-06-16 15:19:13 +06:00
parent 733d998e6a
commit 5ac3adbd4f
7 changed files with 164 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -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,15 +49,15 @@ 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();
@ -64,7 +65,13 @@ public:
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. */

View File

@ -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)

View File

@ -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