Skip to content

Commit

Permalink
wrap service handler execution with co
Browse files Browse the repository at this point in the history
  • Loading branch information
sidorares committed May 13, 2015
1 parent 8a2eb08 commit 290dbcb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
90 changes: 47 additions & 43 deletions lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,63 +124,67 @@ module.exports = function bus(conn, opts) {
// exported interfaces handlers
var obj, iface, impl;
if (obj = self.exportedObjects[msg.path]) {
var co = require('co');

if (iface = obj[msg['interface']]) {
// now we are ready to serve msg.member
impl = iface[1];
var func = impl[msg.member];
if (!func) {
// TODO: respond with standard dbus error
console.error('Method ' + msg.member + ' is not implemented ');
throw new Error('Method ' + msg.member + ' is not implemented ');
};
try {
result = func.apply(impl, msg.body);
} catch (e) {
console.error("Caught exception while trying to execute handler: ", e);
throw e;
return self.sendError(msg, 'org.freedesktop.DBus.Error.UnknownMethod', msg.member);
}
// TODO safety check here
var resultSignature = iface[0].methods[msg.member][1];
var reply = {
type: constants.messageType.methodReturn,
destination: msg.sender,
replySerial: msg.serial
};
if (result) {
reply.signature = resultSignature;
reply.body = [result];
}
self.connection.message(reply);
return;
co(function() {
return func.apply(impl, msg.body);
}).then(function(result) {
// TODO safety check here
var resultSignature = iface[0].methods[msg.member][1];

var reply = {
type: constants.messageType.methodReturn,
destination: msg.sender,
replySerial: msg.serial
};
if (result) {
reply.signature = resultSignature;
reply.body = [result];
}
self.connection.message(reply);
}, function(error) {
return self.sendError(msg, 'org.freedesktop.DBus.Error', error.message);
});
} else {
console.error('Interface ' + msg['interface'] + ' is not supported');
// TODO: respond with standard dbus error
return self.sendError(msg, 'org.freedesktop.DBus.Error.UnknownInterface', msg['interface']);
}
}
// setMethodCall handlers
handler = self.methodCallHandlers[self.mangle(msg)];
if (handler) {
var result;
try {
result = handler[0].apply(null, msg.body);
} catch (e) {
console.error("Caught exception while trying to execute handler: ", e);
self.sendError(e.message, e.description);
return;
}
var reply = {
type: constants.messageType.methodReturn,
destination: msg.sender,
replySerial: msg.serial
//, sender: self.name
};
if (result) {
reply.signature = handler[1];
reply.body = result;
}
self.connection.message(reply);
var result;
try {
result = handler[0].apply(null, msg.body);

} catch (e) {
console.error("Caught exception while trying to execute handler: ", e);
self.sendError(e.message, e.description);
return;
}
var reply = {
type: constants.messageType.methodReturn,
destination: msg.sender,
replySerial: msg.serial
//, sender: self.name
};
if (result) {
reply.signature = handler[1];
reply.body = result;
}
self.connection.message(reply);


} else {
// TODO: also implement
// org.freedesktop.DBus.Error.UnknownObject
// org.freedesktop.DBus.Error.UnknownInterface
self.sendError(msg, 'org.freedesktop.DBus.Error.UnknownService', 'Uh oh oh');
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dbus2js": "./bin/dbus2js.js"
},
"dependencies": {
"co": "^4.5.4",
"event-stream": "^3.1.7",
"put": "0.0.6",
"xml2js": "0.1.14"
Expand Down

0 comments on commit 290dbcb

Please sign in to comment.