1 package org.argosfields.multi.server;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import net.sf.hibernate.util.FastHashMap;
7
8 import org.apache.commons.collections.FastArrayList;
9 import org.apache.commons.lang.NullArgumentException;
10 import org.apache.commons.lang.RandomStringUtils;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argosfields.multi.Player;
14 import org.argosfields.service.LoginException;
15 import org.argosfields.spring.ISpringContextHelper;
16 import org.argosfields.spring.ServerContextHelper;
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.jface.preference.JFacePreferences;
19 import org.eclipse.jface.util.ListenerList;
20
21 /***
22 * GameServer.java
23 * @author Xavier Cho
24 * @version $Revision: 1.2 $ $Date: 2004/04/17 18:21:02 $
25 */
26 public final class GameServer implements IGameServer {
27
28 private static final int SESSION_ID_LENGTH = 12;
29
30 private static Log log = LogFactory.getLog(GameServer.class);
31
32 private static GameServer instance;
33
34 private ListenerList listenerList;
35 private FastHashMap sessions;
36 private boolean running = false;
37 private List players;
38
39 private GameServer() {
40 this.listenerList = new ListenerList();
41 this.sessions = new FastHashMap();
42 this.players = new FastArrayList();
43 }
44
45 public static IGameServer getInstance() {
46 if (instance == null) {
47 synchronized (GameServer.class) {
48 instance = new GameServer();
49 }
50 }
51
52 return instance;
53 }
54
55 /***
56 * @see org.argosfields.multi.server.IGameServer#start()
57 */
58 public void start() throws Exception {
59 if (running) {
60 if (log.isWarnEnabled()) {
61 log.warn("ArgosFields server is already running.");
62 }
63 } else {
64 ISpringContextHelper helper = ServerContextHelper.getInstance();
65 helper.reload();
66
67 this.running = true;
68 fireServerStarted();
69
70 if (log.isInfoEnabled()) {
71 log.info("Game server has been started successfully.");
72 }
73 }
74 }
75
76 /***
77 * @see org.argosfields.multi.server.IGameServer#isRunning()
78 */
79 public boolean isRunning() {
80 return running;
81 }
82
83 /***
84 * @see org.argosfields.multi.server.IGameServer#stop()
85 */
86 public void stop() throws Exception {
87 if (running) {
88 ISpringContextHelper helper = ServerContextHelper.getInstance();
89 helper.close();
90
91 this.running = false;
92 fireServerStopped();
93
94 if (log.isInfoEnabled()) {
95 log.info("Game server has been shutdown successfully.");
96 }
97 }
98 }
99
100 /***
101 * @see org.argosfields.multi.server.IGameServer#login(java.lang.String)
102 */
103 public Session login(final String userName) throws LoginException {
104 if (userName == null) {
105 throw new NullArgumentException("userName");
106 }
107
108 if (!running) {
109 String msg = "Server is not running.";
110 throw new IllegalStateException(msg);
111 }
112
113 Session session = null;
114 String sessionId = null;
115
116 synchronized (sessions) {
117 IPreferenceStore preferences = JFacePreferences.getPreferenceStore();
118 if (preferences.getInt("max.users") <= sessions.size()) {
119 throw new LoginException("error.server.full");
120 }
121
122 Iterator it = sessions.values().iterator();
123 while (it.hasNext()) {
124 Session s = (Session) it.next();
125 Player player = s.getPlayer();
126 if (player.getUserName().equals(userName)) {
127 throw new LoginException("error.session.exists");
128 }
129 }
130
131 while (sessionId == null || sessions.containsKey(sessionId)) {
132 sessionId = RandomStringUtils
133 .randomAlphanumeric(SESSION_ID_LENGTH);
134 sessionId = sessionId.toUpperCase();
135 }
136
137 Player player = new Player(userName);
138
139 session = new Session(sessionId, player);
140 sessions.put(sessionId, session);
141 players.add(player);
142 }
143
144 if (log.isInfoEnabled()) {
145 log.info("New session has been created :");
146 log.info(" - sessionId : " + sessionId);
147 log.info(" - userName : " + userName);
148 }
149
150 return session;
151 }
152
153 /***
154 * @see org.argosfields.multi.server.IGameServer#logout(java.lang.String)
155 */
156 public void logout(final String sessionId) {
157 if (!running) {
158 String msg = "Server is not running.";
159 throw new IllegalStateException(msg);
160 }
161
162 if (sessions.containsKey(sessionId)) {
163 Session session = null;
164
165 synchronized (sessions) {
166 session = (Session) sessions.get(sessionId);
167
168 Player player = session.getPlayer();
169
170 sessions.remove(sessionId);
171 players.remove(player);
172 }
173
174 if (log.isInfoEnabled()) {
175 log.info("User has been logged out :");
176 log.info(" - sessionId : " + sessionId);
177 log.info(" - player : " + session.getPlayer());
178 }
179 }
180 }
181
182 /***
183 * @see org.argosfields.multi.server.IGameServer#getPlayers()
184 */
185 public List getPlayers() {
186 return players;
187 }
188
189 /***
190 * @see org.argosfields.multi.server.IGameServer#addServerStatusListener(org.argosfields.server.IServerStatusListener)
191 */
192 public void addServerStatusListener(final IServerStatusListener listener) {
193 listenerList.add(listener);
194 }
195
196 /***
197 * @see org.argosfields.multi.server.IGameServer#removeServerStatusListener(org.argosfields.server.IServerStatusListener)
198 */
199 public void removeServerStatusListener(final IServerStatusListener listener) {
200 listenerList.remove(listener);
201 }
202
203 protected void fireServerStarted() {
204 Object[] listeners = null;
205
206 synchronized (listenerList) {
207 listeners = listenerList.getListeners();
208 }
209
210 if (listeners != null) {
211 int size = listeners.length;
212 for (int i = 0; i < size; i++) {
213 IServerStatusListener listener = (IServerStatusListener) listeners[i];
214 listener.serverStarted(this);
215 }
216 }
217 }
218
219 protected void fireServerStopped() {
220 Object[] listeners = null;
221
222 synchronized (listenerList) {
223 listeners = listenerList.getListeners();
224 }
225
226 if (listeners != null) {
227 int size = listeners.length;
228 for (int i = 0; i < size; i++) {
229 IServerStatusListener listener = (IServerStatusListener) listeners[i];
230 listener.serverStopped(this);
231 }
232 }
233 }
234
235 public static void main(final String[] args) {
236 }
237 }
This page was automatically generated by Maven