forked from bazil/bazil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerLocationSet.go
35 lines (31 loc) · 966 Bytes
/
peerLocationSet.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
package control
import (
"log"
"bazil.org/bazil/db"
"bazil.org/bazil/peer"
"bazil.org/bazil/server/control/wire"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
func (c controlRPC) PeerLocationSet(ctx context.Context, req *wire.PeerLocationSetRequest) (*wire.PeerLocationSetResponse, error) {
var pub peer.PublicKey
if err := pub.UnmarshalBinary(req.Pub); err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "bad peer public key: %v", err)
}
setLoc := func(tx *db.Tx) error {
p, err := tx.Peers().Get(&pub)
if err != nil {
return err
}
return p.Locations().Set(req.Netloc)
}
if err := c.app.DB.Update(setLoc); err != nil {
if err == db.ErrPeerNotFound {
return nil, grpc.Errorf(codes.InvalidArgument, "peer not found")
}
log.Printf("db error: setting peer addr: %v", err)
return nil, grpc.Errorf(codes.Internal, "database error")
}
return &wire.PeerLocationSetResponse{}, nil
}