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