📄 server.java
字号:
package edu.odu.cs.zeil.AlgAE.Server;import java.awt.Color;import java.io.InputStream;import java.io.PrintStream;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import edu.odu.cs.zeil.AlgAE.Application;import edu.odu.cs.zeil.AlgAE.MessageIO;import edu.odu.cs.zeil.AlgAE.Server.MenuFunction;import edu.odu.cs.zeil.AlgAE.Server.MenuGenerator;import edu.odu.cs.zeil.AlgAE.Server.MessageHandler;import edu.odu.cs.zeil.AlgAE.Server.VisibleRendering;public class Server{ /** * Pair of IO streams for sending messages to the client. */ private MessageIO msgio; /** * Application-wide information. */ private Application algae; /** * Message send/receive facility */ private MessageHandler msgHandler; /** * Table of all objects that have had <CODE>show()</CODE> * called upon them (and not a subsequent <CODE>hide()</CODE>). **/ private Vector objectsToShow; /** * Queue used to traverse graph of showable objects. */ OneTimeQueue touchQueue; /** * Constructor for AlgAE server. * @param inApplet true if this is being launched from an applet * @param commArgs table of command parameters * @param inMsgs input stream for reading messages from the client * @param outMsgs output stream for sending messages to the client */ public Server(Application applic, MessageIO msgIO) { algae = applic; msgHandler = new MessageHandler (algae.msgs, msgIO); algorithmsMenu = new Hashtable(); algorithmsMenuList = new Vector(); msgio = msgIO; objectsToShow = new Vector(); algae.msgIO = msgio; in = new edu.odu.cs.zeil.AlgAE.Server.InputStream(msgHandler); out = new PrintStream (new edu.odu.cs.zeil.AlgAE.Server.OutputStream(msgHandler), true); algae.inStream = in; algae.outStream = out; } /** * Declare a menu item to appear in the client's Algorithm menu * and a corresponding function to be executed here on the server * in response to a selection of that item. * * @param menuItemTitle The string to appear in the client's menu * @param theOperation code to invoke when the menu item is selected **/ public void menuItem (String menuItemTitle, MenuFunction theOperation) { algorithmsMenu.put (menuItemTitle, theOperation); algorithmsMenuList.addElement (menuItemTitle); } /** * Start the client and begin running the animation server. * @param animationTitle string to place in client's window title bar **/ public void run(String animationTitle, MenuGenerator menuGenerator) { msgHandler.algorithmName (animationTitle, menuGenerator.about()); menuGenerator.menu(); // Send the menu list to the client for (int i = 0; i < algorithmsMenuList.size(); ++i) { String itemName = (String)algorithmsMenuList.elementAt(i); msgHandler.menuItem (itemName); } algorithmsMenuList = null; // Main loop: process client's menu selections String menuSelection = ""; while (!menuSelection.equals("QUIT")) { msgHandler.beginAlgorithm(); describeData(); msgHandler.status ("Select an algorithm"); msgHandler.endAlgorithm(); menuSelection = msgHandler.getMenuSelection (); invoke (menuSelection); } } /** * Return the message handler for this server. **/ public MessageHandler getMessageHandler() { return msgHandler; } /** * @return set of all objects that need to be shown in a frame. */ public Vector getObjectsToShow() {return objectsToShow;} /** * Signal that a frame should be drawn, and the algorithm paused * until the client gives a "continue" signal. * * @param frameidentifier Unique string identifying this code location. * @param message String to display in client's status line **/ public void FRAME (String frameIdentifier, String message) { msgHandler.beginFrame(); msgHandler.status (message); if (msgHandler.clientIsPausing()) describeData(); // Make sure we have permission to go on if (!msgHandler.canContinue()) { algae.exit(0); } msgHandler.endFrame(frameIdentifier); } /** * Signal that a frame should be drawn, and the algorithm paused * until the client gives a "continue" signal. * * @param frameidentifier Unique string identifying this code location. * @param message String to display in client's status line. * Also used as the frame identifier. **/ public void FRAME (String message) { FRAME (message, message); } /** * Pop up a dialog box requesting some input from the person operating * the client. * * @param prompt String to display in the dialog box * @return the user's response. **/ public String promptForInput (String prompt) { try { String response = msgHandler.promptForInput (prompt); return response; } catch (java.io.IOException e) { algae.exit(0); } return ""; } /** * Pop up a dialog box presenting some message to the person operating * the client. * * @param prompt String to display in the dialog box **/ public void message (String msg) { msgHandler.message (msg); } /** * Restore any highlighted nodes and edges to their "normal" colors. * **/ public void unHighlightAll() { VisibleRendering.unHighlightAll(); } /** * Replacement for System.in - reads information from the Client's * std I/O window. **/ public InputStream in; /** * Replacement for System.out - sends information to the Client's * std I/O window. **/ public PrintStream out; /** * Internal function used for issuing error messages. **/ private void error (String msg) { System.err.println (msg); algae.exit (1); } /** * Internal function: sends a picture of the current data state * to the client for portrayal. **/ private void describeData() { // Inform the client of all the nodes. for (Enumeration e = showingObjects(); e.hasMoreElements(); ) { VisibleRendering v = (VisibleRendering)e.nextElement(); // Describe the node to the client String text = v.renderingOf.getAlgAEText(); String name = v.getName(); if (name != null) text = name + ": " + text; msgHandler.node (new Integer(v.getID()), v.color(), text, v.getVertical()); // Now, describe the edges and compound components. v.renderingOf.touchAllPointers(); v.renderingOf.touchAllComponents(); } } class VisibleEnumeration implements Enumeration { VisibleEnumeration () { touchQueue = new OneTimeQueue(); // Start by queuing up the nodes that have had show() calls for (int i = 0; i < objectsToShow.size(); ++i) { VisibleRendering v = (VisibleRendering)objectsToShow.elementAt(i); touchQueue.addToRear (v); } } public boolean hasMoreElements() { return !touchQueue.isEmpty(); } public Object nextElement() { VisibleRendering v = (VisibleRendering)touchQueue.getFront(); touchQueue.removeFront(); return v; } } /** * Permit iteration over all objects that have had <CODE>show()</CODE> * called upon them (and not a subsequent <CODE>hide()</CODE>), or are * reachable from such objects via <CODE>touch()</CODE> calls. **/ Enumeration showingObjects() { return new VisibleEnumeration(); } /** * Indicates that the object vr and any objects reachable from it * via the <code>touchAllPointers</code> and * <code>touchAllComponents</code> activities should be portrayed in * the animation. * * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllPointers * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllComponents **/ public void show(VisibleRendering vr) { objectsToShow.addElement (vr); } /** * Indicates that the object vr should only be portrayed in the * animation if it can be reached from another portrayed object * via the <code>touchAllPointers</code> and * <code>touchAllComponents</code> activities. * * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllPointers * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllComponents **/ public void hide(VisibleRendering vr) { objectsToShow.removeElement (vr); } /** * Indicates that the object vr and any objects "show"n after it * should only be portrayed in the * animation if they can be reached from another portrayed object * via the <code>touchAllPointers</code> and * <code>touchAllComponents</code> activities. * * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllPointers * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllComponents **/ public void hideAllSince(VisibleRendering vr) { int i = objectsToShow.indexOf(vr); if (i >= 0) objectsToShow.setSize (i); } /** * Collection of algorithm menu items. **/ private Hashtable algorithmsMenu; /** * Ordered list of menu titles **/ private Vector algorithmsMenuList; private void invoke (String title) { Object theOp = algorithmsMenu.get(title); if (theOp != null) { MenuFunction mf = (MenuFunction)theOp; mf.selected(); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -