Commit fb764f1a authored by antirek's avatar antirek

changes in context

parent 9b5e7e71
......@@ -6,19 +6,27 @@ var commands = require('./command');
var Context = function (stream, debug) {
EventEmitter.call(this);
this.debug = debug;
this.stream = new Readable();
//this.stream = stream;
this.stream.setEncoding('utf8');
this.stream.wrap(stream);
this.state = state.init;
this.msg = "";
var self = this;
this.stream.on('readable', function () {
//always keep the 'leftover' part of the message
self.msg = self.read();
});
this.msg = this.read();
//this.msg = this.read();
this.variables = {};
this.pending = null;
this.stream.on('error', this.emit.bind(this, 'error'));
this.stream.on('close', this.emit.bind(this, 'close'));
};
......@@ -27,46 +35,64 @@ require('util').inherits(Context, EventEmitter);
Context.prototype.read = function() {
var buffer = this.stream.read();
if(!buffer) return this.msg;
this.msg += buffer.toString('utf8');
if(this.state === state.init) {
if (!buffer) return this.msg;
this.msg += buffer; //.toString('utf8');
if (this.state === state.init) {
if(this.msg.indexOf('\n\n') < 0) return this.msg; //we don't have whole message
this.readVariables(this.msg);
} else if(this.state === state.waiting) {
} else if (this.state === state.waiting) {
if(this.msg.indexOf('\n') < 0) return this.msg; //we don't have whole message
this.readResponse(this.msg);
}
return "";
};
Context.prototype.readVariables = function (msg) {
var lines = msg.split('\n');
/*
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
var split = line.split(':')
var name = split[0];
var value = split[1];
this.variables[name] = (value||'').trim();
}
}*/
lines.map(function (line) {
var split = line.split(':');
var name = split[0];
var value = split[1];
this.variables[name] = (value || '').trim();
}, this);
this.emit('variables', this.variables);
this.setState(state.waiting);
return "";
//return "";
};
Context.prototype.readResponse = function (msg) {
var lines = msg.split('\n');
/*
for(var i = 0; i < lines.length; i++) {
this.readResponseLine(lines[i]);
}
return "";
*/
lines.map(function (line) {
this.readResponseLine(line);
}, this);
//return "";
};
Context.prototype.readResponseLine = function (line) {
if(!line) return;
if (!line) return;
var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
if(!parsed) {
if (!parsed) {
return this.emit('hangup');
}
var response = {
......@@ -75,7 +101,7 @@ Context.prototype.readResponseLine = function (line) {
};
//our last command had a pending callback
if(this.pending) {
if (this.pending) {
var pending = this.pending;
this.pending = null;
pending(null, response);
......@@ -137,7 +163,7 @@ commands.map(function (command) {
};
});
var prepareArgs = function(args, argsRules){
var prepareArgs = function (args, argsRules) {
var q;
if (argsRules) {
q = args.map(function (arg, i) {
......
......@@ -21,16 +21,18 @@ var context = function(cb) {
ctx.sent = ctx.sent || [];
ctx.sent.push(msg);
};
ctx.once('variables', function(vars) {
cb(ctx);
});
writeVars(stream);
};
describe('Context', function() {
beforeEach(function(done) {
beforeEach(function (done) {
var self = this;
context(function(context) {
context(function (context) {
self.context = context;
done();
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment