forked from PretendoNetwork/nex-protocols-go
/
health.go
146 lines (112 loc) · 4.43 KB
/
health.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
package nexproto
import (
"fmt"
nex "github.com/PretendoNetwork/nex-go"
)
const (
// HealthProtocolID is the protocol ID for the Health protocol
HealthProtocolID = 0x12
// HealthMethodPingDaemon is the method ID for the method PingDaemon
HealthMethodPingDaemon = 0x1
// HealthMethodPingDatabase is the method ID for the method PingDatabase
HealthMethodPingDatabase = 0x2
// HealthMethodRunSanityCheck is the method ID for the method RunSanityCheck
HealthMethodRunSanityCheck = 0x3
// HealthMethodFixSanityErrors is the method ID for the method FixSanityErrors
HealthMethodFixSanityErrors = 0x4
)
// HealthProtocol handles the Health protocol
type HealthProtocol struct {
server *nex.Server
PingDaemonHandler func(err error, client *nex.Client, callID uint32)
PingDatabaseHandler func(err error, client *nex.Client, callID uint32)
RunSanityCheckHandler func(err error, client *nex.Client, callID uint32)
FixSanityErrorsHandler func(err error, client *nex.Client, callID uint32)
}
// Setup initializes the protocol
func (healthProtocol *HealthProtocol) Setup() {
nexServer := healthProtocol.server
nexServer.On("Data", func(packet nex.PacketInterface) {
request := packet.RMCRequest()
if HealthProtocolID == request.ProtocolID() {
switch request.MethodID() {
case HealthMethodPingDaemon:
go healthProtocol.handlePingDaemon(packet)
case HealthMethodPingDatabase:
go healthProtocol.handlePingDatabase(packet)
case HealthMethodRunSanityCheck:
go healthProtocol.handleRunSanityCheck(packet)
case HealthMethodFixSanityErrors:
go healthProtocol.handleFixSanityErrors(packet)
default:
fmt.Printf("Unsupported Health method ID: %#v\n", request.MethodID())
}
}
})
}
func (healthProtocol *HealthProtocol) handlePingDaemon(packet nex.PacketInterface) {
if healthProtocol.PingDaemonHandler == nil {
logger.Warning("HealthProtocol::PingDaemon not implemented")
go respondNotImplemented(packet, HealthProtocolID)
return
}
client := packet.Sender()
request := packet.RMCRequest()
callID := request.CallID()
go healthProtocol.PingDaemonHandler(nil, client, callID)
}
func (healthProtocol *HealthProtocol) handlePingDatabase(packet nex.PacketInterface) {
if healthProtocol.PingDatabaseHandler == nil {
logger.Warning("HealthProtocol::PingDatabase not implemented")
go respondNotImplemented(packet, HealthProtocolID)
return
}
client := packet.Sender()
request := packet.RMCRequest()
callID := request.CallID()
go healthProtocol.PingDatabaseHandler(nil, client, callID)
}
func (healthProtocol *HealthProtocol) handleRunSanityCheck(packet nex.PacketInterface) {
if healthProtocol.RunSanityCheckHandler == nil {
logger.Warning("HealthProtocol::RunSanityCheck not implemented")
go respondNotImplemented(packet, HealthProtocolID)
return
}
client := packet.Sender()
request := packet.RMCRequest()
callID := request.CallID()
go healthProtocol.RunSanityCheckHandler(nil, client, callID)
}
func (healthProtocol *HealthProtocol) handleFixSanityErrors(packet nex.PacketInterface) {
if healthProtocol.FixSanityErrorsHandler == nil {
logger.Warning("HealthProtocol::FixSanityErrors not implemented")
go respondNotImplemented(packet, HealthProtocolID)
return
}
client := packet.Sender()
request := packet.RMCRequest()
callID := request.CallID()
go healthProtocol.FixSanityErrorsHandler(nil, client, callID)
}
// PingDaemon sets the PingDaemon handler function
func (healthProtocol *HealthProtocol) PingDaemon(handler func(err error, client *nex.Client, callID uint32)) {
healthProtocol.PingDaemonHandler = handler
}
// PingDatabase sets the PingDatabase handler function
func (healthProtocol *HealthProtocol) PingDatabase(handler func(err error, client *nex.Client, callID uint32)) {
healthProtocol.PingDatabaseHandler = handler
}
// RunSanityCheck sets the RunSanityCheck handler function
func (healthProtocol *HealthProtocol) RunSanityCheck(handler func(err error, client *nex.Client, callID uint32)) {
healthProtocol.RunSanityCheckHandler = handler
}
// FixSanityErrors sets the FixSanityErrors handler function
func (healthProtocol *HealthProtocol) FixSanityErrors(handler func(err error, client *nex.Client, callID uint32)) {
healthProtocol.FixSanityErrorsHandler = handler
}
// NewHealthProtocol returns a new HealthProtocol
func NewHealthProtocol(server *nex.Server) *HealthProtocol {
healthProtocol := &HealthProtocol{server: server}
healthProtocol.Setup()
return healthProtocol
}