eventstartsending.java

来自「JAVA版的蚂蚁算法(Ant Colony Optimization Algor」· Java 代码 · 共 89 行

JAVA
89
字号
package dk.itu.nulx30.eventSimulation;

import dk.itu.nulx30.networkModel.NetworkPackage;
import dk.itu.nulx30.networkModel.RouterQueue;

import dk.itu.nulx30.networkModel.exceptions.RouterQueueEmptyException;

import dk.itu.nulx30.util.BinaryHeap;

/**
 * This class represents the event, that a package is ready to be sent over the
 * <code>Wire</code> from the output queue of a <code>Router</code>.
 * When this event occurs the package is removed from the output queue and
 * an <code>EventInputQueue</code> event is put into the "global"
 * <code>eventHolder</code>.
 *
 * @author Jacob Wahl Winther
 * @author Mikkel Bundgaard
 * @author Troels C. Damgaard
 * @author Federico Decara
 *
 * @see EventInputQueue
 */
public class EventStartSending extends Event {
  /** the output queue, which holds the package to send*/
  private RouterQueue outputQueue;
  /** the package to remove from the output queue and send over the wire*/
  private NetworkPackage pack;
  /** binary heap, which handles all the events in the whole system*/
  private BinaryHeap eventHolder;
  /** the simulation which in this event has occurred*/
  private EventSimulation simulation;

  /**
   * Class constructor for <code>EventStartSending</code> which takes a time, a
   * <code>RouterQueue</code>, a <code>NetworkPackage</code> and a
   * <code>BinaryHeap</code> as arguments.
   *
   * @param time the time when this event is about to occur
   * @param q the router queue, which contains the package to send
   * @param np the networkpackage to remove from the output queue and to
   * send over the wire
   * @param bh the "global" binary heap, which handles all the events in the
   * system
   * @param sim the simulation in which this event has occurred   
   */
  public EventStartSending( double time, RouterQueue q, NetworkPackage np,
                                     BinaryHeap bh, EventSimulation sim ) {
    super( time );
    outputQueue = q;
    pack = np;
    eventHolder = bh;
    simulation = sim;
  }

  /**
   * This method is called when this event occurs. When this event is being
   * executed, we know that the wire is ready for use, since the event is set
   * to be executed at that time. We also know that no other package will be
   * sent over the line during transmission, because the "nextSendEvent" has
   * been incremented. Only task is to ensure that the package is removed from
   * the outputQueue.
   */
  public void doEvent() {
    EventInputQueue newE;
    pack.setTime( getTime() ); // Update timecounter in ANT (package)
    try {
      outputQueue.removePackage(); // take package out of output queue

      newE = new EventInputQueue( getTime() + outputQueue.getWire().getDelay(),
             outputQueue.getWire().getTarget(), pack, eventHolder, simulation );
      eventHolder.add( newE );
    }
    catch ( RouterQueueEmptyException RQEe ) {
      RQEe.printStackTrace();
      // -------------------
      // Hmm we a removing a package that is not there. This could be used
      // to catch events from dead routers. By setting the queues as empty
      // this event will occur whenever a package is being processed on
      // a "dead" router. But then we wouldn't know weather it was because
      // the router was dead or an actual error had occured! But since errors
      // never occur when I am in charge, this is irrelevant ;-)
      System.err.println( "The queue is empty therefore no package " +
                                                           "could be removed" );
      System.exit( 1 );
    }
  }
}

⌨️ 快捷键说明

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