diff --git a/larvitsmpp.js b/larvitsmpp.js index 14c1428..6e63c64 100644 --- a/larvitsmpp.js +++ b/larvitsmpp.js @@ -12,8 +12,6 @@ var log = require('winston'), /** * Set up SMPP server * - * The option "checkUserAndPass" should be a function, taking three parameters: username, password and callback(err) - * * @param obj options * * port - what port to bind to, defaults to 2775 * * checkUserAndPass - to require username and password to bind @@ -122,11 +120,101 @@ exports.server = function(options) { }); smppSession.on('enquire_link', function(pdu) { + var sendRet; + log.silly('larvitsmpp: server() - Enquire_link - client heart beat.'); - smppSession.send(pdu.response({ + sendRet = smppSession.send(pdu.response({ 'command_status': smpp.ESME_ROK })); + + if ( ! sendRet) { + // No writeable socket, do something! + } + }); }).listen(options.port); +}; + +/** + * Connect as a client to an SMPP server + * + * @param obj options + * * host (default 127.0.0.1) + * * port (default 2775) + * * username (default false) + * * password (default false) + * * mode (default 'transceiver', other options: 'receiver', 'transmitter') + * * heartbeat - set the heartbeat interval (default 10000) + * @param func callback(err, returnObj) + */ +exports.client = function(options, callback) { + var smppSession, + heartbeatTimer, + err, + returnObj = {'smppSession': smppSession}, + bindOptions = {}; + + function heartbeat() { + log.silly('larvitsmpp: client() - heartbeat() - called'); + + smppSession.enquire_link(); + heartbeatTimer = setTimeout(heartbeat, options.heartbeat); + // Here we need a timeout and then we need to reconnect + } + + returnObj.close = function() { + clearTimeout(heartbeatTimer); + smppSession.close(); + }; + + options = merge({ + 'host': '127.0.0.1', + 'port': 2775, + 'username': undefined, + 'password': undefined, + 'mode': 'transceiver', + 'heartbeat': 10000 + }, options); + + log.info('larvitsmpp: client() - Connecting to SMPP server at ' + options.host + ':' + options.port); + smppSession = smpp.connect(options.host, options.port); + + if (options.username !== undefined && options.password !== undefined) { + bindOptions.system_id = options.username; + bindOptions.password = options.password; + } + + if (options.mode === 'transceiver') { + log.error('bajs'); + } else if (options.mode === 'receiver') { + log.error('skabb'); + } else if (options.mode === 'transmitter') { + smppSession.bind_transmitter(bindOptions, function(pdu) { + var err; + + log.info('larvitsmpp: client() - bind_transmitter done.'); + + if (pdu.command_status === 0) { + log.info('larvitsmpp: client() - bind_transmitter returned 0, success!'); + + heartbeat(); + callback(null, returnObj); + } else { + err = new Error('models/smppsend.js: bind_transmitter returned "' + pdu.command_status + '", fail!'); + log.warn(err.message, pdu); + smppSession.close(); + callback(err); + } + }); + } else { + err = new Error('larvitsmpp: client() - Invalid connection mode'); + log.error(err.message); + callback(err); + } + + smppSession.on('pdu', function(pdu) { + log.silly('larvitsmpp: client() - Received command "' + pdu.command + '"'); + }); + }; \ No newline at end of file