sim.java

来自「一个小型网络仿真器的实现」· Java 代码 · 共 586 行 · 第 1/2 页

JAVA
586
字号
/*
   JaNetSim  ---  Java Network Simulator
   -------------------------------------

   This software was developed at the Network Research Lab, Faculty of
   Computer Science and Information Technology (FCSIT), University of Malaya.
   This software may be used and distributed freely. FCSIT assumes no responsibility
   whatsoever for its use by other parties, and makes no guarantees, expressed or
   implied, about its quality, reliability, or any other characteristic.

   We would appreciate acknowledgement if the software is used.

   FCSIT ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION AND
   DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
   FROM THE USE OF THIS SOFTWARE.
*/

package janetsim;

import java.io.*;

public class Sim implements Runnable {

///////////////// all tunable constants are here ////////////////////////

//Note: MUST recompile everything if these constants change...

  public static final String SIM_NAME="JaNetSim";
  public static final String SIM_VERSION="0.63";

  public static final String SIM_CONFIG_FILE="janetsim.cfg";
  public static final String SIM_PACKAGE="janetsim";
  public static final String SIM_COMP_PACKAGE="janetsim.component";

  public static final int DEFAULT_COMP_WIDTH = 50;
  public static final int DEFAULT_COMP_HEIGHT = 20;
  public static final int DEFAULT_COMP_POINTSIZE = 12;
  public static final int METER_MINWIDTH = 150;
  public static final int METER_MINHEIGHT = 100;

  public static final long DEFAULT_YIELD = 9;
  public static final long BATCH_YIELD = 50000;
  public static final long GUI_PROBE_THRESHOLD = 50;

  public static final int MAX_DELAY = 83; //max animation delay (12 fps);
  public static final int OPTIMUM_DELAY = 10; //optimum animation delay (100 fps);

////////////////////// end tunable constants ////////////////////////////


  private SimGUI theGUI=null; //GUI manager

  private java.util.List eventQueue=null; //the global event queue
  private java.util.List components=null; //all SimComponents in here

  private long currentTick=0; //the global time (in ticks)
  private boolean startState=true; //state in the beginning

  private Thread eventDispatcher=null; //the main simulation thread

  private boolean isPausing=false; //thread control
  private boolean isResetting=false; //thread control
  private int GUIprobing=0; //GUI thread synchronization

  private long stopTick=Long.MAX_VALUE; //used by non-GUI sim
  private boolean simEnd=false; //used by non-GUI sim

  Sim(boolean isGUI) {
    eventQueue=new java.util.ArrayList();
    components=new java.util.ArrayList();

    currentTick=0;
    startState=true;

    SimLog.setWriter(SIM_PACKAGE+".log",false);
    SimLog.setLogID(0);

    if(isGUI) {
      theGUI=new SimGUI(this);
    }
  }

////////////////////////////  member services ////////////////////////////

  public void addNewComponent(SimComponent comp) {
    components.add(comp);
    if(theGUI!=null) theGUI.componentsChanged();
  }

  public void removeComponents(java.util.List comps) {
    components.removeAll(comps);
    if(theGUI!=null) theGUI.componentsChanged();
  }

  public java.util.List getSimComponents() {
    return new java.util.ArrayList(components);
  }

  public boolean isRunning() {
    return (eventDispatcher!=null);
  }

  public long getCurrentTick() {
    return currentTick;
  }

  public long now() {
    return currentTick;
  }

  public boolean isCompNameDuplicate(String name) {
    int i;
    for(i=0;i<components.size();i++) {
      if(name.equals(((SimComponent)components.get(i)).getName()))
        return true;
    }
    return false;
  }

  boolean saveSim(File savefile) {
    try {
      FileOutputStream fo=new FileOutputStream(savefile);
      ObjectOutputStream oo=new ObjectOutputStream(fo);
      oo.writeLong(currentTick);
      oo.writeBoolean(startState);
      oo.writeInt(SimLog.getLogID());
      oo.writeObject(components);
      oo.writeObject(eventQueue);
      oo.flush();
      fo.close();

      SimLog.setWriter(savefile.getName()+".log",true);
    } catch (Exception ex) {
      System.out.println(ex.toString());
      return false;
    }
    return true;
  }

