mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 16:23:12 +09:00
Move events and requests into messages
Merge the events and requests categories in lib/link into messages and use the class name of the message to determine which handler to call. Also swaps the order of the arguments for forwardInstanceRequest to be consistent with the rest of the lib/link code and fixes up the docs for requests and events.
This commit is contained in:
1 parent
278dc9b2d2
commit
039b41d3e6
5 files changed
+68
-57
No files matched your search
@@ -289,7 +289,7 @@ class Slave extends link.Client {
|
|||||||
*
|
*
|
||||||
* Called by the handler for InstanceRequest.
|
* Called by the handler for InstanceRequest.
|
||||||
*/
|
*/
|
||||||
async forwardInstanceRequest(message, handler) {
|
async forwardInstanceRequest(handler, message) {
|
||||||
let instance = this.instances.get(message.data.instance_id);
|
let instance = this.instances.get(message.data.instance_id);
|
||||||
if (!instance) {
|
if (!instance) {
|
||||||
throw new errors.RequestError(`Instance with ID ${instanceId} does not exist`);
|
throw new errors.RequestError(`Instance with ID ${instanceId} does not exist`);
|
||||||
@@ -312,19 +312,19 @@ class Slave extends link.Client {
|
|||||||
this.updateInstances();
|
this.updateInstances();
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSaveRequestHandler(instance, message) {
|
async createSaveInstanceRequestHandler(instance, message) {
|
||||||
await instance.createSave();
|
await instance.createSave();
|
||||||
}
|
}
|
||||||
|
|
||||||
async startInstanceRequestHandler(instance, message) {
|
async startInstanceInstanceRequestHandler(instance, message) {
|
||||||
await instance.start(this.config, this.socket);
|
await instance.start(this.config, this.socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
async stopInstanceRequestHandler(instance, message) {
|
async stopInstanceInstanceRequestHandler(instance, message) {
|
||||||
await instance.stop();
|
await instance.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteInstanceRequestHandler(instance, message) {
|
async deleteInstanceInstanceRequestHandler(instance, message) {
|
||||||
await instance.stop();
|
await instance.stop();
|
||||||
this.instances.delete(instance.config.id);
|
this.instances.delete(instance.config.id);
|
||||||
|
|
||||||
@@ -332,7 +332,7 @@ class Slave extends link.Client {
|
|||||||
this.updateInstances();
|
this.updateInstances();
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendRconRequestHandler(instance, message) {
|
async sendRconInstanceRequestHandler(instance, message) {
|
||||||
let result = await instance.server.sendRcon(message.data.command);
|
let result = await instance.server.sendRcon(message.data.command);
|
||||||
return { result };
|
return { result };
|
||||||
}
|
}
|
||||||
@@ -378,7 +378,7 @@ class Slave extends link.Client {
|
|||||||
|
|
||||||
hookInstance(instance) {
|
hookInstance(instance) {
|
||||||
instance.server.on('output', (output) => {
|
instance.server.on('output', (output) => {
|
||||||
link.events.instanceOutput.send(this, { instance_id: instance.config.id, output })
|
link.messages.instanceOutput.send(this, { instance_id: instance.config.id, output })
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,7 +391,7 @@ class Slave extends link.Client {
|
|||||||
name: instance.config.name,
|
name: instance.config.name,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
link.events.updateInstances.send(this, { instances: list });
|
link.messages.updateInstances.send(this, { instances: list });
|
||||||
}
|
}
|
||||||
|
|
||||||
async start() {
|
async start() {
|
||||||
|
|||||||
+13
-13
@@ -103,7 +103,7 @@ async function resolveInstance(control, instanceName) {
|
|||||||
if (/^-?\d+$/.test(instanceName)) {
|
if (/^-?\d+$/.test(instanceName)) {
|
||||||
instanceId = parseInt(instanceName, 10);
|
instanceId = parseInt(instanceName, 10);
|
||||||
} else {
|
} else {
|
||||||
let response = await link.requests.listInstances.send(control);
|
let response = await link.messages.listInstances.send(control);
|
||||||
for (let instance of response.list) {
|
for (let instance of response.list) {
|
||||||
if (instance.name === instanceName) {
|
if (instance.name === instanceName) {
|
||||||
instanceId = instance.id;
|
instanceId = instance.id;
|
||||||
@@ -123,7 +123,7 @@ let commands = [];
|
|||||||
commands.push(new Command({
|
commands.push(new Command({
|
||||||
definition: ["list-slaves", "List slaves connected to the master"],
|
definition: ["list-slaves", "List slaves connected to the master"],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await link.requests.listSlaves.send(control);
|
let response = await link.messages.listSlaves.send(control);
|
||||||
console.log(asTable(response.list));
|
console.log(asTable(response.list));
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -131,7 +131,7 @@ commands.push(new Command({
|
|||||||
commands.push(new Command({
|
commands.push(new Command({
|
||||||
definition: ["list-instances", "List instances known to the master"],
|
definition: ["list-instances", "List instances known to the master"],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await link.requests.listInstances.send(control);
|
let response = await link.messages.listInstances.send(control);
|
||||||
console.log(asTable(response.list));
|
console.log(asTable(response.list));
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
@@ -148,7 +148,7 @@ commands.push(new Command({
|
|||||||
if (/^-?\d+$/.test(args.slave)) {
|
if (/^-?\d+$/.test(args.slave)) {
|
||||||
slaveId = parseInt(args.slave, 10);
|
slaveId = parseInt(args.slave, 10);
|
||||||
} else {
|
} else {
|
||||||
let response = await link.requests.listSlaves.send(control);
|
let response = await link.messages.listSlaves.send(control);
|
||||||
for (let slave of response.list) {
|
for (let slave of response.list) {
|
||||||
if (slave.name === args.slave) {
|
if (slave.name === args.slave) {
|
||||||
slaveId = slave.id;
|
slaveId = slave.id;
|
||||||
@@ -160,7 +160,7 @@ commands.push(new Command({
|
|||||||
throw new errors.CommandError(`No slave named ${args.slave}`);
|
throw new errors.CommandError(`No slave named ${args.slave}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let response = await link.requests.createInstanceCommand.send(control, {
|
let response = await link.messages.createInstanceCommand.send(control, {
|
||||||
name: args.name,
|
name: args.name,
|
||||||
slave_id: slaveId,
|
slave_id: slaveId,
|
||||||
});
|
});
|
||||||
@@ -176,8 +176,8 @@ commands.push(new Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await resolveInstance(control, args.instance);
|
let instanceId = await resolveInstance(control, args.instance);
|
||||||
await link.requests.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
|
await link.messages.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
|
||||||
let response = await link.requests.createSave.send(control, {
|
let response = await link.messages.createSave.send(control, {
|
||||||
instance_id: instanceId,
|
instance_id: instanceId,
|
||||||
});
|
});
|
||||||
console.log(response);
|
console.log(response);
|
||||||
@@ -192,8 +192,8 @@ commands.push(new Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await resolveInstance(control, args.instance);
|
let instanceId = await resolveInstance(control, args.instance);
|
||||||
await link.requests.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
|
await link.messages.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
|
||||||
let response = await link.requests.startInstance.send(control, {
|
let response = await link.messages.startInstance.send(control, {
|
||||||
instance_id: instanceId,
|
instance_id: instanceId,
|
||||||
});
|
});
|
||||||
console.log(response);
|
console.log(response);
|
||||||
@@ -208,8 +208,8 @@ commands.push(new Command({
|
|||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let instanceId = await resolveInstance(control, args.instance);
|
let instanceId = await resolveInstance(control, args.instance);
|
||||||
await link.requests.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
|
await link.messages.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
|
||||||
let response = await link.requests.stopInstance.send(control, {
|
let response = await link.messages.stopInstance.send(control, {
|
||||||
instance_id: instanceId,
|
instance_id: instanceId,
|
||||||
});
|
});
|
||||||
console.log(response);
|
console.log(response);
|
||||||
@@ -223,7 +223,7 @@ commands.push(new Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await link.requests.deleteInstance.send(control, {
|
let response = await link.messages.deleteInstance.send(control, {
|
||||||
instance_id: await resolveInstance(control, args.instance),
|
instance_id: await resolveInstance(control, args.instance),
|
||||||
});
|
});
|
||||||
console.log(response);
|
console.log(response);
|
||||||
@@ -238,7 +238,7 @@ commands.push(new Command({
|
|||||||
});
|
});
|
||||||
}],
|
}],
|
||||||
handler: async function(args, control) {
|
handler: async function(args, control) {
|
||||||
let response = await link.requests.sendRcon.send(control, {
|
let response = await link.messages.sendRcon.send(control, {
|
||||||
instance_id: await resolveInstance(control, args.instance),
|
instance_id: await resolveInstance(control, args.instance),
|
||||||
command: args.command
|
command: args.command
|
||||||
})
|
})
|
||||||
|
|||||||
+15
-2
@@ -80,6 +80,19 @@ with an empty data payload.
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Events
|
||||||
|
------
|
||||||
|
|
||||||
|
Events are messages send in one direction over the link, that the
|
||||||
|
receiver is expected to act upon, but not make any replies (for that
|
||||||
|
there's [Requests](#requests). By convention a message types ending in
|
||||||
|
`*_event` is an event. The data payload of an event consists solely of
|
||||||
|
event specific properties.
|
||||||
|
|
||||||
|
See [lib/link/messages.js](lib/link/messages.js) for the recognized
|
||||||
|
events and their contents.
|
||||||
|
|
||||||
|
|
||||||
Requests
|
Requests
|
||||||
--------
|
--------
|
||||||
|
|
||||||
@@ -100,8 +113,8 @@ error property in the data payload is sent instead.
|
|||||||
- seq - integer - The seq number of the request message.
|
- seq - integer - The seq number of the request message.
|
||||||
- error - string - Human readable text message describing the error.
|
- error - string - Human readable text message describing the error.
|
||||||
|
|
||||||
See [lib/link/requests.js](lib/link/requests.js) for the recognized
|
See [lib/link/messages.js](lib/link/messages.js) for the recognized
|
||||||
requests and their implementation.
|
requests and their contents.
|
||||||
|
|
||||||
|
|
||||||
Closing the connection
|
Closing the connection
|
||||||
|
|||||||
+29
-31
@@ -182,11 +182,15 @@ class InstanceRequest {
|
|||||||
*/
|
*/
|
||||||
attach(link, handler) {
|
attach(link, handler) {
|
||||||
this._masterRequest.attach(link, async (message) => {
|
this._masterRequest.attach(link, async (message) => {
|
||||||
return await link.forwardInstanceRequest(message, this);
|
return await link.forwardInstanceRequest(this, message);
|
||||||
});
|
|
||||||
this._slaveRequest.attach(link, async (message) => {
|
|
||||||
return await link.forwardInstanceRequest(message, handler);
|
|
||||||
});
|
});
|
||||||
|
if (handler) {
|
||||||
|
this._slaveRequest.attach(link, async (message) => {
|
||||||
|
return await link.forwardInstanceRequest(handler, message);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this._slaveRequest.attach(link);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -215,10 +219,10 @@ class InstanceRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let requests = {}
|
let messages = {}
|
||||||
|
|
||||||
// Management requests
|
// Management requests
|
||||||
requests.listSlaves = new Request({
|
messages.listSlaves = new Request({
|
||||||
type: 'list_slaves',
|
type: 'list_slaves',
|
||||||
sources: ['control'],
|
sources: ['control'],
|
||||||
targets: ['master'],
|
targets: ['master'],
|
||||||
@@ -240,7 +244,7 @@ requests.listSlaves = new Request({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
requests.listInstances = new Request({
|
messages.listInstances = new Request({
|
||||||
type: 'list_instances',
|
type: 'list_instances',
|
||||||
sources: ['control'],
|
sources: ['control'],
|
||||||
targets: ['master'],
|
targets: ['master'],
|
||||||
@@ -260,7 +264,7 @@ requests.listInstances = new Request({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
requests.setInstanceOutputSubscriptions = new Request({
|
messages.setInstanceOutputSubscriptions = new Request({
|
||||||
type: 'set_instance_output_subscriptions',
|
type: 'set_instance_output_subscriptions',
|
||||||
sources: ['control'],
|
sources: ['control'],
|
||||||
targets: ['master'],
|
targets: ['master'],
|
||||||
@@ -272,7 +276,7 @@ requests.setInstanceOutputSubscriptions = new Request({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
requests.createInstanceCommand = new Request({
|
messages.createInstanceCommand = new Request({
|
||||||
type: 'create_instance',
|
type: 'create_instance',
|
||||||
sources: ['control'],
|
sources: ['control'],
|
||||||
targets: ['master'],
|
targets: ['master'],
|
||||||
@@ -282,7 +286,7 @@ requests.createInstanceCommand = new Request({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
requests.createInstance = new Request({
|
messages.createInstance = new Request({
|
||||||
type: 'create_instance',
|
type: 'create_instance',
|
||||||
sources: ['master'],
|
sources: ['master'],
|
||||||
targets: ['slave'],
|
targets: ['slave'],
|
||||||
@@ -310,27 +314,27 @@ requests.createInstance = new Request({
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
requests.createSave = new InstanceRequest({
|
messages.createSave = new InstanceRequest({
|
||||||
type: 'create_save',
|
type: 'create_save',
|
||||||
sources: ['control'],
|
sources: ['control'],
|
||||||
});
|
});
|
||||||
|
|
||||||
requests.startInstance = new InstanceRequest({
|
messages.startInstance = new InstanceRequest({
|
||||||
type: 'start_instance',
|
type: 'start_instance',
|
||||||
sources: ['control'],
|
sources: ['control'],
|
||||||
});
|
});
|
||||||
|
|
||||||
requests.stopInstance = new InstanceRequest({
|
messages.stopInstance = new InstanceRequest({
|
||||||
type: 'stop_instance',
|
type: 'stop_instance',
|
||||||
sources: ['control'],
|
sources: ['control'],
|
||||||
});
|
});
|
||||||
|
|
||||||
requests.deleteInstance = new InstanceRequest({
|
messages.deleteInstance = new InstanceRequest({
|
||||||
type: 'delete_instance',
|
type: 'delete_instance',
|
||||||
sources: ['control'],
|
sources: ['control'],
|
||||||
});
|
});
|
||||||
|
|
||||||
requests.sendRcon = new InstanceRequest({
|
messages.sendRcon = new InstanceRequest({
|
||||||
type: 'send_rcon',
|
type: 'send_rcon',
|
||||||
sources: ['control'],
|
sources: ['control'],
|
||||||
requestProperties: {
|
requestProperties: {
|
||||||
@@ -388,8 +392,7 @@ class Event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let events = {};
|
messages.updateInstances = new Event({
|
||||||
events.updateInstances = new Event({
|
|
||||||
type: 'update_instances',
|
type: 'update_instances',
|
||||||
sources: ['slave'],
|
sources: ['slave'],
|
||||||
targets: ['master'],
|
targets: ['master'],
|
||||||
@@ -409,7 +412,7 @@ events.updateInstances = new Event({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
events.instanceOutput = new Event({
|
messages.instanceOutput = new Event({
|
||||||
type: 'instance_output',
|
type: 'instance_output',
|
||||||
sources: ['slave', 'master'],
|
sources: ['slave', 'master'],
|
||||||
targets: ['master', 'control'],
|
targets: ['master', 'control'],
|
||||||
@@ -438,22 +441,18 @@ events.instanceOutput = new Event({
|
|||||||
/**
|
/**
|
||||||
* Attaches all requests and events to the given link
|
* Attaches all requests and events to the given link
|
||||||
*
|
*
|
||||||
* Loops through all requests and events defined and attaches all of
|
* Loops through all builtin messages defined and attaches all of them
|
||||||
* them to the link. For requests the handler used is looked up on the
|
* to the link. The handler used for the messageis looked up on the
|
||||||
* link instance as the name of the request + 'RequestHandler'. For
|
* link instance as the concatenation of the name of the message, the
|
||||||
* events the the handler used is looked up on the link instance as the
|
* name of message class, and 'Handler'.
|
||||||
* name of the event + 'EventHandler'.
|
|
||||||
*/
|
*/
|
||||||
function attachAllMessages(link) {
|
function attachAllMessages(link) {
|
||||||
for (let [name, request] of Object.entries(requests)) {
|
for (let [name, message] of Object.entries(messages)) {
|
||||||
request.attach(link, link[name + 'RequestHandler']);
|
message.attach(link, link[name + message.constructor.name + 'Handler']);
|
||||||
}
|
|
||||||
|
|
||||||
for (let [name, event] of Object.entries(events)) {
|
|
||||||
event.attach(link, link[name + 'EventHandler']);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Request,
|
Request,
|
||||||
InstanceRequest,
|
InstanceRequest,
|
||||||
@@ -461,6 +460,5 @@ module.exports = {
|
|||||||
|
|
||||||
attachAllMessages,
|
attachAllMessages,
|
||||||
|
|
||||||
requests,
|
messages,
|
||||||
events,
|
|
||||||
}
|
}
|
||||||
@@ -254,7 +254,7 @@ class BaseConnection extends link.Connection {
|
|||||||
link.attachAllMessages(this);
|
link.attachAllMessages(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async forwardInstanceRequest(message, request) {
|
async forwardInstanceRequest(request, message) {
|
||||||
let instance = db.instances.get(message.data.instance_id);
|
let instance = db.instances.get(message.data.instance_id);
|
||||||
if (!instance) {
|
if (!instance) {
|
||||||
throw new errors.RequestError(`Instance with ID ${instance_id} does not exist`);
|
throw new errors.RequestError(`Instance with ID ${instance_id} does not exist`);
|
||||||
@@ -321,7 +321,7 @@ class ControlConnection extends BaseConnection {
|
|||||||
throw new errors.RequestError("Slave is not connected");
|
throw new errors.RequestError("Slave is not connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
await link.requests.createInstance.send(connection, {
|
await link.messages.createInstance.send(connection, {
|
||||||
id: Math.random() * 2**31 | 0, // TODO: add id option
|
id: Math.random() * 2**31 | 0, // TODO: add id option
|
||||||
options: {
|
options: {
|
||||||
'name': name,
|
'name': name,
|
||||||
@@ -414,7 +414,7 @@ class SlaveConnection extends BaseConnection {
|
|||||||
let { instance_id, output } = message.data;
|
let { instance_id, output } = message.data;
|
||||||
for (let controlConnection of controlConnections) {
|
for (let controlConnection of controlConnections) {
|
||||||
if (controlConnection.instanceOutputSubscriptions.has(instance_id)) {
|
if (controlConnection.instanceOutputSubscriptions.has(instance_id)) {
|
||||||
link.events.instanceOutput.send(controlConnection, message.data);
|
link.messages.instanceOutput.send(controlConnection, message.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user