Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@

'use strict';

const yargs = require('yargs');
const readline = require('readline');

const Cli = require('../lib/cli/cli');

let rl = null;

const args = yargs
.option('verbose', {
alias: 'v',
type: 'count',
describe: 'Verbosely print incoming and outgoing messages',
})
.option('no-reconnect', {
alias: 'R',
type: 'boolean',
describe: 'Disable reconnection',
})
.option('heartbeat-interval', {
alias: 'i',
requiresArg: true,
type: 'number',
describe: 'Heartbeat interval in milliseconds',
})
.strict().argv;

const log = (msg) => {
const userInput = rl.line;
if (userInput) rl.clearLine();
Expand All @@ -21,7 +41,7 @@ const finish = () => {
process.exit();
};

const cli = new Cli(log);
const cli = new Cli(log, args);

cli.on('exit', () => finish());

Expand Down
40 changes: 38 additions & 2 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
'use strict';

const EventEmitter = require('events').EventEmitter;
const mdsf = require('mdsf');

const CommandProcessor = require('./command-processor');
const LineProcessor = require('./line-processor');
const utils = require('./utils');

const VERBOSENESS_LEVEL = {
QUIET: 0,
NO_HEARTBEAT: 1,
ALL: 2,
};

module.exports = class Cli extends EventEmitter {
// log - logger function to print results,
// help and error messages
constructor(log) {
constructor(log, options) {
super();

this.log = log;

this.client = null;
this.client = {
heartbeatInterval: options.heartbeatInterval,
};

if (options.verbose) {
this.client.logger = new EventEmitter();

this.client.logger.on('outgoingMessage', (message) => {
if (
(!message.pong && !message.ping) ||
options.verbose === VERBOSENESS_LEVEL.ALL
) {
this.log(`Outgoing message:\n${mdsf.stringify(message)}`);
}
});

this.client.logger.on('incomingMessage', (message) => {
if (
(!message.pong && !message.ping) ||
options.verbose === VERBOSENESS_LEVEL.ALL
) {
this.log(`Incoming message:\n${mdsf.stringify(message)}`);
}
});
}

if (options.noReconnect) {
this.client.reconnector = () => {};
}

this.connection = null;
this.api = {};

Expand Down
37 changes: 15 additions & 22 deletions lib/cli/command-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,27 @@ module.exports = class CommandProcessor extends EventEmitter {
}

call(interfaceName, methodName, args, callback) {
if (!this.cli.connectInitiated) {
callback(new Error('Not connected'));
return;
}
if (!this.cli.connection) {
callback(new Error('Connection in progress'));
callback(new Error('Not connected'));
return;
}
this.cli.connection.callMethod(interfaceName, methodName, args, callback);
}

event(interfaceName, eventName, args, callback) {
if (!this.cli.connectInitiated) {
callback(new Error('Not connected'));
return;
}
if (!this.cli.connection) {
callback(new Error('Connection in progress'));
callback(new Error('Not connected'));
return;
}
this.cli.connection.emitRemoteEvent(interfaceName, eventName, args);
callback();
}

connect(protocol, host, port, app, interfaces, callback) {
if (this.cli.connection) {
this._disconnect();
}

let transport;
let args;

Expand All @@ -140,15 +136,13 @@ module.exports = class CommandProcessor extends EventEmitter {
callback(new Error(`Unknown protocol '${protocol}'`));
return;
}
this.cli.connectInitiated = true;
transport.connectAndInspect(app, null, interfaces, ...args,
transport.connectAndInspect(app, this.cli.client, interfaces, ...args,
(err, connection, api) => {
this.cli.connection = connection;
if (err) {
this.cli.connectInitiated = false;
callback(err);
return;
}
this.cli.connection = connection;
this.cli.api = filterApiCompletions(api, ['_', 'domain']);
// TODO: make event registering generic
connection.on('event', (interfaceName, eventName, args) => {
Expand All @@ -164,21 +158,20 @@ module.exports = class CommandProcessor extends EventEmitter {
}

disconnect(callback) {
if (!this.cli.connectInitiated) {
callback(new Error('Not connected'));
return;
}
if (!this.cli.connection) {
callback(new Error('Wait till connection is established'));
callback(new Error('Not connected'));
return;
}
this.cli.connection.close();
this.cli.connection = null;
this.cli.connectInitiated = false;
this._disconnect();
callback();
}

exit() {
this.emit('exit');
}

_disconnect() {
this.cli.connection.close();
this.cli.connection = null;
}
};
Loading