-
Notifications
You must be signed in to change notification settings - Fork 1
/
xpl.go
186 lines (155 loc) · 3.62 KB
/
xpl.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type xplMessage struct {
messageType string
source string
target string
schema string
body map[string]string
}
var xplPort *net.UDPConn
var sendAddr *net.UDPAddr
var metricXPLProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "slimytm_xpl_processed_total",
Help: "The total number of xPL messages that have been processed and accepted",
})
var metricXPLSent = promauto.NewCounter(prometheus.CounterOpts{
Name: "slimytm_xpl_sent_total",
Help: "The total number of xPL messages that have been sent",
})
func xplInit() {
var err error
sendAddr, err = net.ResolveUDPAddr("udp4", "255.255.255.255:3865")
if err != nil {
logger.Panicw("unable to resolve xPL broadcast addr",
"err", err)
}
xplPort, err = net.ListenUDP("udp4", nil)
if err != nil {
logger.Panicw("unable to open xPL listen addr",
"err", err)
}
go xplHeartbeat()
}
func sendXPL(m xplMessage, source string) {
metricXPLSent.Inc()
out := compileXPL(m, source)
xplPort.WriteToUDP([]byte(out), sendAddr)
}
func compileXPL(x xplMessage, source string) string {
m := fmt.Sprintf(`%v
{
hop=1
source=%v
target=%v
}
%v
{
`, x.messageType, source, x.target, x.schema)
for k, v := range x.body {
m += k + "=" + v + "\n"
}
return m + "}\n"
}
func parseXPL(msg string) xplMessage {
x := xplMessage{body: make(map[string]string)}
split := strings.Split(msg, "\n")
x.messageType = split[0]
x.schema = split[6]
for _, line := range split {
sub := strings.SplitN(line, "=", 2)
if len(sub) == 2 {
x.body[sub[0]] = sub[1]
}
}
x.source = x.body["source"]
delete(x.body, "source")
x.target = x.body["target"]
delete(x.body, "target")
delete(x.body, "hop")
return x
}
func xplListener() {
for {
b := make([]byte, 1024)
n, err := xplPort.Read(b)
if err != nil {
logger.Errorw("unable to read xPL message",
"err", err)
continue
}
x := parseXPL(string(b[:n]))
target := strings.Split(x.target, ".")
if target[0] == "slimdev-slimserv" {
if x.schema == "osd.basic" {
metricXPLProcessed.Inc()
delay, ok := x.body["delay"]
if !ok {
delay = "5"
}
d, err := strconv.Atoi(delay)
if err != nil {
logger.Errorw("unable to convert osd.basic delay to integer",
"err", err)
d = 5
}
for _, v := range queues {
if v.Player.GetName() == target[1] {
cleaned := strings.TrimLeft(x.body["text"], "\\n")
cleaned = strings.TrimLeft(cleaned, "\n")
logger.Debugw("received xPL",
"for", v.Player.GetName())
ctx, _ := context.WithTimeout(context.Background(), time.Second*time.Duration(d))
v.Texts = append(v.Texts, text{
bufs: v.Player.DisplayText(cleaned, ctx),
ctx: ctx,
})
break
}
}
}
}
}
}
func xplHeartbeat() {
addrs, err := net.InterfaceAddrs()
if err != nil {
logger.Panicw("unable to get ip interfaces",
"err", err)
}
var addr string
for _, v := range addrs {
a := v.(*net.IPNet).IP.To4()
if a.IsGlobalUnicast() {
addr = a.String()
logger.Infow("setting xPL remote-ip",
"addr", addr)
break
}
}
port := strings.Split(xplPort.LocalAddr().String(), ":")[1]
for {
for _, v := range persistent.Clients {
sendXPL(xplMessage{
messageType: "xpl-stat",
target: "*",
schema: "hbeat.app",
body: map[string]string{
"interval": "1",
"port": port,
"remote-ip": addr,
},
}, "slimdev-slimserv."+v.Name)
}
time.Sleep(time.Minute)
}
}