Some restructuring etc

This commit is contained in:
2015-05-10 18:23:38 +02:00
parent 8c6c50e8d0
commit 2307800d22
3 changed files with 60 additions and 34 deletions
+10 -9
View File
@@ -59,17 +59,18 @@ function clientSession(sock, options) {
returnObj.login(); returnObj.login();
returnObj.resetEnqLinkTimer(); returnObj.resetEnqLinkTimer();
// Handle incoming Pdu Objects
returnObj.on('incomingPduObj', function(pduObj) { returnObj.on('incomingPduObj', function(pduObj) {
if (pduObj.cmdName === 'deliver_sm') { // Call the appropriate handleCmd function
returnObj.deliverSm(pduObj);
} else if (pduObj.cmdName === 'enquire_link') { if (typeof returnObj.handleCmd[pduObj.cmdName] === 'function') {
returnObj.enquireLink(); log.debug('larvitsmpp: lib/client.js: clientSession() - returnObj.on(incomingPduObj) - Running cmd handling function returnObj.handleCmd.' + pduObj.cmdName + '()');
} else if (pduObj.cmdName === 'submit_sm') {
returnObj.submitSm(pduObj); returnObj.handleCmd[pduObj.cmdName](pduObj);
} else if (pduObj.cmdName === 'unbind') {
returnObj.sendReturn(pduObj, 'ESME_ROK', undefined, true);
} else { } else {
// All other commands we do not support // No command handling function is registered, return error "invalid command"
log.info('larvitsmpp: lib/client.js: clientSession() - returnObj.on(incomingPduObj) - No handling function found for command: "' + pduObj.cmdName + '"');
returnObj.sendReturn(pduObj, 'ESME_RINVCMDID'); returnObj.sendReturn(pduObj, 'ESME_RINVCMDID');
} }
}); });
+16 -7
View File
@@ -93,20 +93,29 @@ function serverSession(sock, options) {
returnObj.resetEnqLinkTimer(); returnObj.resetEnqLinkTimer();
// Handle incoming Pdu Objects
returnObj.on('incomingPduObj', function(pduObj) { returnObj.on('incomingPduObj', function(pduObj) {
// Call the appropriate handleCmd function
// Unbind is always ok
if (pduObj.cmdName === 'unbind') { if (pduObj.cmdName === 'unbind') {
returnObj.sendReturn(pduObj, 'ESME_ROK', undefined, true); returnObj.sendReturn(pduObj, 'ESME_ROK', undefined, true);
// If client is not logged in, always run the login function
} else if (returnObj.loggedIn === false) { } else if (returnObj.loggedIn === false) {
log.debug('larvitsmpp: lib/server.js: serverSession() - returnObj.handleIncomingPdu() - Not logged in, running login function'); log.debug('larvitsmpp: lib/server.js: serverSession() - returnObj.handleIncomingPdu() - Not logged in, running login function');
returnObj.login(pduObj); returnObj.login(pduObj);
} else if (pduObj.cmdName === 'deliver_sm') {
returnObj.deliverSm(pduObj); // Client is logged in, try to match a handling function
} else if (pduObj.cmdName === 'enquire_link') { } else if (typeof returnObj.handleCmd[pduObj.cmdName] === 'function') {
returnObj.enquireLink(pduObj); log.debug('larvitsmpp: lib/server.js: serverSession() - returnObj.on(incomingPduObj) - Running cmd handling function returnObj.handleCmd.' + pduObj.cmdName + '()');
} else if (pduObj.cmdName === 'submit_sm') {
returnObj.submitSm(pduObj); returnObj.handleCmd[pduObj.cmdName](pduObj);
// No command handling function is registered, return error "invalid command"
} else { } else {
// All other commands we do not support log.info('larvitsmpp: lib/server.js: serverSession() - returnObj.on(incomingPduObj) - No handling function found for command: "' + pduObj.cmdName + '"');
returnObj.sendReturn(pduObj, 'ESME_RINVCMDID'); returnObj.sendReturn(pduObj, 'ESME_RINVCMDID');
} }
}); });
+29 -13
View File
@@ -135,9 +135,6 @@ function session(sock) {
log.debug('larvitsmpp: lib/session.js: session() - returnObj.send() - returnObj.on(incomingPduObj) - cmdName: ' + incPduObj.cmdName + ' seqNr: ' + incPduObj.seqNr + ' cmdStatus: ' + incPduObj.cmdStatus); log.debug('larvitsmpp: lib/session.js: session() - returnObj.send() - returnObj.on(incomingPduObj) - cmdName: ' + incPduObj.cmdName + ' seqNr: ' + incPduObj.seqNr + ' cmdStatus: ' + incPduObj.cmdStatus);
// Clean up by removing this listener or else it will lurk along forever
returnObj.removeAllListeners('incomingPdu' + pduObj.seqNr);
// Make sure this is the actual response to the sent PDU // Make sure this is the actual response to the sent PDU
if (incPduObj.isResponse() && incPduObj.seqNr === pduObj.seqNr) { if (incPduObj.isResponse() && incPduObj.seqNr === pduObj.seqNr) {
callback(null, incPduObj); callback(null, incPduObj);
@@ -377,8 +374,12 @@ function session(sock) {
}); });
}; };
// Handle incomming commands.
// This is intended to be extended
returnObj.handleCmd = {};
// Handle incoming deliver_sm // Handle incoming deliver_sm
returnObj.deliverSm = function(pduObj) { returnObj.handleCmd.deliver_sm = function(pduObj) {
var dlrObj; var dlrObj;
// TLV message_state must exists // TLV message_state must exists
@@ -415,8 +416,15 @@ function session(sock) {
returnObj.sendReturn(pduObj); returnObj.sendReturn(pduObj);
}; };
// Enquire link
returnObj.handleCmd.enquire_link = function(pduObj) {
log.silly('larvitsmpp: lib/session.js: session() - enquireLink() - Enquiring link');
returnObj.resetEnqLinkTimer();
returnObj.sendReturn(pduObj);
};
// Handle incoming submit_sm // Handle incoming submit_sm
returnObj.submitSm = function(pduObj) { returnObj.handleCmd.submit_sm = function(pduObj) {
var smsObj = {}; var smsObj = {};
smsObj = { smsObj = {
@@ -440,6 +448,11 @@ function session(sock) {
returnObj.emit('sms', smsObj, smsReceived); returnObj.emit('sms', smsObj, smsReceived);
}; };
// Handle incoming unbind
returnObj.handleCmd.unbind = function(pduObj) {
returnObj.sendReturn(pduObj, 'ESME_ROK', undefined, true);
};
// Dummy, should be extended by serverSession or clientSession // Dummy, should be extended by serverSession or clientSession
returnObj.login = function() { returnObj.login = function() {
log.info('larvitsmpp: lib/session.js: session() - login() - Dummy login function ran, this might be a mistake'); log.info('larvitsmpp: lib/session.js: session() - login() - Dummy login function ran, this might be a mistake');
@@ -451,12 +464,7 @@ function session(sock) {
log.silly('larvitsmpp: lib/session.js: session() - resetEnqLinkTimer() - Resetting the kill timer'); log.silly('larvitsmpp: lib/session.js: session() - resetEnqLinkTimer() - Resetting the kill timer');
}; };
returnObj.enquireLink = function(pduObj) { // Unbind this session
log.silly('larvitsmpp: lib/session.js: session() - enquireLink() - Enquiring link');
returnObj.resetEnqLinkTimer();
returnObj.sendReturn(pduObj);
};
returnObj.unbind = function() { returnObj.unbind = function() {
returnObj.send({ returnObj.send({
'cmdName': 'unbind' 'cmdName': 'unbind'
@@ -502,15 +510,21 @@ function session(sock) {
returnObj.emit('incomingPdu', pdu); returnObj.emit('incomingPdu', pdu);
} }
if (returnObj.dataQueue.length === 0) {
log.silly('larvitsmpp: lib/session.js: session() - sock.on(data) - All queue hanlded, breaking while loop.');
break;
}
// If the command length is larger than the queue, we need to wait for more data. Stop processing! // If the command length is larger than the queue, we need to wait for more data. Stop processing!
if (cmdLength > returnObj.dataQueue) { if (cmdLength > returnObj.dataQueue) {
log.debug('larvitsmpp: lib/session.js: session() - sock.on(data) - Incomplete PDU found in dataQueue, waiting for more data to continue.'); log.debug('larvitsmpp: lib/session.js: session() - sock.on(data) - Incomplete PDU found in dataQueue, waiting for more data to continue. Current cmdLength: ' + cmdLength + ' current queue: ' + returnObj.dataQueue.toString('hex'));
break; break;
} }
} }
}); });
// Handle incoming Pdu Buffers
returnObj.on('incomingPdu', function(pdu) { returnObj.on('incomingPdu', function(pdu) {
utils.pduToObj(pdu, function(err, pduObj) { utils.pduToObj(pdu, function(err, pduObj) {
if (err) { if (err) {
@@ -523,6 +537,9 @@ function session(sock) {
if (pduObj.isResponse()) { if (pduObj.isResponse()) {
// We do this so we can remove the dynamic event listeners to not have a memory leak // We do this so we can remove the dynamic event listeners to not have a memory leak
returnObj.emit('incomingPduObj' + pduObj.seqNr, pduObj); returnObj.emit('incomingPduObj' + pduObj.seqNr, pduObj);
// Clean up by removing this listener or else it will lurk along forever
returnObj.removeAllListeners('incomingPduObj' + pduObj.seqNr);
} else { } else {
returnObj.emit('incomingPduObj', pduObj); returnObj.emit('incomingPduObj', pduObj);
} }
@@ -530,7 +547,6 @@ function session(sock) {
}); });
}); });
// Add a 'close' event handler to this instance of socket // Add a 'close' event handler to this instance of socket
sock.on('close', function() { sock.on('close', function() {
returnObj.emit('close'); returnObj.emit('close');