Chunk is purely a wrapper class, any 'state variables', like 'characters in room...
[UnsignedByte.git] / src / Server / main.cpp
blob4f80a544c475972e6b7f314d612839ec5d6f9583
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"
41 void exitfunc()
43 printf("exiting...\n");
44 std::cin.get();
45 return;
48 void add(SocketHandler* h,int port)
50 ListenSocket<UBSocket> *l = new ListenSocket<UBSocket>(*h);
51 printf("Attempting bind on port %d... ", port);
52 if (l -> Bind(port))
54 printf("Not successful\n");
55 delete l;
56 return;
58 printf("OK\n");
59 l -> SetDeleteByHandler();
60 h->Add(l);
63 void addSQL(SocketHandler* h, int port)
65 ListenSocket<SQLSocket> *l = new ListenSocket<SQLSocket>(*h);
66 printf("Attempting SQL bind on port %d... ", port);
67 if (l -> Bind(port))
69 printf("Not successful\n");
70 delete l;
71 return;
73 printf("OK\n");
74 l -> SetDeleteByHandler();
75 h->Add(l);
78 static void logToFile(const std::string& text)
80 std::string output = text;
81 output.append("\n");
83 std::ofstream errorfile;
84 errorfile.open("errorlog.txt", std::ios_base::app);
86 if(!errorfile.is_open())
88 printf("Could not open errorlog file.\n");
89 return;
92 errorfile.write(output.c_str(), output.size());
93 if(errorfile.bad())
95 printf("Could not write to errorlog file.\n");
96 return;
99 return;
102 bool g_quit = false;
103 extern bool g_shutdown;
104 extern bool g_nocatch;
106 int main(int argc, char** argv)
108 if(argc > 1)
110 if(!strncmp("-n", argv[1], 2))
111 g_nocatch = true;
114 printf("Opening database...\n");
115 std::string dbname = game::vname;
116 dbname.append(".db");
117 DatabaseMgr::Initialize(dbname);
118 printf("Done.\n");
120 db::TableImpls::Get()->Initialize();
122 printf("Binding ports...\n");
124 add(UBHandler::Get(), 4000);
125 add(UBHandler::Get(), 4040);
126 add(UBHandler::Get(), 5060);
127 addSQL(UBHandler::Get(), 9090);
129 printf("Running!\n");
131 while (!g_quit)
133 if(g_nocatch)
135 UBHandler::Get()->Select(0, 200000);
136 UBHandler::Get()->SwitchEditors();
138 else
140 try {
141 UBHandler::Get()->Select(0, 200000);
142 UBHandler::Get()->SwitchEditors();
143 } catch(std::exception& e) {
144 printf("Cought exception '%s', trying to quit gracefully.\n", e.what());
145 logToFile(e.what());
146 g_quit = true;
151 try {
152 UBHandler::Get()->Shutdown();
153 UBHandler::Get()->Select();
154 } catch(std::exception& e) {
155 printf("Cought exception '%s' while closing sockets, trying to quit gracefully.\n", e.what());
156 logToFile(e.what());
159 try {
160 UBHandler::Free();
161 } catch(std::exception& e) {
162 printf("Cought exception '%s' while freeing the UBHandler, trying to quit gracefully.\n", e.what());
163 logToFile(e.what());
166 g_shutdown = true;
168 try {
169 mud::Managers::Free();
170 } catch(std::exception& e) {
171 printf("Cought exception '%s' while freeing mud::Managers, trying to quit gracefully.\n", e.what());
172 logToFile(e.what());
175 try {
176 SqliteMgr::Free();
177 } catch(std::exception& e) {
178 printf("Cought exception '%s' while freeing the SqliteMgr, trying to quit gracefully.\n", e.what());
179 logToFile(e.what());
182 try {
183 DatabaseMgr::Free();
184 } catch(std::exception& e) {
185 printf("Cought exception '%s' while freeing the DatabaseMgr, trying to quit gracefully.\n", e.what());
186 logToFile(e.what());
189 printf("End of program.\n");
190 exitfunc();
191 return 0;