Commit fb764f1a authored by antirek's avatar antirek

changes in context

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