From 6079e6de2b9bf4b5cc0e81a9be2de2dbaa165cc8 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 26 Mar 2011 05:03:38 +0000 Subject: [PATCH] - better handling of mod menu threading when user has no internet connection --- source/glest_game/menu/menu_state_mods.cpp | 94 +++++++++++++++++++--- source/glest_game/menu/menu_state_mods.h | 1 + source/shared_lib/include/util/util.h | 2 +- source/shared_lib/sources/util/util.cpp | 7 +- 4 files changed, 92 insertions(+), 12 deletions(-) diff --git a/source/glest_game/menu/menu_state_mods.cpp b/source/glest_game/menu/menu_state_mods.cpp index 41c829a7..5fb405eb 100644 --- a/source/glest_game/menu/menu_state_mods.cpp +++ b/source/glest_game/menu/menu_state_mods.cpp @@ -139,11 +139,14 @@ MenuStateMods::MenuStateMods(Program *program, MainMenu *mainMenu) : GraphicComponent::applyAllCustomProperties(containerName); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); // Start http meta data thread modHttpServerThread = new SimpleTaskThread(this,0,200); modHttpServerThread->setUniqueID(__FILE__); modHttpServerThread->start(); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + // Setup File Transfer thread findDirs(config.getPathListForType(ptTilesets), tilesetFiles); findDirs(config.getPathListForType(ptTechs), techTreeFiles); @@ -178,22 +181,31 @@ MenuStateMods::MenuStateMods(Program *program, MainMenu *mainMenu) : string fileArchiveExtractCommand = config.getString("FileArchiveExtractCommand",""); string fileArchiveExtractCommandParameters = config.getString("FileArchiveExtractCommandParameters",""); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + ftpClientThread = new FTPClientThread(-1,"", mapsPath,tilesetsPath,techtreesPath, this,fileArchiveExtension,fileArchiveExtractCommand, fileArchiveExtractCommandParameters); ftpClientThread->start(); + + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); } void MenuStateMods::simpleTask(BaseThread *callingThread) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); MutexSafeWrapper safeMutexThreadOwner(callingThread->getMutexThreadOwnerValid(),string(__FILE__) + "_" + intToStr(__LINE__)); if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); return; } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + Lang &lang= Lang::getInstance(); Config &config = Config::getInstance(); string fileArchiveExtractCommand = config.getString("FileArchiveExtractCommand",""); @@ -208,23 +220,49 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { std::string tilesetsMetaData = ""; std::string mapsMetaData = ""; + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(config.getString("Masterserver","") != "") { string baseURL = config.getString("Masterserver"); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] About to call first http url..\n",__FILE__,__FUNCTION__,__LINE__); + CURL *handle = SystemFlags::initHTTP(); - techsMetaData = SystemFlags::getHTTP(baseURL + "showTechsForGlest.php",handle); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("techsMetaData [%s]\n",techsMetaData.c_str()); - tilesetsMetaData = SystemFlags::getHTTP(baseURL + "showTilesetsForGlest.php",handle); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("tilesetsMetaData [%s]\n",tilesetsMetaData.c_str()); - mapsMetaData = SystemFlags::getHTTP(baseURL + "showMapsForGlest.php",handle); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("mapsMetaData [%s]\n",mapsMetaData.c_str()); + CURLcode curlResult = CURLE_OK; + techsMetaData = SystemFlags::getHTTP(baseURL + "showTechsForGlest.php",handle,-1,&curlResult); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("techsMetaData [%s] curlResult = %d\n",techsMetaData.c_str(),curlResult); + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + + if(curlResult == CURLE_OK || + (curlResult != CURLE_COULDNT_RESOLVE_HOST && + curlResult != CURLE_COULDNT_CONNECT)) { + + tilesetsMetaData = SystemFlags::getHTTP(baseURL + "showTilesetsForGlest.php",handle); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("tilesetsMetaData [%s]\n",tilesetsMetaData.c_str()); + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + + mapsMetaData = SystemFlags::getHTTP(baseURL + "showMapsForGlest.php",handle); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("mapsMetaData [%s]\n",mapsMetaData.c_str()); + } SystemFlags::cleanupHTTP(&handle); } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); return; } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); tilesetListRemote.clear(); Tokenize(tilesetsMetaData,tilesetListRemote,"\n"); @@ -268,10 +306,15 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { } } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); return; } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + techListRemote.clear(); Tokenize(techsMetaData,techListRemote,"\n"); @@ -332,10 +375,15 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { } } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); return; } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + mapListRemote.clear(); Tokenize(mapsMetaData,mapListRemote,"\n"); @@ -394,9 +442,11 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { } if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); return; } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); int listBoxLength = 400; @@ -418,7 +468,13 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { keyMapScrollBar.setVisibleSize(keyButtonsToRender); keyMapScrollBar.setVisibleStart(0); - modHttpServerThread->signalQuit(); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + + if(modHttpServerThread != NULL) { + modHttpServerThread->signalQuit(); + } + + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); } MapInfo MenuStateMods::loadMapInfo(string file) { @@ -564,30 +620,47 @@ void MenuStateMods::refreshMaps() { } } -MenuStateMods::~MenuStateMods() { +void MenuStateMods::cleanUp() { clearUserButtons(); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); if(modHttpServerThread != NULL) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - modHttpServerThread->setThreadOwnerValid(false); + modHttpServerThread->signalQuit(); + //modHttpServerThread->setThreadOwnerValid(false); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - if( modHttpServerThread->shutdownAndWait() == true) { + if( modHttpServerThread->canShutdown(true) == true && + modHttpServerThread->shutdownAndWait() == true) { delete modHttpServerThread; } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); modHttpServerThread = NULL; } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(ftpClientThread != NULL) { ftpClientThread->setCallBackObject(NULL); if(ftpClientThread->shutdownAndWait() == true) { delete ftpClientThread; ftpClientThread = NULL; } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); } } +MenuStateMods::~MenuStateMods() { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + + cleanUp(); + + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); +} + void MenuStateMods::clearUserButtons() { // Techs while(!keyTechButtons.empty()) { @@ -635,6 +708,7 @@ void MenuStateMods::mouseClick(int x, int y, MouseButton mouseButton) { showMessageBox(szBuf, lang.get("Question"), true); } else { + cleanUp(); mainMenu->setState(new MenuStateRoot(program, mainMenu)); return; } diff --git a/source/glest_game/menu/menu_state_mods.h b/source/glest_game/menu/menu_state_mods.h index 66c19593..d4f7a6e2 100644 --- a/source/glest_game/menu/menu_state_mods.h +++ b/source/glest_game/menu/menu_state_mods.h @@ -133,6 +133,7 @@ public: private: + void cleanUp(); MapInfo loadMapInfo(string file); void showMessageBox(const string &text, const string &header, bool toggle); void clearUserButtons(); diff --git a/source/shared_lib/include/util/util.h b/source/shared_lib/include/util/util.h index 7c5e652b..fb7644fe 100644 --- a/source/shared_lib/include/util/util.h +++ b/source/shared_lib/include/util/util.h @@ -128,7 +128,7 @@ public: static void init(bool haveSpecialOutputCommandLineOption); static SystemFlagsType & getSystemSettingType(DebugType type) { return (*debugLogFileList)[type]; } static size_t httpWriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data); - static std::string getHTTP(std::string URL,CURL *handle=NULL, int timeOut=-1); + static std::string getHTTP(std::string URL,CURL *handle=NULL, int timeOut=-1, CURLcode *savedResult=NULL); static std::string escapeURL(std::string URL, CURL *handle=NULL); static CURL *initHTTP(); diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index e8d815b2..67801fe9 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -103,7 +103,7 @@ std::string SystemFlags::escapeURL(std::string URL, CURL *handle) { return result; } -std::string SystemFlags::getHTTP(std::string URL,CURL *handle,int timeOut) { +std::string SystemFlags::getHTTP(std::string URL,CURL *handle,int timeOut,CURLcode *savedResult) { if(handle == NULL) { handle = SystemFlags::curl_handle; } @@ -155,10 +155,15 @@ std::string SystemFlags::getHTTP(std::string URL,CURL *handle,int timeOut) { if(chunk.memory) { free(chunk.memory); } + + if(savedResult != NULL) { + *savedResult = result; + } if(result != CURLE_OK) { serverResponse = errbuf; } + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] serverResponse [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverResponse.c_str()); return serverResponse;