| 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 C API |
| 18 |
package urldispatcher |
| 19 |
|
| 20 |
import ( |
| 21 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 22 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 23 |
"gitlab.com/ubports/development/core/lomiri-push-service/urldispatcher/curldispatcher" |
| 24 |
) |
| 25 |
|
| 26 |
// A URLDispatcher wrapper. |
| 27 |
type URLDispatcher interface { |
| 28 |
DispatchURL(string, *click.AppId) error |
| 29 |
TestURL(*click.AppId, []string) bool |
| 30 |
} |
| 31 |
|
| 32 |
type urlDispatcher struct { |
| 33 |
log logger.Logger |
| 34 |
} |
| 35 |
|
| 36 |
// New builds a new URL dispatcher that uses the provided bus.Endpoint |
| 37 |
func New(log logger.Logger) URLDispatcher { |
| 38 |
return &urlDispatcher{log} |
| 39 |
} |
| 40 |
|
| 41 |
var _ URLDispatcher = &urlDispatcher{} // ensures it conforms |
| 42 |
|
| 43 |
var cDispatchURL = curldispatcher.DispatchURL |
| 44 |
var cTestURL = curldispatcher.TestURL |
| 45 |
|
| 46 |
func (ud *urlDispatcher) DispatchURL(url string, app *click.AppId) error { |
| 47 |
ud.log.Debugf("dispatching %s", url) |
| 48 |
err := cDispatchURL(url, app.DispatchPackage()) |
| 49 |
if err != nil { |
| 50 |
ud.log.Errorf("DispatchURL failed: %s", err) |
| 51 |
} |
| 52 |
return err |
| 53 |
} |
| 54 |
|
| 55 |
func (ud *urlDispatcher) TestURL(app *click.AppId, urls []string) bool { |
| 56 |
ud.log.Debugf("TestURL: %s", urls) |
| 57 |
var appIds []string |
| 58 |
appIds = cTestURL(urls) |
| 59 |
if len(appIds) == 0 { |
| 60 |
ud.log.Debugf("TestURL: invalid urls: %s - %s", urls, app.Versioned()) |
| 61 |
return false |
| 62 |
} |
| 63 |
for _, appId := range appIds { |
| 64 |
if appId != app.Versioned() { |
| 65 |
ud.log.Debugf("notification skipped because of different appid for actions: %v - %s != %s", urls, appId, app.Versioned()) |
| 66 |
return false |
| 67 |
} |
| 68 |
} |
| 69 |
return true |
| 70 |
} |