Debian Codegraph @

launch_helper/helper.go

raw
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 // launch_helper wraps lomiri_app_launch to enable using application
18 // helpers. The useful part is HelperRunner
19 package launch_helper
20
21 import (
22 "encoding/json"
23
24 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
25 )
26
27 type trivialHelperLauncher struct {
28 log logger.Logger
29 chOut chan *HelperResult
30 chIn chan *HelperInput
31 }
32
33 // a trivial HelperPool that doesn't launch anything at all
34 func NewTrivialHelperPool(log logger.Logger) HelperPool {
35 return &trivialHelperLauncher{log: log}
36 }
37
38 func (triv *trivialHelperLauncher) Start() chan *HelperResult {
39 triv.chOut = make(chan *HelperResult)
40 triv.chIn = make(chan *HelperInput, InputBufferSize)
41
42 go func() {
43 for i := range triv.chIn {
44 res := &HelperResult{Input: i}
45 err := json.Unmarshal(i.Payload, &res.HelperOutput)
46 if err != nil {
47 triv.log.Debugf("failed to parse HelperOutput from message, leaving it alone: %v", err)
48 res.Message = i.Payload
49 res.Notification = nil
50 }
51 triv.chOut <- res
52 }
53 }()
54
55 return triv.chOut
56 }
57
58 func (triv *trivialHelperLauncher) Stop() {
59 close(triv.chIn)
60 }
61
62 func (triv *trivialHelperLauncher) Run(kind string, input *HelperInput) {
63 triv.chIn <- input
64 }