2021-02-24 20:48:55 +00:00
|
|
|
import {
|
|
|
|
Actor,
|
|
|
|
Assertion,
|
|
|
|
Handle,
|
|
|
|
Ref,
|
|
|
|
Turn,
|
|
|
|
attenuate,
|
|
|
|
pdiscard,
|
|
|
|
rfilter,
|
|
|
|
} from './actor.js';
|
2021-02-25 11:29:02 +00:00
|
|
|
import { Record } from 'preserves';
|
|
|
|
import { Dataspace, Observe } from './dataspace.js';
|
2021-02-23 15:16:15 +00:00
|
|
|
|
2021-02-24 20:48:55 +00:00
|
|
|
const BoxState = Record.makeConstructor('BoxState', ['value']);
|
|
|
|
const SetBox = Record.makeConstructor('SetBox', ['newValue']);
|
2021-02-22 18:37:47 +00:00
|
|
|
|
|
|
|
let startTime = Date.now();
|
|
|
|
let prevValue = 0;
|
2021-02-23 08:34:36 +00:00
|
|
|
const LIMIT = 500000;
|
2021-02-22 18:37:47 +00:00
|
|
|
|
2021-02-23 15:16:15 +00:00
|
|
|
function spawnBox(t: Turn, ds: Ref) {
|
2021-02-22 18:37:47 +00:00
|
|
|
t.spawn(t => {
|
|
|
|
console.log('Spawning Box');
|
|
|
|
let valueHandle: Handle | undefined;
|
2021-02-22 21:30:36 +00:00
|
|
|
function setValue(t: Turn, value: number) {
|
2021-02-22 18:37:47 +00:00
|
|
|
valueHandle = t.replace(ds, valueHandle, BoxState(value));
|
|
|
|
}
|
|
|
|
setValue(t, 0);
|
2021-02-22 19:14:35 +00:00
|
|
|
t.assert(ds, Observe(SetBox.constructorInfo.label, t.ref({
|
2021-02-23 14:53:42 +00:00
|
|
|
message(t: Turn, [newValue]: [number]): void {
|
2021-02-23 14:38:57 +00:00
|
|
|
// console.log(`Box: got ${newValue}`);
|
2021-02-22 18:37:47 +00:00
|
|
|
if (newValue % 25000 === 0) {
|
|
|
|
const endTime = Date.now();
|
|
|
|
const delta = (endTime - startTime) / 1000.0;
|
|
|
|
const count = newValue - prevValue;
|
|
|
|
prevValue = newValue;
|
|
|
|
startTime = endTime;
|
|
|
|
console.log(`Box: got ${newValue} (${count / delta} Hz)`);
|
|
|
|
}
|
2021-02-23 08:34:36 +00:00
|
|
|
if (newValue === LIMIT - 20000) t.quit();
|
2021-02-22 18:37:47 +00:00
|
|
|
setValue(t, newValue);
|
|
|
|
}
|
|
|
|
})));
|
|
|
|
});
|
2021-02-23 15:16:15 +00:00
|
|
|
}
|
2021-02-22 18:37:47 +00:00
|
|
|
|
2021-02-23 15:16:15 +00:00
|
|
|
function spawnClient(t: Turn, ds: Ref) {
|
2021-02-22 18:37:47 +00:00
|
|
|
t.spawn(t => {
|
|
|
|
console.log('Spawning Client');
|
|
|
|
let count = 0;
|
2021-02-22 19:14:35 +00:00
|
|
|
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
|
2021-02-23 14:53:42 +00:00
|
|
|
assert(t: Turn, [currentValue]: [number]): void {
|
2021-02-22 18:37:47 +00:00
|
|
|
// console.log(`Client: got ${currentValue}`);
|
2021-02-23 08:34:36 +00:00
|
|
|
if (currentValue === LIMIT) {
|
2021-02-22 18:37:47 +00:00
|
|
|
console.log(`Client: quitting at limit`);
|
|
|
|
t.quit();
|
|
|
|
} else {
|
|
|
|
t.message(ds, SetBox(currentValue + 1));
|
|
|
|
}
|
2021-02-22 19:08:11 +00:00
|
|
|
}
|
2021-02-22 18:37:47 +00:00
|
|
|
})));
|
2021-02-22 19:14:35 +00:00
|
|
|
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
|
2021-02-23 14:53:42 +00:00
|
|
|
assert(_t: Turn, _assertion: Assertion): void { count++; },
|
|
|
|
retract(t: Turn, _handle: Handle) {
|
2021-02-22 18:37:47 +00:00
|
|
|
if (--count === 0) {
|
|
|
|
console.log('Client: detected box termination');
|
|
|
|
t.quit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})));
|
2021-02-22 09:12:10 +00:00
|
|
|
});
|
2021-02-23 15:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Turn.for(new Actor(), async (t: Turn) => {
|
|
|
|
const ds = t.ref(new Dataspace());
|
2021-02-24 20:48:55 +00:00
|
|
|
|
|
|
|
spawnBox(t, attenuate(
|
|
|
|
ds,
|
|
|
|
rfilter(BoxState(pdiscard),
|
|
|
|
Observe(SetBox.constructorInfo.label, pdiscard))));
|
|
|
|
|
|
|
|
spawnClient(t, attenuate(
|
|
|
|
ds,
|
|
|
|
rfilter(SetBox(pdiscard),
|
|
|
|
Observe(BoxState.constructorInfo.label, pdiscard))));
|
2021-02-22 09:12:10 +00:00
|
|
|
});
|