-
Notifications
You must be signed in to change notification settings - Fork 5
/
dagpool_server.go
128 lines (117 loc) · 4.58 KB
/
dagpool_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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package server
import (
"context"
"fmt"
"github.com/filedag-project/filedag-storage/dag/pool"
"github.com/filedag-project/filedag-storage/dag/pool/poolservice/dpuser"
"github.com/filedag-project/filedag-storage/dag/pool/poolservice/dpuser/upolicy"
"github.com/filedag-project/filedag-storage/dag/proto"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"golang.org/x/xerrors"
)
var log = logging.Logger("dag-pool-server")
var policyNotRight = fmt.Sprintf("policy is illegal, it should be: %v,%v,%v", upolicy.ReadOnly, upolicy.WriteOnly, upolicy.ReadWrite)
// DagPoolServer is used to implement DagPoolServer.
type DagPoolServer struct {
proto.UnimplementedDagPoolServer
DagPool pool.DagPool
}
//Add is used to add a block to the dag pool server
func (s *DagPoolServer) Add(ctx context.Context, in *proto.AddReq) (*proto.AddReply, error) {
data := blocks.NewBlock(in.GetBlock())
err := s.DagPool.Add(ctx, data, in.User.User, in.User.Password, in.Pin)
if err != nil {
return &proto.AddReply{Cid: cid.Undef.String()}, err
}
return &proto.AddReply{Cid: data.Cid().String()}, nil
}
//Get is used to get a block from the dag pool server
func (s *DagPoolServer) Get(ctx context.Context, in *proto.GetReq) (*proto.GetReply, error) {
cid, err := cid.Decode(in.Cid)
if err != nil {
return &proto.GetReply{Block: nil}, err
}
get, err := s.DagPool.Get(ctx, cid, in.User.User, in.User.Password)
if err != nil {
return &proto.GetReply{Block: nil}, err
}
return &proto.GetReply{Block: get.RawData()}, nil
}
//GetSize is used to get the size of the block
func (s *DagPoolServer) GetSize(ctx context.Context, in *proto.GetSizeReq) (*proto.GetSizeReply, error) {
cid, err := cid.Decode(in.Cid)
if err != nil {
return &proto.GetSizeReply{Size: 0}, err
}
size, err := s.DagPool.GetSize(ctx, cid, in.User.User, in.User.Password)
if err != nil {
return &proto.GetSizeReply{Size: 0}, err
}
return &proto.GetSizeReply{Size: int32(size)}, nil
}
//Remove is used to remove a block from the dag pool server
func (s *DagPoolServer) Remove(ctx context.Context, in *proto.RemoveReq) (*proto.RemoveReply, error) {
c, err := cid.Decode(in.Cid)
if err != nil {
return &proto.RemoveReply{Message: ""}, err
}
err = s.DagPool.Remove(ctx, c, in.User.User, in.User.Password, in.Unpin)
if err != nil {
return &proto.RemoveReply{Message: ""}, err
}
return &proto.RemoveReply{Message: c.String()}, nil
}
//AddUser is used to add a user to the dag pool server
func (s *DagPoolServer) AddUser(ctx context.Context, in *proto.AddUserReq) (*proto.AddUserReply, error) {
if !upolicy.CheckValid(in.Policy) {
return &proto.AddUserReply{Message: policyNotRight}, xerrors.Errorf(policyNotRight)
}
err := s.DagPool.AddUser(
dpuser.DagPoolUser{
Username: in.Username,
Password: in.Password,
Policy: upolicy.DagPoolPolicy(in.Policy),
Capacity: in.Capacity,
}, in.User.User, in.User.Password)
if err != nil {
return &proto.AddUserReply{Message: fmt.Sprintf("add user err:%v", err)}, err
}
return &proto.AddUserReply{Message: "ok"}, nil
}
//RemoveUser is used to remove a user from the dag pool server
func (s *DagPoolServer) RemoveUser(ctx context.Context, in *proto.RemoveUserReq) (*proto.RemoveUserReply, error) {
err := s.DagPool.RemoveUser(in.Username, in.User.User, in.User.Password)
if err != nil {
return &proto.RemoveUserReply{Message: fmt.Sprintf("del user err:%v", err)}, err
}
return &proto.RemoveUserReply{Message: "ok"}, nil
}
//QueryUser is used to query a user from the dag pool server
func (s *DagPoolServer) QueryUser(ctx context.Context, in *proto.QueryUserReq) (*proto.QueryUserReply, error) {
user, err := s.DagPool.QueryUser(in.Username, in.User.User, in.User.Password)
if err != nil {
return &proto.QueryUserReply{}, err
}
return &proto.QueryUserReply{Username: user.Username, Policy: string(user.Policy), Capacity: user.Capacity}, nil
}
//UpdateUser is used to update a user from the dag pool server
func (s *DagPoolServer) UpdateUser(ctx context.Context, in *proto.UpdateUserReq) (*proto.UpdateUserReply, error) {
user := dpuser.DagPoolUser{
Username: in.Username,
Password: in.NewPassword,
Capacity: in.NewCapacity,
}
if in.NewPolicy != "" {
if !upolicy.CheckValid(in.NewPolicy) {
return &proto.UpdateUserReply{Message: policyNotRight}, xerrors.Errorf(policyNotRight)
}
user.Policy = upolicy.DagPoolPolicy(in.NewPolicy)
}
err := s.DagPool.UpdateUser(user, in.User.User, in.User.Password)
if err != nil {
return &proto.UpdateUserReply{Message: fmt.Sprintf("update user err:%v", err)}, err
}
return &proto.UpdateUserReply{Message: "ok"}, nil
}