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 Jan 17, 2024
1 parent 78617ac commit db7b7fb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 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
17 changes: 10 additions & 7 deletions gateways/js/src/WSConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +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 {number} 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 = {}) {
this.url = new URL('ws://localhost');
this.url.hostname = opts.hostname;
this.url.port = opts.port;
this.url.pathname = opts.pathname;
let host = opts.hostname || 'localhost';
let port = opts.port || 80;
this.url.hostname = host;
this.url.port = port.toString();
this.url.pathname = opts.pathname || '/';
this._reconnectTime = opts.reconnectTime || DEFAULT_RECONNECT_TIME;
this._keepAlive = opts.keepAlive || true;
this.debug = opts.debug || false; // debug info to be logged to console?
Expand Down
32 changes: 22 additions & 10 deletions gateways/js/src/fjage.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,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 @@ -260,17 +262,20 @@ 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;
}
}
} else obj = json;
let qclazz = obj.clazz;
let clazz = qclazz.replace(/^.*\./, '');
let rv = MessageClass[clazz] ? new MessageClass[clazz] : new Message();
Expand Down Expand Up @@ -365,6 +370,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 @@ -463,13 +469,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 @@ -718,6 +726,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 @@ -737,7 +746,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 @@ -751,7 +760,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 @@ -807,7 +816,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 @@ -816,6 +825,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 db7b7fb

Please sign in to comment.