英文:
Error org.freedesktop.DBus.Error.UnknownMethod: Unknown / invalid method 'Notify'
问题
我正在尝试使用godbus创建一个通知服务器,但是我无法正确地将我的服务器对象导出到dbus,dbus只能识别我的introspect 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 {}
}
现在的问题是,尽管introspect xml对客户端是可访问的:
$ gdbus introspect --session --dest com.antarctica.notification --object-path /org/freedesktop/Notifications --xml
> 返回xml
但我无法使用我在服务器代码中编写的org.freedesktop.Notifications方法。例如,Notify是未知/无效的,对于每个方法都是如此:
$ dbus-send --session --print-reply=literal --dest=com.antarctica.notification /org/freedesktop/Notifications org.freedesktop.Notifications.Notify
> 错误 org.freedesktop.DBus.Error.UnknownMethod: Unknown / invalid method 'Notify'
此外,在qdbusviewer中,当我尝试执行任何方法时,它会显示“在接口org.freedesktop.Notifications的路径/org/freedesktop/Notifications上找不到方法x”。
我尝试过的方法:
- 检查dbus是否正在运行
- 检查我的服务器是否正在运行
- 我尝试重新启动dbus服务和我的计算机
- 我认为NotificationServer实例(server)根本没有被导出,但我不知道为什么
英文:
I am trying to make a notification server with godbus but i cant properly export my server object to dbus and dbus only recognizes my introspect xml. I am following https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html to implement it. I have also used _example/server.go in godbus repo as you might notice in my server code provided below.
Here is the server code:
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 {}
}
Now the problem is that eventho the introspect xml is accessible to client:
$ gdbus introspect --session --dest com.antarctica.notification --object-path /org/freedesktop/Notifications --xml
> returns xml
I cant use org.freedesktop.Notifications methods that i wrote in my server code. for example Notify is Unknown/Invalid and this is the same for each method:
$ 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'
Also in qdbusviewer when I try to execute any method it says "Unable to find method x on path /org/freedesktop/Notifications in interface org.freedesktop.Notifications"
What I have tried:
- checked if dbus is running
- checked if my server is running
- I tried restarting dbus service and my computer as well
- I think NotificationServer instance (server) isnt being exported at all but i have no idea why
答案1
得分: 1
这个代码是可以工作的。你犯了两个错误:
- com.antarctica.notification
- func (s *NotificationServer)
你需要将名称请求为'org.freedesktop.Notifications',并且在函数中不能使用指针。
- org.freedesktop.Notifications
- func (s NotificationServer)
- (你也不需要introspection)
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("新通知:%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, "名称已被占用")
os.Exit(1)
}
fmt.Println("监听中...")
select {}
}
英文:
This works. You made two mistakes:
- com.antarctica.notification
- func (s *NotificationServer)
You have to request 'org.freedesktop.Notifications' as name and you can not use a pointer in the func.
- org.freedesktop.Notifications
- func (s NotificationServer)
- (you don't need introspection either)
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 {}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论