mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Reverse the direction of the API for sending messages from
messageDefinition.send(link, { content });
to
link.send(new MessageClass(content));
and convert all message definitions to use the JSON serialisable class
pattern and explicit registration of message handlers, along with a
end-to-end based routing of messages instead of link to link.
The experience I gained from the existing way messages were defined and
sent over links was that it was difficult to understand for people
unfamiliar with the system. The automatic registration of message
handlers ment that code was being called as if by magic from nowhere
with no easy way to discover it. The manual forwarding and rules for
what can be sent where and how obscured its working.
The use of pure JSON as both input and output from message handlers made
it tedious to work with. Data had to be manually converted between the
program representation and the over the wire format, which made handler
code unplesant to write. Particularly because the over the wire format
used snake_case while the program code uses camelCase for variables.
The idea behind the decision to do it this way was to make it more clear
which data came from the wire and which from the program, and while this
worked it didn't make message handlers any nicer to write.
The direction of the API and magic registration of message handlers also
meant that a future migration to TypeScript would be troublesome. While
it may be possible to implement, the degree of TypeScript magic required
would go starkly against the principle of keep it simple stupid.
By refactoring to using the JSON serialisable class pattern most of
these issues go away. There is no longer a need to distinguish the on
wire format of messages with the program representation because the only
place that deals with the wire format of messages are the toJSON and
fromJSON methods on the classes themselves. Message handlers will work
with proper instances of classes instead of raw data, something that
makes them easier to write, and trivial to type with TypeScript.
End-to-end routing also simplifies sending messages that need to do
mulitple hops to reach the destination. And removes the need for having
all messages be specified in plugin's info module, as they can be routed
without having to be parsed completely.
The explicit registration of message handlers is something that I
thought would be tedious boilerplate code that could be automated away.
But experience now shows it to be the opposite. Allthough dull
repetition, the purpose it serves is to link together the code in a way
that humans can easily find. With explicit registration searching the
name of the message handler in the editor will show the registration that
ties it together with the type of message it handles and the kind of
registration. With implicit registration searching the name of the
message handler turns up only the handler with no clue as to what calls
it.
112 lines
3.5 KiB
JavaScript
112 lines
3.5 KiB
JavaScript
"use strict";
|
|
const assert = require("assert").strict;
|
|
const path = require("path");
|
|
|
|
const libConfig = require("@clusterio/lib/config");
|
|
const libData = require("@clusterio/lib/data");
|
|
const Host = require("@clusterio/host/src/Host");
|
|
|
|
describe("Host testing", function() {
|
|
before(function() {
|
|
libConfig.InstanceConfig.finalize();
|
|
});
|
|
|
|
describe("discoverInstances()", function() {
|
|
it("should discover test instance", async function() {
|
|
let instancePath = path.join("test", "file", "instances");
|
|
let instanceInfos = await Host._discoverInstances(instancePath);
|
|
|
|
let referenceConfig = new libConfig.InstanceConfig("host");
|
|
await referenceConfig.init();
|
|
referenceConfig.set("instance.id", 1);
|
|
referenceConfig.set("instance.name", "test");
|
|
|
|
assert.deepEqual(instanceInfos, new Map([
|
|
[1, {
|
|
config: referenceConfig,
|
|
path: path.join(instancePath, "test"),
|
|
}],
|
|
]));
|
|
});
|
|
});
|
|
|
|
describe("class Host", function() {
|
|
describe(".handleSyncUserListsEvent()", function() {
|
|
let mockHost;
|
|
beforeEach(function() {
|
|
mockHost = {
|
|
adminlist: new Set(),
|
|
whitelist: new Set(),
|
|
banlist: new Map(),
|
|
broadcasts: [],
|
|
broadcastEventToInstance(event) {
|
|
this.broadcasts.push(event);
|
|
},
|
|
handleSyncUserListsEvent: Host.prototype.handleSyncUserListsEvent,
|
|
syncLists(adminlist, banlist, whitelist) {
|
|
return this.handleSyncUserListsEvent(
|
|
new libData.SyncUserListsEvent(adminlist, banlist, whitelist)
|
|
);
|
|
},
|
|
};
|
|
});
|
|
|
|
it("should broadcast new entries to adminlist", async function() {
|
|
await mockHost.syncLists(["admin1"], [], []);
|
|
await mockHost.syncLists(["admin1", "admin2"], [], []);
|
|
|
|
assert.deepEqual(mockHost.broadcasts, [
|
|
new libData.InstanceAdminlistUpdateEvent("admin1", true),
|
|
new libData.InstanceAdminlistUpdateEvent("admin2", true),
|
|
]);
|
|
});
|
|
|
|
it("should broadcast removals from adminlist", async function() {
|
|
mockHost.adminlist.add("admin1").add("admin2");
|
|
await mockHost.syncLists(["admin1"], [], []);
|
|
assert.deepEqual(mockHost.broadcasts, [
|
|
new libData.InstanceAdminlistUpdateEvent("admin2", false),
|
|
]);
|
|
});
|
|
|
|
it("should broadcast new entries to whitelist", async function() {
|
|
await mockHost.syncLists([], [], ["player1"]);
|
|
await mockHost.syncLists([], [], ["player1", "player2"]);
|
|
|
|
assert.deepEqual(mockHost.broadcasts, [
|
|
new libData.InstanceWhitelistUpdateEvent("player1", true),
|
|
new libData.InstanceWhitelistUpdateEvent("player2", true),
|
|
]);
|
|
});
|
|
|
|
it("should broadcast removals from whitelist", async function() {
|
|
mockHost.whitelist.add("player1").add("player2");
|
|
await mockHost.syncLists([], [], ["player1"]);
|
|
|
|
assert.deepEqual(mockHost.broadcasts, [
|
|
new libData.InstanceWhitelistUpdateEvent("player2", false),
|
|
]);
|
|
});
|
|
|
|
it("should broadcast new entries to banlist", async function() {
|
|
await mockHost.syncLists([], [["badie1", "greifing"]], []);
|
|
await mockHost.syncLists([], [["badie1", "greifing"], ["badie2", "annoying"]], []);
|
|
|
|
assert.deepEqual(mockHost.broadcasts, [
|
|
new libData.InstanceBanlistUpdateEvent("badie1", true, "greifing"),
|
|
new libData.InstanceBanlistUpdateEvent("badie2", true, "annoying"),
|
|
]);
|
|
});
|
|
|
|
it("should broadcast removals to banlist", async function() {
|
|
mockHost.banlist.set("badie1", "greifing").set("badie2", "annoying");
|
|
await mockHost.syncLists([], [["badie1", "greifing"]], []);
|
|
|
|
assert.deepEqual(mockHost.broadcasts, [
|
|
new libData.InstanceBanlistUpdateEvent("badie2", false, ""),
|
|
]);
|
|
});
|
|
});
|
|
});
|
|
});
|