⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glomorunthread.java

📁 无线网络仿真工具Glomosim2.03
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;import javax.swing.*;public class GlomoRunThread extends Thread implements ActionListener {    private String executableName, traceFileName;    private GlomoMobileNode mobileNodes[];    private GlomoCanvas gCanvas;    private GlomoMenuBar menuBar;    private GlomoStatusPanel statusPanel;    private GlomoToolBarPanel toolBarPanel;    private Process proc=null;    private JFrame topFrame;    private int demoOption, magnification;    private JDialog runDialog;    private boolean nodeArraySet = false;    private int nodesSetSoFar = 0;    private boolean showDemoColorsOption;    private Hashtable demoColorHtbl;    private long currentSimTime = 0;     private boolean showTxRangeOption;    private boolean showNodeConnectionsOption;    public static final int STOP_STATE  = 0;    // suspended the simulation    public static final int RUN_STATE   = 1;    // run the simulation    public static final int STEP_STATE  = 2;    // step the simulation    public static final int ABORT_STATE = 3;    // abort the simulation    private volatile boolean initialWaiting = false;    private volatile int simState = RUN_STATE;    // used by setSimlationSpeed function    public static final int SLOWER = -2;    public static final int FASTER = -3;    private volatile int numIterations = 100;    private int speedGap = 1;    private int lastIsSpeedUp = 0;    // options for displaying simulation    public static final int REAL_TIME = -4;    public static final int WRITE_TRACE = -5;    public static final int PLAYBACK_TRACE = -6;    // options for specifying which layer of GloMoSim to display    // for now, only LAYER_RADIO and LAYER_MOBILITY_ONLY are implemented    public static final int LAYER_TCP = -7;    public static final int LAYER_MAC = -8;    public static final int LAYER_RADIO = -9;    public static final int LAYER_MOBILITY_ONLY = -10;    // different commands for drawing things on the canvas    private static final byte INITIALIZE = 0;    private static final byte MOVE_NODE = 1;    private static final byte DRAW_BROADCAST = 2;    private static final byte DRAW_LINE = 3;    private static final byte DRAW_THICKLINE = 4;    private static final byte DRAW_LINK = 5;    private static final byte ERASE_LINK = 6;    private static final Color colors[] = {        Color.black, Color.blue, Color.cyan, Color.darkGray,        Color.gray, Color.green, Color.lightGray, Color.magenta,        Color.orange, Color.pink, Color.red, Color.white, Color.yellow    };    public synchronized void setSimulationSpeed(int choice) {        if (choice == SLOWER) {            if(lastIsSpeedUp == -1){                if(speedGap < 64){                    speedGap += speedGap;                }            }else if(lastIsSpeedUp == 1){                speedGap = 1;            }            lastIsSpeedUp = -1;            if ((numIterations+speedGap) >= 2000) {                numIterations = 2000;                return;            }            else {                numIterations += speedGap;            }        }        else if (choice == FASTER) {            if(lastIsSpeedUp == 1){                if(speedGap < 64){                    speedGap += speedGap;                }            }else if(lastIsSpeedUp == -1){                speedGap = 1;            }            lastIsSpeedUp = 1;            if ((numIterations-speedGap) <= 1) {                numIterations = 1;                return;            }            else {                numIterations -= speedGap;            }        }    }    public synchronized void stopInitialWaiting () {        if (initialWaiting == true) {            initialWaiting = false;            notify();        }    }    public synchronized void abortSim() {         simState = ABORT_STATE;        notify();    }    public synchronized void pauseSim() {        simState = STOP_STATE;    }    public synchronized void resumeSim() {        simState = RUN_STATE;        notify();    }    public synchronized void stepSim() {        simState = STEP_STATE;        notify();    }    public boolean isSimPaused() {        return (simState == STOP_STATE);    }    public void setParams(String e, String t) {        executableName = e;        traceFileName = t;    }    public void setMagnification(int m) {        magnification = m;    }   public GlomoRunThread(String c, JFrame f, GlomoCanvas gc, GlomoMenuBar gmb,                          GlomoStatusPanel sp, GlomoToolBarPanel tb, int op,                          boolean stro, boolean snco,                          boolean sdco, Hashtable htbl) {        executableName = c;        topFrame = f;        gCanvas = gc;        menuBar = gmb;        statusPanel = sp;        toolBarPanel = tb;        demoOption = op;        showTxRangeOption = stro;        showNodeConnectionsOption = snco;        showDemoColorsOption = sdco;        demoColorHtbl = htbl;        runDialog = new JDialog(topFrame, "Running...", false);    }    public void run() {        GlomoMessageDialog messageDialog = new GlomoMessageDialog(topFrame);        JLabel runLabel = new JLabel("Executing " + executableName);         runDialog.setResizable(true);        runDialog.getContentPane().setLayout(new GridLayout(0, 1));        runDialog.getContentPane().add(runLabel);         JButton cancel = new JButton("Cancel");        cancel.addActionListener(this);        runDialog.getContentPane().add(cancel);        runDialog.pack();        try {            switch (demoOption) {            case WRITE_TRACE:                runDialog.setVisible(true);                proc = Runtime.getRuntime().exec(executableName);                BufferedReader brFromGlomo = new BufferedReader(                                new InputStreamReader(proc.getInputStream()));                BufferedWriter traceFile = new BufferedWriter(                                            new FileWriter(traceFileName));                String line;                while ((line = brFromGlomo.readLine()) != null) {                    traceFile.write(line);                    traceFile.newLine();                }                traceFile.close();                break;            case REAL_TIME:            case PLAYBACK_TRACE:                // clear the canvas first                 gCanvas.repaint();                BufferedReader br;                if (demoOption == REAL_TIME) {                    proc = Runtime.getRuntime().exec(executableName);                    br= new BufferedReader(                          new InputStreamReader(proc.getInputStream()));                }                else {                    br = new BufferedReader(new FileReader(traceFileName));                }                showSimulation(br);                break;            default:                    messageDialog.setMessage("", "RunThread: no such option!");            } // end switch(demoOption)        } catch (FileNotFoundException fnfe) {            messageDialog.setMessage("", "Error! File not found.");            if(proc != null){                proc.destroy();                proc = null;            }        } catch (IOException ex) {            messageDialog.setMessage("", "Error! I/O Exception occured. " +                                     "Check that GloMoSim is working properly");            if(proc != null){                proc.destroy();                proc = null;            }        } catch (InterruptedException e) {            messageDialog.setMessage("", "Error! "                                          + "Interrupted Exception occured.");         } finally {            // only displayed for the WRITE_TRACE case            runDialog.setTitle("Done");            runLabel.setText("Simulation successfully completed! " +                              "Ready for Play Back.");            cancel.setText("Ok");            runDialog.pack();            menuBar.resetMenuAndToolBars();        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -