⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 router.java

📁 JAVA版的蚂蚁算法(Ant Colony Optimization Algorithms)
💻 JAVA
字号:
package dk.itu.nulx30.networkModel;

import dk.itu.nulx30.eventSimulation.EventLocation;

import dk.itu.nulx30.eventSimulation.exceptions.IllegalTimeException;

import dk.itu.nulx30.graph.Vertex;

import dk.itu.nulx30.networkModel.exceptions.PackageTTLException;
import dk.itu.nulx30.networkModel.exceptions.RouterNotFoundException;
import dk.itu.nulx30.networkModel.exceptions.RouterQueueEmptyException;
import dk.itu.nulx30.networkModel.exceptions.RouterQueueFullException;
import dk.itu.nulx30.networkModel.exceptions.UninitializedWireException;

/**
 * Class Router represents the network nodes in the network model.<br>
 * The network model contains no other type of nodes. A Router contains a
 * collection of RouterQueues, divided into two groups, a inputQueue and
 * output queues. The inputQueue is used by all wires connected to the router,
 * and as such should be viewed as a shared resource. Each outputQueue contains
 * a reference to the wire they can send their packages over. A inputQueue contains
 * no reference to a wire, but is implicitly connected to all incomming wires.
 * A router has a delay which is the Routers capacity in packages/sec normalized
 * to a Integer. A low delay signifies a high capacity, while a high delay
 * signifies a low capacity. A router is identified by its name.
 * When a package arrives at a Router and the inputQueue is not full, the
 * current Router is added to the path list of that package.
 *
 * @author Jacob Wahl Winther
 * @author Mikkel Bundgaard
 * @author Troels C. Damgaard
 * @author Federico Decara
 */
public class Router extends Vertex implements EventLocation {
  /** The input queue of this <code>Router</code> */
  private RouterQueue inputQueue = null;
  /** The output queues of this <code>Router</code> */
  private RouterQueue[] outputQueues = null;
  /** The delay of this <code>Router</code> */
  private int delay = 0;
  /** The time for the next event of this <code>Router</code> */
  private double nextEvent = 0;
  /** The number of outputQueues*/
  private int numOutputQueues = 0;
  /** The routingQueue */
  private RoutingTable routingTable = null;

 /**
  * Public constructor of a router object. <code>name</code> should contain
  * the name of the router and the <code>routerDelay</code> should be the routers
  * delay. When this constructor is used, the router contains an input queue,
  * with a capacity of <code>inputQueueCapcity</code>.
  * The outputQueues have not been initialized.
  *
  * @param index the index of this router
  * @param routerDelay the delay of this router
  * @param inputQueueCapacity the capacity of this router
  */
  public Router( int index, int routerDelay, int inputQueueCapacity ) {
    super( index );
    delay = routerDelay;
    // Input queues contains no references to a wire, therefore the constructor
    // is given a null reference.
    inputQueue = new RouterQueue( this, null, inputQueueCapacity );
    outputQueues = new RouterQueue[ 5 ];
  }

  /**
   * Creates the RoutingTable for <code>this</code> router.
   * The number of routers in the network should be known before
   * using this method.
   *
   * @param numberOfRoutersInNetwork The number of routers in the network
   */
  public void initRoutingTable( int numberOfRoutersInNetwork ) {
    routingTable = new RoutingTable( this, numberOfRoutersInNetwork );
  }

  /**
   * Returns the routingtable for <code>this</code> router
   *
   * @return the routingtable for <code>this</code> router
   */
  public RoutingTable getRoutingTable() {
    return routingTable;
  }

  /**
   * Public constructor of a router object. <code>name</code> should contain
   * the name of the router and the <code>routerDelay</code> should be the routers
   * delay. The parameter <code>outQueues</code> must be an array of
   * <code>RouterQueue</code> objects. The <code>RouterQueue</code> objects should
   * have been initialized with a reference to a <code>Wire</code> object, that
   * contains a reference to another Router Object, therey connecting the current
   * <code>Router</code> with <code>this</code>
   *
   * @param index the index of this router
   * @param routerDelay the delay of this router
   * @param outQueues all the output queues of this router
   */
  public Router( int index, int routerDelay, RouterQueue[] outQueues ) {
    super( index );
    delay = routerDelay;
    outputQueues = outQueues;
  }

  /**
   * Adds a wire object to a <code>RouterQueue</code> object and adds the
   * <code>RouterQueue</code> to the <code>outputQueues</code> array.
   *
   * @param wire the given output wire
   * @param outputQueueCapacity the capacity of the output queue
   */
  public void addOutputWire( Wire wire, int outputQueueCapacity ) {
    // Use a dynamic array (double the size of the array if necessary)
    if ( numOutputQueues == outputQueues.length ) {
      RouterQueue[] temp = new RouterQueue[ outputQueues.length * 2 ];
      System.arraycopy( outputQueues, 0, temp, 0, outputQueues.length );
      outputQueues = temp;
    }
    // Add the new wire
    outputQueues[ numOutputQueues++ ] =
                             new RouterQueue( this, wire, outputQueueCapacity );
  }


  /**
   * Adds a new wire object, connected to the router and with
   * a delay of <code>wireDelay</code>. A outputQueue is created and the
   * reference to the Wire object is contained in that object. The outputQueue
   * is created with a capacity of <code>outputQueueCapacity</code>
   *
   * @param r the destination router
   * @param wireDelay the delay of the wire
   * @param outputQueueCapacity the capacity of the output queue
   *
   * @return returns the new output wire  
   *
   * @exception UninitializedWireException an
   * <code>UninitializedWireException</code> is thrown if the wire
   * is not created correct.
   */
  public Wire addOutputWire( Router r, int wireDelay, int outputQueueCapacity )
                                             throws UninitializedWireException {
    Wire res = new Wire( this, r, wireDelay );
    addOutputWire( res, outputQueueCapacity );
    return res;
  }

  /**
   * Adds a package to the inputQueue. Since the RouterQueue does not
   * contain any references to package objects, this is implemented as a
   * incrementation of the <code>currentSize</code> variable in the
   * inputQueue. Also ensures that the package is notified of
   * the arrival, such that the current router can be added to the path list
   * in the package.
   *
   * @param p the new package to add to the inputQueue.
   *
   * @exception RouterQueueFullException a <code>RouterQueueFullException</code>
   *            is thrown if the router's queue is full.
   * @see dk.itu.nulx30.networkModel.RouterQueue
   * @see dk.itu.nulx30.networkModel.NetworkPackage
   */
  public void addPackageToInputQueue( NetworkPackage p )
                          throws RouterQueueFullException {
   // addPackage() might throw a RouterQueueFullExcption
   inputQueue.addPackage();
  }

  /**
   * Removes a package from the inputQueue, by decrementing the
   * <code>currentSize</code> variable in the <code>inputQueue</code>.
   *
   * @exception RouterQueueEmptyException a
   * <code>RouterQueueEmptyException</code> is thrown if the routerqueue is empty
   */
  public void removePackageFromInputQueue() throws RouterQueueEmptyException {
    inputQueue.removePackage();
  }

  /**
   * Adds a package to the outputQueue containing the wire, connected
   * to destination Router <code>r</code>.
   *
   * @param r the destination router
   * @param p the networkpackage to add to the outputqueue
   *
   * @return the ouputqueue which the package were put on 
   *
   * @exception RouterQueueFullException a <code>RouterQueueFullException</code>
   * is thrown if the router's queue is full.
   * @exception RouterNotFoundException if the specified router is
   * not found to be connected to <code>this</code> router.
   * @exception PackageTTLException a <code>PackageTTLException</code> is thrown
   * if the package has exceeded its TTL
   */
  public RouterQueue addPackageToOutputQueue( Router r, NetworkPackage p )
      throws RouterQueueFullException, RouterNotFoundException, PackageTTLException {
    RouterQueue rqOut = getOutputQueue( r );
    p.addRouterToPathList( rqOut.getWire(),  this );
    rqOut.addPackage();
    return rqOut;
  }

