📄 floodingrouter.java
字号:
/* * ** Network and Service Differentiation Extensions to GridSim 3.0 ** * * Gokul Poduval & Chen-Khong Tham * Computer Communication Networks (CCN) Lab * Dept of Electrical & Computer Engineering * National University of Singapore * August 2004 * * Licence: GPL - http://www.gnu.org/copyleft/gpl.html * * FloodingRouter.java - Simulates a network router with Flooding as * the advertising protocol * */package gridsim.net;import eduni.simjava.*;import gridsim.*;import gridsim.util.*;import java.util.*;/** * This class implements a Router using a form of Flooding for routing. * The routing protocol is run before Gridlets etc. can be * submitted. * <p> * In case there are more than two routes to a destination, the * route with the lower hopcount is used. Since in this simulation routers * relay perfect information and links do not break down, flooding should be a * reliable protocol to use. * * @invariant $none * @since GridSim Toolkit 3.1 * @author Gokul Poduval & Chen-Khong Tham, National University of Singapore */public class FloodingRouter extends Router{ private Hashtable linkTable; private Hashtable schedTable; // table of schedulers private Hashtable hostTable; private Hashtable routerTable; private Hashtable forwardTable; private int id; private static final int BITS = 8; // 1 byte in bits /** * Creates a new FloodingRouter object. By default, * <b>no recording or logging</b> * is done for packets' activities. If you want to log operations of this * entity, please use {@link #FloodingRouter(String, boolean)}. * * @param name Name of this router * @throws NullPointerException This happens when name is empty or null * @see #FloodingRouter(String, boolean) * @pre name != null * @post $none */ public FloodingRouter(String name) throws NullPointerException { this(name, false); } /** * Creates a new FloodingRouter object with logging facility if it * is turned on. * <br> * NOTE: If logging facility is turned on, there are some overheads * in terms of performance and memory consumption. * * @param name Name of this router * @param trace <tt>true</tt> if you want to record this router's * activity, <tt>false</tt> otherwise * @throws NullPointerException This happens when name is empty or null * @pre name != null * @post $none */ public FloodingRouter(String name, boolean trace) throws NullPointerException { super(name, trace); init(); } /** * Initialises all variables * @pre $none * @post $none */ private void init() { this.id = super.get_id(); linkTable = new Hashtable(); hostTable = new Hashtable(); routerTable = new Hashtable(); forwardTable = new Hashtable(); schedTable = new Hashtable(); } /** * Informs the registered entities regarding to the end of a simulation. * @pre $none * @post $none */ protected void processEndSimulation() { // ... empty - maybe need to clean up all the hashtables } /** * Joins two routers with a Link. * @param router The router on the other side to which this one will * be attached. * @param link This is the link that will be used to connect the two * routers. * @param thisSched The scheduling policy used on this routers egress port * when sending data through it. * @param otherSched The scheduling policy that will be used on the * egress port of the router being connected to when * sending data to this router. * @pre router != null * @pre link != null * @pre thisSched != null * @pre otherSched != null * @post $none */ public void attachRouter(Router router, Link link, PacketScheduler thisSched, PacketScheduler otherSched) { String msg = super.get_name() + ".attachRouter(): Error - "; if (router == null) { System.out.println(msg + "the router is null."); return; } if (link == null) { System.out.println(msg + "the link is null."); return; } if (thisSched == null || otherSched == null) { System.out.println(msg + "the one or more packet schedulers are null."); return; } thisSched.setBaudRate( link.getBaudRate() ); otherSched.setBaudRate( link.getBaudRate() ); link.attach(this, router); this.attachRouter(router, link, thisSched); router.attachRouter(this, link, otherSched); } /** * Joins two routers together. This is called by the routers themselves * and should not be called by other entities. * * @param router The Router to which this router will be connected. * @param link The Link that will be used to join these routers. * @param sched The scheduling policy used on the egress port of the * router when sending data through this route. * @pre router != null * @pre link != null * @pre sched != null * @post $none */ public void attachRouter(Router router, Link link, PacketScheduler sched) { String msg = super.get_name() + ".attachRouter(): Error - "; if (router == null) { System.out.println(msg + "the router is null."); return; } if (link == null) { System.out.println(msg + "the link is null."); return; } if (sched == null) { System.out.println(msg + "the packet scheduler is null."); return; } linkTable.put(router.get_name(), link.get_name()); if (!schedTable.containsKey( link.get_name()) ) { schedTable.put(link.get_name(), sched); } routerTable.put( link.get_name(), router.get_name() ); hostTable.put( link.get_name(), router.get_name() ); // logging or recording ... if (reportWriter_ != null) { StringBuffer sb = null; sb = new StringBuffer("attach this ROUTER, with router, "); sb.append( router.get_name() ); sb.append(", with link, "); sb.append( link.get_name() ); sb.append(", with packet scheduler, "); sb.append( sched.getSchedName() ); super.write( sb.toString() ); } } /** * Attaches an entity to this router. The link between the router and the * entity being attached is taken from * {@link gridsim.GridSimCore#getLink()}. * * @param entity The entity to be attached. * @param sched The scheduling policy that will be used on the egress * port when the router sends data to the entity being * joined. * @see gridsim.GridSimCore#getLink() * @pre entity != null * @pre sched != null * @post $none */ public void attachHost(GridSimCore entity, PacketScheduler sched) { String msg = super.get_name() + ".attachHost(): Error - "; if (entity == null) { System.out.println(msg + "the entity is null."); return; } if (sched == null) { System.out.println(msg + "the packet scheduler is null."); return; } Link link = entity.getLink(); sched.setBaudRate( link.getBaudRate() ); link.attach(this, entity); linkTable.put( entity.get_name(), link.get_name() ); if (!schedTable.containsKey( link.get_name() )) { schedTable.put(link.get_name(), sched); } hostTable.put( link.get_name(), entity.get_name() ); // recording ... if (reportWriter_ != null) { StringBuffer sb = null; sb = new StringBuffer("attach this ROUTER, to entity, "); sb.append( entity.get_name() ); sb.append(", with packet scheduler, "); sb.append( sched.getSchedName() ); super.write( sb.toString() ); } } /** * Processes incoming events * @param ev a Sim_event object * @pre ev != null * @post $none */ protected synchronized void processEvent(Sim_event ev) { switch ( ev.get_tag() ) { case GridSimTags.PKT_FORWARD: case GridSimTags.JUNK_PKT: processNetPacket(ev, ev.get_tag()); break; case GridSimTags.ROUTER_AD: receiveAd(ev); break; case GridSimTags.INSIGNIFICANT: processInternalEvent(ev); break; default: System.out.println(super.get_name() + ".body(): Unable to " + "handle request from GridSimTags " + "with constant number " + ev.get_tag() ); break; } } /** * Processes incoming network packets, one at a time. * The incoming packet will be split up into smaller pieces if * the packet size > MTU of the other end. * * @param ev a Sim_event object * @pre ev != null * @post $none */ private synchronized void processNetPacket(Sim_event ev, int tag) { double nextTime = 0; Packet pkt = (Packet) ev.get_data(); PacketScheduler sched = getScheduler(pkt); // if a packet scheduler is not found, then try reschedule this packet // in the future if (sched == null) { System.out.println(super.get_name() + ".processNetPacket(): " + "Warning - can't find a packet scheduler for " + pkt); System.out.println("-> Will reschedule it again in the future."); super.sim_schedule(super.get_id(), Router.DELAY, tag, pkt); return; } // process ping() request if (pkt instanceof InfoPacket) { ((InfoPacket) pkt).addHop(id); ((InfoPacket) pkt).addEntryTime( GridSim.clock() ); ((InfoPacket) pkt).addBaudRate(sched.getBaudRate()); } // check downlink MTU, and split accordingly String linkName = getLinkName( pkt.getDestID() ); Link downLink = (Link) Sim_system.get_entity(linkName); int MTU = downLink.getMTU(); int numPackets = (int) Math.ceil(pkt.getSize() / (MTU * 1.0)); // if no packets at the moment if (sched.size() == 0) { if (numPackets == 1) { nextTime = (pkt.getSize() * BITS) / sched.getBaudRate(); } else { nextTime = (MTU * BITS * 1.0) / sched.getBaudRate(); } sendInternalEvent(nextTime, sched); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -