Go 无法导出两个dbus对象

Go 无法导出两个dbus对象,go,dbus,Go,Dbus,使用库并根据它们的服务器,我试图导出具有不同接口的两个不同对象。一个对象路径是/a/b/c,另一个是/a/b/c/d。如果我出口其中一个,一切正常。即使没有重叠,一切正常(/a/b/c&/w/x/y/z)。但是导出/a/b/c&/a/b/c/d只会导致DBU上有一个。这是我的密码: package main import ( "fmt" "os" "github.com/godbus/dbus/v5"

使用库并根据它们的服务器,我试图导出具有不同接口的两个不同对象。一个对象路径是
/a/b/c
,另一个是
/a/b/c/d
。如果我出口其中一个,一切正常。即使没有重叠,一切正常(
/a/b/c
&
/w/x/y/z
)。但是导出
/a/b/c
&
/a/b/c/d
只会导致DBU上有一个。这是我的密码:

package main

import (
    "fmt"
    "os"

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

const introP = `
<node>
    <interface name="a.b.c.Ping">
        <method name="Ping">
            <arg direction="out" type="s"/>
        </method>
    </interface>` + introspect.IntrospectDataString + `</node> `

type ping string

func (p ping) Ping() (string, *dbus.Error) {
    fmt.Println(p)
    return string(p), nil
}

const introZ = `
<node>
    <interface name="a.b.c.d.Zing">
        <method name="Zing">
            <arg direction="out" type="s"/>
        </method>
    </interface>` + introspect.IntrospectDataString + `</node> `

type zing string

func (z zing) Zing() (string, *dbus.Error) {
    fmt.Println(z)
    return string(z), nil
}

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

    reply, err := conn.RequestName("a.b.c",
        dbus.NameFlagDoNotQueue)
    if err != nil {
        panic(err)
    }
    if reply != dbus.RequestNameReplyPrimaryOwner {
        fmt.Fprintln(os.Stderr, "name already taken")
        os.Exit(1)
    }

    p := ping("Pong")
    conn.Export(p, "/a/b/c", "a.b.c.Ping")
    conn.Export(introspect.Introspectable(introP), "/a/b/c",
        "org.freedesktop.DBus.Introspectable")

    z := zing("Zong")
    conn.Export(z, "/a/b/c/d", "a.b.c.d.Zing")
    conn.Export(introspect.Introspectable(introZ), "/a/b/c/d",
        "org.freedesktop.DBus.Introspectable")

    fmt.Println("Listening on dbus...")
    select {}
}
主程序包
进口(
“fmt”
“操作系统”
“github.com/godbus/dbus/v5”
“github.com/godbus/dbus/v5/introspect”
)
常量插入=`
`+introspect.IntrospectDataString+``
键入ping字符串
func(p ping)ping()(字符串,*dbus.Error){
fmt.Println(p)
返回字符串(p),nil
}
常数introZ=`
`+introspect.IntrospectDataString+``
键入字符串
func(z zing)zing()(字符串,*dbus.Error){
fmt.Println(z)
返回字符串(z),零
}
func main(){
conn,err:=dbus.ConnectSessionBus()
如果错误!=零{
恐慌(错误)
}
延迟连接关闭()
回复,错误:=conn.RequestName(“a.b.c”,
dbus.namefagdonotqueue)
如果错误!=零{
恐慌(错误)
}
如果回答!=dbus.RequestNameReplyPrimaryOwner{
fmt.Fprintln(os.Stderr,“已取名称”)
操作系统退出(1)
}
p:=乒乓球
控制出口(p,“/a/b/c”,“a.b.c.Ping”)
连接导出(内省可内省),“/a/b/c”,
“org.freedesktop.DBus.Introspectable”)
z:=zing(“宗”)
连接出口(z,“/a/b/c/d”,“a.b.c.d.z”)
conn.Export(内省可内省)(内省),“/a/b/c/d”,
“org.freedesktop.DBus.Introspectable”)
fmt.Println(“监听DBU…”)
选择{}
}
package main

import (
    "fmt"
    "os"

    "github.com/godbus/dbus/v5"
    "github.com/godbus/dbus/v5/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.SessionBus()
    if err != nil {
        panic(err)
    }
    replyP, errP := conn.RequestName("a.b.c.d.Ping",
        dbus.NameFlagDoNotQueue)
    if errP != nil {
        panic(errP)
    }
    if replyP != dbus.RequestNameReplyPrimaryOwner {
        fmt.Fprintln(os.Stderr, "name already taken")
        os.Exit(1)
    }

    p := ping("Pong")
    var introP = &introspect.Node{
        Name: "/a/b/c/d/Ping",
        Interfaces: []introspect.Interface{
            introspect.IntrospectData,
            {
                Name:    "a.b.c.d.Ping",
                Methods: introspect.Methods(p),
            },
        },
    }

    conn.Export(p, "/a/b/c/d/Ping", "a.b.c.d.Ping")

    z := zing("Zong")
    var introZ = &introspect.Node{
        Name: "/a/b/c/Zing",
        Interfaces: []introspect.Interface{
            introspect.IntrospectData,
            {
                Name:    "a.b.c.Zing",
                Methods: introspect.Methods(z),
            },
        },
    }

    conn.Export(z, "/a/b/c/Zing", "a.b.c.Zing")

    conn.Export(introspect.NewIntrospectable(&introspect.Node{
        Name: "/",
        Children: []introspect.Node{
            {
                Name: "a",
            },
        },
    }), "/", "org.freedesktop.DBus.Introspectable")
    conn.Export(introspect.NewIntrospectable(&introspect.Node{
        Name: "/a",
        Children: []introspect.Node{
            {
                Name: "b",
            },
        },
    }), "/com", "org.freedesktop.DBus.Introspectable")
    conn.Export(introspect.NewIntrospectable(&introspect.Node{
        Name: "/a/b",
        Children: []introspect.Node{
            {
                Name: "c",
            },
        },
    }), "/a/b", "org.freedesktop.DBus.Introspectable")
    conn.Export(introspect.NewIntrospectable(&introspect.Node{
        Name: "/a/b/c",
        Children: []introspect.Node{
            {
                Name: "d",
            },
            {
                Name: "Zing",
            },
        },
    }), "/a/b/c", "org.freedesktop.DBus.Introspectable")
    conn.Export(introspect.NewIntrospectable(&introspect.Node{
        Name: "/a/b/c/d",
        Children: []introspect.Node{
            {
                Name: "Ping",
            },
        },
    }), "/a/b/c/d", "org.freedesktop.DBus.Introspectable")
    conn.Export(introspect.NewIntrospectable(introP), "/a/b/c/d/Ping",
        "org.freedesktop.DBus.Introspectable")

    conn.Export(introspect.NewIntrospectable(introZ), "/a/b/c/Zing",
        "org.freedesktop.DBus.Introspectable")

    fmt.Printf("Listening on %s / %s ...\n", "a.b.c...", "/a/b/c...")
    select {}
}