generated from djthorpe/goapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
112 lines (94 loc) · 2.06 KB
/
event.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package mdns
import (
"fmt"
"net"
"strings"
// Modules
. "github.com/djthorpe/go-server"
dns "github.com/miekg/dns"
)
///////////////////////////////////////////////////////////////////////////////
// TYPES
type message struct {
*dns.Msg
Addr net.Addr
Index int
Zone string
Err error
}
type event struct {
EventType
service
}
type EventType int
type ServiceEvent interface {
Event
Type() EventType
Service() Service
}
///////////////////////////////////////////////////////////////////////////////
// GLOBALS
const (
EVENT_TYPE_ADDED EventType = (1 << iota)
EVENT_TYPE_REMOVED
EVENT_TYPE_EXPIRED
EVENT_TYPE_CHANGED
EVENT_TYPE_SERVICE
EVENT_TYPE_NONE EventType = 0
EVENT_TYPE_MIN = EVENT_TYPE_ADDED
EVENT_TYPE_MAX = EVENT_TYPE_SERVICE
)
///////////////////////////////////////////////////////////////////////////////
// EVENT IMPLEMENTATION
func (e event) Name() string {
return e.service.Instance()
}
func (e event) Value() interface{} {
return &e.service
}
func (e event) Type() EventType {
return e.EventType
}
func (e event) Service() Service {
return &e.service
}
///////////////////////////////////////////////////////////////////////////////
// STRINGIFY
func (e event) String() string {
str := "<mdns.event"
if t := e.EventType; t != EVENT_TYPE_NONE {
str += " type=" + e.EventType.String()
}
str += fmt.Sprint(" ", e.service)
return str + ">"
}
func (f EventType) String() string {
if f == EVENT_TYPE_NONE {
return f.FlagString()
}
str := ""
for v := EVENT_TYPE_MIN; v <= EVENT_TYPE_MAX; v <<= 1 {
if f&v == v {
str += v.FlagString() + "|"
}
}
return strings.TrimSuffix(str, "|")
}
func (v EventType) FlagString() string {
switch v {
case EVENT_TYPE_NONE:
return "EVENT_TYPE_NONE"
case EVENT_TYPE_ADDED:
return "EVENT_TYPE_ADDED"
case EVENT_TYPE_REMOVED:
return "EVENT_TYPE_REMOVED"
case EVENT_TYPE_EXPIRED:
return "EVENT_TYPE_EXPIRED"
case EVENT_TYPE_CHANGED:
return "EVENT_TYPE_CHANGED"
case EVENT_TYPE_SERVICE:
return "EVENT_TYPE_SERVICE"
default:
return "[?? Invalid EventType]"
}
}