Skip to content

Commit

Permalink
sidorares#9 : wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Mahé committed Jan 5, 2017
1 parent ad26940 commit 44e9ec7
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 165 deletions.
114 changes: 74 additions & 40 deletions lib/DBusObjectLibs.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class DBusObject {
let ifaces = keys.filter (utils.isValidIfaceName)
let objs = keys.filter (utils.isValidPathComponent)
let xml = xmlbuilder.create ('node', {headless: true}) // Create root element without the <?xml version="1.0"?>
.dtd ('-//freedesktop//DTD D-BUS Object Introspection 1.0//EN',
'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd')
.root() // don't forget to return to the root elem so that elems are not added to the DTD
.dtd ('-//freedesktop//DTD D-BUS Object Introspection 1.0//EN',
'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd')
.root() // don't forget to return to the root elem so that elems are not added to the DTD

// Have each interface generate its introspection data and add it to the root XML element
for (let iface of ifaces) {
Expand Down Expand Up @@ -115,56 +115,90 @@ class DBusObject {
}

/**
* Add a {@link DBusObject} as a child of this one.
* Used to add a child object to either a {@link DBusService} or a {@link DBusObject}.
* @param {DBusObject} object The child object to add
* @param {string} relativePath The relative path at which add the child object
*
* @throws {TypeError}
*/
addObject (object = mandatory(), relativePath = mandatory()) {
addObject (this, object, relativePath)
}
}
let pathComponents = relativePath.split ('/')

/**
* Used to add a child object to either a {@link DBusService} or a {@link DBusObject}.
* @param {DBusService|DBusObject} parent The object to which add the child
* @param {DBusObject} object The child object to add
* @param {string} relativePath The relative path at which add the child object
*
* @throws {TypeError}
*/
function addObject (parent = mandatory(), object = mandatory(), relativePath = mandatory()) {
let pathComponents = relativePath.split ('/')
// Check that 'object' is a DBusObject
if (! (object instanceof DBusObject)) {
throw new TypeError (`'object' is not a DBusObject.`)
}

// Check that all paths components are valid
if (!pathComponents.every (utils.isValidPathComponent)) {
throw new TypeError (`'${relativePath}' contains non-valid path components.`)
}

/*
* Everything looks good, traverse the object according to the path components, and add the obj as child
*/
let currObj = this
// traverse the object
while (pathComponents.length > 1) {
let currPathComponent = pathComponents.shift()

// If the current object doesn't already have an object at this path component, create one
if (typeof currObj[currPathComponent] === 'undefined') {
currObj[currPathComponent] = new DBusObject()
this._linkParentObject(currObj, currPathComponent)
}

// Check that 'object' is a DBusObject
if (! (object instanceof DBusObject)) {
throw new TypeError (`'object' is not a DBusObject.`)
// traverse the object
currObj = currObj[currPathComponent]
}

// Now we have traversed our object and reached the object path to host the child, so add it
if (currObj[pathComponents[0]]) {
throw new Error(`path ${relativePath} already exists`)
}
currObj[pathComponents[0]] = object

this._linkParentObject(currObj, pathComponents[0])

if (this.getService()) {
this.getService().bus.exposeObject(this.__service, this, this.getPath())
}
}

// Check that all paths components are valid
if (!pathComponents.every (utils.isValidPathComponent)) {
throw new TypeError (`'${relativePath}' contains non-valid path components.`)
_linkParentObject (parent, pathComponent) {
parent[pathComponent].__parentObject = parent
parent[pathComponent].__relativePath = pathComponent
}

/*
Everything looks good, traverse the object according to the path components, and add the obj as child
*/
let currObj = parent
// traverse the object
while (pathComponents.length > 1) {
let currPathComponent = pathComponents.shift()

// If the current object doesn't already have an object at this path component, create one
if (typeof currObj[currPathComponent] === 'undefined') {
currObj[currPathComponent] = new DBusObject()
}
getParentObject () {
return this.__parentObject
}

// traverse the object
currObj = currObj[currPathComponent]
getRelativePath () {
return this.__relativePath
}

// Now we have traversed our object and reached the object path to host the child, so add it
currObj[pathComponents.shift()] = object
getService () {
return this.__service
}

getPath () {
let path = []
let currObj = this
let component

while (currObj && (component = currObj.getRelativePath())) {
path.unshift(component)
currObj = currObj.getParentObject()
}

path = path.join('/')
if (this.getService()) {
path = '/'+path
}
}
}

module.exports = {
DBusObject,
addObject,
}
4 changes: 3 additions & 1 deletion lib/DBusService.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function DBusService (obj, relativePath) {
}

DBusService.prototype.addObject = function (object = mandatory(), relativePath = mandatory()) {
DBusObjectLibs.addObject (this['/'], object, relativePath)
this['/'].addObject (object, relativePath)
}


module.exports = DBusService
Loading

0 comments on commit 44e9ec7

Please sign in to comment.