Skip to content

Commit

Permalink
Add DBusClient node
Browse files Browse the repository at this point in the history
  • Loading branch information
arlo-phoenix committed Nov 18, 2023
1 parent 2126d2c commit 4ffce73
Show file tree
Hide file tree
Showing 15 changed files with 422 additions and 130 deletions.
10 changes: 5 additions & 5 deletions project/demo/dbus_client_example.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ var num2=0
func test_request():
var client=DBusClient.new()

client.destination="net.poettering.Calculator"
client.path="/net/poettering/Calculator"
client.interface="net.poettering.Calculator"

var destination="net.poettering.Calculator"
var path="/net/poettering/Calculator"
var interface="net.poettering.Calculator"
var member="Multiply"

client.open()

var request_message=client.create_request(member)
var request_message=client.create_request(destination, path, interface, member)
request_message.append(num1)
request_message.append(num2)
var response_message=client.send_request(request_message)
Expand Down
10 changes: 10 additions & 0 deletions project/demo/dbus_client_node_example.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends DBusClientNode


func _on_request_finished(response: DBusMessage):
assert(response)
print(response.read_single(TYPE_FLOAT))

func _on_timer_timeout():
open()
var err=request("Divide", _on_request_finished, 50, 10)
12 changes: 12 additions & 0 deletions project/demo/dbus_client_util.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extends DBusClientNode

func _ready():
open()
#godots XMLParser is horrible so if you want to display this use some C++ library or plugin
print(introspect())

# if you have a parser would need to call this down the tree
# to replicate busctl tree <destination>
print(introspect_path("/org"))

close()
10 changes: 10 additions & 0 deletions project/demo/dbus_client_util.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=3 uid="uid://c670mp7v4rfln"]

[ext_resource type="Script" path="res://demo/dbus_client_util.gd" id="1_lk0kj"]

[node name="DBusClientUtil" type="DBusClientNode"]
destination = "org.freedesktop.portal.Desktop"
path = "/org/freedesktop/portal/desktop"
interface = "org.freedesktop.portal.Desktop"
autostart = false
script = ExtResource("1_lk0kj")
20 changes: 10 additions & 10 deletions project/demo/dbus_server_example.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ extends DBusServerNode

@onready var client=$"../DBusClient"

func _multiply(message: DBusResponse) -> int:
var message_signature=message.read()
func _multiply(request: DBusRequest) -> int:
var message_signature=request.read()
var response=Array()
response.append(message_signature[0] * message_signature[1])
message.reply(response)
request.reply(response)
return 0

func _empty(message: DBusResponse) -> int:
func _empty(request: DBusRequest) -> int:
var response=Array()
print("do shit")
print("do sth")
response.append(true)
message.reply(response)
request.reply(response)
return 0

func _divide(message: DBusResponse) -> int:
var message_signature=message.read()
func _divide(request: DBusRequest) -> int:
var message_signature=request.read()
var num1:float=message_signature[0]
var num2:float=message_signature[1]
if num2==0:
message.set_error("net.poettering.DivisionByZero", "Division by zero not allowed")
request.set_error("net.poettering.DivisionByZero", "Division by zero not allowed")
return -ERR_INVALID_PARAMETER
var response=Array()
response.append(num1/num2)
message.reply(response)
request.reply(response)
return 0

func _ready() -> void:
Expand Down
15 changes: 13 additions & 2 deletions project/demo/server_client_example.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[gd_scene load_steps=13 format=3 uid="uid://c7u64tpv6byvb"]
[gd_scene load_steps=14 format=3 uid="uid://c7u64tpv6byvb"]

[ext_resource type="Script" path="res://demo/dbus_server_example.gd" id="1_jyovi"]
[ext_resource type="Script" path="res://demo/dbus_client_example.gd" id="2_ik254"]
[ext_resource type="Script" path="res://demo/dbus_client_node_example.gd" id="3_pfkpg"]

[sub_resource type="DBusMethodArgument" id="DBusMethodArgument_7xm51"]

Expand Down Expand Up @@ -39,13 +40,23 @@ result = Array[DBusMethodArgument]([SubResource("DBusMethodArgument_rishs")])
object_path = "/net/poettering/Calculator"
interface_name = "net.poettering.Calculator"
methods = [SubResource("DBusMethod_pftgm"), SubResource("DBusMethod_1nn3q"), SubResource("DBusMethod_02o38")]
autostart = true
script = ExtResource("1_jyovi")

[node name="DBusClient" type="Node" parent="."]
script = ExtResource("2_ik254")

[node name="Timer" type="Timer" parent="."]
wait_time = 2.0
one_shot = true
autostart = true

[node name="DBusClientNode" type="DBusClientNode" parent="."]
destination = "net.poettering.Calculator"
path = "/net/poettering/Calculator"
interface = "net.poettering.Calculator"
autostart = false
use_threads = true
script = ExtResource("3_pfkpg")

