Moved more functionality to pre declared instead of inline

This commit is contained in:
2015-06-13 14:39:52 +02:00
parent e18a155364
commit 9b88428ebd
2 changed files with 84 additions and 76 deletions
+2 -2
View File
@@ -115,8 +115,8 @@ function client(options, callback) {
}); });
session.on('loginFailed', function() { session.on('loginFailed', function() {
var err = new Error('larvitsmpp: lib/client.js: client() - Remote host refused login.'); var err = new Error('Remote host refused login.');
log.warn(err.message); log.warn('larvitsmpp: lib/client.js: client() - ' + err.message);
callback(err); callback(err);
}); });
}); });
+82 -74
View File
@@ -6,6 +6,85 @@ var log = require('winston'),
session = require('./session'), session = require('./session'),
smppUtils = require('./utils'); smppUtils = require('./utils');
/**
* Try to log a connecting peer in
*
* @param obj pduObj
*/
function login(pduObj) {
var that = this;
log.debug('larvitsmpp: lib/server.js: login() - Data received and session is not loggedIn');
// Pause socket so we do not receive any other commands until we have processed the login
this.sock.pause();
// Only bind_* is accepted when the client is not logged in
if (pduObj.cmdName !== 'bind_transceiver' && pduObj.cmdName !== 'bind_receiver' && pduObj.cmdName !== 'bind_transmitter') {
log.debug('larvitsmpp: lib/server.js: login() - Session is not loggedIn and no bind_* command is given. Return error "ESME_RINVBNDSTS');
smppUtils.pduReturn(pduObj, 'ESME_RINVBNDSTS', function(err, retPdu) {
if (err) {
that.closeSocket();
return;
}
that.sock.resume();
that.sockWrite(retPdu);
});
return;
}
// If there is a checkuserpass(), use it to check system_id and password from the PDU
if (typeof this.options.checkuserpass === 'function') {
this.options.checkuserpass(pduObj.params.system_id, pduObj.params.password, function(err, res) {
if (err) {
that.closeSocket();
return;
}
if ( ! res) {
log.info('larvitsmpp: lib/server.js: serverSession() - login() - Login failed! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
that.sock.resume();
that.sendReturn(pduObj, 'ESME_RBINDFAIL');
return;
}
log.verbose('larvitsmpp: lib/server.js: serverSession() - login() - Login successful! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
that.loggedIn = true;
that.sock.resume();
that.sendReturn(pduObj);
});
return;
}
// If we arrived here it means we are not logged in and that a bind_* event happened and no checkuserpass() method exists. Lets login!
this.loggedIn = true;
this.sock.resume();
this.sendReturn(pduObj);
}
/**
* Reset the enquire link timer
* If this is not ran within options.timeout milliseconds, this session will self terminate
*/
function resetEnqLinkTimer() {
var that = this;
log.silly('larvitsmpp: lib/server.js: resetEnqLinkTimer() - Resetting the kill timer');
if (this.enqLinkTimer) {
clearTimeout(this.enqLinkTimer);
}
this.enqLinkTimer = setTimeout(function() {
log.info('larvitsmpp: lib/server.js: resetEnqLinkTimer() - Closing session due to timeout');
that.closeSocket();
}, this.options.timeout);
}
/** /**
* Server session function - inherits from session() * Server session function - inherits from session()
* *
@@ -16,80 +95,9 @@ var log = require('winston'),
function serverSession(sock, options) { function serverSession(sock, options) {
var returnObj = session(sock); var returnObj = session(sock);
/** returnObj.options = options;
* Try to log a connecting peer in returnObj.login = login;
* returnObj.resetEnqLinkTimer = resetEnqLinkTimer;
* @param obj pduObj
*/
returnObj.login = function(pduObj) {
log.debug('larvitsmpp: lib/server.js: serverSession() - login() - Data received and session is not loggedIn');
// Pause socket so we do not receive any other commands until we have processed the login
returnObj.sock.pause();
// Only bind_* is accepted when the client is not logged in
if (pduObj.cmdName !== 'bind_transceiver' && pduObj.cmdName !== 'bind_receiver' && pduObj.cmdName !== 'bind_transmitter') {
log.debug('larvitsmpp: lib/server.js: serverSession() - login()) - Session is not loggedIn and no bind_* command is given. Return error "ESME_RINVBNDSTS');
smppUtils.pduReturn(pduObj, 'ESME_RINVBNDSTS', function(err, retPdu) {
if (err) {
returnObj.closeSocket();
return;
}
returnObj.sock.resume();
returnObj.sockWrite(retPdu);
});
return;
}
// If there is a checkuserpass(), use it to check system_id and password from the PDU
if (typeof options.checkuserpass === 'function') {
options.checkuserpass(pduObj.params.system_id, pduObj.params.password, function(err, res) {
if (err) {
returnObj.closeSocket();
return;
}
if ( ! res) {
log.info('larvitsmpp: lib/server.js: serverSession() - login() - Login failed! Connected host: ' + sock.remoteAddress + ':' + sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
returnObj.sock.resume();
returnObj.sendReturn(pduObj, 'ESME_RBINDFAIL');
return;
}
log.verbose('larvitsmpp: lib/server.js: serverSession() - login() - Login successful! Connected host: ' + sock.remoteAddress + ':' + sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
returnObj.loggedIn = true;
returnObj.sock.resume();
returnObj.sendReturn(pduObj);
});
return;
}
// If we arrived here it means we are not logged in and that a bind_* event happened and no checkuserpass() method exists. Lets login!
returnObj.loggedIn = true;
returnObj.sock.resume();
returnObj.sendReturn(pduObj);
};
/**
* Reset the enquire link timer
* If this is not ran within options.timeout milliseconds, this session will self terminate
*/
returnObj.resetEnqLinkTimer = function() {
log.silly('larvitsmpp: lib/server.js: serverSession() - resetEnqLinkTimer() - Resetting the kill timer');
if (returnObj.enqLinkTimer) {
clearTimeout(returnObj.enqLinkTimer);
}
returnObj.enqLinkTimer = setTimeout(function() {
log.info('larvitsmpp: lib/server.js: serverSession() - resetEnqLinkTimer() - Closing session due to timeout');
returnObj.closeSocket();
}, options.timeout);
};
returnObj.resetEnqLinkTimer(); returnObj.resetEnqLinkTimer();