Пример #1
0
  /**
   * If this firing is triggered by an event at <i>waitee</i>, then output the waiting time for each
   * prior event arrival at <i>waiter</i> since the last arrival of waitee. If there is no event at
   * <i>waitee</i>, then record the time of arrival of the events at <i>waiter</i>, and produce no
   * output.
   *
   * @exception IllegalActionException If get() or send() throws it.
   */
  public void fire() throws IllegalActionException {
    super.fire();
    Time currentTime = ((DEDirector) getDirector()).getModelTime();

    while (waiter.hasToken(0)) {
      waiter.get(0);
      _waiting.addElement(currentTime);
    }

    boolean godot = false;

    while (waitee.hasToken(0)) {
      waitee.get(0);
      godot = true;
    }

    if (godot) {
      for (int i = 0; i < _waiting.size(); i++) {
        Time previousTime = (Time) _waiting.elementAt(i);
        DoubleToken outToken = new DoubleToken(currentTime.subtract(previousTime).getDoubleValue());
        output.send(0, outToken);
      }

      _waiting.removeAllElements();
    }
  }
Пример #2
0
 private void handleFirstRX(SinkData channel, Time currentTime)
     throws NoTokenException, IllegalActionException {
   // Method to handle the first stage, receiving the first synchronisation pulse
   IntToken token = (IntToken) input.get(0); // Retrieve the token from the input
   Time timeDelta =
       currentTime.subtract(
           waitTime); // Calculate a delta between the current time and when the channel was
                      // changed, to find the waiting time
   if (timeDelta.getDoubleValue() <= 1.5
       && token.equals(
           1)) { // Token is 1, and we have been waiting for longer than 1.5s. So we will not have
                 // a follow-up token, so can't be used for determining t
     channelQueue
         .remove(); // Send the current channel to the back of the queue so we can do something
                    // useful while it is in its sleep state
     channelQueue.add(currentChannel);
     return;
   }
   if (!channel
       .secondRun) { // If this is the first time we have received packets on the channel we will
                     // need the current time to calculate a t
     channel.t = currentTime;
   }
   channel.state = states.SECONDRX; // Set the channel state to the next stage
   channel.firstValue = token.intValue(); // Store the value we have received
   System.out.println("FIRSTRX on channel " + currentChannel + " currentTime is " + currentTime);
 }
Пример #3
0
 private void handleNCalc(SinkData channel, Time currentTime)
     throws NoTokenException, IllegalActionException {
   // Method to handle the fourth stage, where we listen to the first synchronisation pulse to find
   // the value of n
   System.out.println(channel.nextFireTime.getDoubleValue() >= currentTime.getDoubleValue() + 0.4);
   if (channel.nextFireTime.getDoubleValue() >= currentTime.getDoubleValue()) {
     input.get(0);
     return;
   } else if (channel
       .secondRun) { // If we are on the second run through, we have already transmitted twice so
                     // we can stop here
     channel.state = states.FINISHED;
     return;
   } else if (currentTime.subtract(channel.nextFireTime).getDoubleValue()
       > 2) { // There is a large delta between when we were supposed to fire and when we fired, so
              // we probably have a collision with another channel
     channel.secondRun = true; // Store that we will need to go around again for this channel
     channel.state = states.FIRSTRX; // Set the stage back to the beginning so we can restart
     System.out.println(
         "NCALC on channel "
             + currentChannel
             + " n is: "
             + channel.n
             + ". t is "
             + channel.t
             + ". nextFireTime is "
             + channel.nextFireTime
             + " currentTime is "
             + currentTime);
     nextChannel(currentChannel, currentTime);
   } else {
     channel.n = ((IntToken) input.get(0)).intValue();
     setNextFireTime(
         channel, currentTime.getDoubleValue() + (channel.t.getDoubleValue() * channel.n));
     channel.state = states.SECONDTX;
     System.out.println(
         "NCALC on channel "
             + currentChannel
             + " n is: "
             + channel.n
             + ". t is "
             + channel.t
             + ". nextFireTime is "
             + channel.nextFireTime
             + " currentTime is "
             + currentTime);
     nextChannel(currentChannel, currentTime);
     removeFromQueue(currentChannel);
   }
 }
Пример #4
0
 private void handleSecondRX(SinkData channel, Time currentTime)
     throws NoTokenException, IllegalActionException {
   // Method to handle the second stage, receiving the second synchronisation pulse
   if (!channel
       .secondRun) { // If this is the second time we have received packets on the channel we
                     // already have t so no need to calculate it
     channel.t = currentTime.subtract(channel.t);
   }
   int currentValue = ((IntToken) input.get(0)).intValue(); // Retrieve the value from the input
   if (channel.firstValue == 1 && currentValue == 1) { // If the first two values are 1 then n is 1
     channel.n = 1;
     channel.t =
         new Time(getDirector())
             .add(
                 channel.t.getDoubleValue()
                     / 12); // Calculate t given that n is 1, i.e divide the period by 12 as per
                            // the protocol
   }
   setNextFireTime(
       channel,
       currentTime.getDoubleValue()
           + (channel.t.getDoubleValue()
               * currentValue)); // Manually set the next fire time for this channel
   channel.state = states.FIRSTTX; // Set the channel state to the next stage
   System.out.println(
       "SECONDRX on channel "
           + currentChannel
           + ". Current value is "
           + currentValue
           + ". t is "
           + channel.t
           + ". nextFireTime is "
           + channel.nextFireTime
           + " currentTime is "
           + currentTime);
   nextChannel(currentChannel, currentTime);
   removeFromQueue(currentChannel); // We can now move onto listening on the next channel
 }