Skip to content
Browse files

avoid repeated connecting

if SP.linking=true, do not connecting again
if SP connection is closed, allow re-connecting to SMG
when connection is closed, and have pending request, setTimeout to
connect

Merge branch 'master' of github.com:kaven276/sms

Conflicts:
	package.json
  • Loading branch information...
2 parents 55b238b + 2391deb commit 80152513785c4b9ec1459f04f07cccd5eee9a752 @kaven276 committed Jun 23, 2015
Showing with 18 additions and 11 deletions.
  1. +18 −11 lib/nodes/nodeSP.js
View
29 lib/nodes/nodeSP.js
@@ -37,7 +37,8 @@ function SP(NodeID, SMGHost, SMGPort, SMGUser, SMGPass, SPPort, SPHost, SPUser,
m.listen(SPPort, SPHost);
}
- m.linkStatus = false;
+ m.linkStatus = false; // if connected and authenticated connection established
+ m.linking = false; // if in connecting to SMG, pending status, to prevent repeated connecting attempt
m.sendQueue = [];
m.ackQueue = [];
m.superQueue = new SuperQueue();
@@ -96,23 +97,29 @@ SP.prototype.findAckReq = function(resPDU){
throw new Error('no acknownledge queue message corresponding to the response PDU.');
};
-SP.prototype.connect = function(cb){
+SP.prototype.connect = function(){
+ if (this.linking) return; else this.linking = true;
var me = this
, client = me.socket = new net.Socket()
, bound = false
;
- client.on('close', function(){
+ client.on('close', function(had_error){
me.linkStatus = false;
- logSMGConnect('socket close');
+ me.linking = false;
+ logSMGConnect('socket close', had_error);
+ if (me.sendQueue.length > 0) {
+ setTimeout(function(){
+ me.connect();
+ }, 3000);
+ }
});
client.on('end', function(){
- me.linkStatus = false;
logSMGConnect('socket end');
});
- client.on('error', function(){
+ client.on('error', function(err){
logSMGConnect('socket error!');
- console.error('connection to SMG socket error!');
+ console.error('connection to SMG socket error!', err);
logSMGConnect(me);
});
client.on('timeout', function(){
@@ -173,7 +180,7 @@ SP.prototype.send = function(msg, callback){
msg.callback = callback;
}
var me = this;
- me.superQueue.enqueue({weight : msg.splits || 1}, function(option, actcb){
+ me.superQueue.enqueue({weight: msg.splits || 1}, function(option, actcb){
function _send(){
me.socket.write(msg.makePDU(null, me.NodeID));
me.ackQueue.push(msg);
@@ -182,7 +189,7 @@ SP.prototype.send = function(msg, callback){
actcb();
}
- if (this.linkStatus) {
+ if (me.linkStatus) {
_send();
} else {
me.sendQueue.push(_send);
@@ -290,7 +297,7 @@ if (bUnitText) {
msg = new Submit('8615620001781', encoding, content, option); // 8618602247757 8615620001781
} else {
// todo: for multiple numbers, SMG result is 5 (bad format)
- msg = new Submit(['8615620007256', '8615620001781' , '8618602247741'], encoding, content);
+ msg = new Submit(['8615620007256', '8615620001781', '8618602247741'], encoding, content);
}
// console.log(msg);
@@ -325,7 +332,7 @@ if (bUnitText) {
});
break;
case -1:
- option = { 'TP_udhi' : 1, 'ScheduleTime' : utl.afterNow(1)};
+ option = {'TP_udhi': 1, 'ScheduleTime': utl.afterNow(1)};
// for send with response callback mode
var parts = 10;
content.writeUInt8(21, 3);

0 comments on commit 8015251

Please sign in to comment.
Something went wrong with that request. Please try again.