📄 trafficgenerator.java
字号:
package simulator;import java.util.ArrayList;import java.util.Iterator;import util.CommandStatus;public class TrafficGenerator extends CommandParser{ private Task defaultTask = new Task(); private Task newTask = null; private ArrayList<Task> tasks = new ArrayList<Task>(); private Event nextEvent = null; public TrafficGenerator() { super((Network) null); } public CommandStatus parseCommandPart(ArrayList<String> part, String path) { String param = part.get(0); CommandStatus ok = new CommandStatus(CommandStatus.COMMAND_OK); if (part.size() != 2) return null; assert newTask != null; try { if (param.equals("start")) { newTask.fireTime = Double.parseDouble(part.get(1)); return ok; } else if (param.equals("stop")) { newTask.stopTime = Double.parseDouble(part.get(1)); return ok; } else if (param.equals("interval")) { newTask.sendInterval = Double.parseDouble(part.get(1)); return ok; } else if (param.equals("fire_count") || param.equals("count")) { newTask.sendCounter = Integer.parseInt(part.get(1)); return ok; } else if (param.equals("msg_size")) { newTask.msgDataSize = Integer.parseInt(part.get(1)); return ok; } else if (param.equals("msgs_per_fire")) { newTask.msgNumPerSend = Integer.parseInt(part.get(1)); return ok; } else if (param.equals("src") || param.equals("source")) { newTask.src = part.get(1); return ok; } else if (param.equals("dest") || param.equals("dst") || param.equals("destination")) { newTask.dest = part.get(1); return ok; } } catch (NumberFormatException e) { return new CommandStatus("Error parsing value of parameter '" + param + "': " + e); } return null; } // We override parseLocalCommand from CommandParser - we need information // about complete set of parameters in each input line public CommandStatus parseLocalCommand(String cmd, ArrayList<ArrayList<String>> args, String parentPath) { CommandStatus ret; assert newTask == null; newTask = new Task(defaultTask); ret = super.parseLocalCommand(cmd, args, parentPath); if (ret == null || ret.getStatus() != CommandStatus.COMMAND_OK) return ret; if (cmd.equals("default_traffic")) { defaultTask = new Task(newTask); newTask = null; return ret; } if (newTask.stopTime < 0 && newTask.sendCounter < 0) { return new CommandStatus("You have to set stop time or send counter!"); } if (newTask.dest == null || newTask.src == null) { return new CommandStatus("You have to set source and destination nodes!"); } if (newTask.fireTime < 0) newTask.fireTime = network.getCurrentTime(); if (newTask.sendCounter < 0 && newTask.stopTime < newTask.fireTime) { return new CommandStatus("Stop time can not be lower than start time!"); } tasks.add(newTask); newTask = null; checkTasks(); return ret; } public void checkTasks() { Iterator<Task> i = tasks.iterator(); double nextTime = -1; double curTime = network.getCurrentTime(); while (i.hasNext()) { Task t = i.next(); double time = t.nextFire(curTime); if (time < 0) { i.remove(); } else { if (nextTime < 0 || time < nextTime) { nextTime = time; } } } if (nextTime >= 0) { if (nextEvent != null) { // nextEvent is the same as we would like to set - we don't have // to change anything. if (nextEvent.getTime() == nextTime) return; // nextEvent has different time - we should cancel it. cancelEvent(nextEvent); } nextEvent = new Event(nextTime, this); scheduleEvent(nextEvent); } else { // We don't need any events, but there is existing event. // Maybe some events were removed (although current set of commands // doesn't let to do this...) - we want to cancel this event. if (nextEvent != null) cancelEvent(nextEvent); nextEvent = null; } } // We have separate runTasks method, even though checkTask could fire them // as well. But we don't want tasks to be fired immediately when they are // added. protected void receiveLocalEvent(Event ev) { assert ev == nextEvent; nextEvent = null; double curTime = network.getCurrentTime(); Iterator<Task> i = tasks.iterator(); // We don't want to add separate event for every task // which should be fired at the same time, so we will // fire all of them now. while (i.hasNext()) { Task t = i.next(); if (t.nextFire(curTime) == curTime) t.fire(); } checkTasks(); } private class Task { public double fireTime = -1; public double stopTime = -1; public double sendInterval = 10; public int sendCounter = -1; public int msgDataSize = 10; public int msgNumPerSend = 1; public String src = null; public String dest = null; Task() { } Task(Task org) { fireTime = org.fireTime; stopTime = org.stopTime; sendInterval = org.sendInterval; sendCounter = org.sendCounter; msgDataSize = org.msgDataSize; msgNumPerSend = org.msgNumPerSend; } public double nextFire(double curTime) { if (sendCounter < 0 && (curTime > stopTime || fireTime > stopTime)) return -1; if (stopTime < 0 && sendCounter < 1) return -1; assert fireTime >= curTime; return fireTime; } public boolean fire() { fireTime += sendInterval; if (sendCounter > 0) --sendCounter; for (int i = 0; i < msgNumPerSend; ++i) { network.getNode(src).sendNewMessage(msgDataSize, network.getNode(dest)); } return true; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -