ipfs-cluster/rpc.go

82 lines
1.6 KiB
Go
Raw Normal View History

2016-12-02 18:33:39 +00:00
package ipfscluster
import cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
// ClusterRPC supported operations.
2016-12-02 18:33:39 +00:00
const (
PinRPC = iota
UnpinRPC
PinListRPC
IPFSPinRPC
IPFSUnpinRPC
VersionRPC
MemberListRPC
RollbackRPC
LeaderRPC
StatusPinnedRPC
StatusPinningRPC
StatusUnpinnedRPC
StatusUnpinningRPC
StatusPinErrorRPC
StatusUnPinErrorRPC
2016-12-02 18:33:39 +00:00
)
// RPCMethod identifies which RPC-supported operation we are trying to make
type RPCOp int
type ClusterRPC interface {
Op() RPCOp
ResponseCh() chan RPCResponse
}
2016-12-02 18:33:39 +00:00
type baseRPC struct {
method RPCOp
responseCh chan RPCResponse
}
// Method returns the RPC method for this request
func (brpc *baseRPC) Op() RPCOp {
return brpc.method
}
// ResponseCh returns a channel to send the RPCResponse
func (brpc *baseRPC) ResponseCh() chan RPCResponse {
return brpc.responseCh
}
// GenericClusterRPC is used to let Cluster perform operations as mandated by
2016-12-02 18:33:39 +00:00
// its ClusterComponents. The result is placed on the ResponseCh channel.
type GenericClusterRPC struct {
baseRPC
Arguments interface{}
}
type CidClusterRPC struct {
baseRPC
CID *cid.Cid
2016-12-02 18:33:39 +00:00
}
// RPC builds a ClusterRPC request.
func RPC(m RPCOp, args interface{}) ClusterRPC {
c, ok := args.(*cid.Cid)
if ok { // Its a CID
r := new(CidClusterRPC)
r.method = m
r.CID = c
r.responseCh = make(chan RPCResponse)
return r
2016-12-02 18:33:39 +00:00
}
// Its not a cid, make a generic
r := new(GenericClusterRPC)
r.method = m
r.Arguments = args
r.responseCh = make(chan RPCResponse)
return r
2016-12-02 18:33:39 +00:00
}
// RPC response carries the result of an ClusterRPC-requested operation
type RPCResponse struct {
Data interface{}
Error error
}