import java.util.Stack; /** * ChatData provides the control data and access methods to allow the * different threads to exchange information. For example, the user * interface passes an outbound message to the sender thread to be * forwarded to the JMS topic. * Conversely, the receiver thread passes an inbound message to the * user interface to be displayed. */ public class ChatData { private boolean exitFlag; private boolean receiverConnected; private boolean senderConnected; private boolean dataToTopic; private boolean dataFromTopic; private String incomingMessage; private String outgoingMessage; /** * Class constructor. */ public ChatData() { exitFlag = false; receiverConnected = false; senderConnected = false; dataToTopic = false; dataFromTopic = false; incomingMessage = null; outgoingMessage = null; } /** * Wait for the JMS connections to be established. */ public synchronized void waitUntilConnected() { // Wait until sender and receiver connected while ((senderConnected == false) || (receiverConnected == false)) { try { wait(); } catch (InterruptedException e) { System.out.println("Exception occurred: " + e.toString()); } } notifyAll(); } /** * Indicate that the receiver (subscriber) thread has * established its JMS connection. */ public synchronized void receiverConnected() { receiverConnected = true; notifyAll(); } /** * Indicate that the sender (publisher) thread has * established its JMS connection. */ public synchronized void senderConnected() { senderConnected = true; notifyAll(); } /** * Retrieve a message received from the JMS topic. * Called by the user interface to get an inbound * message from the receiver thread. * * @return string containing the stored JMS message * @exception InterruptedException if interrupted */ public synchronized String retrieveInbound() throws InterruptedException { // Only get message when there is one while (dataFromTopic == false) { wait(); } dataFromTopic = false; notifyAll(); return incomingMessage; } /** * Store a message received from the JMS topic. * Called by the receiver thread to pass an inbound * message to the user interface. * * @param text the message to be stored * @exception InterruptedException if interrupted */ public synchronized void storeInbound(String text) throws InterruptedException { // Only store message if room while (dataFromTopic == true) { wait(); } dataFromTopic = true; this.incomingMessage = text; notifyAll(); } /** * Retrieve a message received from the user interface. * Called by the sender thread to get an outbound * message from the user interface. * * @return string containing the stored user input * @exception InterruptedException if interrupted */ public synchronized String retrieveOutbound() throws InterruptedException { // Only get message when there is one while (dataToTopic == false) { wait(); } dataToTopic = false; notifyAll(); return outgoingMessage; } /** * Store a message received from the user interface. * Called by the user interface to pass an outbound * message to the sender thread. * * @param text the message to be stored */ public synchronized void storeOutbound(String text) { // Only store message if room while (dataToTopic == true) { try { wait(); } catch (InterruptedException e) { System.out.println("Exception occurred: " + e.toString()); } } dataToTopic = true; this.outgoingMessage = text; notifyAll(); } /** * Exit processing. Set flag to indicate it is time to exit. */ public synchronized void setExitFlag() { exitFlag = true; } /** * Test the value of the exit flag. * * @return the boolean flag which is true if it is time to exit. */ public synchronized boolean timeToExit() { return exitFlag; } }