Skip to content
Browse files

Adding the new pump.

  • Loading branch information...
1 parent b996f8e commit 388b078ec0d8f1bc323cf60f85be330d0eae2be7 @miksago committed Jan 19, 2010
Showing with 119 additions and 19 deletions.
  1. +3 −2 examples/example-client.js
  2. +10 −13 lib/smtp/client.js
  3. +52 −4 lib/smtp/packetHandler.js
  4. +54 −0 lib/vendor/Pump.js
View
5 examples/example-client.js
@@ -15,9 +15,10 @@ var message = "Date: "+(new Date()).toString()+"\n\
From: Node-SMTP <"+config.from+">\n\
To: <"+config.to+"\n\
Subject: Node SMTP Works!\n\
-So, It looks like node-smtp all works, which is great news! Time for some sleep I think though.\n\
+So, It looks like node-smtp all works, which is great news!\n\
\n\
-G'night Micheil.";
+Your's Truly,\n\
+SMTP Client.";
client.connect(config.port, config.host).addCallback(function(){
client.mail(config.from).addCallback(function(){
View
23 lib/smtp/client.js
@@ -46,17 +46,8 @@ var Client = function(){
this.debug = true;
- this.packetHandler.addListener("idle", function(){
- setTimeout(function(){
- if(client.packetHandler._stack.length == 0 && !client.waiting){
- client.emit("idle");
- }
- }, 1000);
- });
this.packetHandler.addListener("packet", function(packet){
- if(client.debug){
- sys.puts("\033[0;31m>> "+packet.status+" "+packet.data.join("\n")+"\033[0m")
- }
+ sys.puts(JSON.stringify(packet));
});
};
@@ -72,7 +63,8 @@ Client.prototype.connect = function(port, host){
this.socket = new tcp.createConnection(this.port, this.host);
this.socket.setEncoding("ascii");
- this.packetHandler.addOnce("packet", function(packet){
+ this.packetHandler.addListener("packet", function(packet){
+ client.packetHandler.removeListener("packet", arguments.callee);
if(packet.status == "220"){
if(/ESMTP/i.test(packet.data)){
client.esmtp = true;
@@ -89,7 +81,7 @@ Client.prototype.connect = function(port, host){
if(client.debug){
sys.puts("\033[0;33m>> "+data.replace(/[\r\n]/gi, "")+"\033[0m")
}
- client.packetHandler.push(data);
+ client.packetHandler.receive(data);
});
return promise;
@@ -122,7 +114,11 @@ Client.prototype.send = function(){
var promise = new process.Promise();
this.waiting = true;
- this.packetHandler.addOnce("packet", function(){
+ this.packetHandler.addListener("packet", function(){
+ sys.puts("send got packet back!");
+
+ client.packetHandler.removeListener("packet", arguments.callee);
+
promise.emitSuccess.apply(promise, arguments);
client.waiting = false;
});
@@ -173,6 +169,7 @@ Client.prototype.ehlo = function(){
var promise = new process.Promise();
this.send("EHLO", this.host).addCallback(function(packet){
+ sys.puts(JSON.stringify(packet));
if(packet.status == "250"){
client.parseCapabilities(packet.data);
client.connected = true;
View
56 lib/smtp/packetHandler.js
@@ -15,14 +15,61 @@
var sys = require("sys");
// Local
var EventEmitter = require("../vendor/eventEmitter");
-var Queue = require("../vendor/Queue/lib/Queue").Queue;
+var Pump = require("../vendor/Pump").Pump;
/*-----------------------------------------------
PacketHandler
-----------------------------------------------*/
-exports.PacketHandler = function(){};
-exports.PacketHandler.prototype = new Queue();
+exports.PacketHandler = function(){
+ var self = this;
+ this.pump = new Pump(this._PumpProcess, this);
+ this.pump.addListener("drain", function(){
+ self._drain.call(self)
+ });
+ this.pump.addListener("data", function(data){
+ sys.puts(">>> "+data);
+ });
+ return this;
+};
+
+process.inherits(exports.PacketHandler, process.EventEmitter);
+
+
+exports.PacketHandler.prototype.receive = function(data){
+ var lines = data.split("\r\n");
+
+ while(lines.length && (line = lines.shift())){
+ this.pump.push(line);
+ }
+};
+exports.PacketHandler.prototype._PumpProcess = function(pump){
+ var pStatus = null, pContinues = true, pData = [], line;
+
+ while(pContinues && pump._stream.length && (line = pump._stream.shift())){
+ var packet = /([0-9]{3})([\ \-]{1})(.*)/.exec(line);
+
+ pStatus = packet[1];
+ pData.push(packet[3]);
+
+ pContinues = (packet[2] == "-");
+ }
+
+ this.emit("packet", {
+ status: pStatus,
+ data: pData
+ });
+};
+
+exports.PacketHandler.prototype._drain = function(){
+ sys.puts("drain");
+ this.emit("drain");
+};
+
+
+
+//exports.PacketHandler.prototype = new Queue();
+/*
exports.PacketHandler.prototype.push = function(data){
var packets = data.split("\r\n");
while(packets.length && (packet = packets.shift())){
@@ -58,4 +105,5 @@ exports.PacketHandler.prototype.process = function(){
this.emit("drain");
}
-};
+};
+*/
View
54 lib/vendor/Pump.js
@@ -0,0 +1,54 @@
+/*===============================================
+ File: Pump.js
+
+ Author: Micheil Smith
+ Description:
+ A simple data pump
+===============================================*/
+
+var sys = require("sys");
+
+var Pump = exports.Pump = function(/*Function*/ handler, /*Scope*/ scope){
+ this._stream = [];
+ this.process = handler || this.defaultProcess;
+ this.scope = scope;
+
+ var pump = this;
+ this.addListener("push", function(){
+ pump._process();
+ });
+};
+
+process.inherits(Pump, process.EventEmitter);
+
+Pump.prototype.push = function(/*Mixed*/ data){
+ clearTimeout(this._bufTimer);
+ this._stream.push(data);
+
+ var scope = this;
+
+ this._bufTimer = setTimeout(function(){
+ scope.emit("push", data);
+ }, 100);
+};
+
+Pump.prototype._process = function(ignore){
+ if( ! this.running){
+ while(this._stream && this._stream.length > 0){
+ this.running = true;
+ this.process.call(this.scope, this);
+ }
+
+ if (this._stream && !(this._stream.length > 0)) {
+ this.emit("drain");
+ }
+
+ this.running = false;
+ }
+};
+
+Pump.prototype.defaultProcess = function(/*Scope*/ pump){
+ while(pump._stream.length && (packet = pump._stream.shift())){
+ pump.emit("data", pump._stream.shift());
+ }
+};

0 comments on commit 388b078

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