Skip to content

Commit a368484

Browse files
authored
Merge pull request #157 from bryanburgers/disconnect
Add bus.disconnect and service.disconnect
2 parents 38c36b0 + 284aa10 commit a368484

21 files changed

+241
-24
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ bus.getInterface('com.example.Library', '/com/example/Library/authors/DAdams', '
136136
});
137137
```
138138

139+
#### `Bus.prototype.disconnect()`
140+
141+
Disconnect from DBus. This disconnection makes it so that Node isn't kept
142+
running based on this active connection. It also makes this bus, and all of its
143+
children (interfaces that have been retrieved, etc.) unusable.
144+
139145

140146
### Interface
141147

@@ -231,6 +237,13 @@ Remove (or unexpose) an object that has been created.
231237
service.removeObject(object);
232238
```
233239

240+
#### `Service.prototype.disconnect()`
241+
242+
Disconnect from DBus. This disconnection makes it so that Node isn't kept
243+
running based on this active connection. It also disconnects all of the objects
244+
created by this service.
245+
246+
234247
### ServiceObject
235248

236249
An object that is exposed over DBus.

lib/bus.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ var Bus = module.exports = function(dbus, busName) {
5151

5252
util.inherits(Bus, events.EventEmitter);
5353

54+
Object.defineProperty(Bus.prototype, 'connected', {
55+
get: function() {
56+
return this.connection !== null;
57+
}
58+
});
59+
60+
Bus.prototype.disconnect = function(callback) {
61+
var self = this;
62+
63+
delete self.dbus.signalHandlers[self.connection.uniqueName];
64+
65+
self.dbus._dbus.releaseBus(self.connection);
66+
67+
self.connection = null;
68+
69+
if (callback)
70+
process.nextTick(callback);
71+
};
72+
5473
Bus.prototype.reconnect = function(callback) {
5574
var self = this;
5675

@@ -84,6 +103,13 @@ Bus.prototype.reconnect = function(callback) {
84103
Bus.prototype.introspect = function(serviceName, objectPath, callback) {
85104
var self = this;
86105

106+
if (!self.connected) {
107+
process.nextTick(function() {
108+
callback(new Error('Bus is no longer connected'));
109+
});
110+
return;
111+
}
112+
87113
// Getting scheme of specific object
88114
self.dbus.callMethod(self.connection,
89115
serviceName,

lib/dbus.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var Utils = require('./utils');
66
var Bus = require('./bus');
77
var Service = require('./service');
88

9-
var busMap = {};
109
var serviceMap = {};
1110

1211
var enabledSignal = false;
@@ -41,14 +40,7 @@ DBus.Signature = Utils.Signature;
4140
DBus.prototype.getBus = function(busName) {
4241
var self = this;
4342

44-
// Return bus existed
45-
if (busMap[busName])
46-
return busMap[busName];
47-
48-
var bus = new Bus(self, busName);
49-
busMap[busName] = bus;
50-
51-
return bus;
43+
return new Bus(self, busName);
5244
};
5345

5446
DBus.prototype.parseIntrospectSource = function(source) {

lib/interface.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ var Interface = module.exports = function(bus, serviceName, objectPath, interfac
1515

1616
util.inherits(Interface, events.EventEmitter);
1717

18+
Object.defineProperty(Interface.prototype, 'connected', {
19+
get: function() {
20+
return this.bus.connected;
21+
}
22+
});
23+
1824
Interface.prototype.init = function(callback) {
1925
var self = this;
2026

@@ -48,6 +54,10 @@ Interface.prototype.init = function(callback) {
4854
var error = this[method].error || null
4955

5056
process.nextTick(function() {
57+
if (!self.connected) {
58+
callback(new Error('Bus is no longer connected'));
59+
return;
60+
}
5161

5262
try {
5363
self.bus.dbus.callMethod(self.bus.connection,
@@ -90,6 +100,13 @@ Interface.prototype.init = function(callback) {
90100
Interface.prototype.setProperty = function(propertyName, value, callback) {
91101
var self = this;
92102

103+
if (!self.connected) {
104+
process.nextTick(function() {
105+
callback(new Error('Bus is no longer connected'));
106+
});
107+
return;
108+
}
109+
93110
self.bus.dbus.callMethod(self.bus.connection,
94111
self.serviceName,
95112
self.objectPath,
@@ -108,6 +125,13 @@ Interface.prototype.setProperty = function(propertyName, value, callback) {
108125
Interface.prototype.getProperty = function(propertyName, callback) {
109126
var self = this;
110127

128+
if (!self.connected) {
129+
process.nextTick(function() {
130+
callback(new Error('Bus is no longer connected'));
131+
});
132+
return;
133+
}
134+
111135
self.bus.dbus.callMethod(self.bus.connection,
112136
self.serviceName,
113137
self.objectPath,
@@ -126,6 +150,13 @@ Interface.prototype.getProperty = function(propertyName, callback) {
126150
Interface.prototype.getProperties = function(callback) {
127151
var self = this;
128152

153+
if (!self.connected) {
154+
process.nextTick(function() {
155+
callback(new Error('Bus is no longer connected'));
156+
});
157+
return;
158+
}
159+
129160
self.bus.dbus.callMethod(self.bus.connection,
130161
self.serviceName,
131162
self.objectPath,

lib/service.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ var Service = module.exports = function(bus, serviceName) {
2222

2323
util.inherits(Service, events.EventEmitter);
2424

25+
Object.defineProperty(Service.prototype, 'connected', {
26+
get: function() {
27+
return this.bus.connected;
28+
}
29+
});
30+
2531
Service.prototype.createObject = function(objectPath) {
2632
var self = this;
2733

@@ -39,3 +45,9 @@ Service.prototype.removeObject = function(object) {
3945

4046
self.bus.dbus._dbus.unregisterObjectPath(self.bus.connection, object.path);
4147
};
48+
49+
Service.prototype.disconnect = function() {
50+
var self = this;
51+
52+
self.bus.disconnect();
53+
};

lib/service_interface.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ ServiceInterface.prototype.getProperties = function(callback) {
166166
ServiceInterface.prototype.emitSignal = function() {
167167
var self = this;
168168

169+
var service = self.object.service;
170+
if (!service.connected) {
171+
throw new Error('Service is no longer connected');
172+
}
173+
169174
var conn = self.object.service.bus.connection;
170175
var objPath = self.object.path;
171176
var interfaceName = self.name;

src/connection.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,6 @@ namespace Connection {
274274

275275
uv_unref((uv_handle_t *)bus->loop);
276276

277-
if (dbus_connection_get_is_connected(connection))
278-
dbus_connection_close(connection);
279-
280277
dbus_connection_unref(connection);
281278
}
282279

test/client-call-add.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ withService('service.js', function(err, done) {
1818
iface.Add(109, 201, function(err, result) {
1919
tap.equal(result, 310);
2020
done();
21+
bus.disconnect();
2122
});
2223
});
2324
});

test/client-call-noargs.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ withService('service.js', function(err, done) {
1818
iface.NoArgs(function(err, result) {
1919
tap.equal(result, 'result!');
2020
done();
21+
bus.disconnect();
2122
});
2223
});
2324
});

test/client-call-timeout.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ withService('service.js', function(err, done) {
1414
tap.notSame(err, null);
1515
tap.same(result, null);
1616
done();
17+
bus.disconnect();
1718
});
1819
});
1920
});

0 commit comments

Comments
 (0)