-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
58 lines (50 loc) · 1.74 KB
/
logger.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 node
import (
"encoding/json"
"io"
"sync"
"github.com/AutoRoute/node/internal"
"github.com/AutoRoute/node/types"
)
// A routingDecision contains identifying information
// for a particular packet and where it was sent
type routingDecision struct {
Dest types.NodeAddress
Next types.NodeAddress
PacketSize int
Amt int64
PacketHash types.PacketHash
}
// Logger is used for logging information about what
// the node knows about the network, where packets
// are being sent, and confirmation that the packets
// are were sent (their receipts). Ideally this is all
// the information required for an intelligent routing algorithm.
type Logger struct {
log_enc *json.Encoder
lock *sync.Mutex
}
func NewLogger(w io.Writer) Logger {
return Logger{json.NewEncoder(w), new(sync.Mutex)}
}
// LogBloomFilter logs the node's conglomerate bloom filter. Should be called
// every time a new bloom filter is received.
func (lgr Logger) LogBloomFilter(brm *internal.BloomReachabilityMap) error {
lgr.lock.Lock()
defer lgr.lock.Unlock()
return lgr.log_enc.Encode(brm.Conglomerate)
}
// LogRoutingDecision logs information on a sent packet. Should be called
// every time a packet is sent.
func (lgr Logger) LogRoutingDecision(dest types.NodeAddress, next types.NodeAddress, packet_size int, amt int64, packet_hash types.PacketHash) error {
lgr.lock.Lock()
defer lgr.lock.Unlock()
return lgr.log_enc.Encode(routingDecision{dest, next, packet_size, amt, packet_hash})
}
// LogPacketReceipt logs a packet hash from a received receipt. Should be
// called every time a receipt is received.
func (lgr Logger) LogPacketReceipt(packet_hash types.PacketHash) error {
lgr.lock.Lock()
defer lgr.lock.Unlock()
return lgr.log_enc.Encode(packet_hash)
}