2018-11-13 21:27:26 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const UI = require("@syndicate-lang/driver-browser-ui");
|
|
|
|
// @jsx UI.html
|
|
|
|
// @jsxFrag UI.htmlFragment
|
|
|
|
|
|
|
|
const Http = activate require("@syndicate-lang/driver-http-node");
|
2018-11-26 13:10:43 +00:00
|
|
|
const S = activate require("@syndicate-lang/driver-streams-node");
|
2019-05-23 09:33:59 +00:00
|
|
|
const P = activate require("./internal_protocol");
|
2019-05-23 14:52:10 +00:00
|
|
|
const C = activate require("./client");
|
2019-05-23 09:33:59 +00:00
|
|
|
const Server = activate require("./server");
|
2019-05-23 12:51:16 +00:00
|
|
|
const Federation = activate require("./federation");
|
2018-11-26 13:10:43 +00:00
|
|
|
|
2018-11-19 16:57:22 +00:00
|
|
|
import {
|
|
|
|
Set, Bytes,
|
2018-11-19 22:22:39 +00:00
|
|
|
Encoder, Observe,
|
2018-12-12 17:16:10 +00:00
|
|
|
Dataspace, Skeleton, currentFacet, genUuid, RandomID
|
2018-11-19 16:57:22 +00:00
|
|
|
} from "@syndicate-lang/core";
|
2018-11-13 21:27:26 +00:00
|
|
|
|
2019-05-28 10:56:29 +00:00
|
|
|
assertion type AvailableTransport(spec);
|
|
|
|
assertion type WebSocketTransport(port, path);
|
|
|
|
// S.TcpListener specifies TCP transport
|
|
|
|
// S.UnixSocketServer specifies Unix socket transport
|
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
const fs = require('fs');
|
2018-12-12 17:16:10 +00:00
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
let currentManagementScope = 'local';
|
|
|
|
|
|
|
|
function usage() {
|
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
console.info('Usage: syndicate-server [ OPTION [ OPTION ... ] ]');
|
|
|
|
console.info('');
|
|
|
|
console.info('where OPTION may be repeated any number of times and is drawn from:');
|
|
|
|
console.info('');
|
|
|
|
console.info(' --tcp PORTNUMBER Create a plain TCP service on the given port');
|
|
|
|
console.info(' --http PORTNUMBER Create an HTTP WebSocket service on the given port');
|
|
|
|
console.info(' --unix PATH Create a Unix socket service at the given path');
|
|
|
|
console.info('');
|
|
|
|
console.info(' --monitor PORTNUMBER Serve a simple HTML/JS monitoring app on the port');
|
|
|
|
console.info('');
|
|
|
|
console.info(' --management SCOPE Set the management scope for --uplink etc to use');
|
|
|
|
console.info(' --uplink LOCALSCOPE WEBSOCKETURL REMOTESCOPE');
|
|
|
|
console.info(' Establish a federation uplink from the named local');
|
|
|
|
console.info(' scope to the remote scope within the server at the URL');
|
|
|
|
}
|
|
|
|
|
|
|
|
const uplinks = [];
|
|
|
|
function process_command_line(args) {
|
|
|
|
const strArg = () => args.shift();
|
|
|
|
const numArg = () => Number.parseInt(args.shift());
|
|
|
|
while (args.length) {
|
|
|
|
const opt = args.shift();
|
|
|
|
switch (opt) {
|
2019-05-28 10:56:29 +00:00
|
|
|
case "--tcp": spawnTcpServer(numArg()); break;
|
|
|
|
case "--http": spawnWebSocketServer(numArg()); break;
|
|
|
|
case "--unix": spawnUnixSocketServer(strArg()); break;
|
|
|
|
case "--monitor": spawnMonitorAppServer(numArg()); break;
|
2019-05-23 14:52:10 +00:00
|
|
|
case "--management": currentManagementScope = strArg(); break;
|
|
|
|
case "--uplink": {
|
|
|
|
const localScope = strArg();
|
|
|
|
const target = strArg();
|
|
|
|
const remoteScope = strArg();
|
|
|
|
uplinks.push(Federation.Uplink(localScope,
|
|
|
|
C.WSServer(target, currentManagementScope),
|
|
|
|
remoteScope));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
console.error("Unsupported command-line argument: " + opt);
|
|
|
|
/* FALL THROUGH */
|
|
|
|
case '--help':
|
|
|
|
case '-h':
|
|
|
|
usage();
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 17:16:10 +00:00
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
process_command_line(process.argv.slice(2));
|
2018-11-21 14:23:30 +00:00
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
spawn named 'server' {
|
|
|
|
assert Federation.ManagementScope(currentManagementScope);
|
|
|
|
uplinks.forEach((link) => {
|
|
|
|
assert P.Proposal(currentManagementScope, link);
|
|
|
|
});
|
2018-11-13 21:27:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 10:56:29 +00:00
|
|
|
spawn named 'helpful info output' {
|
|
|
|
on asserted AvailableTransport($spec) console.info('Transport:', spec.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
spawn named 'federationRoutingInfo' {
|
|
|
|
during Federation.ManagementScope($managementScope) {
|
|
|
|
during $t(AvailableTransport(_)) assert P.Proposal(managementScope, t);
|
2018-11-13 21:27:26 +00:00
|
|
|
}
|
2019-05-23 14:52:10 +00:00
|
|
|
}
|
2018-11-21 14:23:30 +00:00
|
|
|
|
2019-05-28 10:56:29 +00:00
|
|
|
function _spawnStreamServer(spec) {
|
|
|
|
spawn named spec {
|
|
|
|
assert AvailableTransport(spec);
|
|
|
|
on asserted S.IncomingConnection($id, spec) Server.streamServerActor(id, [spec, id]);
|
2019-05-12 22:26:01 +00:00
|
|
|
}
|
2019-05-23 14:52:10 +00:00
|
|
|
}
|
2019-05-12 22:26:01 +00:00
|
|
|
|
2019-05-28 10:56:29 +00:00
|
|
|
function spawnTcpServer(port) {
|
|
|
|
_spawnStreamServer(S.TcpListener(port));
|
|
|
|
}
|
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
function spawnUnixSocketServer(path) {
|
2019-05-28 10:56:29 +00:00
|
|
|
_spawnStreamServer(S.UnixSocketServer(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
function spawnWebSocketServer(port) {
|
|
|
|
const spec = WebSocketTransport(port, '/');
|
|
|
|
spawn named spec {
|
|
|
|
const server = Http.HttpServer(null, port);
|
|
|
|
assert AvailableTransport(spec);
|
|
|
|
during Http.WebSocket($reqId, server, [], _) spawn named [spec, reqId] {
|
|
|
|
Server.websocketServerFacet(reqId);
|
2019-05-23 14:52:10 +00:00
|
|
|
}
|
2019-05-12 22:26:01 +00:00
|
|
|
}
|
2019-05-23 14:52:10 +00:00
|
|
|
}
|
2019-05-12 22:26:01 +00:00
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
function spawnMonitorAppServer(port) {
|
2019-05-28 10:56:29 +00:00
|
|
|
console.info('Monitor app on port', port);
|
2019-05-23 14:52:10 +00:00
|
|
|
spawn named ['monitorAppServer', port] {
|
|
|
|
const server = Http.HttpServer(null, port);
|
|
|
|
|
|
|
|
during Http.Request($reqId, server, 'get', [], _, _) {
|
|
|
|
assert :snapshot Http.Response(reqId, 200, "OK", {"Content-type": "text/html"},
|
|
|
|
'<!DOCTYPE html>' + UI.htmlToString(
|
|
|
|
<html>
|
|
|
|
<head><meta charset="utf-8"></meta></head>
|
|
|
|
<body><script src="dist/monitor.js"></script></body>
|
|
|
|
</html>));
|
|
|
|
}
|
2018-11-21 14:23:30 +00:00
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
function assertFileResponse(reqId, path) {
|
|
|
|
assert :snapshot Http.Response(reqId, 200, "OK", {}, fs.readFileSync(path));
|
|
|
|
}
|
2018-11-13 21:27:26 +00:00
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
during Http.Request($reqId, server, 'get', ['chat.html'], _, _)
|
|
|
|
assertFileResponse(reqId, __dirname + '/../chat.html');
|
2018-12-12 17:16:10 +00:00
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
during Http.Request($reqId, server, 'get', ['style.css'], _, _)
|
|
|
|
assertFileResponse(reqId, __dirname + '/../style.css');
|
2018-11-13 21:27:26 +00:00
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
during Http.Request($reqId, server, 'get', ['dist', $file], _, _)
|
|
|
|
assertFileResponse(reqId, __dirname + '/../dist/' + file);
|
2018-11-19 16:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 14:52:10 +00:00
|
|
|
spawn named 'monitorApp' {
|
|
|
|
during P.POAScope($connId, $scope) assert P.Proposal('monitor', P.POAScope(connId, scope));
|
|
|
|
on message P.Envelope('monitor', P.Disconnect($connId)) send P.Disconnect(connId);
|
2018-11-13 21:27:26 +00:00
|
|
|
}
|
2018-12-12 17:16:10 +00:00
|
|
|
|
2019-05-28 10:56:29 +00:00
|
|
|
// const localId = RandomID.randomId(8, false);
|
|
|
|
// const dataspaceId = 'EToUNUJI0ykSfudmN9Z99wu62qGQB1nd8SHvjNtL5tM'; // public key of root server
|
|
|
|
// const gatewayId = dataspaceId + ':' + localId;
|
|
|
|
//
|
|
|
|
// const M = activate require("@syndicate-lang/driver-mdns");
|
|
|
|
// // assert M.Publish(M.Service(gatewayId, '_syndicate._tcp'), null, port, []);
|
|
|
|
// // assert M.Publish(M.Service(gatewayId, '_syndicate+ws._tcp'), null, port, ["path=/"]);
|
|
|
|
// spawn named 'peerDiscovery' {
|
|
|
|
// console.info('Peer discovery running');
|
|
|
|
// // during M.DefaultGateway($gwif, _) {
|
|
|
|
// // on start console.log('GW+', gwif);
|
|
|
|
// // on stop console.log('GW-', gwif);
|
|
|
|
// during M.Discovered(
|
|
|
|
// M.Service($name, '_syndicate+ws._tcp'), $host, $port, $txt, $addr, "IPv4", $gwif)
|
|
|
|
// {
|
|
|
|
// const [dsId, peerId] = name.split(':');
|
|
|
|
//
|
|
|
|
// let tier = null;
|
|
|
|
// txt.forEach((t) => {
|
|
|
|
// t.split(' ').forEach((kv) => {
|
|
|
|
// const [k, v] = kv.split('=');
|
|
|
|
// if (k === 'tier') {
|
|
|
|
// tier = Number.parseInt(v);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// on start console.log('+ws', gwif, tier, name, host, port, addr);
|
|
|
|
// on stop console.log('-ws', gwif, tier, name, host, port, addr);
|
|
|
|
// }
|
|
|
|
// // }
|
|
|
|
// }
|