在 go 中导出 dbus 接口似乎无法按预期工作
Exporting dbus interface in go seems not to work as expected
首先向阅读本文的各位问好,
我目前在实现 Go dbus 接口时遇到问题。问题是我正在为方法“Ping”和“Zing”定义一个接口,这似乎有效。但是当我导出它们并想调用它们(通过 d-feet)时,只有最后导出的方法有效。所以对于我的 Opionion,Export 函数一次只导出一个方法并覆盖以前的方法。我也尝试使用 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 获取调用的 functions/method 的实现中。最后,您必须初始化并将其导出到 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 接口时遇到问题。问题是我正在为方法“Ping”和“Zing”定义一个接口,这似乎有效。但是当我导出它们并想调用它们(通过 d-feet)时,只有最后导出的方法有效。所以对于我的 Opionion,Export 函数一次只导出一个方法并覆盖以前的方法。我也尝试使用 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 获取调用的 functions/method 的实现中。最后,您必须初始化并将其导出到 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 {}
}