-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
67 lines (58 loc) · 931 Bytes
/
server.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
package als
import (
"fmt"
"log"
"net"
"net/http"
"net/rpc"
)
type ALS struct {
db *DB
Quit chan struct{}
}
type GetArgs struct {
Key []byte
}
type GetReply struct {
Val []byte
}
func (als *ALS) GetKey(args *GetArgs, reply *GetReply) error {
val, err := als.db.Get(args.Key)
if err != nil {
return err
}
*reply = GetReply{Val: val}
return nil
}
//type SetArgs struct {
// key []byte
//}
//
//type SetReply struct {
// val []byte
//}
//
//func (als *ALS) SetKey() {
//
//}
func (als *ALS) server() {
rpc.Register(als)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":9527")
if e != nil {
log.Fatal("listen error:", e)
}
go http.Serve(l, nil)
}
func Clear(als *ALS) {
als.db.Close()
}
func MakeServer(dbfile string) *ALS {
quit := make(chan struct{})
als := ALS{db: &DB{}, Quit: quit}
if err := als.db.Init(dbfile); err != nil {
fmt.Printf("db init error: %v", err)
}
als.server()
return &als
}