Replace sources/targets with links in messages

Replace the sources/targets concept with a links array in the message
definitions.  Fixes the problem of the actual links the messages applies
to being ambigious when there's multiple sources and targets.
This commit is contained in:
Hornwitser committed 2019-11-30 00:08:10 +01:00
1 parent 3be264c9a9
commit c6fd46e3c3
2 files changed
+18 -31

No files matched your search

+16 -27
View File
@@ -10,11 +10,10 @@ const errors = require("lib/errors");
*/ */
class Request { class Request {
constructor({ constructor({
type, sources, targets, requestProperties = {}, responseProperties = {} type, links, requestProperties = {}, responseProperties = {}
}) { }) {
this.type = type; this.type = type;
this.sources = sources; this.links = links;
this.targets = targets;
this.requestType = type + '_request'; this.requestType = type + '_request';
this.responseType = type + '_response'; this.responseType = type + '_response';
@@ -76,12 +75,12 @@ class Request {
*/ */
attach(link, handler) { attach(link, handler) {
// Source side. // Source side.
if (this.sources.includes(link.source) && this.targets.includes(link.target)) { if (this.links.includes(`${link.source}-${link.target}`)) {
link.setValidator(this.responseType, this._responseValidator); link.setValidator(this.responseType, this._responseValidator);
} }
// Target side. Note: Source and target is reversed here. // Target side. Note: Source and target is reversed here.
if (this.targets.includes(link.source) && this.sources.includes(link.target)) { if (this.links.includes(`${link.target}-${link.source}`)) {
if (!handler) { if (!handler) {
throw new Error(`Missing handler for ${this.requestType} on ${link.target}-${link.source} link`); throw new Error(`Missing handler for ${this.requestType} on ${link.target}-${link.source} link`);
} }
@@ -145,8 +144,7 @@ class InstanceRequest {
}) { }) {
this._masterRequest = new Request({ this._masterRequest = new Request({
type: type + '_master', type: type + '_master',
sources, links: sources.map(source => `${source}-master`),
targets: ['master'],
requestProperties: { requestProperties: {
"instance_id": { type: "integer" }, "instance_id": { type: "integer" },
...requestProperties, ...requestProperties,
@@ -155,8 +153,7 @@ class InstanceRequest {
}); });
this._slaveRequest = new Request({ this._slaveRequest = new Request({
type: type + '_slave', type: type + '_slave',
sources: ['master'], links: ['master-slave'],
targets: ['slave'],
requestProperties: { requestProperties: {
"instance_id": { type: "integer" }, "instance_id": { type: "integer" },
...requestProperties, ...requestProperties,
@@ -224,8 +221,7 @@ let messages = {}
// Management requests // Management requests
messages.listSlaves = new Request({ messages.listSlaves = new Request({
type: 'list_slaves', type: 'list_slaves',
sources: ['control'], links: ['control-master'],
targets: ['master'],
responseProperties: { responseProperties: {
"list": { "list": {
type: "array", type: "array",
@@ -246,8 +242,7 @@ messages.listSlaves = new Request({
messages.listInstances = new Request({ messages.listInstances = new Request({
type: 'list_instances', type: 'list_instances',
sources: ['control'], links: ['control-master'],
targets: ['master'],
responseProperties: { responseProperties: {
"list": { "list": {
type: "array", type: "array",
@@ -266,8 +261,7 @@ messages.listInstances = new Request({
messages.setInstanceOutputSubscriptions = new Request({ messages.setInstanceOutputSubscriptions = new Request({
type: 'set_instance_output_subscriptions', type: 'set_instance_output_subscriptions',
sources: ['control'], links: ['control-master'],
targets: ['master'],
requestProperties: { requestProperties: {
"instance_ids": { "instance_ids": {
type: "array", type: "array",
@@ -278,8 +272,7 @@ messages.setInstanceOutputSubscriptions = new Request({
messages.createInstanceCommand = new Request({ messages.createInstanceCommand = new Request({
type: 'create_instance', type: 'create_instance',
sources: ['control'], links: ['control-master'],
targets: ['master'],
requestProperties: { requestProperties: {
"name": { type: "string" }, "name": { type: "string" },
"slave_id": { type: "integer" }, "slave_id": { type: "integer" },
@@ -288,8 +281,7 @@ messages.createInstanceCommand = new Request({
messages.createInstance = new Request({ messages.createInstance = new Request({
type: 'create_instance', type: 'create_instance',
sources: ['master'], links: ['master-slave'],
targets: ['slave'],
requestProperties: { requestProperties: {
"id": { type: "integer" }, "id": { type: "integer" },
"options": { "options": {
@@ -347,10 +339,9 @@ messages.sendRcon = new InstanceRequest({
class Event { class Event {
constructor({ type, sources, targets, eventProperties = {} }) { constructor({ type, links, eventProperties = {} }) {
this.type = type; this.type = type;
this.sources = sources; this.links = links;
this.targets = targets;
this.eventType = type + '_event'; this.eventType = type + '_event';
this._eventValidator = schema.compile({ this._eventValidator = schema.compile({
@@ -368,7 +359,7 @@ class Event {
attach(link, handler) { attach(link, handler) {
// Target side. Note: Source and target is reversed here. // Target side. Note: Source and target is reversed here.
if (this.targets.includes(link.source) && this.sources.includes(link.target)) { if (this.links.includes(`${link.target}-${link.source}`)) {
if (!handler) { if (!handler) {
throw new Error(`Missing handler for ${this.eventType} on ${link.target}-${link.source} link`); throw new Error(`Missing handler for ${this.eventType} on ${link.target}-${link.source} link`);
} }
@@ -394,8 +385,7 @@ class Event {
messages.updateInstances = new Event({ messages.updateInstances = new Event({
type: 'update_instances', type: 'update_instances',
sources: ['slave'], links: ['slave-master'],
targets: ['master'],
eventProperties: { eventProperties: {
"instances": { "instances": {
type: "array", type: "array",
@@ -414,8 +404,7 @@ messages.updateInstances = new Event({
messages.instanceOutput = new Event({ messages.instanceOutput = new Event({
type: 'instance_output', type: 'instance_output',
sources: ['slave', 'master'], links: ['slave-master', 'master-control'],
targets: ['master', 'control'],
eventProperties: { eventProperties: {
"instance_id": { type: "integer" }, "instance_id": { type: "integer" },
"output": { "output": {
+2 -4
View File
@@ -15,8 +15,7 @@ describe("lib/link/messages", function() {
describe("class Request", function() { describe("class Request", function() {
let testRequest = new link.Request({ let testRequest = new link.Request({
type: 'test', type: 'test',
sources: 'source', links: ['source-target'],
targets: 'target',
}); });
describe(".attach()", function() { describe(".attach()", function() {
@@ -114,8 +113,7 @@ describe("lib/link/messages", function() {
describe("class Event", function() { describe("class Event", function() {
let testEvent = new link.Event({ let testEvent = new link.Event({
type: 'test', type: 'test',
sources: 'source', links: ['source-target'],
targets: 'target',
}); });
describe(".attach()", function() { describe(".attach()", function() {