-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement htp and ltp merging receivers
cherypick changes from #43 into this branch
- Loading branch information
Showing
6 changed files
with
268 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './packet'; | ||
export * from './receiver'; | ||
export * from './receiver/htp'; | ||
export * from './receiver/ltp'; | ||
export * from './sender'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Options, Packet } from '../../packet'; | ||
import { PacketWithTime } from '../../types/merging'; | ||
import { HTPMergingReceiver } from '../htp'; | ||
import { LTPMergingReceiver } from '../ltp'; | ||
|
||
/** helper function to make it easier to create the Map */ | ||
function createPackets( | ||
latestPackets: Record< | ||
string, | ||
Omit<Options, 'sequence' | 'universe'> & { timestamp?: number } | ||
>, | ||
) { | ||
const latestPacketsMap = new Map<string, PacketWithTime>(); | ||
for (const server in latestPackets) { | ||
const { timestamp = NaN, ...options } = latestPackets[server]!; | ||
latestPacketsMap.set(server, { | ||
packet: new Packet({ ...options, universe: 14, sequence: NaN }), | ||
timestamp, | ||
}); | ||
} | ||
return latestPacketsMap; | ||
} | ||
|
||
describe('htp', () => { | ||
const reciever = new HTPMergingReceiver({ universes: [14], reuseAddr: true }); | ||
// @ts-expect-error -- `protected` is not enforced at runtime. | ||
// This is a hack to make the test simpler. | ||
const merge = reciever.mergeData; | ||
|
||
afterAll(() => { | ||
reciever.close(); | ||
}); | ||
|
||
it('merges using htp', () => { | ||
const merged = merge({ | ||
maximumPriority: 124, | ||
universe: 14, | ||
universeData: { | ||
referenceData: [NaN, 1, 2, 3, 4, 5], | ||
servers: createPackets({ | ||
'main console': { priority: 124, payload: [NaN, 101, 102, 103] }, | ||
'backup console': { | ||
priority: 123, | ||
payload: [NaN, 201, 202, 203, 204], | ||
}, | ||
}), | ||
}, | ||
}); | ||
expect(merged).toStrictEqual({ | ||
// HTP applies to the whole packet, so channel 4 is 0, instead of 204 | ||
// referenceData is irrelevant for HTP, so channel 5 is 0, instead of 5 | ||
1: 101, | ||
2: 102, | ||
3: 103, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('ltp', () => { | ||
const reciever = new LTPMergingReceiver({ universes: [14], reuseAddr: true }); | ||
|
||
// @ts-expect-error -- `protected` is not enforced at runtime. | ||
// This is a hack to make the test simpler. | ||
const merge = reciever.mergeData; | ||
|
||
afterAll(() => { | ||
reciever.close(); | ||
}); | ||
|
||
it('merges using ltp', () => { | ||
const merged = merge({ | ||
maximumPriority: 124, | ||
universe: 14, | ||
universeData: { | ||
referenceData: [NaN, 1, 2, 3, 4, 5], | ||
servers: createPackets({ | ||
'main console': { | ||
priority: 124, | ||
payload: [NaN, 101, 102, 103], | ||
timestamp: 1241, | ||
}, | ||
'backup console': { | ||
priority: 123, | ||
payload: [NaN, 201, 202, 203, 204], | ||
timestamp: 1241, | ||
}, | ||
}), | ||
}, | ||
}); | ||
expect(merged).toStrictEqual({ | ||
// HTP applies to the whole packet, so channel 4 is 0, instead of 204 | ||
// referenceData is irrelevant for HTP, so channel 5 is 0, instead of 5 | ||
1: 101, | ||
2: 102, | ||
3: 103, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { AbstractMergingReceiver } from './abstract'; | ||
import { PreparedData } from '../types/merging'; | ||
import type { Payload } from '../util'; | ||
|
||
export class HTPMergingReceiver extends AbstractMergingReceiver { | ||
// eslint-disable-next-line class-methods-use-this | ||
protected mergeData(data: PreparedData): Payload { | ||
const mergedData: Payload = {}; | ||
|
||
for (const [, { packet }] of data.universeData.servers) { | ||
if ( | ||
packet.priority === data.maximumPriority && | ||
packet.universe === data.universe | ||
) { | ||
for (let ch = 1; ch <= 512; ch += 1) { | ||
const newValue = packet.payload[ch] || 0; | ||
if ((mergedData[ch] ?? 0) < newValue) { | ||
mergedData[ch] = newValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return mergedData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { PreparedData } from '../types/merging'; | ||
import { AbstractMergingReceiver } from './abstract'; | ||
import type { Payload } from '../util'; | ||
|
||
export class LTPMergingReceiver extends AbstractMergingReceiver { | ||
// eslint-disable-next-line class-methods-use-this | ||
protected mergeData(data: PreparedData): Payload { | ||
const { referenceData } = data.universeData; | ||
const mergedData: Payload = {}; | ||
|
||
for (let ch = 1; ch <= 512; ch += 1) { | ||
const referenceTime = 0; | ||
mergedData[ch] = referenceData[ch] || 0; | ||
|
||
for (const [, { packet, timestamp }] of data.universeData.servers) { | ||
if (timestamp > referenceTime) { | ||
mergedData[ch] = packet.payload[ch] || mergedData[ch]!; | ||
} | ||
} | ||
} | ||
|
||
return mergedData; | ||
} | ||
} |
Oops, something went wrong.