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); } }