Skip to content
Browse files

Fixed errors, added feature:

errors property needed to be an object, not array.
fixed calling close on incorrect object in sentence function while
processing a fatal response.
Fixed trap handling code to make sure the channel is still open when
emitting a trap event on a channel.
Fixed trap handling code when logging in. Trap now emitted properly.
Fixed null callback error in channel write. Now, write callbacks are not
required. It will be sent, and all respnose data ignored. Unless there
is a prior event to handle the data, and clearEvents is false.
Added clearEvents property/getter/setter to channel. Normally, when a
channel emits a done event, all done, data, and trap events are cleared.
If this property is set to false, then this does not happen.
  • Loading branch information...
1 parent 49b3751 commit 2519a96e3e0cc7c7d5f04d7a94fa8c71cefbd8a6 @Trakkasure committed Jul 5, 2011
Showing with 36 additions and 12 deletions.
  1. +36 −12 lib/index.js
View
48 lib/index.js
@@ -82,7 +82,7 @@ api=function (host,user,password,options) {
this.packet=[]; // current packet
this.channel={}; // all channels in use
this.trap=false; // we encountered a trap.
- this.error=[]; //Buffer errors
+ this.error={}; //Buffer errors
this.closeOnDone=false; // when !done event is called, close the connection.
this.closeOnFatal=false; // when !fatal occurs, close the connection.
this.datalen=0; // Used to look-ahead to see if more data is available after !done is received.
@@ -96,7 +96,7 @@ api.prototype.sentence=function(data) {
//util.debug('Sentence: fatal error: '+data);
this.packet.push(data);
this.handler.emit('fatal',this.packet,this);
- if (!this.closing) this.close();
+ if (!this.closing) this.handler.close();
return;
} else
if (data=='!fatal') { //we were sent a fatal message... wait for next sentence to get message.
@@ -109,7 +109,7 @@ api.prototype.sentence=function(data) {
if (this.trap) {// we previously caught a trap
this.trap=false;
var e=this.error;
- this.error=[];
+ this.error={};
if (this.debug>2) util.debug('Sentence: Sending trap.');
this.handler.emit('trap',e,this.handler);
} else {// no trap. Send general packet.
@@ -128,7 +128,7 @@ api.prototype.sentence=function(data) {
if (this.trap) { // if we're in a trap, send trap globally, since tag terminates a trap.
this.trap=false;
var e=this.error;
- this.error=[];
+ this.error={};
if (this.debug>2) util.debug('Sentence: Sending trap.');
this.channel[channel].emit('trap',e,this.channel[channel]);
}
@@ -145,11 +145,14 @@ api.prototype.sentence=function(data) {
this.buffer=[];
var channel=data.substring(5);
if (this.trap) {// we previously caught a trap
- this.trap=false;
+ if (!this.channel[channel]) return util.debug('ERROR: No channel for trap');;
+ this.trap=false; // we're emitting the trap. Clear it.
var e=this.error;
- this.error=[];
- if (this.debug>2) util.debug('Sentence: Sending trap in channel: '+channel);
- if (this.channel[channel]) this.channel[channel].emit('trap',e,this.channel[channel]);
+ this.error={};
+ if (this.channel[channel]) {
+ if (this.debug>2) util.debug('Sentence: Sending trap in channel: '+channel+" data:"+JSON.stringify(e));
+ this.channel[channel].emit('trap',e,this.channel[channel]);
+ }
} else // no trap. Send general packet.
if (this.channel[channel])
this.channel[channel].data(this.packet);
@@ -325,6 +328,19 @@ function Connection(instance) {
}
break;
case 'Sending Credentials':
+ if (self.trap) {
+ if (self.trap===true) {self.trap={"TRAP":"Logging in"}}
+ if (d=='!done') {
+ self.emit('trap',self.trap);
+ self.trap=false;
+ this.status="Connecting";
+ return;
+ } else {
+ d=d.split(/=/); // Catch multiple trap return keys.
+ if (d.length>2)
+ self.trap[d[1]]=d[2];
+ }
+ } else
if (d=='!done') {
self.status='Connected';
instance.removeAllListeners('sentence');
@@ -339,6 +355,9 @@ function Connection(instance) {
self.connectionCallback=null;
}
} else {
+ if (d=='!trap') {
+ return self.trap=true;
+ }
if (instance.debug>2)
util.debug(self.status);
instance.sentence(d); // start off trap processing.
@@ -464,6 +483,7 @@ function apiChannel(id,conn) {
this.closing=false;
this.closedg=false;
this.commands=[];
+ this.clearEvents=true;
return this.handler=new Channel(this);
}
util.inherits(Channel,events.EventEmitter);
@@ -489,6 +509,7 @@ function Channel(instance) {
return instance.closed;
},
closeOnDone: function(b) { return typeof(b)=='boolean'?instance.closeOnDone=b:instance.closeOnDone; },
+ clearEvents: function(b) { return typeof(b)=='boolean'?instance.clearEvents=b:instance.clearEvents; },
write:function(d,writeCallback) {
if (this.closing()) return;
if (d) {
@@ -511,7 +532,8 @@ function Channel(instance) {
this.buffer=[];
b.push('.tag='+this.getId());
this.callback=writeCallback;
- this.callback(this); // allow setup before sending command.
+ if (this.callback)
+ this.callback(this); // allow setup before sending command.
this.callback=null;
if (this.getConnection().debug>0) util.debug('Channel ('+this.getConnection().getHost()+":"+this.getId()+') write lines: '+b.join("\n"));
this.getConnection().write(b); // Send command.
@@ -523,9 +545,11 @@ function Channel(instance) {
var p=instance.packet;
instance.packet=[];
this.emit('done',p,this);
- this.removeAllListeners('done');
- this.removeAllListeners('data');
- this.removeAllListeners('read');
+ if (instance.clearEvents) {
+ this.removeAllListeners('done');
+ this.removeAllListeners('data');
+ this.removeAllListeners('read');
+ }
this.running(false);
if (instance.commands.length) {
var c=instance.commands.shift();

0 comments on commit 2519a96

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