在Go中导出DBus接口存在问题可能并不如预期的那样工作
时间:2024-03-26
浏览:349
作者:佚名
在 Go 中导出 DBus 接口时,如果一个接口包含多个方法,直接导出方法可能会导致只有最后一个导出的方法有效。为了解决此问题,需要定义一个新的数据类型,并通过该数据类型导出接口方法。此数据类型是一个空结构,可以处理所有内容。通过这种方法,所有接口方法都可以在 DBus 上被调用。
问题内容
首先向所有阅读本文的人问好,
我目前在实现 go dbus 接口时遇到问题。问题是我正在定义一个与方法“ping”和“zing”的接口,这似乎有效。但是,当我导出它们并想要调用它们(通过 d-feet)时,只有最后导出的方法才有效。因此,对于我的观点,导出功能一次仅导出一个方法并覆盖前一个方法。我也尝试使用 exportall 来实现,但这也不起作用。如果有人对我有想法或只是提示,那就太好了!
下面你可以看到我的源代码:
package main
import (
"fmt"
"os"
"github.com/godbus/dbus"
"github.com/godbus/dbus/introspect"
)
type ping string
func (p ping) Ping() (string, *dbus.Error) {
fmt.Println(p)
return string(p), nil
}
type zing string
func (z zing) Zing() (string, *dbus.Error) {
fmt.Println(z)
return string(z), nil
}
func main() {
conn, err := dbus.ConnectSystemBus()
if err != nil {
panic(err)
}
replyP, errP := conn.RequestName("test.Ping", dbus.NameFlagDoNotQueue)
if errP != nil {
panic(errP)
}
if replyP != dbus.RequestNameReplyPrimaryOwner {
fmt.Fprintln(os.Stderr, "name already taken")
os.Exit(1)
}
z := zing("Zong")
p := ping("Pong")
var intro = &introspect.Node{
//Name: "/",
Interfaces: []introspect.Interface{
introspect.IntrospectData,
{
Name: "test.test",
Methods: []introspect.Method{
{
Name: "Zing",
Args: []introspect.Arg{
{"out", "s", "out"},
},
},
{
Name: "Ping",
Args: []introspect.Arg{
{"out", "s", "out"},
},
},
},
},
},
}
conn.Export(z, "/", "test.test")
conn.Export(p, "/", "test.test")
conn.Export(introspect.NewIntrospectable(intro), "/", "org.freedesktop.DBus.Introspectable")
fmt.Printf("Listening on %s / %s ...\n", "test.test", "/...")
select {}
}正确答案
关键是,当您有一个具有两种或多种方法的 dbus 接口时,您必须定义一种新的数据类型,然后再导出该数据类型。为此,我创建了一个新的类型 type ping struct{}。它基本上是一个空结构,因此它可以处理所有内容。新类型发生在通过 dbus 调用的函数/方法的实现中。最后,您必须初始化并将其导出到 dbus p := ping{} 和 conn.export(p, "/", "test.test")。
package main
import (
"fmt"
"os"
"github.com/godbus/dbus"
"github.com/godbus/dbus/introspect"
)
type ping struct{}
func (z ping) Ping(s string, i uint8) (*dbus.Error) {
fmt.Println(s, i)
return nil
}
func (p ping) Zing(t string) (*dbus.Error) {
fmt.Println(t)
return nil
}
func main() {
conn, err := dbus.ConnectSystemBus()
if err != nil {
panic(err)
}
replyP, errP := conn.RequestName("test.Ping", dbus.NameFlagDoNotQueue)
if errP != nil {
panic(errP)
}
if replyP != dbus.RequestNameReplyPrimaryOwner {
fmt.Fprintln(os.Stderr, "name already taken")
os.Exit(1)
}
p := ping{}
var intro = &introspect.Node{
Name: "/",
Interfaces: []introspect.Interface{
introspect.IntrospectData,
{
Name: "test.test",
Methods: []introspect.Method{
{
Name: "Zing",
Args: []introspect.Arg{
{"str", "s", "in"},
},
},
{
Name: "Ping",
Args: []introspect.Arg{
{"str", "s", "in"},
{"int", "y", "in"},
},
},
},
},
},
}
conn.Export(p, "/", "test.test")
conn.Export(introspect.NewIntrospectable(intro), "/", "org.freedesktop.DBus.Introspectable")
fmt.Printf("Listening on %s / %s \n", "test.test", "/...")
select {}
}好了,本文到此结束,带大家了解了《在Go中导出DBus接口存在问题可能并不如预期的那样工作》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
