Skip to content

Commit

Permalink
Fixing server getDirectory replies
Browse files Browse the repository at this point in the history
  • Loading branch information
dufourgilles committed Jan 9, 2019
1 parent 74b15ed commit 1f3cf0f
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 17 deletions.
28 changes: 22 additions & 6 deletions ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,15 @@ Root.prototype.addChild = function(child) {

Root.prototype.encode = function(ber) {
ber.startSequence(BER.APPLICATION(0));

if(this.elements !== undefined) {
ber.startSequence(BER.APPLICATION(11));
ber.startSequence(BER.CONTEXT(0));
for(var i=0; i<this.elements.length; i++) {
ber.startSequence(BER.CONTEXT(0));
this.elements[i].encode(ber);
ber.endSequence(); // BER.CONTEXT(0)
}
ber.endSequence(); // BER.CONTEXT(0)
ber.endSequence();
}

ber.endSequence(); // BER.APPLICATION(0)
}

Expand Down Expand Up @@ -163,6 +161,10 @@ TreeNode.prototype.isFunction = function() {
return (this instanceof QualifiedFunction);
}

TreeNode.prototype.isRoot = function() {
return this._parent == null;
}

TreeNode.prototype.isQualified = function() {
return ((this instanceof QualifiedParameter)||
(this instanceof QualifiedNode) ||
Expand Down Expand Up @@ -192,6 +194,20 @@ TreeNode.prototype.cancelCallbacks = function() {
}
}

TreeNode.prototype.getMinimalContent = function() {
let obj;
if (this.isQualified()) {
obj = new this.constructor(this.path);
}
else {
obj = new this.constructor(this.number);
}
if (this.contents !== undefined) {
obj.contents= this.contents;
}
return obj;
};

TreeNode.prototype.getDuplicate = function() {
let obj = this.getMinimal();
obj.update(this);
Expand Down Expand Up @@ -545,7 +561,7 @@ QualifiedNode.prototype.getMinimal = function(complete = false) {

QualifiedNode.prototype.update = function(other) {
callbacks = QualifiedNode.super_.prototype.update.apply(this);
if((other === undefined) && (other.contents !== undefined)) {
if((other !== undefined) && (other.contents !== undefined)) {
if (this.contents == null) {
this.contents = other.contents;
}
Expand Down Expand Up @@ -2297,7 +2313,7 @@ Parameter.prototype.setValue = function(value, callback) {
}

Parameter.prototype.toQualified = function() {
let qp = new QualifiedNode(this.getPath());
let qp = new QualifiedParameter(this.getPath());
qp.update(this);
return qp;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "emberplus",
"version": "1.7.19",
"version": "1.7.20",
"description": "Javascript implementation of the Ember+ automation protocol",
"main": "index.js",
"scripts": {
Expand Down
21 changes: 11 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,32 +358,33 @@ TreeServer.prototype.getResponse = function(element) {

TreeServer.prototype.getQualifiedResponse = function(element) {
let res = new ember.Root();
let dup = element.toQualified();
let dup;
if (element.isRoot() === false) {
dup = element.toQualified();
}
let children = element.getChildren();
if (children != null) {
for (let i = 0; i < children.length; i++) {
dup.addChild(children[i].getDuplicate());
res.addChild(children[i].toQualified().getMinimalContent());
}
}
res.elements = [dup];
else {
res.addChild(dup);
}
return res;
}

TreeServer.prototype.handleGetDirectory = function(client, element) {

if (client !== undefined) {
if ((element.isMatrix() || element.isParameter()) &&
(!element.isStream())) {
// ember spec: parameter without streamIdentifier should
// report their value changes automatically.
this.subscribe(client, element);
}
let res;
if (client.request.path == null) {
res = this.getResponse(element);
}
else {
res = this.getQualifiedResponse(element);
let res = this.getQualifiedResponse(element);
if (this._debug) {
console.log("getDirectory response", res);
}
client.sendBERNode(res);
}
Expand Down
148 changes: 148 additions & 0 deletions test/Server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const expect = require("expect");
const TreeServer = require("../server");
const DeviceTree = require("../").DeviceTree;


const LOCALHOST = "127.0.0.1";
const PORT = 9008;


const init = function(_src,_tgt) {
const targets = _tgt === undefined ? [ "tgt1", "tgt2", "tgt3" ] : _tgt;
const sources = _src === undefined ? [ "src1", "src2", "src3" ] : _src;
const labels = function(endpoints) {
let labels = [];
for (let i = 0; i < endpoints.length; i++) {
let endpoint = endpoints[i];
let l = { identifier: `Label-${i}` };
if (endpoint) {
l.value = endpoint;
}
labels.push(l);
}
return labels;
};

const buildConnections = function(s, t) {
let connections = [];
for (let i = 0; i < t.length; i++) {
connections.push({target: `${i}`});
}
return connections;
};

return [
{
// path "0"
identifier: "scoreMaster",
children: [
{
// path "0.0"
identifier: "identity",
children: [
{identifier: "product", value: "S-CORE Master"},
{identifier: "company", value: "EVS"},
{identifier: "version", value: "1.2.0"},
{identifier: "author", value: "g.dufour@evs.com"}
]
},
{
// path "0.1"
identifier: "router",
children: [
{
// path 0.1.0
identifier: "matrix",
type: "oneToN",
mode: "linear",
targetCount: targets.length,
sourceCount: sources.length,
connections: buildConnections(sources, targets),
labels: ["0.1.1000"]
},
{
identifier: "labels",
// path "0.1.1000"
number: 1000,
children: [
{
identifier: "targets",
// Must be 1
number: 1,
children: labels(targets)
},
{
identifier: "sources",
// Must be 2
number: 2,
children: labels(sources)
}
]
}
]
}
]
}
];

}
describe("server", function() {

describe("JSONtoTree", function() {
let jsonTree;
before(function() {
jsonTree = init();
});
it("should generate an ember tree from json", function() {
const root = TreeServer.JSONtoTree(jsonTree);
expect(root).toBeDefined();
expect(root.elements).toBeDefined();
expect(root.elements.length).toBe(1);
console.log("root", root.elements[0].contents);
expect(root.elements[0].contents.identifier).toBe("scoreMaster");
expect(root.elements[0].children.length).toBe(2);
});
});

describe("Server - Client communication", function() {
let server,client;
before(function() {
jsonTree = init();
const root = TreeServer.JSONtoTree(jsonTree);
server = new TreeServer(LOCALHOST, PORT, root);
//server._debug = true;
return server.listen().then(() => {
console.log("server listening");
});
});
after(function() {
client.disconnect();
server.close();
})
it("should receive and decode the full tree", function () {
client = new DeviceTree(LOCALHOST, PORT);
//client._debug = true;
return Promise.resolve()
.then(() => client.connect())
.then(() => {
console.log("client connected");
return client.getDirectory()
})
.then(() => {
expect(client.root).toBeDefined();
expect(client.root.elements).toBeDefined();
expect(client.root.elements.length).toBe(1);
expect(client.root.elements[0].contents.identifier).toBe("scoreMaster");
return client.getDirectory(client.root.elements[0]);
})
.then(() => {
expect(client.root.elements[0].children.length).toBe(2);
return client.getDirectory(client.root.elements[0].children[0]);
})
.then(() => {
expect(client.root.elements[0].children[0].children.length).toBe(4);
expect(client.root.elements[0].children[0].children[3].contents.identifier).toBe("author");
});
});
});
});

0 comments on commit 1f3cf0f

Please sign in to comment.