Dependency updates and new API for logging
This commit is contained in:
+17
-16
@@ -2,12 +2,12 @@
|
||||
|
||||
const topLogPrefix = 'larvitsmpp: lib/client.js: ',
|
||||
session = require(__dirname + '/session'),
|
||||
LUtils = require('larvitutils'),
|
||||
merge = require('utils-merge'),
|
||||
log = require('winston'),
|
||||
net = require('net'),
|
||||
tls = require('tls');
|
||||
|
||||
function login() {
|
||||
function login(options) {
|
||||
const logPrefix = topLogPrefix + 'login() - ',
|
||||
that = this;
|
||||
|
||||
@@ -26,11 +26,11 @@ function login() {
|
||||
if (err) return that.emit('loginFailed');
|
||||
|
||||
if (retPduObj.cmdStatus === 'ESME_ROK') {
|
||||
log.info(logPrefix + 'Successful login system_id: "' + loginPdu.params.system_id + '"');
|
||||
options.log.info(logPrefix + 'Successful login system_id: "' + loginPdu.params.system_id + '"');
|
||||
that.loggedIn = true;
|
||||
that.emit('loggedIn');
|
||||
} else {
|
||||
log.info(logPrefix + 'Login failed system_id: "' + loginPdu.params.system_id + '". Status msg: ' + retPduObj.cmdStatus);
|
||||
options.log.info(logPrefix + 'Login failed system_id: "' + loginPdu.params.system_id + '". Status msg: ' + retPduObj.cmdStatus);
|
||||
that.emit('loginFailed');
|
||||
}
|
||||
});
|
||||
@@ -40,7 +40,7 @@ function resetEnqLinkTimer() {
|
||||
const logPrefix = topLogPrefix + 'resetEnqLinkTimer() - ',
|
||||
that = this;
|
||||
|
||||
log.silly(logPrefix + 'Resetting the kill timer');
|
||||
that.log.silly(logPrefix + 'Resetting the kill timer');
|
||||
if (that.enqLinkTimer) {
|
||||
clearTimeout(that.enqLinkTimer);
|
||||
}
|
||||
@@ -61,27 +61,27 @@ function resetEnqLinkTimer() {
|
||||
* @return {object} (returnObj)
|
||||
*/
|
||||
function clientSession(sock, options) {
|
||||
const returnObj = session(sock),
|
||||
const returnObj = session({'sock': sock, 'log': options.log}),
|
||||
logPrefix = topLogPrefix + 'clientSession() - ';
|
||||
|
||||
returnObj.options = options;
|
||||
returnObj.login = login;
|
||||
returnObj.resetEnqLinkTimer = resetEnqLinkTimer;
|
||||
|
||||
returnObj.login();
|
||||
returnObj.resetEnqLinkTimer();
|
||||
returnObj.login({'log': options.log});
|
||||
returnObj.resetEnqLinkTimer({'log': options.log});
|
||||
|
||||
// Handle incoming Pdu Objects
|
||||
returnObj.on('incomingPduObj', function (pduObj) {
|
||||
// Call the appropriate handleCmd function
|
||||
|
||||
if (typeof returnObj.handleCmd[pduObj.cmdName] === 'function') {
|
||||
log.debug(logPrefix + 'returnObj.on(incomingPduObj) - Running cmd handling function returnObj.handleCmd.' + pduObj.cmdName + '()');
|
||||
options.log.debug(logPrefix + 'returnObj.on(incomingPduObj) - Running cmd handling function returnObj.handleCmd.' + pduObj.cmdName + '()');
|
||||
|
||||
returnObj.handleCmd[pduObj.cmdName](pduObj);
|
||||
} else {
|
||||
// No command handling function is registered, return error "invalid command"
|
||||
log.info(logPrefix + 'returnObj.on(incomingPduObj) - No handling function found for command: "' + pduObj.cmdName + '"');
|
||||
options.log.info(logPrefix + 'returnObj.on(incomingPduObj) - No handling function found for command: "' + pduObj.cmdName + '"');
|
||||
|
||||
returnObj.sendReturn(pduObj, 'ESME_RINVCMDID');
|
||||
}
|
||||
@@ -93,7 +93,7 @@ function clientSession(sock, options) {
|
||||
/**
|
||||
* Setup a client.
|
||||
*
|
||||
* @param {object} options - host, port, username, password, tls, enqLinkTiming
|
||||
* @param {object} options - host, port, username, password, tls, enqLinkTiming, log
|
||||
* @param {function} cb(err, session)
|
||||
*/
|
||||
function client(options, cb) {
|
||||
@@ -112,8 +112,9 @@ function client(options, cb) {
|
||||
'port': 2775,
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'tls': false,
|
||||
'enqLinkTiming': 20000 // 20 sec
|
||||
'tls': false,
|
||||
'enqLinkTiming': 20000, // 20 sec
|
||||
'log': new (new LUtils()).Log()
|
||||
}, options || {});
|
||||
|
||||
if (options && options.tls && options.tls === true) {
|
||||
@@ -122,11 +123,11 @@ function client(options, cb) {
|
||||
sock = new net.Socket();
|
||||
}
|
||||
|
||||
log.debug(logPrefix + 'Connecting to ' + options.host + ':' + options.port);
|
||||
options.log.debug(logPrefix + 'Connecting to ' + options.host + ':' + options.port);
|
||||
sock.connect(options, function () {
|
||||
const session = clientSession(sock, options);
|
||||
|
||||
log.info(logPrefix + 'Connected to ' + sock.remoteAddress + ':' + sock.remotePort);
|
||||
options.log.info(logPrefix + 'Connected to ' + sock.remoteAddress + ':' + sock.remotePort);
|
||||
|
||||
session.on('loggedIn', function () {
|
||||
cb(null, session);
|
||||
@@ -134,7 +135,7 @@ function client(options, cb) {
|
||||
|
||||
session.on('loginFailed', function () {
|
||||
const err = new Error('Remote host refused login.');
|
||||
log.warn(logPrefix + err.message);
|
||||
options.log.warn(logPrefix + err.message);
|
||||
cb(err);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user