00001 /* 00002 * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #include "adchpp.h" 00020 00021 #include "LogManager.h" 00022 #include "TimerManager.h" 00023 #include "SocketManager.h" 00024 #include "ClientManager.h" 00025 #include "PluginManager.h" 00026 #include "File.h" 00027 00028 namespace adchpp { 00029 00030 using namespace std; 00031 00032 const char compileTime[] = __DATE__ " " __TIME__; 00033 00034 static bool initialized = false; 00035 static bool running = false; 00036 00037 void initialize(const string& configPath) { 00038 if (initialized) { 00039 throw Exception("Already initialized"); 00040 } 00041 00042 #ifdef _WIN32 00043 WSADATA wsaData; 00044 WSAStartup(MAKEWORD(2, 2), &wsaData); 00045 #endif 00046 00047 Util::initialize(configPath); 00048 00049 LogManager::newInstance(); 00050 TimerManager::newInstance(); 00051 SocketManager::newInstance(); 00052 ClientManager::newInstance(); 00053 PluginManager::newInstance(); 00054 00055 initialized = true; 00056 } 00057 00058 void startup(void(*f)()) { 00059 if (!initialized) { 00060 throw Exception("adchpp not initialized"); 00061 } 00062 Stats::startTime = GET_TIME(); 00063 00064 if (f) 00065 f(); 00066 SocketManager::getInstance()->startup(); 00067 if (f) 00068 f(); 00069 PluginManager::getInstance()->load(); 00070 if (f) 00071 f(); 00072 00073 running = true; 00074 } 00075 00076 void shutdown(void(*f)()) { 00077 if (!running) { 00078 return; 00079 } 00080 00081 if (f) 00082 f(); 00083 PluginManager::getInstance()->shutdown(); 00084 if (f) 00085 f(); 00086 SocketManager::getInstance()->shutdown(); 00087 if (f) 00088 f(); 00089 00090 running = false; 00091 } 00092 00093 void cleanup() { 00094 if (!initialized) { 00095 return; 00096 } 00097 if (running) { 00098 shutdown(0); 00099 } 00100 00101 PluginManager::deleteInstance(); 00102 ClientManager::deleteInstance(); 00103 SocketManager::deleteInstance(); 00104 LogManager::deleteInstance(); 00105 TimerManager::deleteInstance(); 00106 00107 #ifdef _WIN32 00108 WSACleanup(); 00109 #endif 00110 00111 initialized = false; 00112 } 00113 00114 //#ifdef _DEBUG 00115 void logAssert(const char* file, int line, const char* exp) { 00116 try { 00117 File f(Util::getCfgPath() + _T("exceptioninfo.txt"), File::WRITE, File::OPEN | File::CREATE); 00118 f.setEndPos(0); 00119 00120 f.write(string(file) + "(" + Util::toString(line) + "): " + string(exp) + "\r\n"); 00121 } catch (const FileException& e) { 00122 dcdebug("logAssert: %s\n", e.getError().c_str()); 00123 } 00124 } 00125 //#endif // _DEBUG 00126 00127 }