Skip to content

Commit 26d0a9f

Browse files
committed
Handling overflows and express 'no delay' by undefined
1 parent 10459ef commit 26d0a9f

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

src/core/federation.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ function isANodeJSCodedError(e: Error): e is NodeJSCodedError {
292292
* A network sender is a reactor containing a portAbsentReaction.
293293
*/
294294
export class NetworkSender extends Reactor {
295-
296295
/**
297296
* The last reaction of a NetworkSender reactor is the "port absent" reaction.
298297
* @returns the "port absent" of this reactor
@@ -790,9 +789,9 @@ class RTIClient extends EventEmitter {
790789
* @param federatePortID The ID of the receiving port.
791790
*/
792791
public sendRTIPortAbsent(
793-
intendedTag: Tag,
794792
federateID: number,
795-
federatePortID: number
793+
federatePortID: number,
794+
intendedTag: Tag
796795
): void {
797796
const msg = Buffer.alloc(17);
798797
msg.writeUInt8(RTIMessageTypes.MSG_TYPE_PORT_ABSENT, 0);
@@ -1505,7 +1504,7 @@ export class FederatedApp extends App {
15051504
msg: T,
15061505
destFederateID: number,
15071506
destPortID: number,
1508-
time: TimeValue
1507+
time: TimeValue | undefined
15091508
): void {
15101509
const absTime = this.util.getCurrentTag().getLaterTag(time).toBinary();
15111510
Log.debug(this, () => {
@@ -1597,9 +1596,9 @@ export class FederatedApp extends App {
15971596
* @param destPortID The ID of the receiving port.
15981597
*/
15991598
public sendRTIPortAbsent(
1600-
additionalDelay: TimeValue,
16011599
destFederateID: number,
1602-
destPortID: number
1600+
destPortID: number,
1601+
additionalDelay: TimeValue | undefined
16031602
): void {
16041603
const intendedTag = this.util.getCurrentTag().getLaterTag(additionalDelay);
16051604
Log.debug(this, () => {
@@ -1609,7 +1608,7 @@ export class FederatedApp extends App {
16091608
)} to federate ID: ${destFederateID}` + ` port ID: ${destPortID}.`
16101609
);
16111610
});
1612-
this.rtiClient.sendRTIPortAbsent(intendedTag, destFederateID, destPortID);
1611+
this.rtiClient.sendRTIPortAbsent(destFederateID, destPortID, intendedTag);
16131612
}
16141613

16151614
/**

src/core/reactor.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,9 +1777,9 @@ interface UtilityFunctions {
17771777
time: TimeValue
17781778
) => void;
17791779
sendRTIPortAbsent: (
1780-
additionalDealy: TimeValue,
17811780
destFederateID: number,
1782-
destPortID: number
1781+
destPortID: number,
1782+
additionalDelay: TimeValue
17831783
) => void;
17841784
}
17851785

@@ -1915,11 +1915,11 @@ export class App extends Reactor {
19151915
}
19161916

19171917
public sendRTIPortAbsent(
1918-
additionalDelay: TimeValue,
19191918
destFederateID: number,
1920-
destPortID: number
1919+
destPortID: number,
1920+
additionalDelay: TimeValue
19211921
): void {
1922-
this.app.sendRTIPortAbsent(additionalDelay, destFederateID, destPortID);
1922+
this.app.sendRTIPortAbsent(destFederateID, destPortID, additionalDelay);
19231923
}
19241924
})(this);
19251925

@@ -2096,9 +2096,9 @@ export class App extends Reactor {
20962096
* @param destPortID The ID of the receiving port.
20972097
*/
20982098
protected sendRTIPortAbsent(
2099-
additionalDelay: TimeValue,
21002099
destFederateID: number,
2101-
destPortID: number
2100+
destPortID: number,
2101+
additionalDelay: TimeValue
21022102
): void {
21032103
throw new Error(
21042104
"Cannot call sendRTIPortAbsent from an App. sendRTIPortAbsent may be called only from a FederatedApp"

src/core/time.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,18 @@ export class TimeValue {
118118
* @param other The time value to add to this one.
119119
*/
120120
add(other: TimeValue): TimeValue {
121-
if (other.isNever()) {
122-
return this;
121+
if (
122+
(this.isNever() && other.isForever()) ||
123+
(this.isForever() && other.isNever())
124+
) {
125+
// The sum of minus infinity and infinity is zero.
126+
return TimeValue.zero();
127+
} else if (this.isNever() || other.isNever()) {
128+
// The sum of minus infinity and a normal tag is minus infinity.
129+
return TimeValue.never();
130+
} else if (this.isForever() || other.isForever()) {
131+
// The sum of infinity and a normal tag is infinity.
132+
return TimeValue.forever();
123133
}
124134
let seconds = this.seconds + other.seconds;
125135
let nanoseconds = this.nanoseconds + other.nanoseconds;
@@ -129,7 +139,14 @@ export class TimeValue {
129139
seconds += 1;
130140
nanoseconds -= TimeUnit.sec;
131141
}
132-
return TimeValue.secsAndNs(seconds, nanoseconds);
142+
143+
// Check an overflow.
144+
if (!Number.isSafeInteger(seconds)) {
145+
// The overflow happens.
146+
return TimeValue.forever();
147+
} else {
148+
return TimeValue.secsAndNs(seconds, nanoseconds);
149+
}
133150
}
134151

135152
/**
@@ -205,6 +222,14 @@ export class TimeValue {
205222
}
206223
}
207224

225+
isForever(): boolean {
226+
if (this.seconds === Number.MAX_SAFE_INTEGER && this.nanoseconds === 0) {
227+
return true;
228+
} else {
229+
return false;
230+
}
231+
}
232+
208233
/**
209234
* Return true if this time value denotes a time interval of smaller length
210235
* than the time interval encoded by the time value given as a parameter;
@@ -438,12 +463,15 @@ export class Tag {
438463
* the given delay. The `microstep` of this time instant is ignored;
439464
* the returned time instant has a `microstep` of zero if the delay
440465
* is greater than zero. If the delay equals zero, the tag is returned
441-
* unchanged with its current `microstep`.
466+
* unchanged with its current `microstep`. If the delay is undefined,
467+
* the tag is returned as it was.
442468
* @param delay The time interval to add to this time instant.
443469
*/
444-
getLaterTag(delay: TimeValue): Tag {
445-
if (delay.isZero() || delay.isNever()) {
470+
getLaterTag(delay: TimeValue | undefined): Tag {
471+
if (delay === undefined) {
446472
return this;
473+
} else if (delay.isZero()) {
474+
return new Tag(this.time, this.microstep + 1);
447475
} else {
448476
return new Tag(delay.add(this.time), 0);
449477
}

0 commit comments

Comments
 (0)