forked from rand0x0m/chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.go
81 lines (67 loc) · 1.31 KB
/
dfs.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
package chord
import (
"errors"
"net"
)
type DFS struct {
N *Node
Config *Config
}
type Config struct {
BindAddr *net.TCPAddr
BootstrapAddr *net.TCPAddr
}
func (dfs *DFS) Put(value string) error {
if dfs.N.isBootstrap() {
return errors.New("disconnected")
} else {
succ, err := remoteFindSucc(dfs.N.Info, hashData(value))
if err != nil {
return err
}
return remotePut(succ, value)
}
}
func (dfs *DFS) Get(value string) (string, error) {
key := hashData(value)
if dfs.N.isBootstrap() {
return "", errors.New("disconnected")
} else {
succ, err := remoteFindSucc(dfs.N.Info, key)
if err != nil {
return "", err
}
return remoteGet(succ, key)
}
}
func (dfs *DFS) Delete(value string) error {
key := hashData(value)
if dfs.N.isBootstrap() {
return errors.New("disconnected")
} else {
succ, err := remoteFindSucc(dfs.N.Info, key)
if err != nil {
return err
}
return remoteDelete(succ, key)
}
}
func (dfs *DFS) Shutdown() {
dfs.N.shutdown()
}
func (dfs *DFS) Connect() error {
err := dfs.N.join(*dfs.Config.BootstrapAddr)
if err != nil {
return err
}
return nil
}
func NewDFS(config *Config) (*DFS, error) {
dfs := new(DFS)
dfs.N = NewNode(*config.BindAddr)
dfs.Config = config
if err := dfs.N.listen(); err != nil {
return nil, err
}
return dfs, nil
}