/
push_stdout.go
98 lines (78 loc) · 2.09 KB
/
push_stdout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Package stdout is a sample implementation of a push plugin.
// If enabled, it writes every notification to stdout.
package stdout
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/tinode/chat/server/push"
)
var handler stdoutPush
// How much to buffer the input channel.
const defaultBuffer = 32
type stdoutPush struct {
initialized bool
input chan *push.Receipt
channel chan *push.ChannelReq
stop chan bool
}
type configType struct {
Enabled bool `json:"enabled"`
Buffer int `json:"buffer"`
}
// Init initializes the handler
func (stdoutPush) Init(jsonconf string) error {
// Check if the handler is already initialized
if handler.initialized {
return errors.New("already initialized")
}
var config configType
if err := json.Unmarshal([]byte(jsonconf), &config); err != nil {
return errors.New("failed to parse config: " + err.Error())
}
handler.initialized = true
if !config.Enabled {
return nil
}
if config.Buffer <= 0 {
config.Buffer = defaultBuffer
}
handler.input = make(chan *push.Receipt, config.Buffer)
handler.channel = make(chan *push.ChannelReq, config.Buffer)
handler.stop = make(chan bool, 1)
go func() {
for {
select {
case msg := <-handler.input:
fmt.Fprintln(os.Stdout, msg)
case msg := <-handler.channel:
fmt.Fprintln(os.Stdout, msg)
case <-handler.stop:
return
}
}
}()
return nil
}
// IsReady checks if the handler is initialized.
func (stdoutPush) IsReady() bool {
return handler.input != nil
}
// Push returns a channel that the server will use to send messages to.
// If the adapter blocks, the message will be dropped.
func (stdoutPush) Push() chan<- *push.Receipt {
return handler.input
}
// Channel returns a channel that caller can use to subscribe/unsubscribe devices to channels (FCM topics).
// If the adapter blocks, the message will be dropped.
func (stdoutPush) Channel() chan<- *push.ChannelReq {
return handler.channel
}
// Stop terminates the handler's worker and stops sending pushes.
func (stdoutPush) Stop() {
handler.stop <- true
}
func init() {
push.Register("stdout", &handler)
}