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:
Hornwitser committed 2019-11-30 00:08:10 +01:00
1 parent 278dc9b2d2
commit 039b41d3e6
5 files changed
+68 -57

No files matched your search

+8 -8
View File
@@ -289,7 +289,7 @@ class Slave extends link.Client {
*
* Called by the handler for InstanceRequest.
*/
async forwardInstanceRequest(message, handler) {
async forwardInstanceRequest(handler, message) {
let instance = this.instances.get(message.data.instance_id);
if (!instance) {
throw new errors.RequestError(`Instance with ID ${instanceId} does not exist`);
@@ -312,19 +312,19 @@ class Slave extends link.Client {
this.updateInstances();
}
async createSaveRequestHandler(instance, message) {
async createSaveInstanceRequestHandler(instance, message) {
await instance.createSave();
}
async startInstanceRequestHandler(instance, message) {
async startInstanceInstanceRequestHandler(instance, message) {
await instance.start(this.config, this.socket);
}
async stopInstanceRequestHandler(instance, message) {
async stopInstanceInstanceRequestHandler(instance, message) {
await instance.stop();
}
async deleteInstanceRequestHandler(instance, message) {
async deleteInstanceInstanceRequestHandler(instance, message) {
await instance.stop();
this.instances.delete(instance.config.id);
@@ -332,7 +332,7 @@ class Slave extends link.Client {
this.updateInstances();
}
async sendRconRequestHandler(instance, message) {
async sendRconInstanceRequestHandler(instance, message) {
let result = await instance.server.sendRcon(message.data.command);
return { result };
}
@@ -378,7 +378,7 @@ class Slave extends link.Client {
hookInstance(instance) {
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,
});
}
link.events.updateInstances.send(this, { instances: list });
link.messages.updateInstances.send(this, { instances: list });
}
async start() {
+13 -13
View File
@@ -103,7 +103,7 @@ async function resolveInstance(control, instanceName) {
if (/^-?\d+$/.test(instanceName)) {
instanceId = parseInt(instanceName, 10);
} else {
let response = await link.requests.listInstances.send(control);
let response = await link.messages.listInstances.send(control);
for (let instance of response.list) {
if (instance.name === instanceName) {
instanceId = instance.id;
@@ -123,7 +123,7 @@ let commands = [];
commands.push(new Command({
definition: ["list-slaves", "List slaves connected to the master"],
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));
},
}));
@@ -131,7 +131,7 @@ commands.push(new Command({
commands.push(new Command({
definition: ["list-instances", "List instances known to the master"],
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));
}
}));
@@ -148,7 +148,7 @@ commands.push(new Command({
if (/^-?\d+$/.test(args.slave)) {
slaveId = parseInt(args.slave, 10);
} else {
let response = await link.requests.listSlaves.send(control);
let response = await link.messages.listSlaves.send(control);
for (let slave of response.list) {
if (slave.name === args.slave) {
slaveId = slave.id;
@@ -160,7 +160,7 @@ commands.push(new Command({
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,
slave_id: slaveId,
});
@@ -176,8 +176,8 @@ commands.push(new Command({
}],
handler: async function(args, control) {
let instanceId = await resolveInstance(control, args.instance);
await link.requests.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
let response = await link.requests.createSave.send(control, {
await link.messages.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
let response = await link.messages.createSave.send(control, {
instance_id: instanceId,
});
console.log(response);
@@ -192,8 +192,8 @@ commands.push(new Command({
}],
handler: async function(args, control) {
let instanceId = await resolveInstance(control, args.instance);
await link.requests.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
let response = await link.requests.startInstance.send(control, {
await link.messages.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
let response = await link.messages.startInstance.send(control, {
instance_id: instanceId,
});
console.log(response);
@@ -208,8 +208,8 @@ commands.push(new Command({
}],
handler: async function(args, control) {
let instanceId = await resolveInstance(control, args.instance);
await link.requests.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
let response = await link.requests.stopInstance.send(control, {
await link.messages.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] });
let response = await link.messages.stopInstance.send(control, {
instance_id: instanceId,
});
console.log(response);
@@ -223,7 +223,7 @@ commands.push(new Command({
});
}],
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),
});
console.log(response);
@@ -238,7 +238,7 @@ commands.push(new Command({
});
}],
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),
command: args.command
})
+15 -2
View File
@@ -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
--------
@@ -100,8 +113,8 @@ error property in the data payload is sent instead.
- seq - integer - The seq number of the request message.
- error - string - Human readable text message describing the error.
See [lib/link/requests.js](lib/link/requests.js) for the recognized
requests and their implementation.
See [lib/link/messages.js](lib/link/messages.js) for the recognized
requests and their contents.
Closing the connection
+29 -31
View File
@@ -182,11 +182,15 @@ class InstanceRequest {
*/
attach(link, handler) {
this._masterRequest.attach(link, async (message) => {
return await link.forwardInstanceRequest(message, this);
});
this._slaveRequest.attach(link, async (message) => {
return await link.forwardInstanceRequest(message, handler);
return await link.forwardInstanceRequest(this, message);
});
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
requests.listSlaves = new Request({
messages.listSlaves = new Request({
type: 'list_slaves',
sources: ['control'],
targets: ['master'],
@@ -240,7 +244,7 @@ requests.listSlaves = new Request({
},
});
requests.listInstances = new Request({
messages.listInstances = new Request({
type: 'list_instances',
sources: ['control'],
targets: ['master'],
@@ -260,7 +264,7 @@ requests.listInstances = new Request({
},
});
requests.setInstanceOutputSubscriptions = new Request({
messages.setInstanceOutputSubscriptions = new Request({
type: 'set_instance_output_subscriptions',
sources: ['control'],
targets: ['master'],
@@ -272,7 +276,7 @@ requests.setInstanceOutputSubscriptions = new Request({
},
});
requests.createInstanceCommand = new Request({
messages.createInstanceCommand = new Request({
type: 'create_instance',
sources: ['control'],
targets: ['master'],
@@ -282,7 +286,7 @@ requests.createInstanceCommand = new Request({
},
});
requests.createInstance = new Request({
messages.createInstance = new Request({
type: 'create_instance',
sources: ['master'],
targets: ['slave'],
@@ -310,27 +314,27 @@ requests.createInstance = new Request({
});
requests.createSave = new InstanceRequest({
messages.createSave = new InstanceRequest({
type: 'create_save',
sources: ['control'],
});
requests.startInstance = new InstanceRequest({
messages.startInstance = new InstanceRequest({
type: 'start_instance',
sources: ['control'],
});
requests.stopInstance = new InstanceRequest({
messages.stopInstance = new InstanceRequest({
type: 'stop_instance',
sources: ['control'],
});
requests.deleteInstance = new InstanceRequest({
messages.deleteInstance = new InstanceRequest({
type: 'delete_instance',
sources: ['control'],
});
requests.sendRcon = new InstanceRequest({
messages.sendRcon = new InstanceRequest({
type: 'send_rcon',
sources: ['control'],
requestProperties: {
@@ -388,8 +392,7 @@ class Event {
}
}
let events = {};
events.updateInstances = new Event({
messages.updateInstances = new Event({
type: 'update_instances',
sources: ['slave'],
targets: ['master'],
@@ -409,7 +412,7 @@ events.updateInstances = new Event({
},
});
events.instanceOutput = new Event({
messages.instanceOutput = new Event({
type: 'instance_output',
sources: ['slave', 'master'],
targets: ['master', 'control'],
@@ -438,22 +441,18 @@ events.instanceOutput = new Event({
/**
* Attaches all requests and events to the given link
*
* Loops through all requests and events defined and attaches all of
* them to the link. For requests the handler used is looked up on the
* link instance as the name of the request + 'RequestHandler'. For
* events the the handler used is looked up on the link instance as the
* name of the event + 'EventHandler'.
* Loops through all builtin messages defined and attaches all of them
* to the link. The handler used for the messageis looked up on the
* link instance as the concatenation of the name of the message, the
* name of message class, and 'Handler'.
*/
function attachAllMessages(link) {
for (let [name, request] of Object.entries(requests)) {
request.attach(link, link[name + 'RequestHandler']);
}
for (let [name, event] of Object.entries(events)) {
event.attach(link, link[name + 'EventHandler']);
for (let [name, message] of Object.entries(messages)) {
message.attach(link, link[name + message.constructor.name + 'Handler']);
}
}
module.exports = {
Request,
InstanceRequest,
@@ -461,6 +460,5 @@ module.exports = {
attachAllMessages,
requests,
events,
messages,
}
+3 -3
View File
@@ -254,7 +254,7 @@ class BaseConnection extends link.Connection {
link.attachAllMessages(this);
}
async forwardInstanceRequest(message, request) {
async forwardInstanceRequest(request, message) {
let instance = db.instances.get(message.data.instance_id);
if (!instance) {
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");
}
await link.requests.createInstance.send(connection, {
await link.messages.createInstance.send(connection, {
id: Math.random() * 2**31 | 0, // TODO: add id option
options: {
'name': name,
@@ -414,7 +414,7 @@ class SlaveConnection extends BaseConnection {
let { instance_id, output } = message.data;
for (let controlConnection of controlConnections) {
if (controlConnection.instanceOutputSubscriptions.has(instance_id)) {
link.events.instanceOutput.send(controlConnection, message.data);
link.messages.instanceOutput.send(controlConnection, message.data);
}
}
}