Skip to content

Instantly share code, notes, and snippets.

@cuu
Created February 3, 2019 08:23
Show Gist options
  • Save cuu/6f427658ed7ac3329ccb0e6d9feb1c60 to your computer and use it in GitHub Desktop.
Save cuu/6f427658ed7ac3329ccb0e6d9feb1c60 to your computer and use it in GitHub Desktop.
godbus server example
package main
import (
"fmt"
"github.com/godbus/dbus"
"github.com/godbus/dbus/introspect"
"os"
//"encoding/xml"
)
const (
path_name = "/com/github/guelfey/Demo"
iface_name = "com.github.guelfey.Demo"
)
type foo string
func (f foo) Foo() (string, *dbus.Error) {
fmt.Println(f)
return string(f), nil
}
func main() {
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
}
reply, err := conn.RequestName(iface_name,
dbus.NameFlagDoNotQueue)
if err != nil {
panic(err)
}
if reply != dbus.RequestNameReplyPrimaryOwner {
fmt.Fprintln(os.Stderr, "name already taken")
os.Exit(1)
}
f := foo("NeedsExternalCalls")
conn.Export(f, path_name, iface_name)
node := &introspect.Node{}
node.Name = iface_name
iface := &introspect.Interface{}
iface.Name = iface_name
mts := introspect.Methods(f)
iface.Methods = mts
node.Interfaces = append(node.Interfaces,*iface)
dbus_xml_str := introspect.NewIntrospectable(node)
fmt.Println(dbus_xml_str)
conn.Export(dbus_xml_str , path_name,
"org.freedesktop.DBus.Introspectable")
fmt.Println(fmt.Sprintf("Listening on %s / %s ...",iface_name,path_name))
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment