D-Bus、UDisk 和 Glibmm 绑定
我对简单的 D-Bus 概念有疑问。我使用 Glibmm D-Bus 绑定(Gio::DBus 命名空间)来访问 UDisks 接口。我想读取系统上找到的每个硬盘的一些属性,因此首先我需要枚举 UDisks 报告的所有磁盘,如下所示:
Glib::RefPtr<Gio::DBus::Connection> bus;
int main() {
using namespace Glib;
using namespace Gio;
Glib::init();
Gio::init();
bus = DBus::Connection::get_sync(Gio::DBus::BUS_TYPE_SYSTEM);
RefPtr<DBus::Proxy> udisks_proxy = DBus::Proxy::create_sync(bus, "org.freedesktop.UDisks", "/org/freedesktop/UDisks", "org.freedesktop.UDisks");
VariantContainerBase devices_variant = udisks_proxy->call_sync("EnumerateDevices");
VariantIter iterator(devices_variant.get_child(0));
Variant<ustring> var;
while(iterator.next_value(var)) {
ustring name = var.get();
LOG("device: '%s", name.c_str());
process_device(name);
}
return 0;
}
这似乎工作正常,因为 call_sync()
返回一个 VariantContainerBase
,它保存 (ao)
对象,该对象基本上是:“对象路径数组的一个结构”。从文档中我读到“对象路径”类型的处理方式与“字符串”类型相同,这就是为什么在 get_child(0)
期间创建非类型化 VariantBase
的原因code> 允许将自身转换为 Variant
对象。使用此参数化变体,使用 var.get()
提取字符串非常简单。
但随后我尝试使用此方法从每个驱动器的属性中读取一些内容(在本例中为 NativePath
属性):
void process_device(const Glib::ustring& objpath) {
using namespace Glib;
using namespace Gio;
RefPtr<DBus::Proxy> attrs = DBus::Proxy::create_sync(bus, "org.freedesktop.UDisks", objpath, "org.freedesktop.DBus.Properties");
std::vector<VariantBase> args;
args.push_back(Variant<ustring>::create(objpath));
args.push_back(Variant<ustring>::create("NativePath"));
VariantContainerBase data = attrs->call_sync("Get", VariantContainerBase::create_tuple(args));
LOG("return type: %s", data.get_type_string().c_str());
}
问题是 VariantContainerBase
对象包含 (v)
签名。这意味着该对象是“变体”,因此我无法将其转换为任何对象。
对属性的内省表明 NativePath
包含一个 string 值。那么为什么 call_sync()
方法返回一个变体类型的对象呢?我错过了什么吗?谁能告诉我,如何正确读取 NativePath
属性,而不使用 get_data()
方法,也不将数据 memcpy
'ing 到我的自己的缓冲区?我想尽可能做到类型安全。
还有一件事。当我使用 data.print(true)
方法时,我以编码形式获取 NativePath
属性的正确内容。这意味着引擎知道这是一个字符串。那么为什么它将其报告为变体呢?这是一个错误还是一个功能? :P
抱歉我的英语,我的困惑,感谢您的帮助。
I'm having a problem with a simple D-Bus concept. I'm using Glibmm D-Bus bindings (Gio::DBus namespace) to access the UDisks interface. I'd like to read some attributes of every hard disk found on the system, so first I need to enumerate all disks that UDisks is reporting, like this:
Glib::RefPtr<Gio::DBus::Connection> bus;
int main() {
using namespace Glib;
using namespace Gio;
Glib::init();
Gio::init();
bus = DBus::Connection::get_sync(Gio::DBus::BUS_TYPE_SYSTEM);
RefPtr<DBus::Proxy> udisks_proxy = DBus::Proxy::create_sync(bus, "org.freedesktop.UDisks", "/org/freedesktop/UDisks", "org.freedesktop.UDisks");
VariantContainerBase devices_variant = udisks_proxy->call_sync("EnumerateDevices");
VariantIter iterator(devices_variant.get_child(0));
Variant<ustring> var;
while(iterator.next_value(var)) {
ustring name = var.get();
LOG("device: '%s", name.c_str());
process_device(name);
}
return 0;
}
This seems to work OK, because call_sync()
returns a VariantContainerBase
, which holds the (ao)
object, which basically is: "one struct of an array of object paths". From the docs I read that the "object path" type is handled in the same way as a "string" type, that's why the untyped VariantBase
that is created during get_child(0)
allows itself to be casted into Variant<ustring>
object. Using this parametrized Variant, it's trivial to extract the string by using var.get()
.
But then I'm trying to read some stuff (in this case, the NativePath
attribute) from the attributes of each drive using this method:
void process_device(const Glib::ustring& objpath) {
using namespace Glib;
using namespace Gio;
RefPtr<DBus::Proxy> attrs = DBus::Proxy::create_sync(bus, "org.freedesktop.UDisks", objpath, "org.freedesktop.DBus.Properties");
std::vector<VariantBase> args;
args.push_back(Variant<ustring>::create(objpath));
args.push_back(Variant<ustring>::create("NativePath"));
VariantContainerBase data = attrs->call_sync("Get", VariantContainerBase::create_tuple(args));
LOG("return type: %s", data.get_type_string().c_str());
}
The problem is that the VariantContainerBase
object contains the (v)
signature. This means that the object is "variant", so I can't cast it to anything.
Introspection of the attributes show that NativePath
holds a string value. So why the call_sync()
method is returning an object of a variant type? Am I missing something? Could anyone tell me, how can I properly read the NativePath
attribute, without using get_data()
method and without memcpy
'ing the data into my own buffer? I'd like to do it as type-safe as possible.
One more thing. When I use the data.print(true)
method, I get the proper contents of NativePath
attribute in an encoded form. This means that the engine knows that this is a string. So why it reports it as a variant? Is it a bug or a feature? :P
Sorry for my english, my confusion, and thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我有解决方案:这似乎是一个错误,或者至少这是一个我不理解的逻辑。
正确的方法是调用直接 Glib 函数
g_variant_get_child()
,而不是调用VariantContainerBase
的get_child(0)
,如下所示:它将正确地将
output
的变体类型设置为v
内部的类型(在我的例子中,它将把output
的类型设置为>s
)。之后,当您获取s
类型的远程数据时,您可以将其转换为Variant
,如下所示:它似乎有效。
OK, I have the solution: this seems to be a bug, or at least it's a logic that I don't understand.
Instead of calling
VariantContainerBase
'sget_child(0)
, a proper way is to call a direct Glib functiong_variant_get_child()
, like f.e. this:It will properly set the
output
's variant type to one that's insidev
(in my case it will set the type ofoutput
tos
). After this, when you get the remote data in thes
type, you can cast it to aVariant<ustring>
like this:It seems to work.