-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.go
128 lines (111 loc) · 3.21 KB
/
handlers.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
package handlers
import (
"bytes"
"encoding/binary"
"log"
"net"
"os"
"github.com/vmihailenco/msgpack"
c "github.com/DemonVex/hashgossip/consts"
"github.com/DemonVex/hashgossip/messenger"
"github.com/DemonVex/hashgossip/models"
"github.com/DemonVex/hashgossip/storages"
"github.com/DemonVex/hashgossip/transport"
)
type UdpHandler struct {
PeerStorage storage.PeerStorage
MessageStorage storage.MessageStorage
HashStorage storage.HashStorage
Gossiper messenger.Gossiper
}
func (u UdpHandler) Handler(src *net.UDPAddr, n int, buf []byte) {
header, body := buf[:c.PrefLen], buf[c.PrefLen:n]
switch {
case bytes.Equal(header, c.PrefMessage):
u.messageHandler(src, body)
case bytes.Equal(header, c.PrefWelcome):
u.welcomeHandler(src, body)
case bytes.Equal(header, c.PrefReport):
u.reportHandler(src, body)
case bytes.Equal(header, c.PrefMonitoring):
u.monitoringHandler(src, body)
case bytes.Equal(header, c.PrefShutdown):
u.shutdownHandler(src, body)
case bytes.Equal(header, c.PrefHello):
u.helloHandler(src, body)
}
}
func (u UdpHandler) messageHandler(src *net.UDPAddr, body []byte) {
var msg models.Message
err := msgpack.Unmarshal(body, &msg)
if err != nil {
log.Println("message unmarshal error ", err)
return
}
log.Printf("msg %+v...", msg.GetPayload()[0:5])
if u.saveMessage(msg) {
u.Gossiper.SendMessage(msg)
}
}
func (u UdpHandler) welcomeHandler(src *net.UDPAddr, body []byte) {
var wp models.WelcomePack
err := msgpack.Unmarshal(body, &wp)
if err != nil {
log.Println("welcome unmarshal error ", err)
return
}
u.PeerStorage.Merge(wp.PeerList)
if !wp.Msg.IsEmpty() {
log.Printf("welcome msg %+v...", wp.Msg.GetPayload()[0:5])
u.saveMessage(wp.Msg)
}
}
func (u UdpHandler) saveMessage(msg models.Message) bool {
if msg.IsValid() {
stored := u.MessageStorage.Set(msg)
if stored {
log.Printf("new message was set %+v", msg.GetPayload()[0:5])
}
newHash := u.HashStorage.Add(msg.GetHash())
return stored && newHash
} else {
log.Println("Invalid message")
}
return false
}
func (u UdpHandler) reportHandler(src *net.UDPAddr, body []byte) {
var msg models.Message
err := msgpack.Unmarshal(body, &msg)
if err != nil {
log.Println("report unmarshal error ", err)
return
}
log.Printf("%+v", msg)
}
func (u UdpHandler) monitoringHandler(src *net.UDPAddr, body []byte) {
peer := models.Peer{IP: src.IP, Port: binary.LittleEndian.Uint16(body)}
address := peer.ToString()
mb, err := msgpack.Marshal(u.MessageStorage.Get())
if err != nil {
log.Println("monitoring marshal error ", err)
return
}
payload := append(c.PrefReport, mb...)
transport.SendPayloadToUDP(address, payload)
}
func (u UdpHandler) helloHandler(src *net.UDPAddr, body []byte) {
peer := models.Peer{IP: src.IP, Port: binary.LittleEndian.Uint16(body)}
address := peer.ToString()
u.PeerStorage.Add(peer)
wb, err := msgpack.Marshal(models.WelcomePack{PeerList: u.PeerStorage.List(), Msg: u.MessageStorage.Get()})
if err != nil {
log.Println("hello marshal error ", err)
return
}
payload := append(c.PrefWelcome, wb...)
transport.SendPayloadToUDP(address, payload)
}
func (u UdpHandler) shutdownHandler(src *net.UDPAddr, body []byte) {
log.Println("got shutdown signal")
os.Exit(2)
}