1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package aigames.soccer.application;
21
22 import aigames.soccer.InvalidMoveException;
23 import aigames.soccer.Move;
24
25 import org.apache.log4j.Logger;
26
27 import org.dom4j.Document;
28 import org.dom4j.Element;
29
30 import snifos.common.UserId;
31
32 import java.util.ArrayList;
33 import java.util.Date;
34 import java.util.Iterator;
35 import java.util.List;
36
37
38 /***
39 * This class describes message passed from communication module. It should be
40 * compatible with AI Soccer protocol.
41 * @version $Id: Message.java,v 1.9 2004/05/08 21:55:27 mwerla Exp $
42 */
43 public class Message {
44 /*** HELLO message type */
45 public static final int HELLO = 1;
46
47 /*** MOVE message type */
48 public static final int MOVE = 2;
49
50 /*** PARAMS message type */
51 public static final int PARAMS = 3;
52
53 /*** LOG message type */
54 public static final int LOG = 4;
55
56 /*** GAME_OVER message type */
57 public static final int GAME_OVER = 5;
58
59 /*** CONFIRMATION message type */
60 public static final int CONFIRMATION = 6;
61
62 /*** UNKNOWN message type */
63 public static final int UNKNOWN = -1;
64 private Document message;
65 private UserId userId;
66 private int messageType = UNKNOWN;
67 private int messageId = -1;
68 private String messageIdAsString = "-1";
69 private String lastValidationReason;
70 private Date sendDate = null;
71 private int resendCounter;
72 private Move move;
73
74 /***
75 * Message constructor.
76 * @param userId Id of user which sent the message.
77 * @param message Sent message.
78 */
79 public Message(UserId userId, Document message) {
80 this.userId = userId;
81 this.message = message;
82
83 String temp = message.valueOf("//message/@id");
84
85 if (temp != null) {
86 messageId = Integer.parseInt(temp);
87 messageIdAsString = temp;
88 }
89
90 String type = message.valueOf("//message/@type");
91
92 if ("confirmation".equals(type)) {
93 messageType = CONFIRMATION;
94 } else if ("move".equals(type)) {
95 messageType = MOVE;
96
97 List moves = new ArrayList();
98
99 for (Iterator i = message.getRootElement().elementIterator("type");
100 i.hasNext();) {
101 Element singleStep = (Element) i.next();
102 moves.add(new Integer(Integer.parseInt(
103 singleStep.getStringValue())));
104 }
105
106 try {
107 move = new Move((Integer[]) moves.toArray(new Integer[0]));
108 } catch (InvalidMoveException e) {
109 Logger.getRootLogger().error(e.getMessage(), e);
110 }
111 } else if ("log".equals(type)) {
112 messageType = LOG;
113 } else if ("params".equals(type)) {
114 messageType = PARAMS;
115 } else if ("hello".equals(type)) {
116 messageType = HELLO;
117 } else if ("game-over".equals(type)) {
118 messageType = GAME_OVER;
119 }
120 }
121
122 /***
123 * Returns validation result - can be used after calling
124 * {@link Message#validate(Validator)
125 * validate(Validator)} method.
126 * @return Validation result.
127 */
128 public String getLastValidationReason() {
129 return lastValidationReason;
130 }
131
132 /***
133 * Returns unique message id.
134 * @return Unique message id.
135 */
136 public int getMessageId() {
137 return messageId;
138 }
139
140 /***
141 * Return message type.
142 * @return Message type.
143 *
144 */
145 public int getMessageType() {
146 return messageType;
147 }
148
149 /***
150 * Returns Document with message body.
151 * @return Document with message body.
152 */
153 public Document getMessage() {
154 return message;
155 }
156
157 /***
158 * Validates message with given validator.
159 * @param validator Validator that should be used to validate message.
160 * @return True if message is valid, otherwise false.
161 */
162 public boolean validate(Validator validator) {
163 boolean validationResult = false;
164 lastValidationReason = "Message is not compatible with protocol!";
165
166 try {
167 validationResult = validator.validate(message);
168 } catch (Exception e) {
169 lastValidationReason = e.getMessage();
170 }
171
172 return validationResult;
173 }
174
175 /***
176 * Returns user id of a user which send the message.
177 * @return User id.
178 */
179 public UserId getUserId() {
180 return userId;
181 }
182
183 /***
184 * Returns string representation of user id.
185 * @return String representation of user id
186 */
187 public String getMessageIdAsString() {
188 return messageIdAsString;
189 }
190
191 /***
192 * Returns date when the message was sent.
193 * @return Date when the message was sent.
194 */
195 public Date getSendDate() {
196 return sendDate;
197 }
198
199 /***
200 * Sets message send date.
201 * @param sendDate The send date to set.
202 */
203 public void setSendDate(Date sendDate) {
204 this.sendDate = sendDate;
205 }
206
207 /***
208 * Increases counter which shows how many times message was resend.
209 */
210 public void incResendCounter() {
211 resendCounter++;
212 }
213
214 /***
215 * Returns move. It should be used only if message type is MOVE.
216 * @return Returns the move.
217 */
218 public Move getMove() {
219 return move;
220 }
221 }