-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
command.go
84 lines (65 loc) · 1.36 KB
/
command.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
package ktable
import (
"github.com/bitmagnet-io/bitmagnet/internal/protocol/dht/ktable/btree"
"net/netip"
)
type Command interface {
exec(*table)
}
type CommandReturn[T any] interface {
Command
Query[T]
}
var _ CommandReturn[btree.PutResult] = PutNode{}
type PutNode struct {
ID ID
Addr netip.AddrPort
Options []NodeOption
}
func (c PutNode) execReturn(t *table) btree.PutResult {
if !c.Addr.IsValid() {
return btree.PutRejected
}
return t.nodes.put(c.ID, c.Addr, c.Options...)
}
func (c PutNode) exec(t *table) {
c.execReturn(t)
}
var _ CommandReturn[bool] = DropNode{}
type DropNode struct {
ID ID
Reason error
}
func (c DropNode) execReturn(t *table) bool {
return t.nodes.drop(c.ID, c.Reason)
}
func (c DropNode) exec(t *table) {
c.execReturn(t)
}
var _ CommandReturn[bool] = DropAddr{}
type DropAddr struct {
Addr netip.Addr
Reason error
}
func (c DropAddr) execReturn(t *table) bool {
id, ok := t.addrs.getPeerIDForAddr(c.Addr)
if !ok {
return false
}
return t.nodes.drop(id, c.Reason)
}
func (c DropAddr) exec(t *table) {
c.execReturn(t)
}
var _ CommandReturn[btree.PutResult] = PutHash{}
type PutHash struct {
ID ID
Peers []HashPeer
Options []HashOption
}
func (c PutHash) execReturn(t *table) btree.PutResult {
return t.hashes.put(c.ID, c.Peers, c.Options...)
}
func (c PutHash) exec(t *table) {
c.execReturn(t)
}