1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
28 #include "FieldImpls.h"
29 #include "GameVersion.h"
30 #include "SavableHeaders.h"
31 #include "TableImpls.h"
32 #include "DatabaseMgr.h"
34 #include "ListenSocket.h"
35 #include "SQLSocket.h"
36 #include "UBHandler.h"
38 #include "SqliteMgr.h"
40 #include "StringUtilities.h"
44 printf("exiting...\n");
49 std::string
add(SocketHandler
* h
,int port
)
51 ListenSocket
<UBSocket
> *l
= new ListenSocket
<UBSocket
>(*h
);
53 std::string msg
= "Attempting bind on port";
54 msg
.append(String::Get()->fromInt(port
));
59 msg
.append("Not successful\n");
65 l
-> SetDeleteByHandler();
72 std::string
addSQL(SocketHandler
* h
, int port
)
74 ListenSocket
<SQLSocket
> *l
= new ListenSocket
<SQLSocket
>(*h
);
76 std::string msg
= "Attempting SQL bind on port ";
77 msg
.append(String::Get()->fromInt(port
));
83 msg
.append("Not successful\n");
89 l
-> SetDeleteByHandler();
97 extern bool g_shutdown
;
98 extern bool g_nocatch
;
99 extern bool g_printsql
;
100 extern bool g_printstatus
;
101 extern bool g_printbugs
;
102 extern bool g_printlogs
;
104 static void logToFile(Global
* global
, const std::string
& text
)
106 std::string output
= text
;
109 std::ofstream errorfile
;
110 errorfile
.open("errorlog.txt", std::ios_base::app
);
112 if(!errorfile
.is_open())
114 global
->printexception("Could not open errorlog file.\n");
118 errorfile
.write(output
.c_str(), output
.size());
121 global
->printexception("Could not write to errorlog file.\n");
128 static void printSyntaxHelp(Global
* global
, const std::string
& name
)
130 std::string msg
= "usage: ";
133 msg
.append(" [-n|--no-catch] [-q|--quiet] [-s|--sql] [-t|--status] [-b|--bugs] [-l|--logs]");
137 global
->printraw(msg
);
140 static bool parseArgs(Global
* global
, int argc
, char** argv
)
144 for(int i
= 1; i
< argc
; i
++)
148 if(!strncmp("-n", argv
[i
], 2) || !strncmp("--no-catch", argv
[i
], 10))
150 global
->printmode("Not catching exceptions.\n");
155 if(!strncmp("-q", argv
[i
], 2) || !strncmp("--quiet", argv
[i
], 5))
157 global
->printmode("Quiet mode enabled.\n");
158 g_printstatus
= false;
166 if(!strncmp("-s", argv
[i
], 2) || !strncmp("--sql", argv
[i
], 5))
169 global
->printmode("Overriding quiet, printing sql anyway.\n");
171 global
->printmode("Printing sql.\n");
177 if(!strncmp("-t", argv
[i
], 2) || !strncmp("--status", argv
[i
], 8))
180 global
->printmode("Overriding quiet, printing status msgs anyway.\n");
182 global
->printmode("Printing status msgs.\n");
184 g_printstatus
= true;
188 if(!strncmp("-b", argv
[i
], 2) || !strncmp("--bugs", argv
[i
], 5))
191 global
->printmode("Overriding quiet, printing bug msgs anyway.\n");
193 global
->printmode("Printing bug msgs.\n");
199 if(!strncmp("-l", argv
[i
], 2) || !strncmp("--log", argv
[i
], 5))
202 global
->printmode("Overriding quiet, printing log msgs anyway.\n");
204 global
->printmode("Printing log msgs.\n");
212 printSyntaxHelp(global
, argv
[0]);
220 int main(int argc
, char** argv
)
222 Global
* global
= Global::Get();
226 bool parsed
= parseArgs(global
, argc
, argv
);
231 global
->printstatus("Opening database...\n");
232 std::string dbname
= game::vname
;
233 dbname
.append(".db");
234 DatabaseMgr::Initialize(dbname
);
235 global
->printstatus("Database opened.\n");
237 global
->printstatus("Initializing in-memory table definitions...\n");
238 db::TableImpls::Get()->Initialize();
239 global
->printstatus("In-memory table definitions initialized.\n");
241 global
->printstatus("Binding ports...\n");
242 std::string portstatus
;
244 portstatus
= add(UBHandler::Get(), 4000);
245 global
->printstatus(portstatus
);
247 portstatus
= add(UBHandler::Get(), 4040);
248 global
->printstatus(portstatus
);
250 portstatus
= add(UBHandler::Get(), 5060);
251 global
->printstatus(portstatus
);
253 portstatus
= addSQL(UBHandler::Get(), 9090);
254 global
->printstatus(portstatus
);
256 global
->printstatus("Ports bound.\n");
257 global
->printstatus("Running!\n");
263 UBHandler::Get()->Select(0, 200000);
264 UBHandler::Get()->SwitchEditors();
269 UBHandler::Get()->Select(0, 200000);
270 UBHandler::Get()->SwitchEditors();
271 } catch(std::exception
& e
) {
272 global
->printexception("While in main loop, trying to quit gracefully", e
.what());
273 logToFile(global
, e
.what());
280 UBHandler::Get()->Shutdown();
281 UBHandler::Get()->Select();
282 } catch(std::exception
& e
) {
283 global
->printexception("While closing sockets, trying to quit gracefully.", e
.what());
284 logToFile(global
, e
.what());
289 } catch(std::exception
& e
) {
290 global
->printexception("While freeing the UBHandler, trying to quit gracefully.", e
.what());
291 logToFile(global
, e
.what());
297 mud::Managers::Free();
298 } catch(std::exception
& e
) {
299 global
->printexception("While freeing mud::Managers, trying to quit gracefully.", e
.what());
300 logToFile(global
, e
.what());
305 } catch(std::exception
& e
) {
306 global
->printexception("While freeing the SqliteMgr, trying to quit gracefully.", e
.what());
307 logToFile(global
, e
.what());
312 } catch(std::exception
& e
) {
313 global
->printexception("While freeing the DatabaseMgr, trying to quit gracefully.", e
.what());
314 logToFile(global
, e
.what());
317 global
->printstatus("End of program.\n");