From 295cc7543ae88426f2bb023730d7a6fb4cf65fd8 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 27 Jul 2023 00:24:24 +0200 Subject: [PATCH] Reverse argument order for sendTo It makes more sense to put the address first then have the large structure to send as the second argument to sendTo. The opposite order was originally chosen to be similar to that of sendto in BSD sockets. --- packages/controller/src/ControlConnection.js | 20 ++++++++-------- packages/controller/src/Controller.js | 6 ++--- packages/ctl/src/commands.js | 20 ++++++++-------- packages/host/src/Instance.js | 23 +++++++++++-------- packages/lib/command.js | 8 +++---- packages/lib/link/link.js | 6 ++--- .../web_ui/src/components/CreateSaveModal.jsx | 2 +- .../web_ui/src/components/InstanceRcon.jsx | 2 +- .../src/components/InstanceViewPage.jsx | 9 +++++--- .../web_ui/src/components/InstancesPage.jsx | 3 ++- .../src/components/LoadScenarioModal.jsx | 2 +- packages/web_ui/src/components/SavesList.jsx | 3 ++- .../components/StartStopInstanceButton.jsx | 6 +++-- packages/web_ui/src/model/saves.jsx | 2 +- plugins/global_chat/control.js | 2 +- plugins/global_chat/instance.js | 2 +- plugins/inventory_sync/instance.js | 10 ++++---- plugins/player_auth/instance.js | 4 ++-- plugins/research_sync/controller.js | 8 +++---- plugins/research_sync/instance.js | 6 ++--- plugins/subspace_storage/controller.js | 2 +- plugins/subspace_storage/instance.js | 6 ++--- test/integration/clusterio.js | 2 +- test/integration/index.js | 7 +++--- 24 files changed, 87 insertions(+), 74 deletions(-) diff --git a/packages/controller/src/ControlConnection.js b/packages/controller/src/ControlConnection.js index fcee23df..de677f07 100644 --- a/packages/controller/src/ControlConnection.js +++ b/packages/controller/src/ControlConnection.js @@ -421,7 +421,7 @@ class ControlConnection extends BaseConnection { // Let host handle request if source and target is on the same host. if (sourceHostId === targetHostId) { - return await this._controller.sendTo(request, { hostId: sourceHostId }); + return await this._controller.sendTo({ hostId: sourceHostId }, request); } // Check connectivity @@ -451,23 +451,23 @@ class ControlConnection extends BaseConnection { // ensure the file size is known prior to the target host pull. await Promise.all([ this._controller.sendTo( + { hostId: sourceHostId }, new libData.InstancePushSaveRequest(request.sourceInstanceId, stream.id, request.sourceName), - { hostId: sourceHostId } ), events.once(stream.events, "source"), ]); // Establish pull from target host to stream and wait for completion. let storedName = await this._controller.sendTo( - new libData.InstancePullSaveRequest(request.targetInstanceId, stream.id, request.targetName), { hostId: targetHostId }, + new libData.InstancePullSaveRequest(request.targetInstanceId, stream.id, request.targetName), ); // Delete source save if this is not a copy if (!request.copy) { await this._controller.sendTo( + { hostId: sourceHostId }, new libData.InstanceDeleteSaveRequest(request.sourceInstanceId, request.sourceName), - { hostId: sourceHostId } ); } @@ -887,7 +887,7 @@ class ControlConnection extends BaseConnection { user.isAdmin = admin; this._controller.userUpdated(user); - this._controller.sendTo(new libData.InstanceAdminlistUpdateEvent(name, admin), "allInstances"); + this._controller.sendTo("allInstances", new libData.InstanceAdminlistUpdateEvent(name, admin)); } async handleUserSetBannedRequest(request) { @@ -905,7 +905,7 @@ class ControlConnection extends BaseConnection { user.isBanned = banned; user.banReason = reason; this._controller.userUpdated(user); - this._controller.sendTo(new libData.InstanceBanlistUpdateEvent(name, banned, reason), "allInstances"); + this._controller.sendTo("allInstances", new libData.InstanceBanlistUpdateEvent(name, banned, reason)); } async handleUserSetWhitelistedRequest(request) { @@ -922,7 +922,7 @@ class ControlConnection extends BaseConnection { user.isWhitelisted = whitelisted; this._controller.userUpdated(user); - this._controller.sendTo(new libData.InstanceWhitelistUpdateEvent(name, whitelisted), "allInstances"); + this._controller.sendTo("allInstances", new libData.InstanceWhitelistUpdateEvent(name, whitelisted)); } async handleUserDeleteRequest(request) { @@ -937,13 +937,13 @@ class ControlConnection extends BaseConnection { this._controller.userUpdated(user); if (user.is_admin) { - this._controller.sendTo(new libData.InstanceAdminlistUpdateEvent(name, false), "allInstances"); + this._controller.sendTo("allInstances", new libData.InstanceAdminlistUpdateEvent(name, false)); } if (user.is_whitelisted) { - this._controller.sendTo(new libData.InstanceWhitelistUpdateEvent(name, false), "allInstances"); + this._controller.sendTo("allInstances", new libData.InstanceWhitelistUpdateEvent(name, false)); } if (user.is_banned) { - this._controller.sendTo(new libData.InstanceBanlistUpdateEvent(name, false, ""), "allInstances"); + this._controller.sendTo("allInstances", new libData.InstanceBanlistUpdateEvent(name, false, "")); } } diff --git a/packages/controller/src/Controller.js b/packages/controller/src/Controller.js index 26db2fa4..6d9ecaed 100644 --- a/packages/controller/src/Controller.js +++ b/packages/controller/src/Controller.js @@ -715,7 +715,7 @@ class Controller { let instance = this.getRequestInstance(instanceId); let hostId = instance.config.get("instance.assigned_host"); if (hostId !== null) { - await this.sendTo(new libData.InstanceDeleteInternalRequest(instanceId), { hostId }); + await this.sendTo({ hostId }, new libData.InstanceDeleteInternalRequest(instanceId)); } this.instances.delete(instanceId); @@ -1028,13 +1028,13 @@ class Controller { * destination argument supports address shorthands, see {@link * module:lib/data.Address.fromShorthand} * - * @param {*} requestOrEvent - The request or event to send. * @param {*|module:lib/data.Address} address - Where to send it. + * @param {*} requestOrEvent - The request or event to send. * @returns {Promise<*>|undefined} * Promise that resolves to the response if a request was sent or * undefined if it was an event. */ - sendTo(requestOrEvent, address) { + sendTo(address, requestOrEvent) { let dst = libData.Address.fromShorthand(address); if (requestOrEvent.constructor.type === "request") { return this.sendRequest(requestOrEvent, dst); diff --git a/packages/ctl/src/commands.js b/packages/ctl/src/commands.js index fe847467..c3a437e5 100644 --- a/packages/ctl/src/commands.js +++ b/packages/ctl/src/commands.js @@ -515,7 +515,7 @@ instanceSaveCommands.add(new libCommand.Command({ }], handler: async function(args, control) { let instanceId = await libCommand.resolveInstance(control, args.instance); - let saves = await control.sendTo(new libData.InstanceListSavesRequest(), { instanceId }); + let saves = await control.sendTo({ instanceId }, new libData.InstanceListSavesRequest()); for (let entry of saves) { entry.mtime = new Date(entry.mtimeMs).toLocaleString(); delete entry.mtimeMs; @@ -540,7 +540,8 @@ instanceSaveCommands.add(new libCommand.Command({ let { seed, mapGenSettings, mapSettings } = await loadMapSettings(args); await control.setLogSubscriptions({ instanceIds: [instanceId] }); await control.sendTo( - new libData.InstanceCreateSaveRequest(args.name, seed, mapGenSettings, mapSettings), { instanceId } + { instanceId }, + new libData.InstanceCreateSaveRequest(args.name, seed, mapGenSettings, mapSettings), ); }, })); @@ -711,7 +712,7 @@ instanceCommands.add(new libCommand.Command({ handler: async function(args, control) { let instanceId = await libCommand.resolveInstance(control, args.instance); await control.setLogSubscriptions({ instanceIds: [instanceId] }); - await control.sendTo(new libData.InstanceExportDataRequest(), { instanceId }); + await control.sendTo({ instanceId }, new libData.InstanceExportDataRequest()); }, })); @@ -722,7 +723,7 @@ instanceCommands.add(new libCommand.Command({ handler: async function(args, control) { let instanceId = await libCommand.resolveInstance(control, args.instance); await control.setLogSubscriptions({ instanceIds: [instanceId] }); - await control.sendTo(new libData.InstanceExtractPlayersRequest(), { instanceId }); + await control.sendTo({ instanceId }, new libData.InstanceExtractPlayersRequest()); }, })); @@ -737,7 +738,7 @@ instanceCommands.add(new libCommand.Command({ handler: async function(args, control) { let instanceId = await libCommand.resolveInstance(control, args.instance); await control.setLogSubscriptions({ instanceIds: [instanceId] }); - await control.sendTo(new libData.InstanceStartRequest(args.save), { instanceId }); + await control.sendTo({ instanceId }, new libData.InstanceStartRequest(args.save)); control.keepOpen = args.keepOpen; }, })); @@ -759,7 +760,8 @@ instanceCommands.add(new libCommand.Command({ await control.setLogSubscriptions({ instanceIds: [instanceId] }); let { seed, mapGenSettings, mapSettings } = await loadMapSettings(args); await control.sendTo( - new libData.InstanceLoadScenarioRequest(args.scenario, seed, mapGenSettings, mapSettings), { instanceId } + { instanceId }, + new libData.InstanceLoadScenarioRequest(args.scenario, seed, mapGenSettings, mapSettings), ); control.keepOpen = args.keepOpen; }, @@ -772,7 +774,7 @@ instanceCommands.add(new libCommand.Command({ handler: async function(args, control) { let instanceId = await libCommand.resolveInstance(control, args.instance); await control.setLogSubscriptions({ instanceIds: [instanceId] }); - await control.sendTo(new libData.InstanceStopRequest(args.save), { instanceId }); + await control.sendTo({ instanceId }, new libData.InstanceStopRequest(args.save)); }, })); @@ -783,7 +785,7 @@ instanceCommands.add(new libCommand.Command({ handler: async function(args, control) { let instanceId = await libCommand.resolveInstance(control, args.instance); await control.setLogSubscriptions({ instanceIds: [instanceId] }); - await control.sendTo(new libData.InstanceKillRequest(args.save), { instanceId }); + await control.sendTo({ instanceId }, new libData.InstanceKillRequest(args.save)); }, })); @@ -804,8 +806,8 @@ instanceCommands.add(new libCommand.Command({ }], handler: async function(args, control) { let result = await control.sendTo( - new libData.InstanceSendRconRequest(args.command), { instanceId: await libCommand.resolveInstance(control, args.instance) }, + new libData.InstanceSendRconRequest(args.command), ); // Factorio includes a newline in its response output. diff --git a/packages/host/src/Instance.js b/packages/host/src/Instance.js index b9d9c2ff..d4e2b8aa 100644 --- a/packages/host/src/Instance.js +++ b/packages/host/src/Instance.js @@ -303,7 +303,7 @@ class Instance extends libLink.Link { name, stats, }; - this.sendTo(new libData.InstancePlayerUpdateEvent("join", name, undefined, stats), "controller"); + this.sendTo("controller", new libData.InstancePlayerUpdateEvent("join", name, undefined, stats)); libPlugin.invokeHook(this.plugins, "onPlayerEvent", event); } @@ -325,7 +325,7 @@ class Instance extends libLink.Link { reason, stats, }; - this.sendTo(new libData.InstancePlayerUpdateEvent("leave", name, reason, stats), "controller"); + this.sendTo("controller", new libData.InstancePlayerUpdateEvent("leave", name, reason, stats)); libPlugin.invokeHook(this.plugins, "onPlayerEvent", event); } @@ -353,7 +353,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " "); name, stats: stats.toJSON(), }; - this.sendTo(new libData.InstancePlayerUpdateEvent("import", name, undefined, stats), "controller"); + this.sendTo("controller", new libData.InstancePlayerUpdateEvent("import", name, undefined, stats)); libPlugin.invokeHook(this.plugins, "onPlayerEvent", event); count += 1; } @@ -405,10 +405,13 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " "); } async sendSaveListUpdate() { - this.sendTo(new libData.InstanceSaveListUpdateEvent( - this.id, - await Instance.listSaves(this.path("saves"), this._loadedSave), - ), "controller"); + this.sendTo( + "controller", + new libData.InstanceSaveListUpdateEvent( + this.id, + await Instance.listSaves(this.path("saves"), this._loadedSave), + ), + ); } async _autosave(name) { @@ -436,12 +439,12 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " "); notifyStatus(status) { this._status = status; this.sendTo( + "controller", new libData.InstanceStatusChangedEvent( this.id, status, this.server && this.server.gamePort || this.config.get("factorio.game_port") || null, ), - "controller" ); } @@ -622,9 +625,9 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " "); const modPackId = this.config.get("factorio.mod_pack"); let modPack; if (modPackId === null) { - modPack = await this.sendTo(new libData.ModPackGetDefaultRequest(), "controller"); + modPack = await this.sendTo("controller", new libData.ModPackGetDefaultRequest()); } else { - modPack = await this.sendTo(new libData.ModPackGetRequest(modPackId), "controller"); + modPack = await this.sendTo("controller", new libData.ModPackGetRequest(modPackId)); } this.activeModPack = modPack; diff --git a/packages/lib/command.js b/packages/lib/command.js index ad9b1bd0..59eea880 100644 --- a/packages/lib/command.js +++ b/packages/lib/command.js @@ -151,7 +151,7 @@ async function resolveHost(client, hostName) { if (/^-?\d+$/.test(hostName)) { hostId = parseInt(hostName, 10); } else { - let hosts = await client.sendTo(new libData.HostListRequest(), "controller"); + let hosts = await client.sendTo("controller", new libData.HostListRequest()); for (let host of hosts) { if (host.name === hostName) { hostId = host.id; @@ -183,7 +183,7 @@ async function resolveInstance(client, instanceName) { if (/^-?\d+$/.test(instanceName)) { instanceId = parseInt(instanceName, 10); } else { - let instances = await client.sendTo(new libData.InstanceDetailsListRequest(), "controller"); + let instances = await client.sendTo("controller", new libData.InstanceDetailsListRequest()); for (let instance of instances) { if (instance.name === instanceName) { instanceId = instance.id; @@ -216,7 +216,7 @@ async function resolveModPack(client, modPackName) { if (/^-?\d+/.test(modPackName)) { modPackId = parseInt(modPackName, 10); } else { - let modPacks = await client.sendTo(new libData.ModPackListRequest(), "controller"); + let modPacks = await client.sendTo("controller", new libData.ModPackListRequest()); for (let modPack of modPacks) { if (modPack.name === modPackName) { modPackId = modPack.id; @@ -244,7 +244,7 @@ async function resolveModPack(client, modPackName) { * @static */ async function retrieveRole(client, roleName) { - let roles = await client.sendTo(new libData.RoleListRequest(), "controller"); + let roles = await client.sendTo("controller", new libData.RoleListRequest()); let resolvedRole; if (/^-?\d+$/.test(roleName)) { diff --git a/packages/lib/link/link.js b/packages/lib/link/link.js index c21592c7..54c33afe 100644 --- a/packages/lib/link/link.js +++ b/packages/lib/link/link.js @@ -374,19 +374,19 @@ class Link { * undefined if it was an event. */ send(requestOrEvent) { - return this.sendTo(requestOrEvent, this.connector.dst); + return this.sendTo(this.connector.dst, requestOrEvent); } /** * Send a request or event to the given address * - * @param {*} requestOrEvent - Request or event to send * @param {*|module:lib/data.Address} destination - Where to send it + * @param {*} requestOrEvent - Request or event to send * @returns {Promise<*>|undefined} * Promise that resolves to the response if a request was sent or * undefined if it was an event. */ - sendTo(requestOrEvent, destination) { + sendTo(destination, requestOrEvent) { let dst = libData.Address.fromShorthand(destination); if (requestOrEvent.constructor.type === "request") { diff --git a/packages/web_ui/src/components/CreateSaveModal.jsx b/packages/web_ui/src/components/CreateSaveModal.jsx index a0ac9511..4f8663d3 100644 --- a/packages/web_ui/src/components/CreateSaveModal.jsx +++ b/packages/web_ui/src/components/CreateSaveModal.jsx @@ -52,8 +52,8 @@ export default function CreateSaveModal(props) { setCreatingSave(true); control.sendTo( + { instanceId: props.instance.id }, new libData.InstanceCreateSaveRequest(name, seed, mapGenSettings, mapSettings), - { instanceId: props.instance.id } ).then(() => { form.resetFields(); setVisible(false); diff --git a/packages/web_ui/src/components/InstanceRcon.jsx b/packages/web_ui/src/components/InstanceRcon.jsx index ebf393a0..3151bc83 100644 --- a/packages/web_ui/src/components/InstanceRcon.jsx +++ b/packages/web_ui/src/components/InstanceRcon.jsx @@ -23,8 +23,8 @@ export default function InstanceRcon(props) { setRunning(true); try { let result = await control.sendTo( + { instanceId: props.id }, new libData.InstanceSendRconRequest(command), - { instanceId: props.id } ); setOutput(result); } finally { diff --git a/packages/web_ui/src/components/InstanceViewPage.jsx b/packages/web_ui/src/components/InstanceViewPage.jsx index 6a8bf784..74a74335 100644 --- a/packages/web_ui/src/components/InstanceViewPage.jsx +++ b/packages/web_ui/src/components/InstanceViewPage.jsx @@ -132,7 +132,8 @@ export default function InstanceViewPage(props) { if (key === "export") { setExportingData(true); control.sendTo( - new libData.InstanceExportDataRequest(), { instanceId } + { instanceId }, + new libData.InstanceExportDataRequest(), ).catch( notifyErrorHandler("Error exporting data") ).finally(() => { @@ -141,12 +142,14 @@ export default function InstanceViewPage(props) { } else if (key === "extract") { control.sendTo( - new libData.InstanceExtractPlayersRequest(), { instanceId } + { instanceId }, + new libData.InstanceExtractPlayersRequest(), ).catch(notifyErrorHandler("Error extracting player data")); } else if (key === "kill") { control.sendTo( - new libData.InstanceKillRequest(), { instanceId } + { instanceId }, + new libData.InstanceKillRequest(), ).catch(notifyErrorHandler("Error killing instance")); } else if (key === "delete") { diff --git a/packages/web_ui/src/components/InstancesPage.jsx b/packages/web_ui/src/components/InstancesPage.jsx index 10ca2655..b943c04e 100644 --- a/packages/web_ui/src/components/InstancesPage.jsx +++ b/packages/web_ui/src/components/InstancesPage.jsx @@ -84,7 +84,8 @@ export default function InstancesPage() { &&