Added a printmode and printraw to Global.
[UnsignedByte.git] / src / Server / main.cpp
bloba33ebad8aeb749e80502faacb1ea804e50b970d5
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 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");
135 g_nocatch = true;
138 if(!strncmp("-s", argv[i], 2) || !strncmp("--sql", argv[i], 5))
140 printf("[MODE ] Printing sql.\n");
141 g_printsql = true;
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;
148 g_printbugs = false;
151 if(!strncmp("-b", argv[i], 2) || !strncmp("--bugs", argv[i], 5))
153 printf("[MODE ] Printing bug msgs.\n");
154 g_printbugs = true;
157 if(!strncmp("-l", argv[i], 2) || !strncmp("--log", argv[i], 5))
159 printf("[MODE ] Printing log msgs.\n");
160 g_printlogs = true;
165 int main(int argc, char** argv)
167 if(argc > 1)
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");
202 while (!g_quit)
204 if(g_nocatch)
206 UBHandler::Get()->Select(0, 200000);
207 UBHandler::Get()->SwitchEditors();
209 else
211 try {
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());
217 g_quit = true;
222 try {
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());
230 try {
231 UBHandler::Free();
232 } catch(std::exception& e) {
233 global->printexception("While freeing the UBHandler, trying to quit gracefully.", e.what());
234 logToFile(global, e.what());
237 g_shutdown = true;
239 try {
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());
246 try {
247 SqliteMgr::Free();
248 } catch(std::exception& e) {
249 global->printexception("While freeing the SqliteMgr, trying to quit gracefully.", e.what());
250 logToFile(global, e.what());
253 try {
254 DatabaseMgr::Free();
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");
261 exitfunc();
262 return 0;