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 parseArgs(int argc
, char** argv
)
130 for(int i
= 1; i
< argc
; i
++)
132 if(!strncmp("-n", argv
[i
], 2) || !strncmp("--no-catch", argv
[i
], 10))
134 printf("[MODE ] Not catching exceptions.\n");
138 if(!strncmp("-s", argv
[i
], 2) || !strncmp("--sql", argv
[i
], 5))
140 printf("[MODE ] Printing sql.\n");
144 if(!strncmp("-q", argv
[i
], 2) || !strncmp("--quiet", argv
[i
], 5))
146 printf("[MODE ] Not printing status and bug msgs.\n");
147 g_printstatus
= false;
151 if(!strncmp("-b", argv
[i
], 2) || !strncmp("--bugs", argv
[i
], 5))
153 printf("[MODE ] Printing bug msgs.\n");
157 if(!strncmp("-l", argv
[i
], 2) || !strncmp("--log", argv
[i
], 5))
159 printf("[MODE ] Printing log msgs.\n");
165 int main(int argc
, char** argv
)
169 parseArgs(argc
, argv
);
172 Global
* global
= Global::Get();
174 global
->printstatus("Opening database...\n");
175 std::string dbname
= game::vname
;
176 dbname
.append(".db");
177 DatabaseMgr::Initialize(dbname
);
178 global
->printstatus("Database opened.\n");
180 global
->printstatus("Initializing in-memory table definitions...\n");
181 db::TableImpls::Get()->Initialize();
182 global
->printstatus("In-memory table definitions initialized.\n");
184 global
->printstatus("Binding ports...\n");
185 std::string portstatus
;
187 portstatus
= add(UBHandler::Get(), 4000);
188 global
->printstatus(portstatus
);
190 portstatus
= add(UBHandler::Get(), 4040);
191 global
->printstatus(portstatus
);
193 portstatus
= add(UBHandler::Get(), 5060);
194 global
->printstatus(portstatus
);
196 portstatus
= addSQL(UBHandler::Get(), 9090);
197 global
->printstatus(portstatus
);
199 global
->printstatus("Ports bound.\n");
200 global
->printstatus("Running!\n");
206 UBHandler::Get()->Select(0, 200000);
207 UBHandler::Get()->SwitchEditors();
212 UBHandler::Get()->Select(0, 200000);
213 UBHandler::Get()->SwitchEditors();
214 } catch(std::exception
& e
) {
215 global
->printexception("While in main loop, trying to quit gracefully", e
.what());
216 logToFile(global
, e
.what());
223 UBHandler::Get()->Shutdown();
224 UBHandler::Get()->Select();
225 } catch(std::exception
& e
) {
226 global
->printexception("While closing sockets, trying to quit gracefully.", e
.what());
227 logToFile(global
, e
.what());
232 } catch(std::exception
& e
) {
233 global
->printexception("While freeing the UBHandler, trying to quit gracefully.", e
.what());
234 logToFile(global
, e
.what());
240 mud::Managers::Free();
241 } catch(std::exception
& e
) {
242 global
->printexception("While freeing mud::Managers, trying to quit gracefully.", e
.what());
243 logToFile(global
, e
.what());
248 } catch(std::exception
& e
) {
249 global
->printexception("While freeing the SqliteMgr, trying to quit gracefully.", e
.what());
250 logToFile(global
, e
.what());
255 } catch(std::exception
& e
) {
256 global
->printexception("While freeing the DatabaseMgr, trying to quit gracefully.", e
.what());
257 logToFile(global
, e
.what());
260 global
->printstatus("End of program.\n");