/
server.go
58 lines (49 loc) · 1.16 KB
/
server.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
package main
import (
"fmt"
"net"
"time"
)
type RegistrationStatus int
const (
RegistrationStatusPending RegistrationStatus = iota // Server is in process of registering
RegistrationStatusSuccess // Server has succeeded the registration process
RegistrationStatusInvalid // Server has become unresponsive
)
type ServerInfo struct {
IP net.IP
Port int
Name string
Version int
Key int
}
func NewServerInfo(ip net.IP, port int, name string, version int, key int) *ServerInfo {
return &ServerInfo {
IP: ip,
Port: port,
Name: name,
Version: version,
Key: key,
}
}
type ServerRecord struct {
Info *ServerInfo
Status RegistrationStatus
LastPong time.Time
}
func NewServerRecord(serverInfo *ServerInfo) *ServerRecord {
return &ServerRecord {
Info: serverInfo,
Status: RegistrationStatusPending,
LastPong: time.Now(),
}
}
func (sr *ServerRecord) TimeSincePong() float64 {
return time.Since(sr.LastPong).Minutes()
}
func (sr *ServerRecord) AddServerString() string {
return fmt.Sprintf("addserver %s %d", sr.Info.IP.String(), sr.Info.Port)
}
func (sr *ServerRecord) GetID() string {
return fmt.Sprintf("%s:%d", sr.Info.IP.String(), sr.Info.Port)
}