-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from 12urenloop/hw_sim
Script to test simulate hardware beacons
- Loading branch information
Showing
10 changed files
with
363 additions
and
44 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
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,34 @@ | ||
# Hardware simulation script | ||
|
||
## Requirements | ||
Any versions of the below should work, but when in doubt, take the latest one | ||
|
||
- `npm` | ||
- `node` | ||
|
||
## Setup | ||
|
||
`npm install` | ||
|
||
## Run | ||
|
||
- First make sure an instance of telraam is running (see below how to specify the telraam address and port) | ||
|
||
- Then run: `node main.js` | ||
|
||
## Arguments | ||
|
||
- `-h`: Show all the help messages. | ||
- `-p`: What port to connect to (default: `4564`) | ||
- `-a`: What address to connect to (default: `127.0.0.1`). | ||
- `-r`: The amount of runners to spawn. | ||
- `-b`: The amount of beacons to spawn. | ||
- `-m`: The average time per round | ||
- `-d`: Standard deviation for the average runner speed. | ||
- `-D`: Standard deviation of round speed, global for all runners. | ||
- `--miss-rate`: Missrate of runner detection. | ||
|
||
### Todo: added bonusses | ||
|
||
- Malformed message rate. We need closing and starting tags to properly detect this. So server code and code here. Look at the class `telraam.beacon.BeaconMessage`. | ||
- Messages per beacon or something, because irl the beacons are probably gonna send more than 1 message for a runner passing by. |
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,116 @@ | ||
'use strict'; | ||
|
||
const ArgumentParser = require('argparse').ArgumentParser; | ||
const net = require('net'); | ||
|
||
var args; | ||
|
||
// Standard Normal variate using Box-Muller transform. | ||
function randn_bm(mean, dev) { | ||
var u = 0, | ||
v = 0; | ||
while (u === 0) u = Math.random(); //Converting [0,1) to (0,1) | ||
while (v === 0) v = Math.random(); | ||
const out = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v); | ||
return out * dev + mean; | ||
} | ||
|
||
class Beacon { | ||
constructor(id) { | ||
this.socket = net.Socket(); | ||
this.socket.connect(args.port, args.address); | ||
this.id = id; | ||
} | ||
|
||
send(id) { | ||
console.log("Beacon", id, "Runner", this.id); | ||
|
||
// This is so ugly | ||
const start_tag = [60, 60, 60, 60]; | ||
const end_tag = [62, 62, 62, 62]; | ||
const actual_message_size = 10; | ||
|
||
const buffer = Buffer.alloc(actual_message_size + start_tag.length + end_tag.length); | ||
|
||
let offset = 0; | ||
for (let tag of start_tag) { | ||
buffer.writeInt8(tag, offset); | ||
offset += 1; // '<' | ||
} | ||
|
||
buffer.writeInt8(this.id, offset); | ||
offset += 1; | ||
buffer.writeInt8(id, offset); | ||
offset += 1; | ||
|
||
buffer.writeBigInt64LE(BigInt(Date.now()), offset); | ||
offset += 8; | ||
|
||
for (let tag of end_tag) { | ||
buffer.writeInt8(tag, offset); | ||
offset += 1; // '<' | ||
} | ||
this.socket.write(buffer); | ||
} | ||
} | ||
|
||
class Runner { | ||
constructor(id, mean, beacons) { | ||
this.mean = mean; | ||
this.dev = args.round_deviation; | ||
this.beacons = beacons; | ||
this.at = 0; | ||
this.id = id; | ||
|
||
this.run = this.send.bind(this); | ||
|
||
this.set_next(); | ||
} | ||
|
||
send() { | ||
if (Math.random() >= args.miss_rate) { | ||
this.beacons[this.at].send(this.id); | ||
} | ||
|
||
this.at++; | ||
|
||
if (this.at >= this.beacons.length) { | ||
this.at = 0; | ||
} | ||
|
||
this.set_next(); | ||
} | ||
|
||
set_next() { | ||
const time_till_next = randn_bm(this.mean, this.dev); | ||
setTimeout(this.run, time_till_next); | ||
} | ||
} | ||
|
||
function main() { | ||
const parser = new ArgumentParser({ | ||
addHelp: true, | ||
description: 'Hardware simulation script for Telraam' | ||
}); | ||
|
||
parser.addArgument(['-p', "--port"], { defaultValue: 4564, type: "int", help: "Port to use" }); | ||
parser.addArgument(['-a', "--address"], { defaultValue: "127.0.0.1", help: "Ip address to test" }); | ||
parser.addArgument(['-b', "--beacons"], { defaultValue: 2, type: "int", help: "Amount of beacons" }); | ||
parser.addArgument(['-r', "--runners"], { defaultValue: 5, type: "int", help: "Amount of runners" }); | ||
parser.addArgument(['-m', "--mean"], { defaultValue: 500, type: "int", help: "Mean of runner speed (ms per round)" }); | ||
parser.addArgument(['-d', "--runner-deviation"], { defaultValue: 10, type: "int", help: "Standard deviation of runner speed (per runner)" }); | ||
parser.addArgument(['-D', "--round-deviation"], { defaultValue: 0, type: "int", help: "Standard deviation of runner speed (per round)" }); | ||
parser.addArgument(['--miss-rate'], { defaultValue: 0, type: "float", help: "Missrate of runner detection." }); | ||
args = parser.parseArgs(); | ||
|
||
const beacons = []; | ||
for (let i = 0; i < args.beacons; i++) { | ||
beacons.push(new Beacon(i + 1)); | ||
} | ||
|
||
const runners = []; | ||
for (let i = 0; i < args.runners; i++) { | ||
runners.push(new Runner(i + 1, randn_bm(args.mean, args.runner_deviation), beacons)); | ||
} | ||
} | ||
main(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
{ | ||
"name": "hw_sim", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"argparse": "^1.0.10" | ||
} | ||
} |
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
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,19 @@ | ||
package telraam.beacon; | ||
|
||
public class BeaconException extends Exception { | ||
protected BeaconException(String reason) { | ||
super(reason); | ||
} | ||
|
||
public static class MsgEndWithNoStart extends BeaconException { | ||
public MsgEndWithNoStart() { | ||
super("Message end tag detected without a start tag"); | ||
} | ||
} | ||
|
||
public static class MsgStartWithNoEnd extends BeaconException { | ||
public MsgStartWithNoEnd() { | ||
super("2 message start tags detected."); | ||
} | ||
} | ||
} |
Oops, something went wrong.