package link; import java.util.*; import timer.*; /***************************************************************** *****************************************************************/ /************************************************************ ************************************************************/ /***************************************************************** A simple broadcast link that implements a randomized delivery model. Behavior is like ethernet in the sense that there is no observable latency, just the transmission time. *****************************************************************/ public class SimpleBroadcastLink extends TimerClient implements Link { /************************************************************ Create a new link with the specified parameters @param seed the seed for the random number generator @param mtu the maximum transmission unit, in bytes @param bandwidthMean the long-term average bandwidth, in Mb/s @param bandwidthSD the standard deviation on the bandwidth distribution @param bandwidthWalk is the tendency towards random walks, from 0.0 to 1.0 @param dropProb is the probability that packets are dropped @param downtime would be the intervals during which the link is down, but this feature isn't implemented yet ************************************************************/ public SimpleBroadcastLink (long seed, int mtu, double bandwidthMean, double bandwidthSD, double bandwidthWalk, double dropProb, Vector downtime) { rand = new Random (seed); this.mtu = mtu; this.bandwidthMean = bandwidthMean / 8.0; this.bandwidthSD = bandwidthSD; this.bandwidthWalk = bandwidthWalk; this.dropProb = dropProb; bandwidthLast = bandwidthMean; nodes = new Vector (); } private Random rand; private int mtu; private double bandwidthMean; private double bandwidthSD; private double bandwidthWalk; private double bandwidthLast; private double dropProb; private Vector nodes; // all connected nodes /************************************************************ Get the list of connected nodes @return an Enumeration of the currently connected nodes. ************************************************************/ protected Enumeration ConnectedNodes () { return nodes.elements (); } /************************************************************ Transmit a packet on the link. Samples to find the transmit time, then waits for that time and delivers the packet to all nodes other than the transmitting node. While one node is transmitting all others are blocked. Fair queuing is not attempted. @param p the packet to transmit @param n the node sending the packet @see Link#TransmitPacket ************************************************************/ public void TransmitPacket (LinkPacket p, Node n) { if (p.data.length > MTU ()) return; bandwidthLast = LinkUtils.SampleDistribution (rand, bandwidthMean, bandwidthSD, bandwidthWalk, bandwidthLast); double transmitTime = p.data.length / bandwidthLast; // in us System.out.println ("Transmitting packet: length is " + p.data.length + ", delay is " + transmitTime); Timer.global.WaitFor (this, (long) transmitTime, Timer.US); if (rand.nextDouble () > dropProb) { Enumeration e = ConnectedNodes (); while (e.hasMoreElements ()) { Node node = (Node) e.nextElement (); if (node != n) node.PacketArrived (p, this); } } } /************************************************************ Get the maximum number of data bytes to transmit @return the maximum transmission unit, in bytes @see Link#MTU ************************************************************/ public int MTU () { return mtu; } /************************************************************ Get the average bandwidth for this link @return the bandwidth, in Mb/s @see Link#Bandwidth ************************************************************/ public double Bandwidth () { return bandwidthMean; } /************************************************************ Get the average latency for this link @return the latency, in ms @see Link#Latency ************************************************************/ public double Latency () { return 0.0; } /************************************************************ Get the names of all the other nodes connected to this link (i.e. all the nodes who would receive a packet that was transmitted on the link) @param n the node making the request (so that it can be excluded from the result) @return an array of the names of the other nodes ************************************************************/ public String[] OtherNodeNames (Node n) { String[] retval = new String[nodes.size () - 1]; int i = 0; Enumeration e = ConnectedNodes (); while (e.hasMoreElements ()) { Node node = (Node) e.nextElement (); if (node != n) { retval[i] = "" + node; ++i; } } return retval; } /************************************************************ Add a node, maintains the list of connected nodes @see Link#AddNode ************************************************************/ public void AddNode (Node l) { if (!nodes.contains (l)) nodes.addElement (l); } /************************************************************ Remove a node @see Link#RemoveNode ************************************************************/ public void RemoveNode (Node l) { nodes.removeElement (l); } }