[connection signal="timeout" from="Timer" to="DBusClient" method="_on_timer_timeout"]
[connection signal="timeout" from="Timer" to="DBusClientNode" method="_on_timer_timeout"]
65 changes: 18 additions & 47 deletions src/dbus_client.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "dbus_client.h"

/*
Code mostly just copied and packed from
https://www.freedesktop.org/software/systemd/man/latest/sd_bus_call_method.html#
Expand All @@ -7,30 +7,21 @@ licensed under:
SPDX-License-Identifier: MIT-0
*/
#include "dbus_client.h"
#include "dbus_privilege.h"

#define ERR_BUS_FAIL(msg) \
ERR_FAIL_COND_MSG(r < 0, String(msg) + ": " + strerror(-r))

#define ERR_BUS_FAIL_V(msg, ret) \
ERR_FAIL_COND_V_MSG(r < 0, ret, String(msg) + ": " + strerror(-r))

void DBusClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_destination", "p_destination"), &DBusClient::set_destination);
ClassDB::bind_method(D_METHOD("get_destination"), &DBusClient::get_destination);

ClassDB::bind_method(D_METHOD("set_path", "p_path"), &DBusClient::set_path);
ClassDB::bind_method(D_METHOD("get_path"), &DBusClient::get_path);

ClassDB::bind_method(D_METHOD("set_interface", "p_interface"), &DBusClient::set_interface);
ClassDB::bind_method(D_METHOD("get_interface"), &DBusClient::get_interface);

ClassDB::bind_method(D_METHOD("create_request", "member"), &DBusClient::create_request);
ClassDB::bind_method(D_METHOD("create_request", "destination", "path", "interface", "member"), &DBusClient::create_request);
ClassDB::bind_method(D_METHOD("send_request", "request"), &DBusClient::send_request);
ClassDB::bind_method(D_METHOD("open"), &DBusClient::open);
ClassDB::bind_method(D_METHOD("close"), &DBusClient::close);

ADD_PROPERTY(PropertyInfo(Variant::STRING, "destination"), "set_destination", "get_destination");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "path"), "set_path", "get_path");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "interface"), "set_interface", "get_interface");
ClassDB::bind_method(D_METHOD("is_open"), &DBusClient::is_open);
}

DBusClient::DBusClient() {
Expand All @@ -42,32 +33,8 @@ DBusClient::~DBusClient() {
}
}

void DBusClient::set_destination(const String &p_destination) {
_destination = p_destination;
}

String DBusClient::get_destination() const {
return _destination;
}

void DBusClient::set_path(const String &p_path) {
_path = p_path;
}

String DBusClient::get_path() const {
return _path;
}

void DBusClient::set_interface(const String &p_interface) {
_interface = p_interface;
}

String DBusClient::get_interface() const {
return _interface;
}

void DBusClient::open() {
int r = sd_bus_open_user(&_bus);
int r = D_BUS_OPEN(&_bus);
ERR_BUS_FAIL("Failed to acquire bus");
_open = true;
}
Expand All @@ -78,16 +45,20 @@ void DBusClient::close() {
_open = false;
}

Ref<DBusMessage> DBusClient::create_request(const String &p_member) {
bool DBusClient::is_open() const {
return _open;
}

Ref<DBusMessage> DBusClient::create_request(const String &p_destination, const String &p_path, const String &p_interface, const String &p_member) {
ERR_FAIL_COND_V(!_open, nullptr);
sd_bus_message *msg = nullptr;
int r = sd_bus_message_new_method_call(_bus, &msg,
_destination.utf8(),
_path.utf8(),
_interface.utf8(),
sd_bus_message *reply = nullptr;
int r = sd_bus_message_new_method_call(_bus, &reply,
p_destination.utf8(),
p_path.utf8(),
p_interface.utf8(),
p_member.utf8());
ERR_BUS_FAIL_V(String("Failed to created request: ") + _error.message, nullptr);
return DBusMessage::from_internal(msg, true);
return DBusMessage::from_internal(reply, true);
}

Ref<DBusMessage> DBusClient::send_request(const Ref<DBusMessage> &p_request) {
Expand Down
16 changes: 2 additions & 14 deletions src/dbus_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ class DBusClient : public RefCounted {
sd_bus *_bus = nullptr;
sd_bus_error _error = SD_BUS_ERROR_NULL;

String _destination;
String _path;
String _interface;

bool _open = false;

protected:
Expand All @@ -26,18 +22,10 @@ class DBusClient : public RefCounted {
DBusClient();
~DBusClient();

void set_destination(const String &p_destination);
String get_destination() const;

void set_path(const String &p_path);
String get_path() const;

void set_interface(const String &p_interface);
String get_interface() const;

void open();
void close();
bool is_open() const;

Ref<DBusMessage> create_request(const String &p_member);
Ref<DBusMessage> create_request(const String &p_destination, const String &p_path, const String &p_interface, const String &p_member);
Ref<DBusMessage> send_request(const Ref<DBusMessage> &p_request);
};
Loading

0 comments on commit 4ffce73

Please sign in to comment.