首頁 > 後端開發 > Golang > 錯誤 org.freedesktop.DBus.Error.UnknownMethod:未知/無效方法'Notify”

錯誤 org.freedesktop.DBus.Error.UnknownMethod:未知/無效方法'Notify”

PHPz
發布: 2024-02-06 08:00:17
轉載
1243 人瀏覽過

错误 org.freedesktop.DBus.Error.UnknownMethod:未知/无效方法“Notify”

問題內容

我正在嘗試使用godbus 建立通知伺服器,但我無法正確地將我的伺服器物件匯出到dbus,而dbus 只能辨識我的內省xml。我按照 https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html 來實作它。我還在 godbus 儲存庫中使用了 _example/server.go,您可能會在下面提供的伺服器程式碼中註意到。 這是伺服器程式碼:

package main

import (
    "fmt"
    "os"

    "github.com/godbus/dbus/v5"
    "github.com/godbus/dbus/v5/introspect"
)

const xml = `
<node>
    <interface name="org.freedesktop.notifications">
        <method name="notify">
            <arg direction="in" type="s"/>
            <arg direction="in" type="u"/>
            <arg direction="in" type="s"/>
            <arg direction="in" type="s"/>
            <arg direction="in" type="s"/>
            <arg direction="in" type="as"/>
            <arg direction="in" type="a{sv}"/>
            <arg direction="in" type="i"/>
            <arg direction="out" type="u"/>
        </method>

        <method name="getcapabilities">
            <arg direction="out" type="as"/>
        </method>

        <method name="getserverinformation">
            <arg direction="out" type="s"/>
            <arg direction="out" type="s"/>
            <arg direction="out" type="s"/>
            <arg direction="out" type="s"/>
        </method>

        <method name="closenotification">
            <arg direction="in" type="u"/>
        </method>

        <signal name="notificationclosed">
            <arg type="u" name="id"/>
            <arg type="u" name="reason"/>
        </signal>
    </interface>` + introspect.introspectdatastring + `</node> `

type notificationserver struct {
}

func (s *notificationserver) notify(appname string, replacesid uint32, appicon string, summary string, body string, actions []string, hints map[string]dbus.variant, expiretimeout int32) (uint32, *dbus.error) {
    fmt.printf("new notification: %s\n", body)
    return 0, nil
}

func (s *notificationserver) getcapabilities() ([]string, *dbus.error) {
    return []string{"action-icons", "actions", "body", "body-hyperlinks", "body-images", "body-markup", "icon-multi", "icon-static", "persistence", "sound"}, nil
}

func (s *notificationserver) getserverinformation() (string, string, string, string, *dbus.error) {
    return "antarctica", "antarctica.com", "1.0", "1.2", nil
}

func (s *notificationserver) closenotification(id uint32) *dbus.error {
    s.notificationclosed(id, 0)
    return nil
}
func (s *notificationserver) notificationclosed(id, reason uint32) {

}

func main() {
    conn, err := dbus.connectsessionbus()
    if err != nil {
        panic(err)
    }
    defer conn.close()

    reply, err := conn.requestname("com.antarctica.notification",
        dbus.nameflagdonotqueue)
    if err != nil {
        panic(err)
    }

    if reply != dbus.requestnamereplyprimaryowner {
        fmt.fprintln(os.stderr, "name already taken")
        os.exit(1)
    }
    server := notificationserver{}
    err = conn.export(server,"/org/freedesktop/notifications","org.freedesktop.notifications")
    if err != nil {
        panic(err)
    }
    conn.export(introspect.introspectable(xml), "/org/freedesktop/notifications", "org.freedesktop.dbus.introspectable")
    fmt.println("listening on com.antarctica.notification / /com/antarctica/notification ...")
    select {}
}
登入後複製

現在的問題是,即使客戶端可以存取內省 xml:

$ gdbus introspect --session --dest com.antarctica.notification --object-path /org/freedesktop/notifications --xml

> returns xml
登入後複製

我無法使用我在伺服器程式碼中編寫的 org.freedesktop.notifications 方法。例如,notify 未知/無效,這對於每種方法都是相同的:

$ dbus-send --session --print-reply=literal --dest=com.antarctica.notification /org/freedesktop/Notifications org.freedesktop.Notifications.Notify

> Error org.freedesktop.DBus.Error.UnknownMethod: Unknown / invalid method 'Notify'
登入後複製

也在 qdbusviewer 中,當我嘗試執行任何方法時,它顯示「無法在介面 org.freedesktop.notifications 中的路徑 /org/freedesktop/notifications 上找到方法 x」

我嘗試過的:

  1. 檢查 dbus 是否正在執行
  2. 檢查我的伺服器是否正在執行
  3. 我也嘗試重新啟動 dbus 服務和我的電腦
  4. 我認為notificationserver實例(伺服器)根本沒有被匯出,但我不知道為什麼


正確答案


這有效。你犯了兩個錯誤:

  1. com.antarctica.notification
  2. ##func (s *notificationserver)

您必須要求「org.freedesktop.notifications」作為名稱,且不能在函數中使用指標。

  1. org.freedesktop.notifications
  2. ##func(通知伺服器)
(你也不需要內省)### ###
package main

import (
    "fmt"
    "os"

    "github.com/godbus/dbus/v5"
)

type notificationServer struct{}

func (s notificationServer) Notify(appName string, replacesID uint32, appIcon string, summary string, body string, actions []string, hints map[string]dbus.Variant, expireTimeout int32) (uint32, *dbus.Error) {
    fmt.Printf("New notification: %s\n", body)
    return 0, nil
}

func (s notificationServer) GetCapabilities() ([]string, *dbus.Error) {
    return []string{"action-icons", "actions", "body", "body-hyperlinks", "body-images", "body-markup", "icon-multi", "icon-static", "persistence", "sound"}, nil
}

func (s notificationServer) GetServerInformation() (string, string, string, string, *dbus.Error) {
    return "antarctica", "antarctica.com", "1.0", "1.2", nil
}

func main() {
    conn, err := dbus.ConnectSessionBus()
    if err != nil {
        panic(err)
    }
    defer conn.Close()

    f := notificationServer{}
    conn.Export(f, "/org/freedesktop/Notifications", "org.freedesktop.Notifications")

    reply, err := conn.RequestName("org.freedesktop.Notifications", dbus.NameFlagDoNotQueue)
    if err != nil {
        panic(err)
    }
    if reply != dbus.RequestNameReplyPrimaryOwner {
        fmt.Fprintln(os.Stderr, "name already taken")
        os.Exit(1)
    }
    fmt.Println("Listening...")
    select {}
}
登入後複製

以上是錯誤 org.freedesktop.DBus.Error.UnknownMethod:未知/無效方法'Notify”的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板