1 package org.argosfields.multi.client;
2
3 import org.apache.commons.lang.NullArgumentException;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.argosfields.multi.IGameClientListener;
7 import org.argosfields.multi.server.ServerInfo;
8 import org.argosfields.service.IGameManager;
9 import org.argosfields.spring.ClientContextHelper;
10 import org.argosfields.spring.ISpringContextHelper;
11 import org.eclipse.jface.util.ListenerList;
12
13 /***
14 * GameClient.java
15 * @author Xavier Cho
16 * @version $Revision: 1.3 $ $Date: 2004/04/17 18:21:01 $
17 */
18 public final class GameClient implements IGameClient {
19
20 private static Log log = LogFactory.getLog(GameClient.class);
21 private static GameClient instance;
22
23 private ListenerList listenerList;
24 private String sessionId;
25
26 private GameClient() {
27 this.listenerList = new ListenerList();
28 }
29
30 public static IGameClient getInstance() {
31 if (instance == null) {
32 synchronized (GameClient.class) {
33 instance = new GameClient();
34 }
35 }
36
37 return instance;
38 }
39
40 /***
41 * @see org.argosfields.multi.client.IGameClient#getSessionId()
42 */
43 public String getSessionId() {
44 return sessionId;
45 }
46
47 /***
48 * @see org.argosfields.multi.client.IGameClient#startSession(java.lang.String,
49 * int, java.lang.String)
50 */
51 public void startSession(
52 final String hostAddress,
53 final int port,
54 final String sessionId) {
55 if (hostAddress == null) {
56 throw new NullArgumentException("hostAddress");
57 }
58
59 if (sessionId == null) {
60 throw new NullArgumentException("sessionId");
61 }
62
63 if (log.isInfoEnabled()) {
64 log.info("New session has been started :");
65 log.info(" - sessionId : " + sessionId);
66 }
67
68 this.sessionId = sessionId;
69
70 ClientContextHelper helper = ClientContextHelper.getInstance();
71 helper.reload();
72
73 if (log.isDebugEnabled()) {
74 log.debug("Reading welcome message from the server...");
75 }
76
77 IGameManager manager = helper.getGameManager();
78 String message = manager.getWelcomeMessage();
79
80 ServerInfo info = new ServerInfo(message, hostAddress, port);
81 fireSessionStarted(info);
82 }
83
84 /***
85 * @see org.argosfields.multi.client.IGameClient#endSession()
86 */
87 public void endSession() {
88 this.sessionId = null;
89
90 ISpringContextHelper helper = ClientContextHelper.getInstance();
91 helper.close();
92
93 fireSessionEnded();
94 }
95
96 /***
97 * @see org.argosfields.multi.client.IGameClient#isSessionStarted()
98 */
99 public boolean isSessionStarted() {
100 return sessionId != null;
101 }
102
103 /***
104 * @see org.argosfields.multi.client.IGameClient#addGameClientListener(org.argosfields.multi.IGameClientListener)
105 */
106 public void addGameClientListener(final IGameClientListener listener) {
107 listenerList.add(listener);
108 }
109
110 /***
111 * @see org.argosfields.multi.client.IGameClient#removeGameClientListener(org.argosfields.multi.IGameClientListener)
112 */
113 public void removeGameClientListener(final IGameClientListener listener) {
114 listenerList.remove(listener);
115 }
116
117 protected void fireSessionStarted(final ServerInfo info) {
118 if (info == null) {
119 throw new NullArgumentException("info");
120 }
121
122 Object[] listeners = null;
123
124 synchronized (listenerList) {
125 listeners = listenerList.getListeners();
126 }
127
128 if (listeners != null) {
129 int size = listeners.length;
130 for (int i = 0; i < size; i++) {
131 IGameClientListener listener = (IGameClientListener) listeners[i];
132 listener.sessionStarted(info);
133 }
134 }
135 }
136
137 protected void fireSessionEnded() {
138 Object[] listeners = null;
139
140 synchronized (listenerList) {
141 listeners = listenerList.getListeners();
142 }
143
144 if (listeners != null) {
145 int size = listeners.length;
146 for (int i = 0; i < size; i++) {
147 IGameClientListener listener = (IGameClientListener) listeners[i];
148 listener.sessionEnded();
149 }
150 }
151 }
152
153 protected void fireSystemMessageReceived(final String message) {
154 if (message == null) {
155 throw new NullArgumentException("message");
156 }
157
158 if (log.isDebugEnabled()) {
159 log.debug("Broadcasting system message :");
160 log.debug(" - message : " + message);
161 }
162
163 Object[] listeners = null;
164
165 synchronized (listenerList) {
166 listeners = listenerList.getListeners();
167 }
168
169 if (listeners != null) {
170 int size = listeners.length;
171 for (int i = 0; i < size; i++) {
172 IGameClientListener listener = (IGameClientListener) listeners[i];
173 listener.systemMessageReceived(message);
174 }
175 }
176 }
177 }
This page was automatically generated by Maven