13

使用基于 QDbusAbstractInterface 构建的 Qt DBus 代理(通过 qdbusxml2cpp),处理您想要接口的服务/对象在启动时不可用的最佳方法是什么?注意:我对简单地了解它不感兴趣(您可以使用 BlahService.isValid() 来找出它);我希望能够知道它是否有效,并知道它何时有效,以便我可以更改状态(并用信号广播该状态更改),并在该状态更改上做其他事情。相反,我想知道由于类似原因它何时不再有效。

在不跟踪服务状态的情况下:

#define CONNECT_DBUS_SIG(x,y) connect(blah,SIGNAL(x),this,SLOT(y))

// FIX - should watch for service, and also handle it going away and
// coming back
blah = new BlahService("com.xyzzy.BlahService", "/com/xyzzy/BlahService",
                           QDBusConnection::sessionBus(), this);
if (!blah)
    return 0;
if (blah.isValid())
{
    CONNECT_DBUS_SIG(foo(),Event_foo());
}
else
{
    // Since we aren't watching for registration, what can we do but exit?
}

可能我们需要观察 DBus 连接对象上的 NameOwnerChanged - 除非 QT 的 dbus 代码为我们执行此操作 - 然后当我们获得该信号更改状态时,如果需要连接或断开来自对象的信号。

我发现的所有示例要么忽略该问题,要么在服务器对象不存在时直接退出,并且不处理它消失。Car/Controller Qt 示例至少注意到服务器是否离开并在使用期间 isValid() 变为 false 时打印“Disconnected”,但它正在轮询 isValid()。

添加:

请注意,QtDbusAbtractInterface 注册服务器所有权的更改 (NameOwnerChanged),并在更改发生时更新 isValid()。因此,我怀疑您可以直接连接到该 serverOwnerChanged 信号以了解所有权更改并将其用作再试一次的指标 - 尽管您将无法信任 isValid 因为它可能会在您收到信号之前或之后更新。

或者(丑陋)您可以设置一个计时器并轮询isValid()。

4

2 回答 2

10

好的,由于没有人回答,我在此期间找到了答案:

您想观看 NameOwnerChanged:

// subscribe to notifications about when a service is registered/unregistered
   connect(QDBusConnection::sessionBus().interface(),
           SIGNAL(serviceOwnerChanged(QString,QString,QString)),
           this,SLOT(serviceOwnerChanged(QString,QString,QString)));

void 
VcsApplicationController::serviceOwnerChanged(const QString &name,
                                              const QString &oldOwner,
                                              const QString &newOwner)
{
    Q_UNUSED(oldOwner);
    if (name == "com.foo.bar.FooService")
    {
        qLog(Whatever) << "serviceOwnerChanged" << name << oldOwner << newOwner;
        if (!newOwner.isEmpty())
        {
            // New owner in town
            emit Initialized();
            // or if you control the interface and both sides, you can wait for
            // a "Ready()" signal before declaring FooService ready for business.
        }
        else
        {
            // indicate we've lost connection, etc
            emit Uninitialized();
        }
    }
}

请注意,从 serviceOwnerChanged 中对 FooService 执行方法可能存在竞争条件 - 我不确定它们是绑定的副作用(我的测试用例中为 dbus-c++),还是 dbus 设计中固有的(可能 - dbus 邮件列表中的 no on 将回答这个问题)。如果存在真正的竞争条件,则可以等待 Ready()/whatever 信号,前提是您控制 DBus API。如果您不控制另一端,您可以添加一个非常短的延迟,或者您也可以观看 AddMatch() 以确保新所有者也已在名称上添加了匹配项。

于 2009-09-24T14:02:12.030 回答
4

在 Qt 5.3 中,serviceOwnerChanged已弃用。使用QDBusServiceWatcherwhich 允许监视特定服务而不是全部。

于 2014-10-22T09:27:38.457 回答