【问题标题】:Exporting two dbus objects not possible无法导出两个 dbus 对象
【发布时间】:2021-04-08 09:54:24
【问题描述】:

使用godbus 库并根据他们的服务器example,我正在尝试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 {}
}

【问题讨论】:

    标签: go dbus


    【解决方案1】:
    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 {}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 2011-12-18
      • 2021-03-26
      • 1970-01-01
      • 2015-02-28
      相关资源
      最近更新 更多