/
transfer_response.go
127 lines (106 loc) · 3.66 KB
/
transfer_response.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
package message1_1
import (
"bytes"
"io"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/datamodel"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/libp2p/go-libp2p-core/protocol"
cbg "github.com/whyrusleeping/cbor-gen"
xerrors "golang.org/x/xerrors"
datatransfer "github.com/filecoin-project/go-data-transfer"
"github.com/filecoin-project/go-data-transfer/encoding"
"github.com/filecoin-project/go-data-transfer/message/types"
)
//go:generate cbor-gen-for --map-encoding TransferResponse1_1
// TransferResponse1_1 is a private struct that satisfies the datatransfer.Response interface
// It is the response message for the Data Transfer 1.1 and 1.2 Protocol.
type TransferResponse1_1 struct {
Type uint64
Acpt bool
Paus bool
XferID uint64
VRes *cbg.Deferred
VTyp datatransfer.TypeIdentifier
}
func (trsp *TransferResponse1_1) TransferID() datatransfer.TransferID {
return datatransfer.TransferID(trsp.XferID)
}
// IsRequest always returns false in this case because this is a transfer response
func (trsp *TransferResponse1_1) IsRequest() bool {
return false
}
// IsNew returns true if this is the first response sent
func (trsp *TransferResponse1_1) IsNew() bool {
return trsp.Type == uint64(types.NewMessage)
}
// IsUpdate returns true if this response is an update
func (trsp *TransferResponse1_1) IsUpdate() bool {
return trsp.Type == uint64(types.UpdateMessage)
}
// IsPaused returns true if the responder is paused
func (trsp *TransferResponse1_1) IsPaused() bool {
return trsp.Paus
}
// IsCancel returns true if the responder has cancelled this response
func (trsp *TransferResponse1_1) IsCancel() bool {
return trsp.Type == uint64(types.CancelMessage)
}
// IsComplete returns true if the responder has completed this response
func (trsp *TransferResponse1_1) IsComplete() bool {
return trsp.Type == uint64(types.CompleteMessage)
}
func (trsp *TransferResponse1_1) IsVoucherResult() bool {
return trsp.Type == uint64(types.VoucherResultMessage) || trsp.Type == uint64(types.NewMessage) || trsp.Type == uint64(types.CompleteMessage) ||
trsp.Type == uint64(types.RestartMessage)
}
// Accepted returns true if the request is accepted in the response
func (trsp *TransferResponse1_1) Accepted() bool {
return trsp.Acpt
}
func (trsp *TransferResponse1_1) VoucherResultType() datatransfer.TypeIdentifier {
return trsp.VTyp
}
func (trsp *TransferResponse1_1) VoucherResult(decoder encoding.Decoder) (encoding.Encodable, error) {
if trsp.VRes == nil {
return nil, xerrors.New("No voucher present to read")
}
return decoder.DecodeFromCbor(trsp.VRes.Raw)
}
func (trq *TransferResponse1_1) IsRestart() bool {
return trq.Type == uint64(types.RestartMessage)
}
func (trsp *TransferResponse1_1) EmptyVoucherResult() bool {
return trsp.VTyp == datatransfer.EmptyTypeIdentifier
}
func (trsp *TransferResponse1_1) MessageForProtocol(targetProtocol protocol.ID) (datatransfer.Message, error) {
switch targetProtocol {
case datatransfer.ProtocolDataTransfer1_2:
return trsp, nil
default:
return nil, xerrors.Errorf("protocol %s not supported", targetProtocol)
}
}
func (trsp *TransferResponse1_1) ToIPLD() (datamodel.Node, error) {
buf := new(bytes.Buffer)
err := trsp.ToNet(buf)
if err != nil {
return nil, err
}
nb := basicnode.Prototype.Any.NewBuilder()
err = dagcbor.Decode(nb, buf)
if err != nil {
return nil, err
}
return nb.Build(), nil
}
// ToNet serializes a transfer response. It's a wrapper for MarshalCBOR to provide
// symmetry with FromNet
func (trsp *TransferResponse1_1) ToNet(w io.Writer) error {
msg := TransferMessage1_1{
IsRq: false,
Request: nil,
Response: trsp,
}
return msg.MarshalCBOR(w)
}