-
Notifications
You must be signed in to change notification settings - Fork 0
/
JmsClient.java
381 lines (325 loc) · 10.4 KB
/
JmsClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.sun.messaging.jmq.jmsclient.TextMessageImpl;
public class JmsClient {
private String host;
private Context jndiContext;
private ConnectionFactory connectionFactory;
private Connection conn;
private Session sess;
private LinkedList<String> localActiveGames;
private Queue playerQueue;
private Queue gameQueue;
private Queue activeGameQueue;
private Queue gameInputQueue;
private Queue gameResultQueue;
private MessageProducer playerQueueSender;
private MessageProducer gameInputSender;
private MessageConsumer queueReceiver;
public Game game;
public JmsClient(String host) { // TODO: consider passing Player object reference
this.host = host;
localActiveGames = new LinkedList<String>();
}
public boolean initialise() {
// Access JNDI
try {
game = null;
createJNDIContext();
// Lookup JMS resources
lookupConnectionFactory();
lookupQueues();
// Create connection->session->sender
createConnection();
createSession();
return true;
} catch (NamingException | JMSException e) {
// TODO Auto-generated catch block
System.out.println("Failed to create connection to Glassfish JMS instance");
e.printStackTrace();
return false;
}
}
/*
* Append a playerID to the game list
*/
public void enqueuePlayer(Player player) throws JMSException {
createPlayerQueueSender();
TextMessage msg = sess.createTextMessage();
msg.setText(player.name + "," + player.id);
playerQueueSender.send(msg);
// send non-text control message to end
// playerQueueSender.send(sess.createMessage());
}
/*
* Append a playerID to the game list
*/
public void submitGameAnswer(Player p, Game g, String answer) throws JMSException {
// Make sure player is in game.
Player[] plys = g.getPlayers();
boolean fraud = true;
for (int i = 0; i < 4; i++) {
if (plys[i].id.equals(p.id)) {
fraud = false;
}
}
if (fraud == true) {
System.err.println("Error, trying to submit answer for game you are not partaking in.");
return;
}
// good to go
createGameInputSender();
TextMessage msg = sess.createTextMessage();
msg.setText(p.id + "," + g.getId() + "," + answer);
System.out.println("uploading gameAnswer: " + msg.getText());
gameInputSender.send(msg);
// send non-text control message to end
// playerQueueSender.send(sess.createMessage());
}
public Game decodeGameMessage(String gameMessage) {
System.out.println("Decoding: " + gameMessage);
String thisGame = gameMessage;
// Decode the message
String[] values = thisGame.split(",");
String gameId = values[0];
String[] playerRaw = values[1].split("&");
String[] cards = values[2].split("&");
// Each row is a player, each of the two cells are id and name respectively.
String[][] playerInfo = new String[playerRaw.length][2];
Player[] players = new Player[playerRaw.length];
// Input format is "id-name"
for (int i = 0; i < playerRaw.length; i++) {
playerInfo[i] = playerRaw[i].split("-");
players[i] = new Player(playerInfo[i][1], playerInfo[i][0]);
System.out.println("Player in this game: " + playerInfo[i][1].toString());
}
Game g = new Game(players[0]);
g.setId(gameId);
g.setCards(cards);
// j = 1, because the first player was already passed
for (int j = 1; j < playerRaw.length; j++) {
g.setPlayer(j, players[j]);
}
return g;
}
public Game findActiveGameWithPlayer(Player player) throws JMSException {
// TODO: utilise the general method "observerqueue" here
// Read queue without consuming messages
QueueBrowser queueBrowser = sess.createBrowser(activeGameQueue);
Enumeration msgs = queueBrowser.getEnumeration();
// Get every game in the list
while (msgs.hasMoreElements()) {
String thisGame;
try {
thisGame = ((TextMessageImpl) msgs.nextElement()).getText();
} catch (ClassCastException e) {
System.err.println("Tried to parse a message that was not text-based.");
thisGame = null;
continue; // Skip this iteration
}
//System.out.println(thisGame);
Game g = decodeGameMessage(thisGame);
// For this game, we check if any of the players match the player we are
// searching for
for (int i = 0; i < 4; i++) {
//System.out.println(g.getPlayers()[i].id);
if (g.getPlayers()[i].id.equals(player.id)) {
// ding ding!
System.out.println("Player: " + player.id + " has found their game, and "
+ "it is active, and ready to be played.");
// game is good to go!
return g;
}
}
}
return null;
}
/*
* Get a game
*/
public Game getGame(Player player) throws JMSException, InterruptedException {
if (game == null) {
// Add player to the list of seeking players
enqueuePlayer(player);
System.out.println("GettingsGame for: " + player.name);
// Every 1 second, try and read available game list
while (game == null) {
System.out.println("Checking activeGame list...");
game = findActiveGameWithPlayer(player);
TimeUnit.SECONDS.sleep(1);
}
}
return game;
}
private void createJNDIContext() throws NamingException {
try {
System.setProperty("org.omg.CORBA.ORBInitialHost", host);
System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
jndiContext = new InitialContext();
} catch (NamingException e) {
System.err.println("Could not create JNDI API context: " + e);
throw e;
}
}
private void lookupConnectionFactory() throws NamingException {
try {
connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/GameConnectionFactory");
} catch (NamingException e) {
System.err.println("JNDI API JMS connection factory lookup failed: " + e);
throw e;
}
}
private void lookupQueues() throws NamingException {
try {
playerQueue = (Queue) jndiContext.lookup("jms/PlayerQueue");
gameQueue = (Queue) jndiContext.lookup("jms/GameQueue");
gameInputQueue = (Queue) jndiContext.lookup("jms/GameInputQueue");
activeGameQueue = (Queue) jndiContext.lookup("jms/ActiveGameQueue");
gameResultQueue = (Queue) jndiContext.lookup("jms/GameResultQueue");
} catch (NamingException e) {
System.err.println("JNDI API JMS queue lookup failed: " + e);
throw e;
}
}
/*
* Parametrised, specify the JMS queue to pull from, as well as the local queue
* to store results in
*/
public void receiveMessages(Queue relevantQueue, LinkedList<String> localQueue) throws JMSException {
createReceiver(relevantQueue);
while (true) {
// The library is designed to hang forever on this until a message is received.
Message m = queueReceiver.receive(2);// Timeout of x seconds
if (m != null && m instanceof TextMessage) {
TextMessage textMessage = (TextMessage) m;
System.out.println("Received message: " + textMessage.getText());
localQueue.add(textMessage.getText());
} else {
queueReceiver.close();
break;
}
}
}
public GameResult awaitResult(Player player, Game game) {
while(true) {
System.out.println("Awaiting result...");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LinkedList<String> gameResultMsgs = null;
try {
gameResultMsgs = observeMessages(gameResultQueue);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Check every result string
for (int i = 0; i < gameResultMsgs.size(); i++) {
System.out.println("Observed GameResult: "+gameResultMsgs.get(i));
String[] components = gameResultMsgs.get(i).split(",");
// If the posted result matches this game
if (components[0].equals(game.getId())) {
// Add the winners from the message to the GameResult
String[] winners = components[1].split("&");
GameResult gR = new GameResult(game, winners);
/*
for(int j = 0; j < winners.length; j++) {
gR.addWinner(winners[j]);
}*/
// Only happens once a matching result has been found
return gR;
}
}
}
}
/*
* Read queue messages WITHOUT consuming them.
*/
public LinkedList<String> observeMessages(Queue relevantQueue) throws JMSException {
// Read queue without consuming messages
QueueBrowser queueBrowser = sess.createBrowser(relevantQueue);
Enumeration msgs = queueBrowser.getEnumeration();
LinkedList<String> msgsReturn = new LinkedList<String>();
// Get every game in the list
while (msgs.hasMoreElements()) {
String thisMsg;
try {
thisMsg = ((TextMessageImpl) msgs.nextElement()).getText();
msgsReturn.add(thisMsg);
} catch (ClassCastException e) {
System.err.println("Tried to parse a message that was not text-based.");
thisMsg = null;
continue; // Skip this iteration
}
}
return msgsReturn;
}
private void createReceiver(Queue relevantQueue) throws JMSException {
try {
queueReceiver = sess.createConsumer(relevantQueue);
} catch (JMSException e) {
System.err.println("Failed to create session: " + e);
throw e;
}
}
private void createConnection() throws JMSException {
try {
conn = connectionFactory.createConnection();
conn.start();
} catch (JMSException e) {
System.err.println("Failed to create connection to JMS provider: " + e);
throw e;
}
}
private void createSession() throws JMSException {
try {
sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
System.err.println("Failed to create session: " + e);
throw e;
}
}
// Was: QueueSender
private void createPlayerQueueSender() throws JMSException {
try {
playerQueueSender = sess.createProducer(playerQueue);
} catch (JMSException e) {
System.err.println("Failed to create session: " + e);
throw e;
}
}
// Was: QueueSender
private void createGameInputSender() throws JMSException {
try {
gameInputSender = sess.createProducer(gameInputQueue);
} catch (JMSException e) {
System.err.println("Failed to create session: " + e);
throw e;
}
}
public void close() {
if (conn != null) {
try {
conn.close();
} catch (JMSException e) {
}
}
}
}