-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
65 lines (54 loc) · 1.15 KB
/
echo.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
package sputnik
import (
"github.com/g41797/kissngoqueue"
)
const EchoBlockName = "echo"
// Echo block is used for debugging
// It replaces not implemented block
// Just assign required responsibility.
// During creation it also get Queue[Msg]
// - created by test
// - stored in factory
// Every retrieved message will be put in this queue.
// for further getting by test
type echo struct {
q *kissngoqueue.Queue[Msg]
done chan struct{}
}
func (bl *echo) init(_ ConfFactory) error {
bl.done = make(chan struct{})
return nil
}
func (bl *echo) finish(init bool) {
close(bl.done)
return
}
func (bl *echo) run(_ BlockCommunicator) {
defer bl.q.CancelMT()
<-bl.done
return
}
func (bl *echo) onMsg(msg Msg) {
if bl.q != nil {
bl.q.PutMT(msg)
}
return
}
type echoFactory struct {
q *kissngoqueue.Queue[Msg]
}
func (ef *echoFactory) createEchoBlock() *Block {
echo := new(echo)
echo.q = ef.q
block := NewBlock(
WithInit(echo.init),
WithRun(echo.run),
WithFinish(echo.finish),
WithOnMsg(echo.onMsg))
return block
}
func EchoBlockFactory(q *kissngoqueue.Queue[Msg]) BlockFactory {
ef := new(echoFactory)
ef.q = q
return ef.createEchoBlock
}