Permalink
Browse files

Refactoring

  • Loading branch information...
1 parent e894dac commit 64cf2cba47c7b1099bae909bf22b0bcacbbd541b @christav christav committed Mar 15, 2013
Showing with 17 additions and 12 deletions.
  1. +17 −12 lib/messagesequencer.js
View
@@ -51,18 +51,32 @@ MessageSequencer.prototype.send = function(name, args) {
};
MessageSequencer.prototype.receiveMessage = function(sourceNodeId, name, message, seq) {
+ this.processMessage([sourceNodeId, name, message, seq]);
+};
+
+MessageSequencer.prototype.receiveBadMessage = function(sourceNodeId, name, seq) {
+ var badMsg = [sourceNodeId, name, null, seq];
+ badMsg.isBadMessage = true;
+ this.processMessage(badMsg);
+};
+
+MessageSequencer.prototype.processMessage = function(message) {
+ var seq = message[3];
if (this.nextExpectedMessageNumber === null) {
this.nextExpectedMessageNumber = seq;
}
if (seq >= this.nextExpectedMessageNumber) {
- this.pendingMessages.push([sourceNodeId, name, message, seq]);
- this.pendingMessages.sort(function (a, b) { return a[3] - b[3]; });
-
+ this.addPendingMessage(message);
this.sendPendingMessages();
}
};
+MessageSequencer.prototype.addPendingMessage = function(message) {
+ this.pendingMessages.push(message);
+ this.pendingMessages.sort(function (a, b) { return a[3] - b[3]; });
+};
+
MessageSequencer.prototype.sendPendingMessages = function() {
while (this.pendingMessages.length > 0 && this.pendingMessages[0][3] === this.nextExpectedMessageNumber)
{
@@ -74,12 +88,3 @@ MessageSequencer.prototype.sendPendingMessages = function() {
}
};
-MessageSequencer.prototype.receiveBadMessage = function(sourceNodeId, name, seq) {
- if (seq >= this.nextExpectedMessageNumber) {
- var badMsg = [sourceNodeId, name, null, seq];
- badMsg.isBadMessage = true;
- this.pendingMessages.push(badMsg);
- this.pendingMessages.sort(function (a, b) {return a[3] - b[3]; });
- this.sendPendingMessages();
- }
-};

0 comments on commit 64cf2cb

Please sign in to comment.