~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/godbus/dbus/_examples/server.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "github.com/godbus/dbus"
 
6
        "github.com/godbus/dbus/introspect"
 
7
        "os"
 
8
)
 
9
 
 
10
const intro = `
 
11
<node>
 
12
        <interface name="com.github.guelfey.Demo">
 
13
                <method name="Foo">
 
14
                        <arg direction="out" type="s"/>
 
15
                </method>
 
16
        </interface>` + introspect.IntrospectDataString + `</node> `
 
17
 
 
18
type foo string
 
19
 
 
20
func (f foo) Foo() (string, *dbus.Error) {
 
21
        fmt.Println(f)
 
22
        return string(f), nil
 
23
}
 
24
 
 
25
func main() {
 
26
        conn, err := dbus.SessionBus()
 
27
        if err != nil {
 
28
                panic(err)
 
29
        }
 
30
        reply, err := conn.RequestName("com.github.guelfey.Demo",
 
31
                dbus.NameFlagDoNotQueue)
 
32
        if err != nil {
 
33
                panic(err)
 
34
        }
 
35
        if reply != dbus.RequestNameReplyPrimaryOwner {
 
36
                fmt.Fprintln(os.Stderr, "name already taken")
 
37
                os.Exit(1)
 
38
        }
 
39
        f := foo("Bar!")
 
40
        conn.Export(f, "/com/github/guelfey/Demo", "com.github.guelfey.Demo")
 
41
        conn.Export(introspect.Introspectable(intro), "/com/github/guelfey/Demo",
 
42
                "org.freedesktop.DBus.Introspectable")
 
43
        fmt.Println("Listening on com.github.guelfey.Demo / /com/github/guelfey/Demo ...")
 
44
        select {}
 
45
}