📄 client.java
字号:
/** * Client.java * * * Created: Sat Apr 25 22:14:01 1998 * * @author Steven J. Zeil * @version */package edu.odu.cs.zeil.AlgAE.Client;import edu.odu.cs.zeil.AlgAE.Client.DataViewer.DataPane;import edu.odu.cs.zeil.AlgAE.Client.SourceViewer.SourcePane;import edu.odu.cs.zeil.AlgAE.Client.IOViewer.IOPane;import edu.odu.cs.zeil.AlgAE.Application;import edu.odu.cs.zeil.AlgAE.ExternalMessageIO;import edu.odu.cs.zeil.AlgAE.Message;import edu.odu.cs.zeil.AlgAE.MessageIO;import edu.odu.cs.zeil.AlgAE.Messages;import edu.odu.cs.zeil.AlgAE.MessageListener;import edu.odu.cs.zeil.AlgAE.Client.MessageDispatcher;import edu.odu.cs.zeil.AlgAE.Client.PerpetualReader;import edu.odu.cs.zeil.AlgAE.Client.ViewerPanel;import edu.odu.cs.zeil.AlgAE.Application;import edu.odu.cs.zeil.AlgAE.Debug;import java.applet.Applet;import java.awt.Button;import java.awt.BorderLayout;import java.awt.Frame;import java.awt.Menu;import java.awt.MenuBar;import java.awt.Panel;import java.awt.PopupMenu;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.Reader;import java.net.InetAddress;import java.net.Socket;import java.util.Hashtable;import java.util.Vector;public class Client extends Applet { private MessageIO msgIO; private MessageIO logOut; private MessageDispatcher dispatcher; private ViewerPanel dataViewer; private Hashtable commandArguments; private Application application; private PopupMenu popupMenu; int framePause = -1; int algorithmPause = -1; public Client(Application appl, Hashtable commArgs) { application = appl; popupMenu = null; init (commArgs, null); } public Client(Application appl, Hashtable commArgs, MessageIO msgio) { application = appl; popupMenu = null; init (commArgs, msgio); } private void init (Hashtable commandArguments, MessageIO msgio) { Debug.show (Debug.threads, "Entering Client.init"); Reader inStream = null; PrintWriter outStream = null; msgIO = msgio; int passes = 100; String passesArg = (String)commandArguments.get("passes"); if (passesArg != null) try { Integer fsize = new Integer (passesArg); passes = fsize.intValue(); } catch (NumberFormatException e) {} String fileName = (String)commandArguments.get("infile"); Debug.show (Debug.threads, "infile = " + fileName); if (fileName == null) inStream = new BufferedReader (new InputStreamReader (System.in)); else try { inStream = new PerpetualReader(fileName, passes); application.inReader = inStream; } catch (java.io.FileNotFoundException e) { Debug.error ("Unable to open " + fileName); System.exit(0); } catch (java.io.IOException e) { Debug.error ("Unable to open " + fileName); System.exit(0); } fileName = (String)commandArguments.get("outfile"); Debug.show (Debug.threads, "outfile = " + fileName); if (fileName == null) outStream = null; else if (fileName == "-") outStream = new PrintWriter (System.out); else try { outStream = new PrintWriter(new BufferedWriter (new FileWriter(fileName))); application.outWriter = outStream; } catch (java.io.IOException e) { System.out.println ("Unable to open " + fileName); System.exit(0); } String ipAddress = (String)commandArguments.get("host"); String socketNumStr = (String)commandArguments.get("port"); Debug.show (Debug.threads, "ipAddress = " + ipAddress); Debug.show (Debug.sockets, "socketNumStr = " + socketNumStr); if ((ipAddress != null) && (socketNumStr != null)) { Debug.show (Debug.sockets, "socketNumStr = " + socketNumStr); try { Integer socketNum = new Integer (socketNumStr); InetAddress addr = InetAddress.getByName(ipAddress); Debug.show (Debug.sockets, "addr = " + addr); Socket socket = new Socket(addr, socketNum.intValue()); Debug.show (Debug.sockets, "socket = " + socket); inStream = new BufferedReader (new InputStreamReader (socket.getInputStream())); // application.register (inStream); outStream = new PrintWriter (new BufferedWriter (new OutputStreamWriter(socket.getOutputStream())),true); application.outWriter = outStream; } catch (java.io.IOException e) { System.out.println ("Unable to open socket " + socketNumStr + " at IP address " + ipAddress); System.exit(0); } } if ((msgIO == null) && (inStream == null)) { System.out.println ("You must specify either an IP address and port or in/out files"); System.exit(0); } if (msgIO == null) msgIO = new ExternalMessageIO(inStream, outStream, application); logOut = null; fileName = (String)commandArguments.get("logfile"); if (fileName != null) try { PrintWriter logStream = new PrintWriter (new BufferedWriter (new FileWriter(fileName))); logOut = new ExternalMessageIO(null, logStream, application); application.logStream = logStream; } catch (java.io.IOException e) { System.out.println ("Unable to open log file " + fileName); System.exit(0); } fileName = (String)commandArguments.get("debug"); if (fileName == null) Debug.out = new PrintWriter (System.out); else try { Debug.out = new PrintWriter (new BufferedWriter (new FileWriter(fileName))); application.debugOut = Debug.out; } catch (java.io.IOException e) { System.out.println ("Unable to open " + fileName); System.exit(0); } Debug.show (Debug.threads, "Client.init: about to create dispatcher"); dispatcher = new MessageDispatcher (msgIO, logOut); String layoutMode = (String)commandArguments.get("layout"); boolean tallGraphics = (layoutMode == null) || layoutMode.toLowerCase().equals("tall"); String autoArrangeMode = (String)commandArguments.get("auto"); boolean doAutoArrange = (autoArrangeMode == null) || autoArrangeMode.toLowerCase().equals("on"); String appletMode = (String)commandArguments.get("applet"); boolean appletPanel = (appletMode != null) && appletMode.toLowerCase().equals("panel"); String fontSizeArg = (String)commandArguments.get("fontsize"); int initialFontSize = 12; if (fontSizeArg != null) try { Integer fsize = new Integer (fontSizeArg); initialFontSize = fsize.intValue(); } catch (NumberFormatException e) {} String incrStepsArg = (String)commandArguments.get("incrSteps"); if (fontSizeArg != null) try { Integer fSteps = new Integer (incrStepsArg); int fs = fSteps.intValue(); dispatcher.getGraphics().setInterpolation (fs+1, fs); } catch (NumberFormatException e) {} String framePauseArg = (String)commandArguments.get("framepause"); if (framePauseArg != null) try { Integer fsize = new Integer (framePauseArg); framePause = fsize.intValue(); } catch (NumberFormatException e) {} String algorithmPauseArg = (String)commandArguments.get("algorithmpause"); if (algorithmPauseArg != null) try { Integer fsize = new Integer (algorithmPauseArg); algorithmPause = fsize.intValue(); } catch (NumberFormatException e) {} String helpCommand = (String)commandArguments.get("help"); Debug.show (Debug.threads, "Client.init: about to create window"); Vector menuList = new Vector(); Button menusButton = null; /* Not sure if I like this feature. I can always put it back. if (appletPanel) menusButton = new Button("menus"); */ DataPane dataPane = new DataPane(application, dispatcher); SourcePane sourcePane = new SourcePane(commandArguments); dataViewer = new ViewerPanel ("AlgAE", application, dataPane, sourcePane, new IOPane(application, dispatcher), dispatcher, !tallGraphics, doAutoArrange, initialFontSize, helpCommand, framePause, algorithmPause, menusButton, menuList); if (appletPanel) { application.applet.setLayout (new BorderLayout()); application.applet.add (dataViewer, "Center"); popupMenu = new PopupMenu("AlgAE"); for (int i = 0; i < menuList.size(); ++i) popupMenu.add ((Menu)menuList.elementAt(i)); application.applet.add (popupMenu); KeyAdapter shortcuts = new KeyAdapter() { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); Debug.show (Debug.events, "Client Key event: " + e); if (e.isAltDown()) { if (c == 'm') popupMenu.show(application.applet, 10, 10); } } }; dataPane.addKeyListener(shortcuts); sourcePane.addKeyListener(shortcuts); /* menusButton.addActionListener (new ActionListener () { public void actionPerformed(ActionEvent e) { popupMenu.show(application.applet, 10, 10); } } ); */ dataPane.getCanvas().addMouseListener (new MouseAdapter () { public void mouseClicked (MouseEvent e) { System.out.println ("Mouse: " + e); if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK) { // This was not the left button. popupMenu.show(application.applet, 10, 10); } } } ); application.applet.invalidate(); application.applet.validate(); dispatcher.getGraphics().invalidate(); application.applet.repaint(); } else { MenuBar menuBar = new MenuBar(); for (int i = 0; i < menuList.size(); ++i) menuBar.add ((Menu)menuList.elementAt(i)); application.dataWindow = new Frame(); Debug.theFrame = application.dataWindow; application.dataWindow.add ("Center", dataViewer); application.dataWindow.setMenuBar (menuBar); application.dataWindow.pack(); application.dataWindow.show(); application.dataWindow.invalidate(); application.dataWindow.validate(); dispatcher.getGraphics().invalidate(); application.dataWindow.repaint(); application.dataWindow.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispatcher.exit(); application.exit (0); } }); } application.msgs.quitting.addMessageListener (new MessageListener() { public void messageReceived (String kind, Vector params) { dispatcher.exit(); application.exit (0); } }); /* application.msgs.beginFrame.addMessageListener (new MessageListener() { public void messageReceived (String kind, Vector params) { dispatcher.denyContinuation(); } }); application.msgs.beginAlgorithm.addMessageListener (new MessageListener() { public void messageReceived (String kind, Vector params) { dispatcher.denyContinuation(); } }); */ Debug.show (Debug.threads, "Client.init: about to launch dispatcher"); // application.register (dispatcher); dispatcher.start(); Debug.show (Debug.threads, "Client.init: completed"); } public static void main(String[] args) { Hashtable arguments = Application.parseCommandLine(args); Client client = new Client (new Application(), arguments); } /** * Constructor used for applets only. */ public Client() { application = new Application(); application.applet = this; popupMenu = null; } private String[][] pinfo = { {"host", "ip-name", "name of machine on which server is running"}, {"port", "integer", "socket # at which to contact the server host"}, {"infile", "url", "input file of previously recorded AlgAE messages"}, {"outfile", "url", "output file for messages from AlgAE client"}, {"logfile", "url", "output file for logging messages for later playback"}, {"debug", "url", "output file for logging debugging info"}, {"layout", "wide | tall", "Should graphics fill window's width or height?"}, {"auto", "on | off", "Should objects be rearranged automatically after each change?"}, {"applet", "panel | window", "Should applet be run as a separate window?"}, {"fontsize", "int", "initial font size for client data/code display"}, {"framepause", "int", "time to pause between frames in replay mode"}, {"passes", "int", "number of passes to make over input file in replay mode"}, {"algorithmpause", "int", "time to pause between algorithms in replay mode"}, {"help", "url", "location of help files"} }; public void init() { Hashtable args = new Hashtable(); for (int i = 0; i < pinfo.length; ++i) { String val = getParameter(pinfo[i][0]); if (val != null) { args.put (pinfo[i][0], val); } } int fileNum = 0; while (true) { String number = "" + fileNum; String name = "source" + number; String val = getParameter(name); if (val != null) { args.put (name, val); } else break; name = "index" + number; val = getParameter(name); if (val != null) { args.put (name, val); } ++fileNum; } try { init(args, null); } catch (Exception e) {destroy();} } public void stop() { Debug.show (Debug.events, "stopping applet"); dispatcher.exit(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -