Skip to content

Commit

Permalink
proxy-object: init from xml string
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Crisci committed Aug 5, 2019
1 parent e84b91b commit 4518825
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
5 changes: 3 additions & 2 deletions lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ class MessageBus extends EventEmitter {
*
* @param name {string} - the well-known name on the bus.
* @param path {string} - the object path exported on the name.
* @param [xml] {string} - xml introspection data.
* @returns {Promise} - a Promise that resolves with the `ProxyObject`.
*/
getProxyObject(name, path) {
getProxyObject(name, path, xml) {
let obj = new ProxyObject(this, name, path);
return obj._init();
return obj._init(xml);
};

/**
Expand Down
54 changes: 32 additions & 22 deletions lib/client/proxy-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,31 +113,41 @@ class ProxyObject {
}
}

_init() {
_init(xml) {
return new Promise((resolve, reject) => {
let introspectMessage = new Message({
destination: this.name,
path: this.path,
interface: 'org.freedesktop.DBus.Introspectable',
member: 'Introspect',
signature: '',
body: []
});
if (xml) {
this._parser.parseString(xml, (err, data) => {
if (err) {
return reject(err);
}
this._initXml(data);
resolve(this);
});
} else {
let introspectMessage = new Message({
destination: this.name,
path: this.path,
interface: 'org.freedesktop.DBus.Introspectable',
member: 'Introspect',
signature: '',
body: []
});

this.bus.call(introspectMessage)
.then((msg) => {
let xml = msg.body[0];
this._parser.parseString(xml, (err, data) => {
if (err) {
return reject(err);
}
this._initXml(data);
resolve(this);
this.bus.call(introspectMessage)
.then((msg) => {
let xml = msg.body[0];
this._parser.parseString(xml, (err, data) => {
if (err) {
return reject(err);
}
this._initXml(data);
resolve(this);
});
})
.catch((err) => {
return reject(err);
});
})
.catch((err) => {
return reject(err);
});
}
});
}

Expand Down
42 changes: 42 additions & 0 deletions test/integration/client-standard-dbus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,45 @@ test('get stats', async () => {
expect(busNames.signature).toBe('u');
expect(busNames.value).toBeGreaterThan(0);
});

test('provided xml', async () => {
let xml = `
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/com/example/sample_object0">
<interface name="com.example.SampleInterface0">
<method name="Frobate">
<arg name="foo" type="i" direction="in"/>
<arg name="bar" type="s" direction="out"/>
<arg name="baz" type="a{us}" direction="out"/>
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
</method>
<method name="Bazify">
<arg name="bar" type="(iiu)" direction="in"/>
<arg name="bar" type="v" direction="out"/>
</method>
<method name="Mogrify">
<arg name="bar" type="(iiav)" direction="in"/>
</method>
<signal name="Changed">
<arg name="new_value" type="b"/>
</signal>
<signal name="ChangedMulti">
<arg name="new_value1" type="b"/>
<arg name="new_value2" type="y"/>
</signal>
<property name="Bar" type="y" access="write"/>
</interface>
<node name="child_of_sample_object"/>
<node name="another_child_of_sample_object"/>
</node>
`;
let object = await bus.getProxyObject('com.example.Sample', '/com/example/sample_object0', xml);

let iface = object.getInterface('com.example.SampleInterface0');
expect(object.nodes.length).toEqual(2);
expect(iface.Frobate).toBeDefined();
expect(iface.Bazify).toBeDefined();
expect(iface.Mogrify).toBeDefined();
expect(iface.$signals.find((s) => s.name == 'Changed')).toBeDefined();
});

0 comments on commit 4518825

Please sign in to comment.