storj/pkg/piecestore/rpc/client/client.go
Kaloyan Raev 916e0b0ee0 gRPC Ranger (#44)
* Added piecestore

* gofmt

* Added requested changes

* Added cli

* Removed ranger because I wanted something that can stand alone

* Add example of http server using piece store

* Changed piecestore code to make it more optial for error handelling

* Merged with piecestore

* Added missing package

* Forgot io import

* gofmt

* gofmt

* Forgot io

* Make path by hash exported

* updated to simplify again whoops

* Updated server to work real good

* Forgot ampersand

* Updated to match FilePiece

* Merged in cam's delete code

* Remove unused io

* Added RPC code

* Give the download request a reader

* Removed http server stuff; changed receive stream to say io.reader

* Added expiration date to shardInfo

* gRPC Ranger

* Change all instances of Shard to Piece; change protobuf name; moved client insance to outside functions

* Adapt to latest changes in piece store rpc api

* added ttl info request

* Initialize grpcRanger type with named fields

* Move scripts to http server pr; added close method for Retrieve api

* added rpc server tests for getting piece meta data and retrieval routes

* Adapt to PieceStreamReader now being a ReadCloser

* Resolved linter errors, moved to prc server to pkg, updated go.mod to use latest protobuf

* Imported cams test

* Bump gometalinter deadline

* Adapt to package name changes

* Remove Garbage

* Adapt to latest changes in piece store rpc api

* NewCustomRoute constructor to allow mocking the gRPC client

* Name struct values in constructor.
2018-06-02 22:20:17 -06:00

73 lines
2.0 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package client
import (
"fmt"
"io"
"log"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "storj.io/storj/protos/piecestore"
)
// Client -- Struct Info needed for protobuf api calls
type Client struct {
ctx context.Context
route pb.PieceStoreRoutesClient
}
// New -- Initilize Client
func New(ctx context.Context, conn *grpc.ClientConn) *Client {
return &Client{ctx: ctx, route: pb.NewPieceStoreRoutesClient(conn)}
}
// NewCustomRoute creates new Client with custom route interface
func NewCustomRoute(ctx context.Context, route pb.PieceStoreRoutesClient) *Client {
return &Client{ctx: ctx, route: route}
}
// PieceMetaRequest -- Request info about a piece by Id
func (client *Client) PieceMetaRequest(id string) (*pb.PieceSummary, error) {
return client.route.Piece(client.ctx, &pb.PieceId{Id: id})
}
// StorePieceRequest -- Upload Piece to Server
func (client *Client) StorePieceRequest(id string, dataOffset int64, length int64, ttl int64, storeOffset int64) (io.WriteCloser, error) {
stream, err := client.route.Store(client.ctx)
if err != nil {
return nil, err
}
// SSend preliminary data
if err := stream.Send(&pb.PieceStore{Id: id, Size: length, Ttl: ttl, StoreOffset: storeOffset}); err != nil {
stream.CloseAndRecv()
return nil, fmt.Errorf("%v.Send() = %v", stream, err)
}
return &StreamWriter{stream: stream}, err
}
// RetrievePieceRequest -- Begin Download Piece from Server
func (client *Client) RetrievePieceRequest(id string, offset int64, length int64) (io.ReadCloser, error) {
stream, err := client.route.Retrieve(client.ctx, &pb.PieceRetrieval{Id: id, Size: length, StoreOffset: offset})
if err != nil {
return nil, err
}
return &StreamReader{stream: stream}, nil
}
// DeletePieceRequest -- Delete Piece From Server
func (client *Client) DeletePieceRequest(id string) error {
reply, err := client.route.Delete(client.ctx, &pb.PieceDelete{Id: id})
if err != nil {
return err
}
log.Printf("Route summary : %v", reply)
return nil
}