-
Notifications
You must be signed in to change notification settings - Fork 216
/
handler.go
40 lines (32 loc) · 934 Bytes
/
handler.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
package p2p
import (
"encoding/json"
"go.uber.org/zap"
)
type Handler interface {
Handle(envelope *Envelope)
}
type HandlerFunc func(envelope *Envelope)
func (f HandlerFunc) Handle(envelope *Envelope) {
f(envelope)
}
// LoggerHandler logs the messages back and forth.
var LoggerHandler = HandlerFunc(func(envelope *Envelope) {
data, err := json.Marshal(envelope)
if err != nil {
logErr("Marshal err", err)
return
}
p2pLog.Info("handler", zap.String("message", string(data)))
})
// StringLoggerHandler simply prints the messages as they go through the client.
var StringLoggerHandler = HandlerFunc(func(envelope *Envelope) {
name, _ := envelope.Packet.Type.Name()
p2pLog.Info(
"handler Packet",
zap.String("name", name),
zap.String("sender", envelope.Sender.Address),
zap.String("receiver", envelope.Receiver.Address),
zap.Stringer("msg", envelope.Packet.P2PMessage), // this will use by String()
)
})