Skip to content

Commit

Permalink
Add support for peer-to-peer DBus
Browse files Browse the repository at this point in the history
  • Loading branch information
oleavr committed Jul 9, 2021
1 parent 850786d commit df509e5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ module.exports.sessionBus = function(opts) {
return createClient(opts);
};

/**
* Create a new {@link MessageBus} client to communicate peer-to-peer.
*
* @param {object} [stream] - Duplex stream for communication.
* @param {object} [options] - Options for `MessageBus` creation.
*/
module.exports.peerBus = function(stream, opts) {
return createClient(Object.assign({}, opts, { stream }));
};

/**
* Use JSBI as a polyfill for long integer types ('x' and 't') in the client
* and the service. This is required for Node verisons that do not support the
Expand Down
44 changes: 29 additions & 15 deletions lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MessageBus extends EventEmitter {

/**
* The unique name of the bus connection. This will be `null` until the
* `MessageBus` is connected.
* `MessageBus` is connected, unless we're operating in peer-to-peer mode.
* @memberof MessageBus#
* @member {string} name
*/
Expand Down Expand Up @@ -159,22 +159,28 @@ class MessageBus extends EventEmitter {
this.emit('error', err);
});

const helloMessage = new Message({
path: '/org/freedesktop/DBus',
destination: 'org.freedesktop.DBus',
interface: 'org.freedesktop.DBus',
member: 'Hello'
});

this.call(helloMessage)
.then((msg) => {
this.name = msg.body[0];
if (conn.mode === 'p2p') {
process.nextTick(() => {
this.emit('connect');
})
.catch((err) => {
this.emit('error', err);
throw new Error(err);
});
} else {
const helloMessage = new Message({
path: '/org/freedesktop/DBus',
destination: 'org.freedesktop.DBus',
interface: 'org.freedesktop.DBus',
member: 'Hello'
});

this.call(helloMessage)
.then((msg) => {
this.name = msg.body[0];
this.emit('connect');
})
.catch((err) => {
this.emit('error', err);
throw new Error(err);
});
}
}

/**
Expand Down Expand Up @@ -468,6 +474,10 @@ class MessageBus extends EventEmitter {

this._matchRules[match] = 1;

if (this._connection.mode === 'p2p') {
return Promise.resolve();
}

// TODO catch error and update refcount
const msg = new Message({
path: '/org/freedesktop/DBus',
Expand Down Expand Up @@ -496,6 +506,10 @@ class MessageBus extends EventEmitter {

delete this._matchRules[match];

if (this._connection.mode === 'p2p') {
return Promise.resolve();
}

// TODO catch error and update refcount
const msg = new Message({
path: '/org/freedesktop/DBus',
Expand Down
11 changes: 9 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,15 @@ function createStream (opts) {
function createConnection (opts) {
const self = new EventEmitter();
opts = opts || {};
const stream = (self.stream = createStream(opts));
stream.setNoDelay && stream.setNoDelay();
let { stream } = opts;
if (stream) {
self.mode = 'p2p';
} else {
self.mode = 'bus';
stream = createStream(opts);
stream.setNoDelay && stream.setNoDelay();
}
self.stream = stream;

stream.on('error', function (err) {
// forward network and stream errors
Expand Down
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

declare module 'dbus-next' {
import { EventEmitter } from "events";
import { Duplex } from "stream";

export type ObjectPath = string;
export type PropertyAccess = "read" | "write" | "readwrite";
Expand Down Expand Up @@ -142,4 +143,5 @@ declare module 'dbus-next' {
export function setBigIntCompat(state: boolean): void;
export function systemBus(): MessageBus;
export function sessionBus(options?: BusOptions): MessageBus;
export function peerBus(stream: Duplex, options?: BusOptions): MessageBus;
}

0 comments on commit df509e5

Please sign in to comment.