📄 messagehandler.java
字号:
package edu.odu.cs.zeil.AlgAE.Server;import java.awt.Color;import java.io.BufferedReader;import java.io.PrintWriter;import java.util.Vector;import edu.odu.cs.zeil.AlgAE.Message;import edu.odu.cs.zeil.AlgAE.Messages;import edu.odu.cs.zeil.AlgAE.MessageIO;import edu.odu.cs.zeil.AlgAE.ParameterizedMessage;/** * Basic message handling for messages sent to/from the AlgAE client. **/public class MessageHandler{ private MessageIO msgIO; private Messages msgs; private boolean pausing; /** * Create a message handler. * * @param msgIO I/O streams for communications with the client. **/ public MessageHandler (Messages messages, MessageIO msgio) { msgs = messages; msgIO = msgio; pausing = true; } // Inputs from the client // [These may block until the client actually sends a signal. They may // also terminate the program if the client shuts down unexpectedly.] /** * After sending an EFRAME message, the Server must not send another * forcing message until a CONTINUING message is received from the * Client. * * @return true if a forcing message may be sent to the client **/ boolean canContinue() { if (continuingPermitted) { continuingPermitted = false; return true; } else { while (true) { ParameterizedMessage pmsg = msgIO.read(); if (pmsg == null) return false; if (pmsg.getMessage() == msgs.continuing) return true; if (pmsg.getMessage() == msgs.pausing) pausing = true; else if (pmsg.getMessage() == msgs.nopausing) pausing = false; } } } /** * Indicates whether client program wants to receive a full frame * description (i.e., all relevant graphics) at each FRAME. * When the user turns off "pausing" at the client, don't bother * sending data descriptions. * * @see edu.odu.cs.zeil.AlgAE.Server.MenuItem **/ boolean clientIsPausing() { return pausing; } /** * Waits for a message from the Client indicating the selection of * a registered MenuItem (a choice from the algorithm menu). * * @see edu.odu.cs.zeil.AlgAE.Server.MenuItem **/ String getMenuSelection() { while (true) { ParameterizedMessage pmsg = msgIO.read(); if (pmsg == null) return msgs.quitting.getMessageKind(); Message msg = pmsg.getMessage(); if (msg == msgs.continuing) { continuingPermitted = true; } else if (msg == msgs.itemSelected) { return (String)pmsg.getParams().elementAt(0); } else if (msg == msgs.quitting) return msgs.quitting.getMessageKind(); else if (msg == msgs.pausing) pausing = true; else if (msg == msgs.nopausing) pausing = false; } } /** * Causes a dialog box to pop up on the client display, containing * the indicated prompt string and a text field in which the operator * may type a reply. * * @param prompt String to display in the dialog box * @return The user's reply **/ String promptForInput (String prompt) throws java.io.IOException { Vector params = new Vector(); params.addElement (prompt); msgIO.send (new ParameterizedMessage(msgs.prompt, params)); ParameterizedMessage pmsg = null; while (true) { pmsg = msgIO.read(); if (pmsg == null) throw new java.io.IOException("lost connection with client"); Message msg = pmsg.getMessage(); if (msg == msgs.continuing) { continuingPermitted = true; } else if (msg == msgs.response) { return (String)pmsg.getParams().elementAt(0); } else if (msg == msgs.quitting) throw new java.io.IOException("QUIT message received from client"); else if (msg == msgs.pausing) pausing = true; else if (msg == msgs.nopausing) pausing = false; } } /** * Waits for a message indicating that the user has typed a line in * the client's std I/O window. * * @return The line typed into the std I/O window **/ String stdInput() throws java.io.IOException { msgIO.send (new ParameterizedMessage(msgs.stdin)); while (true) { ParameterizedMessage pmsg = msgIO.read(); if (pmsg == null) throw new java.io.IOException("lost connection with client"); Message msg = pmsg.getMessage(); if (msg == msgs.continuing) { continuingPermitted = true; } else if (msg == msgs.response) { return (String)pmsg.getParams().elementAt(0); } else if (msg == msgs.quitting) throw new java.io.IOException("QUIT message received from client"); else if (msg == msgs.pausing) pausing = true; else if (msg == msgs.nopausing) pausing = false; } } // Outputs to the client /** * Signal that an arrow should be drawn between two objects. * * @param sourceID ID of the object from which the arrow emerges * @param destID ID of the object the arrow points to * @param dir direction from which the arrow emerges * @param color color in which to draw the arrow * @param label string to print alongside the arrow **/ void touch (Integer sourceID, Integer destID, int dir, Color color, String label) { Vector params = new Vector(); params.addElement (sourceID); params.addElement (destID); params.addElement (new Integer(dir)); params.addElement (new Integer(color.getRed())); params.addElement (new Integer(color.getGreen())); params.addElement (new Integer(color.getBlue())); params.addElement (label); msgIO.send (new ParameterizedMessage(msgs.edge, params)); } /** * Signal that one object should be drawn inside the other. * * @param containerID ID of the containing object * @param componentID ID of the object contained within the container **/ void touch (Integer containerID, Integer componentID) { Vector params = new Vector(); params.addElement (containerID); params.addElement (componentID); msgIO.send (new ParameterizedMessage(msgs.component, params)); } /** * Signal the creation of a new visible object. * * @param objectID the unique ID for this object * @param color the "normal" or default color for this object * @param label the text to print within the object's portrayal * @param vertical If this object has internal components, should they * be stacked vertically? **/ void node (Integer objectID, Color color, String label, boolean vertical) { Vector params = new Vector(); params.addElement (objectID); params.addElement (label); params.addElement (new Integer(vertical ? 1 : 0)); params.addElement (new Integer(color.getRed())); params.addElement (new Integer(color.getGreen())); params.addElement (new Integer(color.getBlue())); msgIO.send (new ParameterizedMessage(msgs.node, params)); } /** * Signals the beginning of a picture sent after the execution of * an algorithm menu selection. */ void beginAlgorithm() { msgIO.send (new ParameterizedMessage(msgs.beginAlgorithm)); } /** * Signals the completion of a picture sent after the execution of * an algorithm menu selection. */ void endAlgorithm() { msgIO.send (new ParameterizedMessage(msgs.endAlgorithm)); } /** * Signals the beginning of a picture sent in response to a FRAME() * command encountered during execution of a selected algorithm. */ void beginFrame() { msgIO.send (new ParameterizedMessage(msgs.beginFrame)); } /** * Signals the completion of a picture sent in response to a FRAME() * command encountered during execution of a selected algorithm. * * @param frameIdentifier The identifying string used to synchronize * the source code viewer */ void endFrame(String frameIdentifier) { msgIO.send (new ParameterizedMessage(msgs.endFrame, frameIdentifier)); } /** * Asks the client to add a new entry to its algorithm menu. * * @param itemName The string to appear in the menu **/ void menuItem (String itemName) { msgIO.send (new ParameterizedMessage(msgs.menuItem, itemName)); } /** * Asks the client to change the text of its main status line * * @param statusMsg The string to place in the status line **/ void status (String statusMsg) { msgIO.send (new ParameterizedMessage(msgs.status, statusMsg)); } /** * Asks the client to change the text of its window title * * @param algName The string to place in the window title **/ void algorithmName (String algName, String about) { Vector params = new Vector(); params.addElement (algName); params.addElement (about); msgIO.send (new ParameterizedMessage(msgs.title, params)); } /** * Tells the client that this server is quitting. * **/ void quit() { msgIO.send (new ParameterizedMessage(msgs.serverQuit)); } /** * Causes a dialog box to pop up on the client display, containing * the indicated message string. * * @param msg String to display in the dialog box **/ void message (String msg) { msgIO.send (new ParameterizedMessage(msgs.message, msg)); } /** * Asks the client to write the indicated text into the std I/O window. * * @param msg text to display **/ void stdOutput (String msg) { msgIO.send (new ParameterizedMessage(msgs.stdout, msg)); } private boolean continuingPermitted = true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -