Unverified Commit 23c8e8f7 authored by Dan Jones's avatar Dan Jones
Browse files

docs: add doc blocks for new adapter methods

parent c0424102
Pipeline #114567 failed with stages
in 32 seconds
...@@ -93,6 +93,10 @@ class Adapter { ...@@ -93,6 +93,10 @@ class Adapter {
}); });
} }
/**
* Setup a timer to call poll every interval seconds
* @param {int} interval
*/
pollEveryXSeconds(interval) { pollEveryXSeconds(interval) {
if (this.pollTimer) clearTimeout(this.pollTimer); if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000); this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000);
...@@ -138,6 +142,11 @@ class Adapter { ...@@ -138,6 +142,11 @@ class Adapter {
}); });
} }
/**
* Process a message received either from poll
* or from socket.onMessage
* @param {object} message
*/
receive(message) { receive(message) {
try { try {
const parsed = JSON.parse(message.message); const parsed = JSON.parse(message.message);
...@@ -153,21 +162,30 @@ class Adapter { ...@@ -153,21 +162,30 @@ class Adapter {
} }
} }
/**
* Decide whether to use http or socket for comms
* with the backbone
* @returns string
*/
getMode() { getMode() {
return ('mode' in this.config) ? this.config.mode : 'http'; return ('mode' in this.config) ? this.config.mode : 'http';
} }
/**
* Create a new socket connection and send an authentication
* message. Websocket doesn't support headers so as soon
* as the socket is open a message is sent containing
* the authorisation header to authenticate the connection
*/
openSocket() { openSocket() {
this.getAuthorizationHeader() this.getAuthorizationHeader()
.then((headers) => { .then((headers) => {
const protocols = []; const protocols = [];
const options = { headers };
const socket = new WebSocket( const socket = new WebSocket(
this.config.socket, this.config.socket,
protocols, protocols,
options,
); );
socket.addEventListener('open', () => { socket.addEventListener('open', () => {
...@@ -193,6 +211,11 @@ class Adapter { ...@@ -193,6 +211,11 @@ class Adapter {
}); });
} }
/**
* If the socket connection is lost ensure that a new socket
* is opened rather than continuing to send messages on this
* socket
*/
closeSocket() { closeSocket() {
this.socket = null; this.socket = null;
} }
...@@ -203,7 +226,6 @@ class Adapter { ...@@ -203,7 +226,6 @@ class Adapter {
* Messages should be passed through encode before sending * Messages should be passed through encode before sending
* @param {string} topic * @param {string} topic
* @param {string} body * @param {string} body
* @param {boolean} is_retry
* @returns * @returns
*/ */
publish(topic, body) { publish(topic, body) {
...@@ -216,6 +238,13 @@ class Adapter { ...@@ -216,6 +238,13 @@ class Adapter {
return response; return response;
} }
/**
* Send via a POST to /send
* @param {string} topic
* @param {string} body
* @param {boolean} is_retry
* @returns Promise
*/
http_publish(topic, body, is_retry = false) { http_publish(topic, body, is_retry = false) {
let adapterConfig = this.config; let adapterConfig = this.config;
return this.getAuthorizationHeader() return this.getAuthorizationHeader()
...@@ -252,6 +281,11 @@ class Adapter { ...@@ -252,6 +281,11 @@ class Adapter {
}); });
} }
/**
* Publish via websocket socket.send
* @param {string} topic
* @param {string} body
*/
socket_publish(topic, body) { socket_publish(topic, body) {
if (this.socket) { if (this.socket) {
this.socket.send(JSON.stringify({ topic, message: body })); this.socket.send(JSON.stringify({ topic, message: body }));
...@@ -271,7 +305,6 @@ class Adapter { ...@@ -271,7 +305,6 @@ class Adapter {
* *
* Messages should be passed through encode before sending * Messages should be passed through encode before sending
* @param {string} body * @param {string} body
* @param {boolean} is_retry
* @returns * @returns
*/ */
broadcast(body) { broadcast(body) {
...@@ -284,6 +317,12 @@ class Adapter { ...@@ -284,6 +317,12 @@ class Adapter {
return response; return response;
} }
/**
* Broadcast via POST to /notify
* @param {string} body
* @param {boolean} is_retry
* @returns Promise
*/
http_broadcast(body, is_retry = false) { http_broadcast(body, is_retry = false) {
let adapterConfig = this.config; let adapterConfig = this.config;
return this.getAuthorizationHeader() return this.getAuthorizationHeader()
...@@ -319,6 +358,10 @@ class Adapter { ...@@ -319,6 +358,10 @@ class Adapter {
}); });
} }
/**
* Broadcast via websocket socket.send
* @param {string} body
*/
socket_broadcast(body) { socket_broadcast(body) {
if (this.socket) { if (this.socket) {
this.socket.send(JSON.stringify({ message: body })); this.socket.send(JSON.stringify({ message: body }));
......
...@@ -95,6 +95,10 @@ class Adapter { ...@@ -95,6 +95,10 @@ class Adapter {
}); });
} }
/**
* Setup a timer to call poll every interval seconds
* @param {int} interval
*/
pollEveryXSeconds(interval) { pollEveryXSeconds(interval) {
if (this.pollTimer) clearTimeout(this.pollTimer); if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000); this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000);
...@@ -140,6 +144,11 @@ class Adapter { ...@@ -140,6 +144,11 @@ class Adapter {
}); });
} }
/**
* Process a message received either from poll
* or from socket.onMessage
* @param {object} message
*/
receive(message) { receive(message) {
try { try {
const parsed = JSON.parse(message.message); const parsed = JSON.parse(message.message);
...@@ -155,21 +164,30 @@ class Adapter { ...@@ -155,21 +164,30 @@ class Adapter {
} }
} }
/**
* Decide whether to use http or socket for comms
* with the backbone
* @returns string
*/
getMode() { getMode() {
return ('mode' in this.config) ? this.config.mode : 'http'; return ('mode' in this.config) ? this.config.mode : 'http';
} }
/**
* Create a new socket connection and send an authentication
* message. Websocket doesn't support headers so as soon
* as the socket is open a message is sent containing
* the authorisation header to authenticate the connection
*/
openSocket() { openSocket() {
this.getAuthorizationHeader() this.getAuthorizationHeader()
.then((headers) => { .then((headers) => {
const protocols = []; const protocols = [];
const options = { headers };
const socket = new WebSocket( const socket = new WebSocket(
this.config.socket, this.config.socket,
protocols, protocols,
options,
); );
socket.addEventListener('open', () => { socket.addEventListener('open', () => {
...@@ -195,6 +213,11 @@ class Adapter { ...@@ -195,6 +213,11 @@ class Adapter {
}); });
} }
/**
* If the socket connection is lost ensure that a new socket
* is opened rather than continuing to send messages on this
* socket
*/
closeSocket() { closeSocket() {
this.socket = null; this.socket = null;
} }
...@@ -205,7 +228,6 @@ class Adapter { ...@@ -205,7 +228,6 @@ class Adapter {
* Messages should be passed through encode before sending * Messages should be passed through encode before sending
* @param {string} topic * @param {string} topic
* @param {string} body * @param {string} body
* @param {boolean} is_retry
* @returns * @returns
*/ */
publish(topic, body) { publish(topic, body) {
...@@ -218,7 +240,14 @@ class Adapter { ...@@ -218,7 +240,14 @@ class Adapter {
return response; return response;
} }
http_publish(topic, body, body, is_retry = false) { /**
* Send via a POST to /send
* @param {string} topic
* @param {string} body
* @param {boolean} is_retry
* @returns Promise
*/
http_publish(topic, body, is_retry = false) {
let adapterConfig = this.config; let adapterConfig = this.config;
return this.getAuthorizationHeader() return this.getAuthorizationHeader()
.then((headers) => { .then((headers) => {
...@@ -254,6 +283,11 @@ class Adapter { ...@@ -254,6 +283,11 @@ class Adapter {
}); });
} }
/**
* Publish via websocket socket.send
* @param {string} topic
* @param {string} body
*/
socket_publish(topic, body) { socket_publish(topic, body) {
if (this.socket) { if (this.socket) {
this.socket.send(JSON.stringify({ topic, message: body })); this.socket.send(JSON.stringify({ topic, message: body }));
...@@ -273,7 +307,6 @@ class Adapter { ...@@ -273,7 +307,6 @@ class Adapter {
* *
* Messages should be passed through encode before sending * Messages should be passed through encode before sending
* @param {string} body * @param {string} body
* @param {boolean} is_retry
* @returns * @returns
*/ */
broadcast(body) { broadcast(body) {
...@@ -286,6 +319,12 @@ class Adapter { ...@@ -286,6 +319,12 @@ class Adapter {
return response; return response;
} }
/**
* Broadcast via POST to /notify
* @param {string} body
* @param {boolean} is_retry
* @returns Promise
*/
http_broadcast(body, is_retry = false) { http_broadcast(body, is_retry = false) {
let adapterConfig = this.config; let adapterConfig = this.config;
return this.getAuthorizationHeader() return this.getAuthorizationHeader()
...@@ -321,6 +360,10 @@ class Adapter { ...@@ -321,6 +360,10 @@ class Adapter {
}); });
} }
/**
* Broadcast via websocket socket.send
* @param {string} body
*/
socket_broadcast(body) { socket_broadcast(body) {
if (this.socket) { if (this.socket) {
this.socket.send(JSON.stringify({ message: body })); this.socket.send(JSON.stringify({ message: body }));
......
...@@ -93,6 +93,10 @@ export class Adapter { ...@@ -93,6 +93,10 @@ export class Adapter {
}); });
} }
/**
* Setup a timer to call poll every interval seconds
* @param {int} interval
*/
pollEveryXSeconds(interval) { pollEveryXSeconds(interval) {
if (this.pollTimer) clearTimeout(this.pollTimer); if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000); this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000);
...@@ -138,6 +142,11 @@ export class Adapter { ...@@ -138,6 +142,11 @@ export class Adapter {
}); });
} }
/**
* Process a message received either from poll
* or from socket.onMessage
* @param {object} message
*/
receive(message) { receive(message) {
try { try {
const parsed = JSON.parse(message.message); const parsed = JSON.parse(message.message);
...@@ -153,21 +162,30 @@ export class Adapter { ...@@ -153,21 +162,30 @@ export class Adapter {
} }
} }
/**
* Decide whether to use http or socket for comms
* with the backbone
* @returns string
*/
getMode() { getMode() {
return ('mode' in this.config) ? this.config.mode : 'http'; return ('mode' in this.config) ? this.config.mode : 'http';
} }
/**
* Create a new socket connection and send an authentication
* message. Websocket doesn't support headers so as soon
* as the socket is open a message is sent containing
* the authorisation header to authenticate the connection
*/
openSocket() { openSocket() {
this.getAuthorizationHeader() this.getAuthorizationHeader()
.then((headers) => { .then((headers) => {
const protocols = []; const protocols = [];
const options = { headers };
const socket = new WebSocket( const socket = new WebSocket(
this.config.socket, this.config.socket,
protocols, protocols,
options,
); );
socket.addEventListener('open', () => { socket.addEventListener('open', () => {
...@@ -193,6 +211,11 @@ export class Adapter { ...@@ -193,6 +211,11 @@ export class Adapter {
}); });
} }
/**
* If the socket connection is lost ensure that a new socket
* is opened rather than continuing to send messages on this
* socket
*/
closeSocket() { closeSocket() {
this.socket = null; this.socket = null;
} }
...@@ -203,7 +226,6 @@ export class Adapter { ...@@ -203,7 +226,6 @@ export class Adapter {
* Messages should be passed through encode before sending * Messages should be passed through encode before sending
* @param {string} topic * @param {string} topic
* @param {string} body * @param {string} body
* @param {boolean} is_retry
* @returns * @returns
*/ */
publish(topic, body) { publish(topic, body) {
...@@ -216,6 +238,12 @@ export class Adapter { ...@@ -216,6 +238,12 @@ export class Adapter {
return response; return response;
} }
/**
* Send via a POST to /send
* @param {string} topic
* @param {string} body
* @returns Promise
*/
http_publish(topic, body, is_retry = false) { http_publish(topic, body, is_retry = false) {
let adapterConfig = this.config; let adapterConfig = this.config;
return this.getAuthorizationHeader() return this.getAuthorizationHeader()
...@@ -252,6 +280,11 @@ export class Adapter { ...@@ -252,6 +280,11 @@ export class Adapter {
}); });
} }
/**
* Publish via websocket socket.send
* @param {string} topic
* @param {string} body
*/
socket_publish(topic, body) { socket_publish(topic, body) {
if (this.socket) { if (this.socket) {
this.socket.send(JSON.stringify({ topic, message: body })); this.socket.send(JSON.stringify({ topic, message: body }));
...@@ -271,7 +304,6 @@ export class Adapter { ...@@ -271,7 +304,6 @@ export class Adapter {
* *
* Messages should be passed through encode before sending * Messages should be passed through encode before sending
* @param {string} body * @param {string} body
* @param {boolean} is_retry
* @returns * @returns
*/ */
broadcast(body) { broadcast(body) {
...@@ -284,6 +316,12 @@ export class Adapter { ...@@ -284,6 +316,12 @@ export class Adapter {
return response; return response;
} }
/**
* Broadcast via POST to /notify
* @param {string} body
* @param {boolean} is_retry
* @returns Promise
*/
http_broadcast(body, is_retry = false) { http_broadcast(body, is_retry = false) {
let adapterConfig = this.config; let adapterConfig = this.config;
return this.getAuthorizationHeader() return this.getAuthorizationHeader()
...@@ -319,6 +357,10 @@ export class Adapter { ...@@ -319,6 +357,10 @@ export class Adapter {
}); });
} }
/**
* Broadcast via websocket socket.send
* @param {string} body
*/
socket_broadcast(body) { socket_broadcast(body) {
if (this.socket) { if (this.socket) {
this.socket.send(JSON.stringify({ message: body })); this.socket.send(JSON.stringify({ message: body }));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment