Repository navigation
Expand file tree
/
Copy pathsend.go
More file actions
103 lines (84 loc) · 1.67 KB
/
Copy pathsend.go
File metadata and controls
103 lines (84 loc) · 1.67 KB
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
package main
import (
"errors"
//"fmt"
"net/rpc"
)
func (n *Node) SendPut(addr string, key string, value Value) error {
// n_id := curr.id
// addr := curr.addr
client, err := rpc.Dial("tcp", addr)
if err != nil {
return err
}
defer client.Close()
args := RPCArgs{
Key: key,
Value: value,
}
var reply bool
err = client.Call("Node.RemotePut", args, &reply)
if err != nil {
return err
}
if !reply {
return errors.New("put failed")
}
return nil
}
func (n *Node) SendGet(addr string, key string) (Value, error) {
// addr := curr.addr
//fmt.Println("sent rpc")
// fmt.Println(addr)
client, err := rpc.Dial("tcp", addr)
if err != nil {
//fmt.Println("failed to open conn")
return Value{}, err
}
defer client.Close()
// args := RPCArgs{
// Key: key,
// Value: "",
// }
args := key
var reply RPCReply
err = client.Call("Node.RemoteGet", args, &reply)
if err != nil {
//fmt.Println("failed to rpc")
return Value{}, err
}
if reply.Exists {
return reply.Value, nil
} else {
//fmt.Println("failed to find key")
return Value{}, errors.New("key not found")
}
}
func (n *Node) SendDelete(addr string, key string) error {
// addr := curr.addr
// fmt.Println(n_id)
// fmt.Println(addr)
client, err := rpc.Dial("tcp", addr)
if err != nil {
//fmt.Println("failed to open conn")
return err
}
defer client.Close()
// args := RPCArgs{
// Key: key,
// Value: "",
// }
args := key
var reply bool
err = client.Call("Node.RemoteDelete", args, &reply)
if err != nil {
//fmt.Println("failed to rpc")
return err
}
if reply {
return nil
} else {
//fmt.Println("failed to find key")
return errors.New("key not found")
}
}