Skip to content

Commit 1f3cf0f

Browse files
committed
Fixing server getDirectory replies
1 parent 74b15ed commit 1f3cf0f

File tree

4 files changed

+182
-17
lines changed

4 files changed

+182
-17
lines changed

ember.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,15 @@ Root.prototype.addChild = function(child) {
106106

107107
Root.prototype.encode = function(ber) {
108108
ber.startSequence(BER.APPLICATION(0));
109-
110109
if(this.elements !== undefined) {
111110
ber.startSequence(BER.APPLICATION(11));
112-
ber.startSequence(BER.CONTEXT(0));
113111
for(var i=0; i<this.elements.length; i++) {
112+
ber.startSequence(BER.CONTEXT(0));
114113
this.elements[i].encode(ber);
114+
ber.endSequence(); // BER.CONTEXT(0)
115115
}
116-
ber.endSequence(); // BER.CONTEXT(0)
117116
ber.endSequence();
118117
}
119-
120118
ber.endSequence(); // BER.APPLICATION(0)
121119
}
122120

@@ -163,6 +161,10 @@ TreeNode.prototype.isFunction = function() {
163161
return (this instanceof QualifiedFunction);
164162
}
165163

164+
TreeNode.prototype.isRoot = function() {
165+
return this._parent == null;
166+
}
167+
166168
TreeNode.prototype.isQualified = function() {
167169
return ((this instanceof QualifiedParameter)||
168170
(this instanceof QualifiedNode) ||
@@ -192,6 +194,20 @@ TreeNode.prototype.cancelCallbacks = function() {
192194
}
193195
}
194196

197+
TreeNode.prototype.getMinimalContent = function() {
198+
let obj;
199+
if (this.isQualified()) {
200+
obj = new this.constructor(this.path);
201+
}
202+
else {
203+
obj = new this.constructor(this.number);
204+
}
205+
if (this.contents !== undefined) {
206+
obj.contents= this.contents;
207+
}
208+
return obj;
209+
};
210+
195211
TreeNode.prototype.getDuplicate = function() {
196212
let obj = this.getMinimal();
197213
obj.update(this);
@@ -545,7 +561,7 @@ QualifiedNode.prototype.getMinimal = function(complete = false) {
545561

546562
QualifiedNode.prototype.update = function(other) {
547563
callbacks = QualifiedNode.super_.prototype.update.apply(this);
548-
if((other === undefined) && (other.contents !== undefined)) {
564+
if((other !== undefined) && (other.contents !== undefined)) {
549565
if (this.contents == null) {
550566
this.contents = other.contents;
551567
}
@@ -2297,7 +2313,7 @@ Parameter.prototype.setValue = function(value, callback) {
22972313
}
22982314

22992315
Parameter.prototype.toQualified = function() {
2300-
let qp = new QualifiedNode(this.getPath());
2316+
let qp = new QualifiedParameter(this.getPath());
23012317
qp.update(this);
23022318
return qp;
23032319
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "emberplus",
3-
"version": "1.7.19",
3+
"version": "1.7.20",
44
"description": "Javascript implementation of the Ember+ automation protocol",
55
"main": "index.js",
66
"scripts": {

server.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -358,32 +358,33 @@ TreeServer.prototype.getResponse = function(element) {
358358

359359
TreeServer.prototype.getQualifiedResponse = function(element) {
360360
let res = new ember.Root();
361-
let dup = element.toQualified();
361+
let dup;
362+
if (element.isRoot() === false) {
363+
dup = element.toQualified();
364+
}
362365
let children = element.getChildren();
363366
if (children != null) {
364367
for (let i = 0; i < children.length; i++) {
365-
dup.addChild(children[i].getDuplicate());
368+
res.addChild(children[i].toQualified().getMinimalContent());
366369
}
367370
}
368-
res.elements = [dup];
371+
else {
372+
res.addChild(dup);
373+
}
369374
return res;
370375
}
371376

372377
TreeServer.prototype.handleGetDirectory = function(client, element) {
373-
374378
if (client !== undefined) {
375379
if ((element.isMatrix() || element.isParameter()) &&
376380
(!element.isStream())) {
377381
// ember spec: parameter without streamIdentifier should
378382
// report their value changes automatically.
379383
this.subscribe(client, element);
380384
}
381-
let res;
382-
if (client.request.path == null) {
383-
res = this.getResponse(element);
384-
}
385-
else {
386-
res = this.getQualifiedResponse(element);
385+
let res = this.getQualifiedResponse(element);
386+
if (this._debug) {
387+
console.log("getDirectory response", res);
387388
}
388389
client.sendBERNode(res);
389390
}

test/Server.test.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
const expect = require("expect");
2+
const TreeServer = require("../server");
3+
const DeviceTree = require("../").DeviceTree;
4+
5+
6+
const LOCALHOST = "127.0.0.1";
7+
const PORT = 9008;
8+
9+
10+
const init = function(_src,_tgt) {
11+
const targets = _tgt === undefined ? [ "tgt1", "tgt2", "tgt3" ] : _tgt;
12+
const sources = _src === undefined ? [ "src1", "src2", "src3" ] : _src;
13+
const labels = function(endpoints) {
14+
let labels = [];
15+
for (let i = 0; i < endpoints.length; i++) {
16+
let endpoint = endpoints[i];
17+
let l = { identifier: `Label-${i}` };
18+
if (endpoint) {
19+
l.value = endpoint;
20+
}
21+
labels.push(l);
22+
}
23+
return labels;
24+
};
25+
26+
const buildConnections = function(s, t) {
27+
let connections = [];
28+
for (let i = 0; i < t.length; i++) {
29+
connections.push({target: `${i}`});
30+
}
31+
return connections;
32+
};
33+
34+
return [
35+
{
36+
// path "0"
37+
identifier: "scoreMaster",
38+
children: [
39+
{
40+
// path "0.0"
41+
identifier: "identity",
42+
children: [
43+
{identifier: "product", value: "S-CORE Master"},
44+
{identifier: "company", value: "EVS"},
45+
{identifier: "version", value: "1.2.0"},
46+
{identifier: "author", value: "[email protected]"}
47+
]
48+
},
49+
{
50+
// path "0.1"
51+
identifier: "router",
52+
children: [
53+
{
54+
// path 0.1.0
55+
identifier: "matrix",
56+
type: "oneToN",
57+
mode: "linear",
58+
targetCount: targets.length,
59+
sourceCount: sources.length,
60+
connections: buildConnections(sources, targets),
61+
labels: ["0.1.1000"]
62+
},
63+
{
64+
identifier: "labels",
65+
// path "0.1.1000"
66+
number: 1000,
67+
children: [
68+
{
69+
identifier: "targets",
70+
// Must be 1
71+
number: 1,
72+
children: labels(targets)
73+
},
74+
{
75+
identifier: "sources",
76+
// Must be 2
77+
number: 2,
78+
children: labels(sources)
79+
}
80+
]
81+
}
82+
]
83+
}
84+
]
85+
}
86+
];
87+
88+
}
89+
describe("server", function() {
90+
91+
describe("JSONtoTree", function() {
92+
let jsonTree;
93+
before(function() {
94+
jsonTree = init();
95+
});
96+
it("should generate an ember tree from json", function() {
97+
const root = TreeServer.JSONtoTree(jsonTree);
98+
expect(root).toBeDefined();
99+
expect(root.elements).toBeDefined();
100+
expect(root.elements.length).toBe(1);
101+
console.log("root", root.elements[0].contents);
102+
expect(root.elements[0].contents.identifier).toBe("scoreMaster");
103+
expect(root.elements[0].children.length).toBe(2);
104+
});
105+
});
106+
107+
describe("Server - Client communication", function() {
108+
let server,client;
109+
before(function() {
110+
jsonTree = init();
111+
const root = TreeServer.JSONtoTree(jsonTree);
112+
server = new TreeServer(LOCALHOST, PORT, root);
113+
//server._debug = true;
114+
return server.listen().then(() => {
115+
console.log("server listening");
116+
});
117+
});
118+
after(function() {
119+
client.disconnect();
120+
server.close();
121+
})
122+
it("should receive and decode the full tree", function () {
123+
client = new DeviceTree(LOCALHOST, PORT);
124+
//client._debug = true;
125+
return Promise.resolve()
126+
.then(() => client.connect())
127+
.then(() => {
128+
console.log("client connected");
129+
return client.getDirectory()
130+
})
131+
.then(() => {
132+
expect(client.root).toBeDefined();
133+
expect(client.root.elements).toBeDefined();
134+
expect(client.root.elements.length).toBe(1);
135+
expect(client.root.elements[0].contents.identifier).toBe("scoreMaster");
136+
return client.getDirectory(client.root.elements[0]);
137+
})
138+
.then(() => {
139+
expect(client.root.elements[0].children.length).toBe(2);
140+
return client.getDirectory(client.root.elements[0].children[0]);
141+
})
142+
.then(() => {
143+
expect(client.root.elements[0].children[0].children.length).toBe(4);
144+
expect(client.root.elements[0].children[0].children[3].contents.identifier).toBe("author");
145+
});
146+
});
147+
});
148+
});

0 commit comments

Comments
 (0)