Source code: marauroa/marauroad.java
1 /* $Id: marauroad.java,v 1.18 2003/12/15 17:00:47 arianne_rpg Exp $ */
2 /***************************************************************************
3 * (C) Copyright 2003 - Marauroa *
4 ***************************************************************************
5 ***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 ***************************************************************************/
13 package marauroa;
14
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import marauroa.game.PlayerDatabase;
18 import marauroa.game.PlayerDatabaseFactory;
19 import marauroa.game.RPObject;
20
21 /**
22 * The launcher of the whole Marauroa Server.
23 *
24 */
25 public class marauroad extends Thread
26 {
27 private static marauroad marauroa;
28 private static Date timestamp;
29 private static SimpleDateFormat formatter;
30
31 private marauroa.net.NetworkServerManager netMan;
32 private marauroa.game.GameServerManager gameMan;
33
34 static
35 {
36 timestamp=new Date();
37 formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
38 }
39
40 private static void setArguments(String[] args)
41 {
42 int i=0;
43
44 while(i!=args.length)
45 {
46 if(args[i].equals("-c"))
47 {
48 Configuration.setConfigurationFile(args[i+1]);
49 }
50 else if(args[i].equals("-h"))
51 {
52 // TODO: Write help
53 }
54
55 ++i;
56 }
57 }
58
59 private void setTestDatabase()
60 {
61 marauroad.trace("marauroad::setTestDatabase",">");
62
63 try
64 {
65 PlayerDatabase playerDatabase=PlayerDatabaseFactory.getDatabase();
66
67 if(playerDatabase.hasPlayer("Test Player"))
68 {
69 playerDatabase.removePlayer("Test Player");
70 }
71
72 if(playerDatabase.hasPlayer("Another Test Player"))
73 {
74 playerDatabase.removePlayer("Another Test Player");
75 }
76
77 playerDatabase.addPlayer("Test Player","Test Password");
78 playerDatabase.addPlayer("Another Test Player","Test Password");
79
80 RPObject SonGoku=new RPObject();
81 SonGoku.put("object_id","1");
82 SonGoku.put("name","Son Goku");
83 SonGoku.put("type","character");
84 playerDatabase.addCharacter("Test Player", "Son Goku",SonGoku);
85
86 RPObject MrBean=new RPObject();
87 MrBean.put("object_id","2");
88 MrBean.put("name","Mr Bean");
89 MrBean.put("type","character");
90 playerDatabase.addCharacter("Another Test Player", "MrBean",MrBean);
91
92 RPObject DrCoreDump=new RPObject();
93 DrCoreDump.put("object_id","3");
94 DrCoreDump.put("name","Dr CoreDump");
95 DrCoreDump.put("type","character");
96 playerDatabase.addCharacter("Test Player", "Dr CoreDump",DrCoreDump);
97 }
98 catch(Exception e)
99 {
100 marauroad.trace("marauroad::setTestDatabase","X",e.getMessage());
101 marauroad.trace("PlayerEntryContainer","!","ABORT: marauroad can't allocate database");
102 System.exit(-1);
103 }
104 finally
105 {
106 marauroad.trace("marauroad::setTestDatabase","<");
107 }
108 }
109
110 public static void main (String[] args)
111 {
112 marauroad.trace("marauroad::main",">");
113 marauroad.setArguments(args);
114 marauroad.getMarauroa().start();
115 marauroad.trace("marauroad::main","<");
116 }
117
118 public synchronized void run()
119 {
120 marauroad.trace("marauroad::run",">");
121 boolean finish=false;
122 println("Marauroa server - An open source MMORPG Server -");
123 println("Running on version @version@");
124 println("(C) 2003 Miguel Angel Blanch Lardin");
125 println();
126 println("This program is free software; you can redistribute it and/or modify");
127 println("it under the terms of the GNU General Public License as published by");
128 println("the Free Software Foundation; either version 2 of the License, or");
129 println("(at your option) any later version.");
130 println();
131 println("This program is distributed in the hope that it will be useful,");
132 println("but WITHOUT ANY WARRANTY; without even the implied warranty of");
133 println("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the");
134 println("GNU General Public License for more details.");
135 println();
136 println("You should have received a copy of the GNU General Public License");
137 println("along with this program; if not, write to the Free Software");
138 println("Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA");
139
140 marauroad instance=marauroad.getMarauroa();
141 instance.setTestDatabase();
142
143 instance.init();
144
145 while(!finish)
146 {
147 try
148 {
149 wait();
150 }
151 catch(InterruptedException e)
152 {
153 finish=true;
154 }
155 }
156
157 instance.finish();
158 marauroad.trace("marauroad::run","<");
159 }
160
161 private marauroad()
162 {
163 super("marauroad");
164 }
165
166 public static marauroad getMarauroa()
167 {
168 if(marauroa==null)
169 {
170 marauroa=new marauroad();
171 }
172
173 return marauroa;
174 }
175
176 public void init()
177 {
178 marauroad.trace("marauroad::init",">");
179 try
180 {
181 netMan=new marauroa.net.NetworkServerManager();
182 gameMan= new marauroa.game.GameServerManager(netMan);
183 }
184 catch(java.net.SocketException e)
185 {
186 marauroad.trace("marauroad::init","X",e.getMessage());
187 marauroad.trace("PlayerEntryContainer","!","ABORT: marauroad can't allocate server socket");
188 System.exit(-1);
189 }
190 finally
191 {
192 marauroad.trace("marauroad::init","<");
193 }
194 }
195
196 public void finish()
197 {
198 marauroad.trace("marauroad::finish",">");
199 netMan.finish();
200 gameMan.finish();
201 marauroad.trace("marauroad::finish","<");
202 }
203
204 private void message(String text)
205 {
206 System.out.println(text);
207 }
208
209 public static void println(String text)
210 {
211 getMarauroa().message(text);
212 }
213
214 public static void println()
215 {
216 getMarauroa().message("");
217 }
218
219 public static void trace(String module,String event)
220 {
221 timestamp.setTime(System.currentTimeMillis());
222 String ts = formatter.format(timestamp);
223 getMarauroa().message(ts+"\t"+event+"\t"+module);
224 }
225
226 public static void trace(String module,String event,String text)
227 {
228 timestamp.setTime(System.currentTimeMillis());
229 String ts = formatter.format(timestamp);
230 getMarauroa().message(ts+"\t"+event+"\t"+module+"\t"+text);
231 }
232
233 public static void report(String text)
234 {
235 getMarauroa().message(new java.sql.Timestamp(new java.util.Date().getTime())+": "+text);
236 }
237 }