~ps-jenkins/ubuntu-push/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to bus/urldispatcher/urldispatcher.go

  • Committer: CI bot
  • Author(s): Roberto Alsina
  • Date: 2014-09-02 17:28:09 UTC
  • mfrom: (126.1.4 ubuntu-push)
  • Revision ID: ps-jenkins@lists.canonical.com-20140902172809-hf7n9m5vsws13w5s
Latest changes from automatic 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 Copyright 2013-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 urldispatcher wraps the url dispatcher's dbus api point
18
 
package urldispatcher
19
 
 
20
 
import (
21
 
        "launchpad.net/ubuntu-push/bus"
22
 
        "launchpad.net/ubuntu-push/click"
23
 
        "launchpad.net/ubuntu-push/logger"
24
 
)
25
 
 
26
 
// UrlDispatcher lives on a well-known bus.Address
27
 
var BusAddress bus.Address = bus.Address{
28
 
        Interface: "com.canonical.URLDispatcher",
29
 
        Path:      "/com/canonical/URLDispatcher",
30
 
        Name:      "com.canonical.URLDispatcher",
31
 
}
32
 
 
33
 
// A URLDispatcher is a simple beast, with a single method that does what it
34
 
// says on the box.
35
 
type URLDispatcher interface {
36
 
        DispatchURL(string, *click.AppId) error
37
 
        TestURL(*click.AppId, []string) bool
38
 
}
39
 
 
40
 
type urlDispatcher struct {
41
 
        endp bus.Endpoint
42
 
        log  logger.Logger
43
 
}
44
 
 
45
 
// New builds a new URL dispatcher that uses the provided bus.Endpoint
46
 
func New(endp bus.Endpoint, log logger.Logger) URLDispatcher {
47
 
        return &urlDispatcher{endp, log}
48
 
}
49
 
 
50
 
var _ URLDispatcher = &urlDispatcher{} // ensures it conforms
51
 
 
52
 
func (ud *urlDispatcher) DispatchURL(url string, app *click.AppId) error {
53
 
        ud.log.Debugf("Dispatching %s", url)
54
 
        err := ud.endp.Call("DispatchURL", bus.Args(url, app.DispatchPackage()))
55
 
        if err != nil {
56
 
                ud.log.Errorf("Dispatch to %s failed with %s", url, err)
57
 
        }
58
 
        return err
59
 
}
60
 
 
61
 
func (ud *urlDispatcher) TestURL(app *click.AppId, urls []string) bool {
62
 
        ud.log.Debugf("TestURL: %s", urls)
63
 
        var appIds []string
64
 
        err := ud.endp.Call("TestURL", bus.Args(urls), &appIds)
65
 
        if err != nil {
66
 
                ud.log.Errorf("TestURL for %s failed with %s", urls, err)
67
 
                return false
68
 
        }
69
 
        for _, appId := range appIds {
70
 
                if appId != app.Versioned() {
71
 
                        ud.log.Debugf("Notification skipped because of different appid for actions: %v - %s != %s", urls, appId, app.Versioned())
72
 
                        return false
73
 
                }
74
 
        }
75
 
        return true
76
 
}