  /**
   * Removes a package from the outputQueue containing the wire, connected
   * to router <code>r</code>
   *
   * @param r the destination router
   *
   * @exception RouterQueueEmptyException a <code>RouterQueueEmptyException</code>
   * is thrown if the router's queue is empty.
   * @exception RouterNotFoundException if the specified router is
   * not found to be connected to <code>this</code> router.
   */
  public void removePackageFromOutputQueue( Router r )
                     throws RouterQueueEmptyException, RouterNotFoundException {
    RouterQueue rqOut = getOutputQueue( r );
    rqOut.removePackage();
  }

  /**
   * Get the delay of this router
   *
   * @return the delay of this router
   */
  public int getDelay() {
    return delay;
  }

  /**
   * Sets the delay of this router
   *
   * @param newDelay the new delay of this router
   */
  public void setDelay( int newDelay ) {
    delay = newDelay;
  }

  /**
   * Returns the <code>RouterQueue</code> used to buffer packages sent
   * to Router <code>target</code>
   *
   * @param target The router whos outputQueue on current router the method
   * should return.
   *
   * @return the outputQueue used to buffer packages to <code>target</code> router.
   *
   * @exception  RouterNotFoundException if the specified <code>target</code> is
   * not found to be connected to <code>this</code> router.
   */
  public RouterQueue getOutputQueue( Router target ) throws RouterNotFoundException {
    int targetIndex = target.getIndex();
    for ( int i = 0; i < numOutputQueues; i++ ) {
      if ( outputQueues[ i ].getTargetIndex() == targetIndex ) {
        return outputQueues[ i ];
      }
    }
    throw new RouterNotFoundException( "outputQueue to router : " + targetIndex +
              " from router : " + getIndex() + " was not found" );
  }

  /**
   * Returns the array of <code>RouterQueue</code>'s used to buffer packages.
   *
   * @return an array of outputQueues used to buffer packages to <code>target</code>
   * router.
   */
  public RouterQueue[] getOutputQueues() {
    RouterQueue[] res = new RouterQueue[ numOutputQueues ];
    System.arraycopy( outputQueues, 0, res, 0, numOutputQueues );
    return res;
  }

  /**
   * Get the inputqueue of this router
   *
   * @return the inputqueue of this router
   */
  public RouterQueue getInputQueue() {
    return inputQueue;
  }

  /**
   * Provides a string representation of this object consisting of
   * the index, delay, number of outputqueues of this object.
   *
   * @return a string representation of this object.
   */
  public String toString() {
    return "RouterIndex: " + getIndex() + ", Delay :" + delay +
           ", No. of OutputQueues : " + numOutputQueues;
  }

  /**
   * Prints out the index of this router followed by a list of other routers
   * which this router is connected to on the standard output.
   */
  public void printConnectedTo() {
    System.out.println("Router : " + getIndex() + " is connected to : " );
    for ( int i = 0; i < numOutputQueues; i++ ) {
      System.out.println( "\t" + outputQueues[i].getTargetIndex() +
                          ", capacity: " + outputQueues[i].getMaxCapacity() +
                          ", delay: " + outputQueues[i].getWire().getDelay() );
    }
  }
  
  /**
   * Implementation of the <code>EventLocation</code> interface.
   * <code>getNextEventTime()</code> returns the next event time of this router.
   *
   * @return the next event time of this router
   */
  public double getNextEventTime() {
    return nextEvent;
  }

  /**
   * Implementation of the <code>EventLocation</code> interface.
   * <code>setNextEventTime()</code> sets the new next-event time.
   *
   * @param newEventTime the new next-event time.
   *
   * @exception IllegalTimeException this exception is thrown if the new
   * next-event time is less than the old next-event time.
   */
  public void setNextEventTime( double newEventTime ) throws IllegalTimeException {
    if ( nextEvent > newEventTime ) {
      throw new IllegalTimeException( "Router " + getIndex() + " eventTime: " +
                                      nextEvent + " newEvent: " + newEventTime );
    }
    nextEvent = newEventTime;
  }

  /**
   * equals return true if this object is the same as the one given as argument
   * otherwise false.
   *
   * @param o the reference object with which to compare.
   *
   * @return true if this object is the same as the obj argument; false otherwise.
   */
  public boolean equals( Object o ) {
    return ( (Router) o ).getIndex() == this.getIndex(); 
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -