forked from fujiwara/go-simple-kvs
/
kvs.go
79 lines (67 loc) · 1.39 KB
/
kvs.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
package kvs
import(
"rpc";
"http";
"log";
"net";
"os";
)
type Server struct {
storage map[string] string;
}
type Args struct {
Key string;
Value string;
}
type Reply struct {
Value string;
}
func RunServer(addr string) {
self := new(Server).Init();
rpc.Register(self);
rpc.HandleHTTP();
l, e := net.Listen("tcp", addr);
if e != nil {
log.Exit("listen error: ", e);
}
http.Serve(l, nil);
}
func (self *Server) Init() *Server {
self.storage = make(map[string] string);
return self;
}
func (self *Server) Get(args *Args, reply *Reply) os.Error {
if value, ok := self.storage[args.Key]; ok {
reply.Value = value;
}
return nil;
}
func (self *Server) Set(args *Args, reply *Reply) os.Error {
self.storage[args.Key] = args.Value;
reply.Value = "ok";
return nil;
}
type Client struct {
client *rpc.Client;
}
func NewClient(addr string) (*Client, os.Error) {
self := new(Client);
client, err := rpc.DialHTTP("tcp", addr);
self.client = client;
return self, err;
}
func (c *Client) Get(key string) (string, os.Error) {
reply := new(Reply);
args := new(Args);
args.Key = key;
err := c.client.Call("Server.Get", args, reply);
return reply.Value, err;
}
func (c *Client) Set(key string, value string) (string, os.Error) {
reply := new(Reply);
args := new(Args);
args.Key = key;
args.Value = value;
err := c.client.Call("Server.Set", args, reply);
return reply.Value, err;
}