导出Go中的dbus接口似乎不像预期的那样工作。

huangapple go评论82阅读模式
英文:

Exporting dbus interface in go seems not to work as expected

问题

首先,向阅读此内容的每个人问好,

我目前在实现一个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{
		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 {}
}
英文:

First of all Hello to everybody who read this,

I have currently a Problem while implementing a Go dbus interface. The Problem is that I am defining an interface with to methods "Ping" and "Zing" this seems to work. But when I export them and want to call them (via d-feet) only the last exported Method work. So for my Opionion the Export funtion only export one method at the time and overwrites the previous. I also tried to to it with ExportAll, but this also dont work. If anybody has an idea or just a hint for me, it would be great!

Below you see my source code:

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 {}
}

答案1

得分: 0

关键是,当你有一个具有两个或更多方法的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 {}
}
英文:

The key is, that when you have a dbus Interface with two or more methods, you MUST define ONE new Datatype, that gets Exported afterwards. For this I create a new Type type ping struct{}. It is basicly an empty struct, so it can handle everything. Than the new Type take place at the implementation of the functions/method that gets calls via dbus. Finally you have to Initalize and Export it to the dbus p := ping{} and 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 {}
}

huangapple
  • 本文由 发表于 2022年1月31日 23:41:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/70928721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定