1
2
3
4
5 package spellcast.server;
6
7 import java.net.InetAddress;
8
9 import org.apache.log4j.Logger;
10 import spellcast.game.Game_Server;
11 import spellcast.net.ConnectionStatus;
12 import spellcast.net.ServerStatus;
13 import spellcast.net.VersionDetails;
14
15 /***
16 * The Server class for Spellcast. This is a singleton class, to create instances
17 * use <code>createServer</code>.
18 *
19 * @author Barrie Treloar
20 */
21 public class Server {
22 private Game_Server gameServer;
23 private InetAddress bindAddress;
24 private int port;
25 private ServerStatusRequestHandler serverStatusRequestHandler;
26 private ServerConnectionHandler serverConnectionHandler;
27 private ConnectionStatus connectionStatus;
28
29 private static Server instance;
30 private static final VersionDetails programVersionDetails =
31 new VersionDetails("Spellcast Server", 0, 0, 1);
32
33 private static final Logger logger = Logger.getLogger("server");
34
35 private Server(InetAddress bindAddress, int port, String gameName)
36 throws ServerException {
37 this.bindAddress = bindAddress;
38 this.port = port;
39 init(gameName);
40 start();
41 }
42
43 public static Server createServer(
44 InetAddress bindAddress,
45 int port,
46 String gameName)
47 throws ServerException {
48 instance = new Server(bindAddress, port, gameName);
49 return instance;
50 }
51
52 public static Server getServer() {
53 return instance;
54 }
55
56 public void init(String gameName) {
57 serverStatusRequestHandler = new ServerStatusRequestHandler();
58 serverConnectionHandler =
59 new ServerConnectionHandler(programVersionDetails, bindAddress, port);
60 gameServer = new Game_Server(serverConnectionHandler, gameName);
61 connectionStatus = ConnectionStatus.ACCEPTING;
62 }
63
64 public void start() throws ServerException {
65 serverStatusRequestHandler.start();
66 serverConnectionHandler.start();
67 gameServer.start();
68 }
69
70 /***
71 * Stop the server. This is not a graceful shutdown.
72 */
73 public void stop() {
74 serverStatusRequestHandler.stop();
75 serverConnectionHandler.stop();
76 gameServer.stop();
77 instance = null;
78 }
79
80 ServerStatus getServerStatus() {
81 return new ServerStatus(this);
82 }
83
84 /***
85 * @return Returns the bindAddress.
86 */
87 public InetAddress getBindAddress() {
88 return bindAddress;
89 }
90 /***
91 * @return Returns the connectionStatus.
92 */
93 public ConnectionStatus getConnectionStatus() {
94 return connectionStatus;
95 }
96 /***
97 * @return Returns the port.
98 */
99 public int getPort() {
100 return port;
101 }
102 /***
103 * @return Returns the gameServer.
104 */
105 public Game_Server getGameServer() {
106 return gameServer;
107 }
108 /***
109 * @return Returns the programVersionDetails.
110 */
111 public VersionDetails getProgramVersionDetails() {
112 return programVersionDetails;
113 }
114 }