Skip to content

Commit

Permalink
Merge pull request #209 from juliangut/feature/failure-callback
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Mar 2, 2022
2 parents de41366 + 52effda commit c31b734
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ This is the base object for a SMPP session. sessions can be created by calling
establishes a connection to the server. In this case the server passes the
session object to the `'session'` event listener.

#### session.send(pdu, [responseCallback], [sendCallback])
#### session.send(pdu, [responseCallback], [sendCallback], [failureCallback])
Sends a pdu request/response to the MC/ESME over the session.
The `pdu` is an instance of `smpp.PDU` which might be either a response or
a request pdu.
Expand All @@ -215,7 +215,9 @@ the proper value.
If the `pdu` is a request pdu, when the relevant response is received, the
optional `responseCallback` parameter will be invoked with the response pdu passed to it.

Optional `sendCallback` will be called when the pdu is flushed.
Optional `sendCallback` will be called when the pdu is successfully flushed.

Optional `failureCallback` will be called whenever it is not possible to write to the socket.

#### session.close([callback])
Closes the current session connection.
Expand All @@ -240,11 +242,11 @@ For all smpp operations you can call methods with the same name as the operation
name, which is equivalent to createing a pdu instance and then sending it over
the session.
For example calling `session.submit_sm(options, callback)` is equivalent to:
For example calling `session.submit_sm(options, [responseCallback], [sendCallback], [failureCallback])` is equivalent to:
``` javascript
var pdu = new smpp.PDU('submit_sm', options);
session.send(pdu, callback);
session.send(pdu, responseCallback);
```
#### Event: 'connect'
Expand Down
29 changes: 22 additions & 7 deletions lib/smpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,13 @@ Session.prototype._extractPDUs = function() {
this._busy = false;
};


Session.prototype.send = function(pdu, responseCallback, sendCallback) {
Session.prototype.send = function(pdu, responseCallback, sendCallback, failureCallback) {
if (!this.socket.writable) {
this.debug('socket.data.error', null, {error: 'Socket is not writable'});
if (failureCallback) {
pdu.command_status = defs.errors.ESME_RSUBMITFAIL;
failureCallback(pdu);
}
return false;
}
if (!pdu.isResponse()) {
Expand All @@ -204,16 +208,27 @@ Session.prototype.send = function(pdu, responseCallback, sendCallback) {
}
pdu.sequence_number = ++this.sequence;
}

if (responseCallback) {
this._callbacks[pdu.sequence_number] = responseCallback;
}
} else if (responseCallback && !sendCallback) {
sendCallback = responseCallback;
}
this.debug("pdu.command.out", pdu.command, pdu);
this.debug('pdu.command.out', pdu.command, pdu);
var buffer = pdu.toBuffer();
this.socket.write(buffer, (function() {
this.socket.write(buffer, (function(err) {
if (err) {
this.debug('socket.data.error', null, {error:'Cannot write command ' + pdu.command + ' to socket'});
if (!pdu.isResponse() && this._callbacks[pdu.sequence_number]) {
delete this._callbacks[pdu.sequence_number];
}
if (failureCallback) {
pdu.command_status = defs.errors.ESME_RSUBMITFAIL;
failureCallback(pdu);
}
return;
}

this.debug("socket.data.out", null, {bytes: buffer.length});
this.emit('send', pdu);
if (sendCallback) {
Expand Down Expand Up @@ -255,14 +270,14 @@ Session.prototype.destroy = function(callback) {
};

var createShortcut = function(command) {
return function(options, responseCallback, sendCallback) {
return function(options, responseCallback, sendCallback, failureCallback) {
if (typeof options == 'function') {
sendCallback = responseCallback;
responseCallback = options;
options = {};
}
var pdu = new PDU(command, options);
return this.send(pdu, responseCallback, sendCallback);
return this.send(pdu, responseCallback, sendCallback, failureCallback);
};
};

Expand Down
25 changes: 18 additions & 7 deletions test/smpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,30 @@ describe('Session', function() {
});
});

it('should receive failure callback', function(done) {
var session = smpp.connect({ port: autoresponder.port}, function() {
session.bind_transceiver({}, function(pdu) {
session.socket.writable = false;
session.submit_sm(new smpp.PDU('submit_sm'), function(pdu) {
throw Error('There should not be response');
},
function(pdu) {
throw Error('There should not be request call');
},
function(pdu) {
assert.equal(pdu.command, 'submit_sm');
assert.equal(pdu.command_status, smpp.ESME_RSUBMITFAIL);
done();
});
});
});
});
});

});

describe('Client/Server simulations', function() {

describe('standard connection simulations', function() {

var server, port, debugBuffer = [], lastServerError;

beforeEach(function (done) {
Expand Down Expand Up @@ -391,7 +407,6 @@ describe('Client/Server simulations', function() {
done();
});
});

});


Expand Down Expand Up @@ -449,10 +464,6 @@ describe('Client/Server simulations', function() {
done(); // Test ok
}
}, 10);

});


});

});

0 comments on commit c31b734

Please sign in to comment.