Skip to content

Commit

Permalink
feat(*): adding types to js. WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
notthetup committed Oct 15, 2024
1 parent 628b7fd commit c3c2707
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
5 changes: 4 additions & 1 deletion gateways/js/src/TCPConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ var createConnection;
* @class
* @ignore
*/
export default class TCPconnector {
export default class TCPConnector {

/**
* Create an TCPConnector to connect to a fjage master over TCP
* @param {Object} opts
* @param {string} [opts.hostname='localhost'] - hostname/ip address of the master container to connect to
* @param {number} [opts.port=1100] - port number of the master container to connect to
* @param {boolean} [opts.keepAlive=true] - try to reconnect if the connection is lost
* @param {boolean} [opts.debug=false] - debug info to be logged to console?
* @param {number} [opts.reconnectTime=5000] - time before reconnection is attempted after an error
*/
constructor(opts = {}) {
Expand All @@ -31,6 +32,7 @@ export default class TCPconnector {
this._firstReConn = true; // if the Gateway has attempted to reconnect to a server before
this.pendingOnOpen = []; // list of callbacks make as soon as gateway is open
this.connListeners = []; // external listeners wanting to listen connection events
this.debug = false;
this._sockInit(host, port);
}

Expand All @@ -44,6 +46,7 @@ export default class TCPconnector {
_sockInit(host, port){
if (!createConnection){
try {
// @ts-ignore
import('net').then(module => {
createConnection = module.createConnection;
this._sockSetup(host, port);
Expand Down
18 changes: 10 additions & 8 deletions gateways/js/src/WSConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ export default class WSConnector {
/**
* Create an WSConnector to connect to a fjage master over WebSockets
* @param {Object} opts
* @param {string} opts.hostname - hostname/ip address of the master container to connect to
* @param {string} opts.port - port number of the master container to connect to
* @param {string} opts.pathname - path of the master container to connect to
* @param {boolean} opts.keepAlive - try to reconnect if the connection is lost
* @param {string} [opts.hostname='localhost'] - hostname/ip address of the master container to connect to
* @param {number} [opts.port=80] - port number of the master container to connect to
* @param {string} [opts.pathname="/"] - path of the master container to connect to
* @param {boolean} [opts.keepAlive=true] - try to reconnect if the connection is lost
* @param {boolean} [opts.debug=false] - debug info to be logged to console?
* @param {number} [opts.reconnectTime=5000] - time before reconnection is attempted after an error
*/
constructor(opts = {}) {
let host = opts.hostname || 'localhost';
let port = opts.port || 80;
this.url = new URL('ws://localhost');
this.url.hostname = opts.hostname;
this.url.port = opts.port;
this.url.pathname = opts.pathname;
this._keepAlive = opts.keepAlive;
this.url.hostname = host;
this.url.port = port.toString();
this.url.pathname = opts.pathname || '/';
this._reconnectTime = opts.reconnectTime || DEFAULT_RECONNECT_TIME;
this.debug = opts.debug || false; // debug info to be logged to console?
this._firstConn = true; // if the Gateway has managed to connect to a server before
Expand Down
49 changes: 28 additions & 21 deletions gateways/js/src/fjage.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ export class Message {
// NOTE: we don't do any base64 encoding for TX as
// we don't know what data type is intended
/**
* @private
*
* @return {string} - JSON string representation of the message
*/
_serialize() {
Expand All @@ -266,28 +268,26 @@ export class Message {

// convert a dictionary (usually from decoding JSON) into a message
/**
* @param {(string|Object)} obj - JSON string or object to be converted to a message
* @private
*
* @param {(string|Object)} json - JSON string or object to be converted to a message
* @returns {Message} - message created from the JSON string or object
* */
static _deserialize(obj) {
if (typeof obj == 'string' || obj instanceof String) {
static _deserialize(json) {
let obj = null;
if (typeof json == 'string') {
try {
obj = JSON.parse(obj);
obj = JSON.parse(json);
}catch(e){
return null;
}
}
try {
let qclazz = obj.clazz;
let clazz = qclazz.replace(/^.*\./, '');
let rv = MessageClass[clazz] ? new MessageClass[clazz] : new Message();
rv.__clazz__ = qclazz;
rv._inflate(obj.data);
return rv;
} catch (err) {
console.warn('Error trying to deserialize JSON object : ', obj, err);
return null;
}
} else obj = json;
let qclazz = obj.clazz;
let clazz = qclazz.replace(/^.*\./, '');
let rv = MessageClass[clazz] ? new MessageClass[clazz] : new Message();
rv.__clazz__ = qclazz;
rv._inflate(obj.data);
return rv;
}
}

Expand Down Expand Up @@ -379,6 +379,7 @@ export class Gateway {
delete this.pending[obj.id];
} else if (obj.action == 'send') {
// incoming message from master
// @ts-ignore
let msg = Message._deserialize(obj.message);
if (!msg) return;
this._sendEvent('rxmsg', msg);
Expand Down Expand Up @@ -477,13 +478,15 @@ export class Gateway {
'hostname':url.hostname,
'port':url.port,
'pathname':url.pathname,
'keepAlive': this._keepAlive
'keepAlive': this._keepAlive,
'debug': this.debug
});
}else if (url.protocol.startsWith('tcp')){
conn = new TCPConnector({
'hostname':url.hostname,
'port':url.port,
'keepAlive': this._keepAlive
'keepAlive': this._keepAlive,
'debug': this.debug
});
} else return null;
conn.setReadCallback(this._onMsgRx.bind(this));
Expand Down Expand Up @@ -775,6 +778,7 @@ export class Gateway {
}
this._sendEvent('txmsg', msg);
let rq = JSON.stringify({ action: 'send', relay: true, message: '###MSG###' });
// @ts-ignore
rq = rq.replace('"###MSG###"', msg._serialize());
return !!this._msgTx(rq);
}
Expand All @@ -794,7 +798,7 @@ export class Gateway {
*
* @param {Message} msg - message to send
* @param {number} [timeout=1000] - timeout in milliseconds
* @returns {Promise<?Message>} - a promise which resolves with the received response message, null on timeout
* @returns {Promise<Message|void>} - a promise which resolves with the received response message, null on timeout
*/
async request(msg, timeout=1000) {
this.send(msg);
Expand All @@ -808,7 +812,7 @@ export class Gateway {
* @param {function|Message|typeof Message} filter - original message to which a response is expected, or a MessageClass of the type
* of message to match, or a closure to use to match against the message
* @param {number} [timeout=0] - timeout in milliseconds
* @returns {Promise<?Message>} - received response message, null on timeout
* @returns {Promise<Message|void>} - received response message, null on timeout
*/
async receive(filter, timeout=0) {
return new Promise(resolve => {
Expand Down Expand Up @@ -864,7 +868,7 @@ export const Services = {
* Creates a unqualified message class based on a fully qualified name.
* @param {string} name - fully qualified name of the message class to be created
* @param {typeof Message} [parent=Message] - class of the parent MessageClass to inherit from
* @returns {Function} - constructor for the unqualified message class
* @constructs Message
* @example
* const ParameterReq = MessageClass('org.arl.fjage.param.ParameterReq');
* let pReq = new ParameterReq()
Expand All @@ -873,6 +877,9 @@ export function MessageClass(name, parent=Message) {
let sname = name.replace(/^.*\./, '');
if (MessageClass[sname]) return MessageClass[sname];
let cls = class extends parent {
/**
* @param {{ [x: string]: any; }} params
*/
constructor(params) {
super();
this.__clazz__ = name;
Expand Down

0 comments on commit c3c2707

Please sign in to comment.