Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding of NEVER and FOREVER in TimeValue class #120

Merged
merged 3 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lingua-franca-ref.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
43f25d02c83645721a42efe82a71ff59a1f2a8c6
3670a83ee2f76696cd260937ac144b17dafaa2cd
9 changes: 5 additions & 4 deletions src/core/federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ class RTIClient extends EventEmitter {
this.socket?.unref(); // Allow the program to exit
}

public sendNeighborStructure(upstreamFedIDs: number[], upstreamFedDelays: bigint[], downstreamFedIDs: number[]) {
public sendNeighborStructure(upstreamFedIDs: number[], upstreamFedDelays: TimeValue[], downstreamFedIDs: number[]) {
let msg = Buffer.alloc(9 + upstreamFedIDs.length * 10 + downstreamFedIDs.length * 2);
msg.writeUInt8(RTIMessageTypes.MSG_TYPE_NEIGHBOR_STRUCTURE);
msg.writeUInt32LE(upstreamFedIDs.length, 1);
Expand All @@ -411,7 +411,8 @@ class RTIClient extends EventEmitter {
let bufferIndex = 9;
for (let i = 0; i < upstreamFedIDs.length; i++) {
msg.writeUInt16LE(upstreamFedIDs[i], bufferIndex);
msg.writeBigUInt64LE(upstreamFedDelays[i], bufferIndex + 2);
let delay = upstreamFedDelays[i].toBinary();
delay.copy(msg, bufferIndex + 2);
bufferIndex += 10;
}

Expand Down Expand Up @@ -916,15 +917,15 @@ export class FederatedApp extends App {
private greatestTimeAdvanceGrant: Tag | null = null;

private upstreamFedIDs: number[] = [];
private upstreamFedDelays: bigint[] = [];
private upstreamFedDelays: TimeValue[] = [];
private downstreamFedIDs: number[] = [];

/**
* The default value, null, indicates there is no output depending on a physical action.
*/
private minDelayFromPhysicalActionToFederateOutput: TimeValue | null = null;

public addUpstreamFederate(fedID: number, fedDelay: bigint) {
public addUpstreamFederate(fedID: number, fedDelay: TimeValue) {
this.upstreamFedIDs.push(fedID);
this.upstreamFedDelays.push(fedDelay);
this._isLastTAGProvisional = true;
Expand Down
48 changes: 35 additions & 13 deletions src/core/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,29 @@ export class TimeValue {
*/
constructor(protected seconds: number, protected nanoseconds: number=0) {
if(!Number.isInteger(seconds) || !Number.isInteger(nanoseconds) || seconds < 0 || nanoseconds < 0) {
throw new Error("Cannot instantiate a time interval based on negative or non-integer numbers.");
if (seconds != Number.MIN_SAFE_INTEGER) {
throw new Error("Cannot instantiate a time interval based on negative or non-integer numbers.");
}
}
}

static zero(): TimeValue {
return new TimeValue(0,0)
}

static NEVER(): TimeValue {
return new TimeValue(Number.MIN_SAFE_INTEGER, 0);
}

static FOREVER(): TimeValue {
return new TimeValue(Number.MAX_SAFE_INTEGER, 0);
}

static secsAndNs(seconds: number, nanoSeconds:number): TimeValue {
return new TimeValue(seconds, nanoSeconds)
}



static secs(seconds: number): TimeValue {
return TimeValue.secsAndNs(seconds, 0)
}
Expand Down Expand Up @@ -291,18 +302,23 @@ export class TimeValue {
* Used by federates.
*/
public toBinary(): Buffer {
const billion = BigInt(TimeUnit.secs);
let bigTime = BigInt(this.nanoseconds) + BigInt(this.seconds) * billion;

// Ensure the TimeValue fits into a 64 unsigned integer.
let clampedTime = BigInt.asUintN(64, bigTime);
if (clampedTime != bigTime) {
throw new Error(`TimeValue ${this.toString()} is too big to fit into `
+ `a 64 bit unsigned integer`);
}

let buff = Buffer.alloc(8);
buff.writeBigUInt64LE(bigTime, 0);
if (this.seconds === Number.MIN_SAFE_INTEGER) {
buff.writeBigUInt64LE(BigInt(0x8000000000000000), 0);
} else if (this.seconds === Number.MAX_SAFE_INTEGER) {
buff.writeBigUInt64LE(BigInt(0x7fffffffffffffff), 0);
} else {
const billion = BigInt(TimeUnit.secs);
let bigTime = BigInt(this.nanoseconds) + BigInt(this.seconds) * billion;

// Ensure the TimeValue fits into a 64 unsigned integer.
let clampedTime = BigInt.asUintN(64, bigTime);
if (clampedTime != bigTime) {
throw new Error(`TimeValue ${this.toString()} is too big to fit into `
+ `a 64 bit unsigned integer`);
}
buff.writeBigUInt64LE(bigTime, 0);
}
return buff;
}

Expand All @@ -315,6 +331,12 @@ export class TimeValue {

// To avoid overflow and floating point errors, work with BigInts.
let bigTime = buffer.readBigUInt64LE(0);
if (bigTime === BigInt(0x8000000000000000)) {
return TimeValue.NEVER();
} else if (bigTime === BigInt(0x7fffffffffffffff)) {
return TimeValue.FOREVER();
}

let bigSeconds = bigTime / billion;
let bigNSeconds = bigTime % billion;

Expand Down