  boolean openSim(File openfile) {
    try {
      FileInputStream fi=new FileInputStream(openfile);
      ObjectInputStream oi=new ObjectInputStream(fi);

      currentTick=oi.readLong();
      startState=oi.readBoolean();
      int logID=oi.readInt();
      components=(java.util.List)oi.readObject();
      eventQueue=(java.util.List)oi.readObject();
      fi.close();

      //IMPORTANT: set back theSim for all components!
      for(int i=0;i<components.size();i++) {
        ((SimComponent)components.get(i)).setSim(this);
      }

      if(theGUI!=null) {
        theGUI.setClockTick(currentTick);
        if(startState) theGUI.setUserState("Start");
        else theGUI.setUserState("Resume");
      }

      SimLog.setWriter(openfile.getName()+".log",true);
      SimLog.setLogID(logID);
    } catch (Exception ex) {
      return false;
    }
    return true;
  }

  void doNew() {
    components.clear();
    eventQueue.clear();
    currentTick=0;
    startState=true;
    if(theGUI!=null) {
      theGUI.setClockTick(currentTick);
      theGUI.setUserState("Start");
    }
    SimLog.setWriter(SIM_PACKAGE+".log",false);
    SimLog.setLogID(0);
  }

  void resetLogFile(String thefile) {
    SimLog.setWriter(thefile,false);

    int i,j;
    for(i=0;i<components.size();i++) {
      SimComponent thisComp=(SimComponent)components.get(i);
      SimParameter [] params=thisComp.getParameters();
      for(j=0;j<params.length;j++) {
        int id=params[j].getLogID();
        if(id!=-1) SimLog.reprintID(thisComp.getName(),params[j].getName(),id);
      }
    }
  }

  void doStart() {
    startState=false;

    if(eventDispatcher==null) {
      for(int i=0;i<components.size();i++) {
        ((SimComponent)components.get(i)).start();
      }
      eventDispatcher=new Thread(this);
      eventDispatcher.start();
    }
  }

  void doPause() {
    if(eventDispatcher!=null) {
      Thread testThread=eventDispatcher;
      //set the pause signal
      isPausing=true;
      //then kill the thread
      eventDispatcher=null;
    }
    else
      actualPause();
  }

  private void actualPause() {
    for(int i=0;i<components.size();i++) {
      ((SimComponent)components.get(i)).pause();
    }
    if(theGUI!=null) theGUI.pause();
    isPausing=false;
  }

  void doResume() {
    if(eventDispatcher==null) {
      for(int i=0;i<components.size();i++) {
        ((SimComponent)components.get(i)).resume();
      }
      if(theGUI!=null) theGUI.resume();

      eventDispatcher=new Thread(this);
      eventDispatcher.start();
    }
  }

  void doReset() {
    if(eventDispatcher!=null) {
      Thread testThread=eventDispatcher;
      //set the reset signal
      isResetting=true;
      //then kill the thread
      eventDispatcher=null;
    }
    else
      actualReset();
  }

  private void actualReset() {
    startState=true;
    currentTick=0; //must set the clock also
    for(int i=0;i<components.size();i++) {
      ((SimComponent)components.get(i)).reset();
    }
    if(theGUI!=null) {
      theGUI.reset();
      theGUI.setClockTick(currentTick);
    }
    //and of course clear the queue
    eventQueue.clear();

    isResetting=false;
  }

  void notifyParamsChange(SimComponent comp) {
    if(theGUI!=null) theGUI.notifyParamsChange(comp);
  }

  public void enqueue(SimEvent e) {
    int left=0;
    int right=eventQueue.size()-1;
    int insertpoint=0;

  //perform binary search and insert the event in correct time slot
    while (left<=right) {
      int middle=(left+right)/2;
      if(e.getTick()==((SimEvent)eventQueue.get(middle)).getTick()) {
        do {
          middle++;
          if(middle==eventQueue.size()) break;
        } while (e.getTick()==((SimEvent)eventQueue.get(middle)).getTick());
        insertpoint=middle;
        break;
      }
      if(e.getTick()>((SimEvent)eventQueue.get(middle)).getTick()) {
        left=middle+1;
        insertpoint=left;
      }

⌨️ 快捷键说明

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