package Simulator; import java.io.*; /** * Models links in the simulator. */ public class Link extends NetComponents { // Fields private int number; // link number double Distance; double LocalTime; // need to be reset to zero in Cprobing Statistic private double propagationTime; private NetworkConfigure simulator; // the following variables are used only for bandwidth measurement // These variables are meaningful only for the bottleneck link double Tstarting_time = 0.0; // traffic measurement starting time //long queuing_size = 0; // holding the message size which is need to be queued. //boolean updated = false; // if set queuing_size=0 double end_time; // what is the time of a general probing end long Transfered_bit = 0; // the transfered bits in a traffic measure cycle boolean BottleneckLink; // whether the link is the bottleneck link PrintWriter OFILE; // only for the bottleneck link log purpose. // Constructor public Link(NetworkConfigure simulator, double distance, int number) { String k = simulator.getClass().getName(); this.simulator = simulator; this.BottleneckLink = false; this.propagationTime = distance / simulator.signalPropagationSpeed; this.number = number; this.LocalTime = 0.0; this.Distance = distance; try { this.OFILE = new PrintWriter(new FileWriter("LFile"+number+".txt")); } catch (IOException e) { } } /* public Link(Measurement simulator, double distance, int number) { this.number = number; this.LocalTime = 0.0; this.Distance = distance; //this.Distance = 1000; this.simulator = simulator; this.BottleneckLink = false; this.propagationTime = distance / simulator.signalPropagationSpeed; try { this.OFILE = new PrintWriter(new FileWriter("LFile"+number+".txt")); } catch (IOException e) { } } public Link(CprobStatistic simulator, double distance, int number) { this.number = number; this.LocalTime = 0.0; this.Distance = distance; //this.Distance = 1000; this.simulator = simulator; this.BottleneckLink = false; this.propagationTime = distance / simulator.signalPropagationSpeed; try { this.OFILE = new PrintWriter(new FileWriter("LFile"+number+".txt")); } catch (IOException e) { } } */ // Commands public boolean EventHandler (Message Mess) { int index=0; double ATime = Mess.getArrivalTime(); if (Mess.MessageType.compareTo("ICMP-request") == 0) index = Mess.NextObjectIndex; else index = 2*(simulator.RoadTable[Mess.RouterTableNum].numOfNode-1)-Mess.NextObjectIndex; //"ICMP-response" if (LocalTime > Mess.getArrivalTime()) { if ((Mess.MessageType.compareTo("ICMP-request") == 0) || (Mess.MessageType.compareTo("ICMP-response") == 0)) Mess.QueuingTime[index] = LocalTime - Mess.getArrivalTime(); } else { LocalTime = Mess.getArrivalTime(); if ((Mess.MessageType.compareTo("ICMP-request") == 0) || (Mess.MessageType.compareTo("ICMP-response") == 0)) Mess.QueuingTime[index] = 0.0; } double old_LocalTime = LocalTime; double transmissionTime = Mess.getSize()/simulator.LinkBandwidth; //LocalTime += transmissionTime + propagationTime; LocalTime += transmissionTime; //Mess.setTime(LocalTime); Mess.setTime(LocalTime+propagationTime); Mess.setNextNode(simulator.RoadTable[Mess.RouterTableNum].numOfNode); if (BottleneckLink && simulator.NeedTraffic) { // this is the bottleneck, count the precise bits sent in a statistic cycle if (LocalTime <= end_time) Transfered_bit = Transfered_bit + Mess.getSize(); else { // Local time is greater than end time if (old_LocalTime < end_time) { // arrival time is less than end_time Transfered_bit = Transfered_bit + (long)(Math.floor(Mess.getSize()* ((end_time-old_LocalTime)/(LocalTime-old_LocalTime)))); // update the log file and chart simulator.UpdateLog(end_time); double old_endtime = end_time; reset_bottlelink(end_time); Transfered_bit = (long)(Math.ceil(Mess.getSize()*((LocalTime-old_endtime)/(LocalTime-old_LocalTime)))); } else { // arrival time is greater than end_time simulator.UpdateLog(end_time); reset_bottlelink(end_time); Transfered_bit = Mess.getSize(); } } } // Link always return False because a link is unlikely to be the // last object in a road. A road always starts and ends at a router OFILE.println(Mess.MessNum + " : "+ATime+" | "+LocalTime+" | "+Mess.MessageType+" | "+Mess.getSize()); return false; } public void reset_bottlelink (double init_time) { this.Tstarting_time = init_time; this.Transfered_bit = 0; // a new measure cycle this.end_time = Tstarting_time + simulator.TBIntervalTime; } public double getLocalTime() { return LocalTime; } }