~online-accounts/account-polld-plugins-go/trunk

« back to all changes in this revision

Viewing changes to pollbus/bus.go

  • Committer: Alberto Mardegan
  • Date: 2016-08-30 09:34:48 UTC
  • Revision ID: alberto.mardegan@canonical.com-20160830093448-s3vcun1ctprnn3m3
Remvoe pollbus package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 Copyright 2014 Canonical Ltd.
3
 
 
4
 
 This program is free software: you can redistribute it and/or modify it
5
 
 under the terms of the GNU General Public License version 3, as published
6
 
 by the Free Software Foundation.
7
 
 
8
 
 This program is distributed in the hope that it will be useful, but
9
 
 WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
 
 PURPOSE.  See the GNU General Public License for more details.
12
 
 
13
 
 You should have received a copy of the GNU General Public License along
14
 
 with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
*/
16
 
 
17
 
package pollbus
18
 
 
19
 
import (
20
 
        "fmt"
21
 
        "runtime"
22
 
 
23
 
        "log"
24
 
 
25
 
        "launchpad.net/go-dbus/v1"
26
 
)
27
 
 
28
 
const (
29
 
        busInterface = "com.ubuntu.AccountPolld"
30
 
        busPath      = "/com/ubuntu/AccountPolld"
31
 
        busName      = "com.ubuntu.AccountPolld"
32
 
)
33
 
 
34
 
type PollBus struct {
35
 
        conn     *dbus.Connection
36
 
        msgChan  chan *dbus.Message
37
 
        PollChan chan bool
38
 
}
39
 
 
40
 
func New(conn *dbus.Connection) *PollBus {
41
 
        p := &PollBus{
42
 
                conn:     conn,
43
 
                msgChan:  make(chan *dbus.Message),
44
 
                PollChan: make(chan bool),
45
 
        }
46
 
        runtime.SetFinalizer(p, clean)
47
 
        return p
48
 
}
49
 
 
50
 
func clean(p *PollBus) {
51
 
        p.conn.UnregisterObjectPath(busPath)
52
 
        close(p.msgChan)
53
 
        close(p.PollChan)
54
 
}
55
 
 
56
 
func (p *PollBus) Init() error {
57
 
        name := p.conn.RequestName(busName, dbus.NameFlagDoNotQueue)
58
 
        err := <-name.C
59
 
        if err != nil {
60
 
                return fmt.Errorf("bus name could not be take: %s", err)
61
 
        }
62
 
 
63
 
        go p.watchMethodCalls()
64
 
        p.conn.RegisterObjectPath(busPath, p.msgChan)
65
 
 
66
 
        return nil
67
 
}
68
 
 
69
 
func (p *PollBus) SignalDone() error {
70
 
        signal := dbus.NewSignalMessage(busPath, busInterface, "Done")
71
 
        if err := p.conn.Send(signal); err != nil {
72
 
                return err
73
 
        }
74
 
        return nil
75
 
}
76
 
 
77
 
func (p *PollBus) watchMethodCalls() {
78
 
        for msg := range p.msgChan {
79
 
                var reply *dbus.Message
80
 
                switch {
81
 
                case msg.Interface == busInterface && msg.Member == "Poll":
82
 
                        log.Println("Received Poll()")
83
 
                        p.PollChan <- true
84
 
                        reply = dbus.NewMethodReturnMessage(msg)
85
 
                default:
86
 
                        log.Println("Received unkown method call on", msg.Interface, msg.Member)
87
 
                        reply = dbus.NewErrorMessage(msg, "org.freedesktop.DBus.Error.UnknownMethod", "Unknown method")
88
 
                }
89
 
                if err := p.conn.Send(reply); err != nil {
90
 
                        log.Println("Could not send reply:", err)
91
 
                }
92
 
        }
93
 
}