首页 > 解决方案 > 无法导出两个 dbus 对象

问题描述

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

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

标签: godbus

解决方案


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

推荐阅读