Made it write the rest too
[UnsignedByte.git] / src / Server / main.cpp
blobc12f238beada3455602c01c70cf9b5b4c5756a87
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
21 #include <iostream>
22 #include <fstream>
24 #ifdef _WIN32
25 #include <winsock2.h>
26 #endif
28 #include "FieldImpls.h"
29 #include "GameVersion.h"
30 #include "SavableHeaders.h"
31 #include "TableImpls.h"
32 #include "DatabaseMgr.h"
33 #include "Global.h"
34 #include "ListenSocket.h"
35 #include "SQLSocket.h"
36 #include "UBHandler.h"
37 #include "UBSocket.h"
38 #include "SqliteMgr.h"
39 #include "Managers.h"
40 #include "StringUtilities.h"
42 void exitfunc()
44 printf("exiting...\n");
45 std::cin.get();
46 return;
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));
55 msg.append("... ");
57 if (l -> Bind(port))
59 msg.append("Not successful\n");
60 delete l;
62 else
64 msg.append("OK\n");
65 l -> SetDeleteByHandler();
66 h->Add(l);
69 return msg;
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));
78 msg.append("... ");
81 if (l -> Bind(port))
83 msg.append("Not successful\n");
84 delete l;
86 else
88 msg.append("OK\n");
89 l -> SetDeleteByHandler();
90 h->Add(l);
93 return msg;
96 bool g_quit = false;
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;
107 output.append("\n");
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");
115 return;
118 errorfile.write(output.c_str(), output.size());
119 if(errorfile.bad())
121 global->printexception("Could not write to errorlog file.\n");
122 return;
125 return;
128 static void printSyntaxHelp(Global* global, const std::string& name)
130 std::string msg = "usage: ";
132 msg.append(name);
133 msg.append(" [-n|--no-catch] [-q|--quiet] [-s|--sql] [-t|--status] [-b|--bugs] [-l|--logs]");
135 msg.append("\n");
137 global->printraw(msg);
140 static bool parseArgs(Global* global, int argc, char** argv)
142 bool quiet = false;
144 for(int i = 1; i < argc; i++)
146 bool parsed = false;
148 if(!strncmp("-n", argv[i], 2) || !strncmp("--no-catch", argv[i], 10))
150 global->printmode("Not catching exceptions.\n");
151 g_nocatch = true;
152 parsed = true;
155 if(!strncmp("-q", argv[i], 2) || !strncmp("--quiet", argv[i], 5))
157 global->printmode("Quiet mode enabled.\n");
158 g_printstatus = false;
159 g_printbugs = false;
160 g_printlogs = false;
161 g_printsql = false;
162 quiet = true;
163 parsed = true;
166 if(!strncmp("-s", argv[i], 2) || !strncmp("--sql", argv[i], 5))
168 if(quiet)
169 global->printmode("Overriding quiet, printing sql anyway.\n");
170 else
171 global->printmode("Printing sql.\n");
173 g_printsql = true;
174 parsed = true;
177 if(!strncmp("-t", argv[i], 2) || !strncmp("--status", argv[i], 8))
179 if(quiet)
180 global->printmode("Overriding quiet, printing status msgs anyway.\n");
181 else
182 global->printmode("Printing status msgs.\n");
184 g_printstatus = true;
185 parsed = true;
188 if(!strncmp("-b", argv[i], 2) || !strncmp("--bugs", argv[i], 5))
190 if(quiet)
191 global->printmode("Overriding quiet, printing bug msgs anyway.\n");
192 else
193 global->printmode("Printing bug msgs.\n");
195 g_printbugs = true;
196 parsed = true;
199 if(!strncmp("-l", argv[i], 2) || !strncmp("--log", argv[i], 5))
201 if(quiet)
202 global->printmode("Overriding quiet, printing log msgs anyway.\n");
203 else
204 global->printmode("Printing log msgs.\n");
206 g_printlogs = true;
207 parsed = true;
210 if(!parsed)
212 printSyntaxHelp(global, argv[0]);
213 return false;
217 return true;
220 int main(int argc, char** argv)
222 Global* global = Global::Get();
224 if(argc > 1)
226 bool parsed = parseArgs(global, argc, argv);
227 if(!parsed)
228 return 1;
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");
259 while (!g_quit)
261 if(g_nocatch)
263 UBHandler::Get()->Select(0, 200000);
264 UBHandler::Get()->SwitchEditors();
266 else
268 try {
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());
274 g_quit = true;
279 try {
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());
287 try {
288 UBHandler::Free();
289 } catch(std::exception& e) {
290 global->printexception("While freeing the UBHandler, trying to quit gracefully.", e.what());
291 logToFile(global, e.what());
294 g_shutdown = true;
296 try {
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());
303 try {
304 SqliteMgr::Free();
305 } catch(std::exception& e) {
306 global->printexception("While freeing the SqliteMgr, trying to quit gracefully.", e.what());
307 logToFile(global, e.what());
310 try {
311 DatabaseMgr::Free();
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");
318 exitfunc();
319 return 0;