Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Queuing mechanism implemented for post req
- Loading branch information
Balaji
committed
Oct 18, 2012
1 parent
d7c731f
commit 6acb9f3
Showing
5 changed files
with
58 additions
and
9 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @description: Implements a Post Request Queue. Buffers the post data and outputs in order. | ||
* | ||
*/ | ||
|
||
var util = require('util'), | ||
EventEmitter = require('events').EventEmitter ; | ||
|
||
var ReqQueue= function(writestream){ | ||
EventEmitter.call(this); | ||
this._q= []; | ||
this._writestream= writestream; | ||
}; | ||
|
||
util.inherits(ReqQueue, EventEmitter); | ||
|
||
ReqQueue.prototype.add= function(req, index){ | ||
var self= this; | ||
var pkt= {complete: false, index: index, data: []}; | ||
this._q.push(pkt); | ||
req.on("data", function(data){ | ||
pkt.data.push(data); | ||
}); | ||
req.on("end", function(){ | ||
pkt.complete= true; | ||
self.flush(); | ||
}); | ||
}; | ||
|
||
ReqQueue.prototype.flush= function(){ | ||
while(this._q.length){ | ||
var pkt= this._q[0]; | ||
if (pkt.complete){ | ||
//console.log("Writing packet -- "+pkt.index); | ||
for (var i=0, count= pkt.data.length; i< count; i++){ | ||
this._writestream.write(pkt.data[i]); | ||
} | ||
this._q.shift(); | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
module.exports= ReqQueue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters