Properly implemented NetworkHttp using a interface and

a singleton-like pattern.


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@11616 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2012-09-20 05:51:19 +00:00
parent d169fdfacc
commit d126d7c76f
11 changed files with 101 additions and 72 deletions

View File

@ -2,6 +2,7 @@
set(STK_SOURCES
src/addons/addon.cpp
src/addons/addons_manager.cpp
src/addons/inetwork_http.cpp
src/addons/network_http.cpp
src/addons/news_manager.cpp
src/addons/request.cpp

View File

@ -301,7 +301,7 @@ void AddonsManager::downloadIcons()
continue;
}
std::string save = "icons/"+icon;
Request *r = network_http->downloadFileAsynchron(url, save,
Request *r = INetworkHttp::get()->downloadFileAsynchron(url, save,
/*priority*/1,
/*manage_mem*/true);
if (r != NULL)

View File

@ -21,6 +21,7 @@
#include "addons/request.hpp"
#include "addons/inetwork_http.hpp"
class XMLNode;
@ -28,7 +29,7 @@ class XMLNode;
* \ingroup addonsgroup
* Dummy implementation used when curl is not available
*/
class DummyNetworkHttp
class DummyNetworkHttp : public INetworkHttp
{
public:

View File

@ -0,0 +1,48 @@
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2012 Joerg Henrichs
//
// 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 3
// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "addons/dummy_network_http.hpp"
#include "addons/inetwork_http.hpp"
#include "addons/network_http.hpp"
#include <assert.h>
INetworkHttp *INetworkHttp::m_network_http = NULL;
/** Creates the network_http instance (depending on compile time options).
*/
void INetworkHttp::create()
{
assert(m_network_http == NULL);
#ifdef NO_CURL
m_network_http = new DummyNetworkHttp();
#else
m_network_http = new NetworkHttp();
#endif
} // create
// ----------------------------------------------------------------------------
/** Destroys the network_http instance.
*/
void INetworkHttp::destroy()
{
if(m_network_http)
{
delete m_network_http;
m_network_http = NULL;
}
} // destroy

View File

@ -30,6 +30,11 @@ class XMLNode;
*/
class INetworkHttp
{
private:
/** The one instance of this object. */
static INetworkHttp *m_network_http;
public:
/** If stk has permission to access the internet (for news
* server etc).
@ -51,9 +56,12 @@ public:
int priority = 1,
bool manage_memory=true) = 0;
virtual void cancelAllDownloads() = 0;
static void create();
static INetworkHttp *get() { return m_network_http; }
static void destroy();
}; // NetworkHttp
extern INetworkHttp *network_http;
#endif

View File

@ -42,16 +42,6 @@ class XMLNode;
*/
class NetworkHttp : public INetworkHttp
{
public:
/** If stk has permission to access the internet (for news
* server etc).
* IPERM_NOT_ASKED: The user needs to be asked if he wants to
* grant permission
* IPERM_ALLOWED: STK is allowed to access server.
* IPERM_NOT_ALLOWED: STK must not access external servers. */
enum InternetPermission {IPERM_NOT_ASKED =0,
IPERM_ALLOWED =1,
IPERM_NOT_ALLOWED=2 };
private:
/** The list of pointes to all requests. */

View File

@ -141,8 +141,7 @@
#include "main_loop.hpp"
#include "addons/addons_manager.hpp"
#include "addons/dummy_network_http.hpp"
#include "addons/network_http.hpp"
#include "addons/inetwork_http.hpp"
#include "addons/news_manager.hpp"
#include "audio/music_manager.hpp"
#include "audio/sfx_manager.hpp"
@ -1101,15 +1100,12 @@ void initRest()
news_manager = new NewsManager();
addons_manager = new AddonsManager();
#ifdef NO_CURL
network_http = new DummyNetworkHttp();
#else
network_http = new NetworkHttp();
#endif
INetworkHttp::create();
// Note that the network thread must be started after the assignment
// to network_http (since the thread might use network_http, otherwise
// a race condition can be introduced resulting in a crash).
network_http->startNetworkThread();
INetworkHttp::get()->startNetworkThread();
music_manager = new MusicManager();
sfx_manager = new SFXManager();
// The order here can be important, e.g. KartPropertiesManager needs
@ -1160,14 +1156,14 @@ void cleanSuperTuxKart()
{
irr_driver->updateConfigIfRelevant();
if(network_http)
network_http->stopNetworkThread();
if(INetworkHttp::get())
INetworkHttp::get()->stopNetworkThread();
//delete in reverse order of what they were created in.
//see InitTuxkart()
Referee::cleanup();
if(ReplayPlay::get()) ReplayPlay::destroy();
if(race_manager) delete race_manager;
if(network_http) delete network_http;
INetworkHttp::destroy();
if(news_manager) delete news_manager;
if(addons_manager) delete addons_manager;
if(network_manager) delete network_manager;
@ -1305,7 +1301,7 @@ int main(int argc, char *argv[] )
{
StateManager::get()->pushScreen(StoryModeLobbyScreen::getInstance());
if(UserConfigParams::m_internet_status ==
NetworkHttp::IPERM_NOT_ASKED)
INetworkHttp::IPERM_NOT_ASKED)
{
class ConfirmServer :
public MessageDialog::IConfirmDialogListener
@ -1313,27 +1309,27 @@ int main(int argc, char *argv[] )
public:
virtual void onConfirm()
{
delete network_http;
INetworkHttp::destroy();
UserConfigParams::m_internet_status =
NetworkHttp::IPERM_ALLOWED;
INetworkHttp::IPERM_ALLOWED;
GUIEngine::ModalDialog::dismiss();
network_http = new NetworkHttp();
INetworkHttp::create();
// Note that the network thread must be started after
// the assignment to network_http (since the thread
// might use network_http, otherwise a race condition
// can be introduced resulting in a crash).
network_http->startNetworkThread();
INetworkHttp::get()->startNetworkThread();
} // onConfirm
// --------------------------------------------------------
virtual void onCancel()
{
delete network_http;
INetworkHttp::destroy();
UserConfigParams::m_internet_status =
NetworkHttp::IPERM_NOT_ALLOWED;
INetworkHttp::IPERM_NOT_ALLOWED;
GUIEngine::ModalDialog::dismiss();
network_http = new NetworkHttp();
network_http->startNetworkThread();
INetworkHttp::create();
INetworkHttp::get()->startNetworkThread();
} // onCancel
}; // ConfirmServer

View File

@ -20,7 +20,7 @@
#include <iostream>
#include "addons/addons_manager.hpp"
#include "addons/network_http.hpp"
#include "addons/inetwork_http.hpp"
#include "guiengine/CGUISpriteBank.h"
#include "guiengine/modaldialog.hpp"
#include "guiengine/scalable_font.hpp"
@ -306,7 +306,7 @@ void AddonsScreen::eventCallback(GUIEngine::Widget* widget,
if (!m_reloading)
{
m_reloading = true;
network_http->insertReInit();
INetworkHttp::get()->insertReInit();
GUIEngine::ListWidget* w_list =
getWidget<GUIEngine::ListWidget>("list_addons");
@ -376,7 +376,7 @@ void AddonsScreen::onUpdate(float dt, irr::video::IVideoDriver*)
{
if (m_reloading)
{
if(UserConfigParams::m_internet_status!=NetworkHttp::IPERM_ALLOWED)
if(UserConfigParams::m_internet_status!=INetworkHttp::IPERM_ALLOWED)
{
// not allowed to access the net. how did you get to this menu in
// the first place??

View File

@ -21,7 +21,7 @@
#include <pthread.h>
#include "addons/addons_manager.hpp"
#include "addons/network_http.hpp"
#include "addons/inetwork_http.hpp"
#include "addons/request.hpp"
#include "config/user_config.hpp"
#include "guiengine/engine.hpp"
@ -281,7 +281,7 @@ void AddonsLoading::startDownload()
std::string file = m_addon.getZipFileName();
std::string save = "tmp/"
+ StringUtils::getBasename(m_addon.getZipFileName());
m_download_request = network_http->downloadFileAsynchron(file, save,
m_download_request = INetworkHttp::get()->downloadFileAsynchron(file, save,
/*priority*/5,
/*manage memory*/false);
} // startDownload

View File

@ -21,7 +21,7 @@
#include <string>
#include "addons/network_http.hpp"
#include "addons/inetwork_http.hpp"
#include "challenges/game_slot.hpp"
#include "challenges/unlock_manager.hpp"
#include "graphics/irr_driver.hpp"
@ -102,7 +102,7 @@ void MainMenuScreen::init()
// the key bindings for the first player the default again.
input_manager->getDeviceList()->clearLatestUsedDevice();
if (UserConfigParams::m_internet_status!=NetworkHttp::IPERM_ALLOWED)
if (UserConfigParams::m_internet_status!=INetworkHttp::IPERM_ALLOWED)
{
IconButtonWidget* w = getWidget<IconButtonWidget>("addons");
w->setDeactivated();
@ -140,7 +140,7 @@ void MainMenuScreen::onUpdate(float delta, irr::video::IVideoDriver* driver)
IconButtonWidget* addons_icon = getWidget<IconButtonWidget>("addons");
if (addons_icon != NULL)
{
if(UserConfigParams::m_internet_status!=NetworkHttp::IPERM_ALLOWED )
if(UserConfigParams::m_internet_status!=INetworkHttp::IPERM_ALLOWED )
{
addons_icon->setDeactivated();
addons_icon->resetAllBadges();
@ -189,7 +189,7 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name,
std::string selection =
ribbon->getSelectionIDString(PLAYER_ID_GAME_MASTER);
/*
if (selection == "story")
{
StateManager::get()->enterGameState();
@ -206,7 +206,6 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name,
//race_manager->startSingleRace("introcutscene2", 999, false);
return;
}
*/
#if DEBUG_MENU_ITEM
if (selection == "options")
@ -300,7 +299,6 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name,
}
else if (selection == "about")
{
CreditsScreen::getInstance()->setVictoryMusic(false);
StateManager::get()->pushScreen(CreditsScreen::getInstance());
}
else if (selection == "help")
@ -312,25 +310,12 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name,
GameSlot* slot = unlock_manager->getCurrentSlot();
if (slot->isFirstTime())
{
/*
slot->setFirstTime(false);
unlock_manager->save();
KartSelectionScreen* s = KartSelectionScreen::getInstance();
s->setMultiplayer(false);
s->setFromOverworld(true);
StateManager::get()->pushScreen( s );
*/
StateManager::get()->enterGameState();
race_manager->setMinorMode(RaceManager::MINOR_MODE_CUTSCENE);
race_manager->setNumKarts( 0 );
race_manager->setNumPlayers(0);
race_manager->setNumLocalPlayers(0);
race_manager->startSingleRace("introcutscene", 999, false);
std::vector<std::string> parts;
parts.push_back("introcutscene");
parts.push_back("introcutscene2");
((CutsceneWorld*)World::getWorld())->setParts(parts);
}
else
{
@ -359,7 +344,7 @@ void MainMenuScreen::onDisabledItemClicked(const std::string& item)
{
if (item == "addons")
{
if (UserConfigParams::m_internet_status != NetworkHttp::IPERM_ALLOWED)
if (UserConfigParams::m_internet_status != INetworkHttp::IPERM_ALLOWED)
{
new MessageDialog( _("The add-ons module is currently disabled in "
"the Options screen") );

View File

@ -17,7 +17,7 @@
#include "states_screens/options_screen_ui.hpp"
#include "addons/network_http.hpp"
#include "addons/inetwork_http.hpp"
#include "audio/music_manager.hpp"
#include "audio/sfx_manager.hpp"
#include "audio/sfx_base.hpp"
@ -125,7 +125,7 @@ void OptionsScreenUI::init()
CheckBoxWidget* news = getWidget<CheckBoxWidget>("enable-internet");
assert( news != NULL );
news->setState( UserConfigParams::m_internet_status
==NetworkHttp::IPERM_ALLOWED );
==INetworkHttp::IPERM_ALLOWED );
CheckBoxWidget* min_gui = getWidget<CheckBoxWidget>("minimal-racegui");
assert( min_gui != NULL );
min_gui->setState( UserConfigParams::m_minimal_race_gui);
@ -223,19 +223,19 @@ void OptionsScreenUI::eventCallback(Widget* widget, const std::string& name, con
{
CheckBoxWidget* news = getWidget<CheckBoxWidget>("enable-internet");
assert( news != NULL );
if(network_http)
if(INetworkHttp::get())
{
network_http->stopNetworkThread();
delete network_http;
INetworkHttp::get()->stopNetworkThread();
INetworkHttp::destroy();
}
UserConfigParams::m_internet_status =
news->getState() ? NetworkHttp::IPERM_ALLOWED
: NetworkHttp::IPERM_NOT_ALLOWED;
network_http = new NetworkHttp();
news->getState() ? INetworkHttp::IPERM_ALLOWED
: INetworkHttp::IPERM_NOT_ALLOWED;
INetworkHttp::create();
// Note that the network thread must be started after the assignment
// to network_http (since the thread might use network_http, otherwise
// a race condition can be introduced resulting in a crash).
network_http->startNetworkThread();
INetworkHttp::get()->startNetworkThread();
}
else if (name=="minimal-racegui")
{