-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.js
276 lines (262 loc) · 7.92 KB
/
publisher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var Redis = require("redis");
class Publisher {
constructor(serviceName, serviceResponses, debug) {
this.serviceName = serviceName;
this.serviceResponses = serviceResponses;
this.debug = debug || false;
this.redisClient = Redis.createClient();
this.redisClient.on("error", (err) => {
this.log("redis client error: " + err);
});
this.redisClient.on("connect", () => {
this.log("redis client connected.");
this.connected = true;
});
}
/**
* Prints the Service Name and a message to the console.
* @param {string} message - The message to print.
*/
log(message) {
if (this.debug) {
console.log(this.serviceName + " Event Publisher: " + message) + "\n";
}
}
/**
* Disconnects the redis client.
*/
exit() {
this.redisClient.quit();
}
publish(message) {
try {
if (this.connected) {
let matchedResponses = this.getResponse(message);
let i = matchedResponses.length - 1;
for (i; i > -1; i--) {
if (
matchedResponses[i].useRegularExpression &&
matchedResponses[i].parsedRegexValue
) {
this.log(
"Publishing to channel: " +
matchedResponses[i].channel +
" with value: " +
matchedResponses[i].parsedRegexValue
);
const valueObject = {
published: Date.now(),
origin: this.serviceName,
value: matchedResponses[i].parsedRegexValue,
persist: matchedResponses[i].persist,
};
this.redisClient.publish(
matchedResponses[i].channel,
JSON.stringify(valueObject)
);
matchedResponses[i].parsedRegexValue = null;
} else if (!matchedResponses[i].useRegularExpression) {
this.log(
"Publishing to channel: " +
matchedResponses[i].channel +
" with value: null"
);
const valueObject = {
published: Date.now(),
origin: this.serviceName,
value: "null",
persist: matchedResponses[i].persist,
};
this.redisClient.publish(
matchedResponses[i].channel,
JSON.stringify(valueObject)
);
}
}
} else {
this.log("Cannot publish message until connected to redis.");
}
} catch (e) {
this.log("Exception publishing message: " + e);
}
}
publishDirect(channel, value) {
try {
this.log("Publishing to channel: " + channel + " with value: " + value);
const valueObject = {
published: Date.now(),
origin: this.serviceName,
value: value,
};
this.redisClient.publish(channel, JSON.stringify(valueObject));
} catch (e) {
this.log("Exception direct publishing message: " + e);
}
}
/**
* Searches the array of serviceResponses for the incoming message.
*
* @param {string} message - A string containing the message to find.
* @returns {json} A json array of service responses.
*/
getResponse(message) {
if (this.serviceResponses) {
return this.serviceResponses
.filter((response) => {
if (
response.useRegularExpression &&
!response.useHex &&
this.checkEndWith(message, response)
) {
return this.parseRegex(message, response);
}
if (response.useRegularExpression && response.useHex) {
var hexString = "";
for (var i = 0; i < message.length; i++) {
if (i != message.length - 1) {
hexString =
hexString +
"0x" +
Buffer.from(message.charAt(i), "utf8").toString("hex") +
" ";
} else {
hexString =
hexString +
"0x" +
Buffer.from(message.charAt(i), "utf8").toString("hex");
}
}
return this.parseRegex(hexString, response);
}
if (response.useHex) {
return this.parseHex(message, response);
}
return this.checkResponsePattern(message, response);
//TODO - check if the above is correct. Perhaps it should be:
// if(this.checkResponsePattern(message, response)){
// return response;
// }
})
.map((response) => response);
}
return [];
}
/**
* Matches a message to a string representation of hex characters.
*
* @param {string} message - A string containing the message to check for a hex value.
* @param {json} response - A json object with a pattern field that contains a string representation of the hex characters to match ex: 0x00,0x0A.
* @returns {bool} True if a match was found.
*/
parseHex(message, response) {
if (response.useHex && response.pattern) {
if (message == Buffer.from(response.pattern.split(" "))) {
return true;
}
}
return false;
}
/**
* Matches a message to a regex and stores the parsed value in the response object's parsedRegexValue field.
*
* @param {string} message - A string containing the message to check for a regex value.
* @param {json} response - A json object with a parsedRegexValue field and a pattern field that contains a regular expression.
* @returns {bool} True if a match was found.
*/
parseRegex(message, response) {
message = message.trim();
if (message === "") {
return false;
}
const r = new RegExp(response.pattern);
const m = r.exec(message);
if (!m) {
return false;
}
if (m.length > 1) {
response.parsedRegexValue = m[1];
}
return true;
}
/**
* Compares a message to the pattern and endWith fields of a response object.
*
* @param {string} message - A string containing the message.
* @param {json} response - A json object with pattern and endWith fields.
* @returns {bool} True if the last message matches the pattern property of the response object.
*/
checkResponsePattern(message, response) {
switch (response.endWith) {
case "none":
if (message === response.pattern) {
return true;
}
break;
case "r":
if (message === response.pattern + "\r") {
return true;
}
break;
case "n":
if (message === response.pattern + "\n") {
return true;
}
break;
case "rn":
if (message === response.pattern + "\r\n") {
return true;
}
break;
default:
if (message === response.pattern) {
return true;
}
}
return false;
}
/**
* Checks the last character(s) of a message for the endWith fields of a response object
*
* @param {string} message - A string containing the message.
* @param {json} response - A json object with an endWith field.
* @returns {bool} True if the last character of the message matches the endWith property of the response object.
*/
checkEndWith(message, response) {
switch (response.endWith) {
case "none":
if (
message.slice(-2) != "\r\n" &&
message.slice(-1) != "\r" &&
message.slice(-1) != "\n"
) {
return true;
}
break;
case "r":
if (message.slice(-1) === "\r") {
return true;
}
break;
case "n":
if (message.slice(-1) === "\n") {
return true;
}
break;
case "rn":
if (message.slice(-2) === "\r\n") {
return true;
}
break;
default:
if (
message.slice(-2) != "\r\n" &&
message.slice(-1) != "\r" &&
message.slice(-1) != "\n"
) {
return true;
}
}
return false;
}
}
module.exports = Publisher;