node-dbus icon indicating copy to clipboard operation
node-dbus copied to clipboard

Bug with associated with registering more than one service + my fix

Open g00dnatur3 opened this issue 9 years ago • 1 comments

The bug is when I register two or more completely different services, the current code trys to send an incoming request to each of the registered services. this results in an error undefined to be thrown because obviously the self.objects[objectPath] is undefined if the incoming request does not have that objectPath.

We can easily know if the incoming request is meant for that service by checking if the service has the request's objectPath...

look at the code below for the fix...

Inside the dbus.js I use a hasObject function to ensure that the service does indeed have that objectPath.

the reason I put the check inside the _dbus.setObjectHandler is to quickly continue and not have wasted cycles of executing the code after the '//fire event' comment.

// Dispatch events to service
_dbus.setObjectHandler(function(uniqueName, sender, objectPath, interfaceName, member, message, args) {

    for (var hash in serviceMap) {
        var service = serviceMap[hash];

        if (service.bus.connection.uniqueName != uniqueName)
            continue;

        **if (!service.hasObject(objectPath))
            continue;**

        // Fire event
        var newArgs = [ 'request' ].concat(Array.prototype.slice.call(arguments));
        service.emit.apply(service, newArgs);

        break;
    }
});

Inside the service.js, I added a hasObject function.

var Service = module.exports = function(bus, serviceName) {
    var self = this;

    self.bus = bus;
    self.serviceName = serviceName;
    self.objects = {};

    self.on('request', function(uniqueName, sender, objectPath, interfaceName, member, message, args) {
        var iface = self.objects[objectPath]['interfaces'][interfaceName];
        if (!iface) {
            console.log('Service.on.request -> iface not found: ' + interfaceName);
            return;
        }
        iface.call.apply(iface, [ member, message, args ]);
    });
};

util.inherits(Service, events.EventEmitter);

Service.prototype.hasObject = function(objectPath) {
    var self = this;
    if (self.objects[objectPath]) return true;
    return false;
}

If you could please add this fix into your code that would be awesome.

Thank you.

g00dnatur3 avatar Aug 28 '16 11:08 g00dnatur3

I came across the same problem. :(

FredericGuilbault avatar Dec 20 '18 18:12 FredericGuilbault