~ubuntu-branches/ubuntu/wily/account-polld/wily-proposed

« back to all changes in this revision

Viewing changes to pollbus/bus.go

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, CI bot, Sergio Schvezov
  • Date: 2014-08-25 16:43:27 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140825164327-vlejuq6euryb6pfh
Tags: 0.1+14.10.20140825.1-0ubuntu1
[ CI bot ]
* Switch from self polling to being polled by the push client that
  manages the power state

[ Sergio Schvezov ]
* Adding gmail avatar's through QtContacts

Show diffs side-by-side

added added

removed removed

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