Skip to content

Commit

Permalink
Add toBinary and fromBinary methods for Tag class.
Browse files Browse the repository at this point in the history
  • Loading branch information
hokeun committed Dec 29, 2021
1 parent 6f1d17a commit ff6f165
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/core/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,32 @@ export class Tag {
public toString(): string {
return "(" + this.time.toString() + ", " + this.microstep + ")";
}

/**
* Get a 12-byte buffer for the tag.
* Used by federates.
*/
public toBinary(): Buffer {
let timeBuffer = this.time.toBinary();
let buf = Buffer.alloc(12);
timeBuffer.copy(buf, 0, 0);
buf.writeInt32LE(this.microstep);
return buf;
}

/**
* Create a Tag from 8-byte TimeValue and 4-byte microstep in a buffer.
* @param buffer A buffer of TimeValue and microstep.
*/
public static fromBinary(buffer: Buffer) {
let timeBuffer = Buffer.alloc(8);
buffer.copy(timeBuffer, 0, 0, 8);
let time = TimeValue.fromBinary(timeBuffer);
let microstep = buffer.readUInt32LE(8);
const billion = BigInt(TimeUnit.secs);

return new Tag(time, microstep);
}
}

/**
Expand Down

0 comments on commit ff6f165

Please sign in to comment.