-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate.go
More file actions
150 lines (118 loc) · 3 KB
/
state.go
File metadata and controls
150 lines (118 loc) · 3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"sync"
"github.com/Redundancy/upto/message"
"github.com/Redundancy/upto/store"
)
// BinaryMessageHandler reads MessagePack encoded messages
type BinaryMessageHandler struct {
MessageHandler
}
func (h *BinaryMessageHandler) ReceiveMessage(store store.UptoDataStore, ip string, buffer []byte) error {
m := &message.UDPMessage{}
m.UnmarshalMsg(buffer)
if m.FillHostWithIP {
m.Host = ip
}
return h.handleMessage(store, m)
}
type HostMap map[string]store.TimelineEvent
type EventHostMap map[string]HostMap
type ContextEventHostMap map[string]EventHostMap
func (h ContextEventHostMap) getEvent(context, event, host string) (evt store.TimelineEvent, exists bool) {
c, contextExists := h[context]
if !contextExists {
exists = false
return
}
e, eventNameExists := c[event]
if !eventNameExists {
exists = false
return
}
evt, exists = e[host]
return
}
func (h ContextEventHostMap) addEvent(context string, event store.TimelineEvent) error {
name := event.EventName
c, contextExists := h[context]
if !contextExists {
c = make(EventHostMap)
h[context] = c
}
e, eventNameExists := c[name]
if !eventNameExists {
e = make(HostMap)
c[name] = e
}
_, exists := e[event.Host]
if exists {
return EventInProgressError(name)
}
e[event.Host] = event
return nil
}
// MessageHandler handles basic message correlation and handling
type MessageHandler struct {
sync.Mutex
ContextEventHostMap
}
func (h *MessageHandler) ensureMessagesMapExists() {
if h.ContextEventHostMap == nil {
h.ContextEventHostMap = make(ContextEventHostMap)
}
}
// EventInProgressError is returned when events are already in progress and
// have not completed
type EventInProgressError string
func (e EventInProgressError) Error() string {
return "Event " + string(e) + " was already in progress"
}
// EventNotStartedError is returned an end event is received for an event that
// has not begun
type EventNotStartedError string
func (e EventNotStartedError) Error() string {
return "Event " + string(e) + " had not had a start event"
}
func (h *MessageHandler) handleMessage(data store.UptoDataStore, m *message.UDPMessage) error {
h.Lock()
defer h.Unlock()
h.ensureMessagesMapExists()
name := m.Name
switch m.Type {
case message.MessageStartEvent:
return h.addEvent(m.Context, store.TimelineEvent{
EventName: name,
Host: m.Host,
Start: m.Time,
End: m.Time,
})
case message.MessageEndEvent:
evt, exists := h.getEvent(m.Context, name, m.Host)
if !exists {
return EventNotStartedError(name)
}
data.AddTimelineEvent(
m.Context,
data.GetLatestTimeline(m.Context),
store.TimelineEvent{
EventName: name,
Host: m.Host,
Start: evt.Start,
End: m.Time,
},
)
case message.MessageSingleEvent:
return fmt.Errorf(
"Single Events not implemented: %v ",
name,
)
case message.MessageNewTimeline:
if !data.ContextExists(m.Context) {
data.CreateContext(m.Context)
}
data.CreateNewTimeline(m.Context)
}
return nil
}