syndicate-js/packages/server/src/monitor.js

117 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-11-21 14:23:30 +00:00
"use strict";
const UI = activate require("@syndicate-lang/driver-browser-ui");
// @jsx UI.html
// @jsxFrag UI.htmlFragment
2019-06-20 22:09:30 +00:00
const { Bytes } = require("@syndicate-lang/core");
2019-05-15 16:26:39 +00:00
const { WSServer, ToServer, FromServer, ServerConnected } = activate require("./client");
2019-05-23 09:33:59 +00:00
const P = activate require("./internal_protocol");
2018-11-21 14:23:30 +00:00
2019-06-06 14:55:01 +00:00
const Federation = require("./federation");
assertion type DetectedOverlay(scope);
assertion type AddressMap(from, nodeId, to);
2019-06-07 13:21:58 +00:00
assertion type OverlayLink(downNode, upNode);
2019-06-20 22:09:30 +00:00
assertion type OverlayNode(id);
assertion type OverlayRoot();
2019-06-06 14:55:01 +00:00
assertion type DisplayingNode(nodeDescription);
2018-11-21 14:23:30 +00:00
spawn {
const ui = new UI.Anchor();
assert ui.html('body',
<div id="main">
2019-06-06 14:55:01 +00:00
<h1>Server monitor</h1>
<h2>Local scopes</h2>
<div id="scopes"></div>
2019-06-07 13:21:58 +00:00
<h2>Federation</h2>
2019-06-06 14:55:01 +00:00
<div id="federated_scopes"></div>
<h2>Overlays</h2>
<div id="overlays"></div>
2018-11-21 14:23:30 +00:00
</div>);
const url = (function () {
const u = new URL(document.location);
u.protocol = u.protocol.replace(/^http/, 'ws');
2019-05-12 22:26:01 +00:00
u.pathname = '/';
2018-11-21 14:23:30 +00:00
return u.toString();
})();
2019-05-15 16:26:39 +00:00
const addr = WSServer(url, "monitor");
2018-11-21 14:23:30 +00:00
2019-05-15 16:26:39 +00:00
during ServerConnected(addr) {
2019-05-23 09:33:59 +00:00
during FromServer(addr, P.POAScope(_, $scope)) {
2018-11-21 14:23:30 +00:00
const ui = new UI.Anchor();
assert ui.html('#scopes',
<div class={`scope_${scope}`}>
<p>Scope: <tt>{scope}</tt></p>
<ul></ul>
</div>);
2019-05-23 09:33:59 +00:00
during FromServer(addr, P.POAScope($id, scope)) {
2018-11-21 14:23:30 +00:00
const ui = new UI.Anchor();
assert ui.html(`#scopes div.scope_${scope} ul`,
<li>{id.toString()} <button class="disconnect">Disconnect</button></li>);
on message UI.UIEvent(ui.fragmentId, 'button.disconnect', 'click', _) {
2019-05-23 09:33:59 +00:00
send ToServer(addr, P.Disconnect(id));
2018-11-21 14:23:30 +00:00
}
}
}
2019-06-06 14:55:01 +00:00
during FromServer(addr, Federation.ManagementScope($scope)) {
const addr = WSServer(url, scope);
during ServerConnected(addr) {
const ui = new UI.Anchor();
assert ui.html('#federated_scopes',
<div class={`fs_${scope}`}>
<p>Management scope <tt>{scope}</tt></p>
<ul></ul>
</div>);
during FromServer(addr, P.FederatedLink($id, $federatedScope)) {
assert DetectedOverlay(federatedScope);
const ui = new UI.Anchor();
assert ui.html(`#federated_scopes div.fs_${scope} ul`,
2019-06-07 13:21:58 +00:00
<li>FederatedLink:
<span> session <tt>{id.toString()}</tt></span>
<span> scope <tt>{federatedScope.toString()}</tt></span>
</li>);
2019-06-06 14:55:01 +00:00
}
}
}
during DetectedOverlay($scope) {
const addr = WSServer(url, scope);
during ServerConnected(addr) {
const ui = new UI.Anchor();
assert ui.html('#overlays',
<div class={`o_${scope}`}>
<p>Overlay <tt>{scope}</tt></p>
2019-06-20 22:09:30 +00:00
<ul class="root"></ul>
2019-06-07 13:21:58 +00:00
<ul class="vaddrs"></ul>
2019-06-06 14:55:01 +00:00
</div>);
assert DisplayingNode(OverlayRoot());
2019-06-20 22:09:30 +00:00
const nodeName = (n) => {
if (OverlayNode.isClassOf(n)) return "node_" + Bytes.from(OverlayNode._id(n)).toHex();
return "root";
};
during FromServer(addr, $item(OverlayLink($down, $up))) {
console.log(down.toString(), 'waiting for', up.toString());
during DisplayingNode(up) {
console.log(down.toString(), 'sees', up.toString());
const ui = new UI.Anchor();
assert ui.html(`#overlays div.o_${scope} ul.${nodeName(up)}`,
<li><tt>{down.toString()}</tt><ul class={nodeName(down)}></ul></li>);
assert DisplayingNode(down);
}
2019-06-07 13:21:58 +00:00
}
2019-06-06 14:55:01 +00:00
during FromServer(addr, $item(AddressMap(_, _, _))) {
const ui = new UI.Anchor();
2019-06-07 13:21:58 +00:00
assert ui.html(`#overlays div.o_${scope} ul.vaddrs`,
2019-06-06 14:55:01 +00:00
<li><tt>{item && item.toString()}</tt></li>);
}
}
}
2018-11-21 14:23:30 +00:00
}
}
2020-08-05 10:36:53 +00:00
require('@syndicate-lang/core').bootModule(module);