-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontext.go
More file actions
91 lines (76 loc) · 2.21 KB
/
context.go
File metadata and controls
91 lines (76 loc) · 2.21 KB
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
package pipe
import (
"github.com/bgpfix/bgpfix/json"
"github.com/bgpfix/bgpfix/msg"
)
// PipeContext tracks message processing progress in a pipe
type PipeContext struct {
Pipe *Pipe // pipe processing the message
Dir *Direction // direction processing the message
// Optional id of message source, by default 0 (disabled).
// Allows for detecting own messages.
SourceId int
// Optional callback Id filter, by default 0 (disabled).
// Allows for injecting messages at arbitrary pipe location.
// Applies only to callbacks with a non-zero Id.
// If >0, skip callback if its Id < SkipId (or Id > SkipId in reverse mode).
// If <0, skip callback if its Id <= -SkipId (or Id >= -SkipId in reverse mode).
SkipId int
Callback *Callback // currently run callback
Action Action // requested message actions
prepared bool // prepare() called
cbs []*Callback // scheduled callbacks to run
kv map[string]any // generic Key-Value store
}
// Context returns pipe Context inside message m,
// updating m.Value if needed.
func Context(m *msg.Msg) *PipeContext {
if m == nil {
return nil
} else if pc, ok := m.Value.(*PipeContext); ok {
return pc
} else {
pc = new(PipeContext)
m.Value = pc
return pc
}
}
// Reset resets pc to empty state
func (pc *PipeContext) Reset() {
*pc = PipeContext{}
}
// Clear resets pc, but preserves ACTION_BORROW if set.
func (pc *PipeContext) Clear() {
a := pc.Action
pc.Reset()
pc.Action = a & ACTION_BORROW
}
// NoCallbacks requests the message to skip running any callbacks
func (pc *PipeContext) NoCallbacks() {
pc.cbs = noCallbacks
}
var noCallbacks = []*Callback{}
// HasKV returns true iff the context already has a Key-Value store.
func (pc *PipeContext) HasKV() bool {
return pc.kv != nil
}
// KV returns a generic Key-Value store, creating it first if needed.
func (pc *PipeContext) KV() map[string]any {
if pc.kv == nil {
pc.kv = make(map[string]interface{})
}
return pc.kv
}
// TODO
func (pc *PipeContext) ToJSON(dst []byte) []byte {
return json.Byte(dst, byte(pc.Action))
}
// TODO
func (pc *PipeContext) FromJSON(src []byte) error {
val, err := json.UnByte(src)
if err != nil {
return err
}
pc.Action = Action(val)
return nil
}