sim.java
来自「一个小型网络仿真器的实现」· Java 代码 · 共 586 行 · 第 1/2 页
JAVA
586 行
else {
insertpoint=middle;
right=middle-1;
}
}
eventQueue.add(insertpoint,e);
}
public void dequeue(SimEvent e) {
eventQueue.remove(e);
}
public void dequeue_by_comp_and_type(SimListener src,SimListener dest,int type) {
java.util.Iterator i=eventQueue.iterator();
while(i.hasNext()) {
SimEvent thisev=(SimEvent)i.next();
if((thisev.getSource()==src || src==null) &&
(thisev.getDest()==dest || dest==null) &&
(thisev.getType()==type))
i.remove();
}
}
public boolean registerAnimation(SimComponent acomp,int compinfo_id) {
if(theGUI!=null) return theGUI.registerAnimation(acomp,compinfo_id);
return false;
}
public void unregisterAnimation(SimComponent acomp,int compinfo_id) {
theGUI.unregisterAnimation(acomp,compinfo_id);
}
////////////////////////// end member services ///////////////////////////
///////////////////// simulation event dispatching thread ////////////////
public void run() {
Thread thisThread = Thread.currentThread();
long yieldPeriod;
if(theGUI!=null) {
//thisThread.setPriority(thisThread.getPriority()-1);
yieldPeriod=theGUI.getYieldPeriod();
}
else yieldPeriod=BATCH_YIELD;
while (eventDispatcher==thisThread) {
for(long i=0;i<yieldPeriod && eventDispatcher==thisThread;i++) {
if(!eventQueue.isEmpty()) {
SimEvent theEvent=(SimEvent)eventQueue.remove(0);
currentTick=theEvent.getTick();
theEvent.getDest().action(theEvent);
}
}
if(theGUI!=null) {
theGUI.setClockTick(currentTick);
//probing GUI responsibility
if(GUIprobing < GUI_PROBE_THRESHOLD) {
GUIprobing++;
Runnable r=new Runnable() {
public void run() {
GUIprobing--;
}
};
theGUI.runProbing(r);
}
else {
//System.out.print("Waiting for GUI thread...");
while(GUIprobing>0) {
try {
thisThread.sleep(50);
}
catch(Exception e) {}
}
//System.out.println("done!");
}
}
if(stopTick<Long.MAX_VALUE) {
if(currentTick>stopTick) { //single session ends
eventDispatcher=null;
System.out.print("\r");
setSimEnd(true);
break;
}
System.out.print("\r"+SimClock.getClockString(currentTick));
}
}
if(isPausing) actualPause();
else if(isResetting) actualReset();
}
synchronized private boolean getSimEnd() {
return simEnd;
}
synchronized private void setSimEnd(boolean end) {
simEnd=end;
}
////////////////////////////////// main ///////////////////////////////////
public static void main(String [] args) {
boolean gotfile=false;
String simfile=null;
boolean showGUI=true;
long simtime=0;
boolean wantsnap=false;
int repcount=1;
boolean useresume=false;
boolean preprocess=false;
boolean postprocess=false;
boolean usereset=false;
boolean savelog=false;
if(args.length>0) {
//first argument is a simulation file name
simfile=args[0];
if(simfile.equals("-h") || simfile.equals("--help")) {
System.out.println("Usage:");
System.out.println(" <invoker> [<filename>] [<time>] [repeat <count>] [snap]");
System.out.println(" [useresume] [pre] [post] [usereset] [savelog]");
System.out.println("\nOptions:");
System.out.println(" <invoker> - normally java janetsim.Sim");
System.out.println(" <filename> - any saved simulation file");
System.out.println(" <time> - simulation end time (in seconds)");
System.out.println(" repeat <count> - number of times to run the simulation (default is 1)");
System.out.println(" snap - generate snap file after each run");
System.out.println(" useresume - perform a resume() instead of start() for each run");
System.out.println(" pre - call preprocess() for each component before each run");
System.out.println(" post - call postprocess() for each component after each run");
System.out.println(" usereset - perform a reset after each run (default is to");
System.out.println(" reload the simulation file after each run)");
System.out.println(" savelog - save the log file (tagging with index) after each run");
System.exit(0);
}
gotfile=true;
}
if(args.length>1) {
//second argument is the simulation time (in seconds)
try {
simtime=SimClock.Sec2Tick(Double.parseDouble(args[1]));
} catch(NumberFormatException ex) {
System.out.println(ex.toString());
System.exit(1);
}
if(simtime<=0) {
System.out.println("Parameter Error: simulation time must be greater than zero!");
System.exit(1);
}
showGUI=false; //once you specify a simulation time, no GUI is shown,
//that is, we are in non-interactive mode!
}
if(args.length>2) {
for(int i=2;i<args.length;i++) {
String arg=args[i];
if(arg.equals("snap")) {
wantsnap=true;
}
else if(arg.equals("repeat")) {
i++;
if(i==args.length) {
System.out.println("Parameter Error: repeat without the repeat count!");
System.exit(1);
}
try {
repcount=Integer.parseInt(args[i]);
} catch(NumberFormatException ex) {
System.out.println(ex.toString());
System.exit(1);
}
if(repcount<0) {
System.out.println("Parameter Error: repeat count is negative!");
System.exit(1);
}
}
else if(arg.equals("useresume")) {
useresume=true;
}
else if(arg.equals("pre")) {
preprocess=true;
}
else if(arg.equals("post")) {
postprocess=true;
}
else if(arg.equals("usereset")) {
usereset=true; //when usereset is true, no reload during each run
}
else if(arg.equals("savelog")) {
savelog=true;
}
else {
System.out.println("Parameter Error: unknown parameter \""+arg+"\"");
System.exit(1);
}
}
}
if(showGUI) {
Sim theApp=new Sim(true);
if(gotfile) {
theApp.theGUI.doOpen(new File(simfile));
}
}
else {
System.out.println("Simulator in non-interactive mode.\n");
Sim theApp=new Sim(false);
if(!theApp.openSim(new File(simfile))) {
System.out.println("Error opening file "+simfile+".");
System.exit(1);
}
for(int i=0;i<repcount;i++) {
if(preprocess) {
for(int j=0;j<theApp.components.size();j++) {
SimComponent thisComp=(SimComponent)theApp.components.get(j);
thisComp.preprocess(i);
}
}
long jobstarttime=System.currentTimeMillis();
System.out.println("Job "+String.valueOf(i)+" started.");
theApp.stopTick=simtime;
theApp.setSimEnd(false);
if(useresume) theApp.doResume();
else theApp.doStart();
while(!theApp.getSimEnd()) {
try {
Thread.currentThread().sleep(500);
} catch(InterruptedException ex2) { }
}
theApp.doPause();
double jobtime=(System.currentTimeMillis()-jobstarttime)/1000.0;
System.out.print("Job "+String.valueOf(i)+" finished. Job time about ");
System.out.print((new java.text.DecimalFormat("0.0")).format(jobtime));
System.out.println(" seconds.\n");
if(postprocess) {
for(int j=0;j<theApp.components.size();j++) {
SimComponent thisComp=(SimComponent)theApp.components.get(j);
thisComp.postprocess(i);
}
}
if(wantsnap) {
theApp.saveSim(new File(simfile+".snap"+String.valueOf(i)));
}
if(savelog) {
try {
SimLog.close();
BufferedReader infile=new BufferedReader(new FileReader(simfile+".log"));
PrintWriter outfile=new PrintWriter(new BufferedWriter(new FileWriter(simfile+".log"+String.valueOf(i))));
for(;;) {
String aline=infile.readLine();
if(aline==null) break;
outfile.println(aline);
}
infile.close();
outfile.close();
theApp.resetLogFile(simfile+".log");
} catch(Exception ex) { }
}
if(usereset) {
theApp.doReset();
}
else {
if(!theApp.openSim(new File(simfile))) {
System.out.println("Error opening file "+simfile+".");
System.exit(1);
}
}
}
SimLog.close(); //MUST ensure log is closed
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?