diff --git a/docs/socket.md b/docs/socket.md index 87d4718a..b0544b9f 100644 --- a/docs/socket.md +++ b/docs/socket.md @@ -114,6 +114,9 @@ Handshake messages Sent by the master server when the WebSocket connection has been opened. - version - string - The version of the master, e.g. "2.0.0". +- plugins - Object<string, string> - + Object mapping plugin names to plugin version for plugins that are + loaded on the master server. ### `register_slave` @@ -124,6 +127,9 @@ Slave handshake for establishing a new connection session. - version - string - The protocol version of the slave, e.g. "2.0.0". - name - string - Name of the slave. - id - integer - ID of the slave. +- plugins - Object<string, string> - + Object mapping plugin names to plugin version for plugins that are + available on the slave. Currently not used by the master server. ### `register_control` diff --git a/docs/writing-plugins.md b/docs/writing-plugins.md index 2b57862d..15710824 100644 --- a/docs/writing-plugins.md +++ b/docs/writing-plugins.md @@ -73,9 +73,9 @@ The following properties are recognized: **instanceEntrypoint**: Path to a Node.js module relative to the plugin directory which contains the InstancePlugin class definition for this plugin. This - is an optional paramater. A plugin can be for instances only, but - for it to be able to send any messages to other instances it will - still have to be loaded on the master server. + is an optional paramater. A plugin may have code only for instances + but it must still be loaded on the master in order for it to be + possible to load it on an instance. **InstanceConfigGroup**: Subclass of `PluginConfigGroup` for defining the per instance diff --git a/lib/link/connectors.js b/lib/link/connectors.js index 5a420323..70e08d53 100644 --- a/lib/link/connectors.js +++ b/lib/link/connectors.js @@ -131,6 +131,13 @@ class WebSocketClientConnector extends WebSocketBaseConnector { this._reconnectDelay = reconnectDelay; this._timeout = 15 * 60; this._startedReconnect = null; + + /** + * Mapping of plugin name to plugin version loaded on the master + * server. Is `null` before handshake has taken place. + * @member {?Object} + */ + this.plugins = null; } _check(expectedStates) { @@ -335,6 +342,7 @@ class WebSocketClientConnector extends WebSocketBaseConnector { let { seq, type, data } = message; if (type == 'hello') { console.log(`SOCKET | received hello from master version ${data.version}`); + this.plugins = data.plugins; if (this._sessionToken) { console.log("SOCKET | Attempting resume"); this.sendHandshake("resume", { diff --git a/lib/schema.js b/lib/schema.js index fec79da6..07e649f6 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -58,9 +58,13 @@ const serverHandshake = ajv.compile({ "type": { const: "hello" }, "data": { additionalProperties: false, - required: ["version"], + required: ["version", "plugins"], properties: { "version": { type: "string" }, + "plugins": { + type: "object", + additionalProperties: { type: "string" }, + }, } }, }, @@ -111,13 +115,17 @@ const clientHandshake = ajv.compile({ "type": { const: "register_slave" }, "data": { additionalProperties: false, - required: ["token", "agent", "version", "name", "id"], + required: ["token", "agent", "version", "name", "id", "plugins"], properties: { "token": { type: "string" }, "agent": { type: "string" }, "version": { type: "string" }, "name": { type: "string" }, "id": { type: "integer" }, + "plugins": { + type: "object", + additionalProperties: { type: "string" }, + }, }, }, }, diff --git a/master.js b/master.js index a144945f..3d7fa7c6 100644 --- a/master.js +++ b/master.js @@ -31,6 +31,7 @@ let masterConfig; let masterConfigPath; let stopAcceptingNewSessions = false; let debugEvents = new events.EventEmitter(); +let pluginList = {}; // homebrew modules const generateSSLcert = require("lib/generateSSLcert"); @@ -581,12 +582,14 @@ class SlaveConnection extends BaseConnection { this._id = registerData.id; this._name = registerData.name; this._version = registerData.version; + this._plugins = registerData.plugins; db.slaves.set(this._id, { agent: this._agent, id: this._id, name: this._name, version: this._version, + plugins: this._plugins, }); this.connector.on("close", () => { @@ -863,7 +866,10 @@ wss.on("connection", function (socket, req) { }; // Start connection handshake. - socket.send(JSON.stringify({ seq: null, type: "hello", data: { version }})); + socket.send(JSON.stringify({ seq: null, type: "hello", data: { + version, + plugins: pluginList + }})); function attachHandler() { pendingSockets.add(socket); @@ -1000,6 +1006,8 @@ async function loadPlugins(pluginInfos) { continue; } + pluginList[pluginInfo.name] = pluginInfo.version; + let pluginLoadStarted = Date.now(); let MasterPlugin = plugin.BaseMasterPlugin; try { diff --git a/slave.js b/slave.js index a08d56bd..78041eb1 100644 --- a/slave.js +++ b/slave.js @@ -117,12 +117,16 @@ class Instance extends link.Link{ } } - async init(pluginInfos, slave) { + async init(pluginInfos, masterPlugins, slave) { await this.server.init(); // load plugins for (let pluginInfo of pluginInfos) { - if (!pluginInfo.instanceEntrypoint || !this.config.group(pluginInfo.name).get("enabled")) { + if ( + !pluginInfo.instanceEntrypoint + || !Object.hasOwnProperty.call(masterPlugins, pluginInfo.name) + || !this.config.group(pluginInfo.name).get("enabled") + ) { continue; } @@ -513,19 +517,26 @@ class InstanceConnection extends link.Link { } class SlaveConnector extends link.WebSocketClientConnector { - constructor(slaveConfig) { + constructor(slaveConfig, pluginInfos) { super(slaveConfig.get("slave.master_url"), slaveConfig.get("slave.reconnect_delay")); this.slaveConfig = slaveConfig; + this.pluginInfos = pluginInfos; } register() { console.log("SOCKET | registering slave"); + let plugins = {}; + for (let pluginInfo of this.pluginInfos) { + plugins[pluginInfo.name] = pluginInfo.version; + } + this.sendHandshake("register_slave", { token: this.slaveConfig.get("slave.master_token"), agent: "Clusterio Slave", version, id: this.slaveConfig.get("slave.id"), name: this.slaveConfig.get("slave.name"), + plugins, }); } } @@ -653,7 +664,7 @@ class Slave extends link.Link { let instance = new Instance( connectionClient, instanceInfo.path, this.config.get("slave.factorio_directory"), instanceInfo.config ); - await instance.init(this.pluginInfos, this); + await instance.init(this.pluginInfos, this.connector.plugins, this); // XXX: race condition on multiple simultanious calls this.instanceConnections.set(instanceId, instanceConnection); @@ -945,7 +956,7 @@ async function startSlave() { return; } - let slaveConnector = new SlaveConnector(slaveConfig); + let slaveConnector = new SlaveConnector(slaveConfig, pluginInfos); let slave = new Slave(slaveConnector, slaveConfig, pluginInfos); // Handle interrupts diff --git a/test/lib/link/connector.js b/test/lib/link/connector.js index b800a679..99c4f6be 100644 --- a/test/lib/link/connector.js +++ b/test/lib/link/connector.js @@ -86,7 +86,7 @@ describe("lib/link/connectors", function() { it("should call register on hello", function() { let called = false; testConnector.register = () => { called = true; }; - testConnector._processHandshake({ seq: 1, type: 'hello', data: { version: "test" }}); + testConnector._processHandshake({ seq: 1, type: 'hello', data: { version: "test", plugins: {} }}); assert(called, "register was not called"); }); it("should emit connected on ready", async function() {