-
Notifications
You must be signed in to change notification settings - Fork 17
/
events.go
191 lines (148 loc) · 6.37 KB
/
events.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package datatransfer
import (
"time"
)
// EventCode is a name for an event that occurs on a data transfer channel
type EventCode int
const (
// Open is an event occurs when a channel is first opened
Open EventCode = iota
// Accept is an event that emits when the data transfer is first accepted
Accept
// Restart is an event that emits when the data transfer is restarted
Restart
// DataReceived is emitted when data is received on the channel from a remote peer
DataReceived
// DataSent is emitted when data is sent on the channel to the remote peer
DataSent
// Cancel indicates one side has cancelled the transfer
Cancel
// Error is an event that emits when an error occurs in a data transfer
Error
// CleanupComplete emits when a request is cleaned up
CleanupComplete
// NewVoucher means we have a new voucher on this channel
NewVoucher
// NewVoucherResult means we have a new voucher result on this channel
NewVoucherResult
// PauseInitiator emits when the data sender pauses transfer
PauseInitiator
// ResumeInitiator emits when the data sender resumes transfer
ResumeInitiator
// PauseResponder emits when the data receiver pauses transfer
PauseResponder
// ResumeResponder emits when the data receiver resumes transfer
ResumeResponder
// FinishTransfer emits when the initiator has completed sending/receiving data
FinishTransfer
// ResponderCompletes emits when the initiator receives a message that the responder is finished
ResponderCompletes
// ResponderBeginsFinalization emits when the initiator receives a message that the responder is finilizing
ResponderBeginsFinalization
// BeginFinalizing emits when the responder completes its operations but awaits a response from the
// initiator
BeginFinalizing
// Disconnected emits when we are not able to connect to the other party
Disconnected
// Complete is emitted when a data transfer is complete
Complete
// CompleteCleanupOnRestart is emitted when a data transfer channel is restarted to signal
// that channels that were cleaning up should finish cleanup
CompleteCleanupOnRestart
// DataQueued is emitted when data is read and queued for sending to the remote peer
DataQueued
// DataQueuedProgress is emitted when a block is queued for sending to the
// remote peer. It is not emitted when the block is resent.
// It is used to measure progress of how much of the total data has been
// queued.
DataQueuedProgress
// DataSentProgress is emitted when a block is sent to the remote peer.
// It is not emitted when the block is resent.
// It is used to measure progress of how much of the total data has
// been sent.
DataSentProgress
// DataReceivedProgress is emitted the first time a block is received from
// the remote peer. It is used to measure progress of how much of the total
// data has been received.
DataReceivedProgress
// DEPRECATED in favour of RequestCancelled
RequestTimedOut
// SendDataError indicates that the transport layer had an error trying
// to send data to the remote peer
SendDataError
// ReceiveDataError indicates that the transport layer had an error
// receiving data from the remote peer
ReceiveDataError
// DEPRECATED in favor of TransferInitiated
TransferRequestQueued
// RequestCancelled indicates that a transport layer request was cancelled by the request opener
RequestCancelled
// Opened is fired when a request for data is sent from this node to a peer
Opened
// SetDataLimit is fired when a responder sets a limit for data it will allow
// before pausing the request
SetDataLimit
// SetRequiresFinalization is fired when a responder sets a limit for data it will allow
// before pausing the request
SetRequiresFinalization
// DataLimitExceeded is fired when a request exceeds it's data limit. It has the effect of
// pausing the responder, but is distinct from PauseResponder to indicate why the pause
// happened
DataLimitExceeded
// TransferInitiated indicates the transport has begun transferring data
TransferInitiated
// SendMessageError indicates an error sending a data transfer message
SendMessageError
)
// Events are human readable names for data transfer events
var Events = map[EventCode]string{
Open: "Open",
Accept: "Accept",
Restart: "Restart",
DataReceived: "DataReceived",
DataSent: "DataSent",
Cancel: "Cancel",
Error: "Error",
CleanupComplete: "CleanupComplete",
NewVoucher: "NewVoucher",
NewVoucherResult: "NewVoucherResult",
PauseInitiator: "PauseInitiator",
ResumeInitiator: "ResumeInitiator",
PauseResponder: "PauseResponder",
ResumeResponder: "ResumeResponder",
FinishTransfer: "FinishTransfer",
ResponderCompletes: "ResponderCompletes",
ResponderBeginsFinalization: "ResponderBeginsFinalization",
BeginFinalizing: "BeginFinalizing",
Disconnected: "Disconnected",
Complete: "Complete",
CompleteCleanupOnRestart: "CompleteCleanupOnRestart",
DataQueued: "DataQueued",
DataQueuedProgress: "DataQueuedProgress",
DataSentProgress: "DataSentProgress",
DataReceivedProgress: "DataReceivedProgress",
RequestTimedOut: "RequestTimedOut",
SendDataError: "SendDataError",
ReceiveDataError: "ReceiveDataError",
TransferRequestQueued: "TransferRequestQueued",
RequestCancelled: "RequestCancelled",
Opened: "Opened",
SetDataLimit: "SetDataLimit",
SetRequiresFinalization: "SetRequiresFinalization",
DataLimitExceeded: "DataLimitExceeded",
TransferInitiated: "TransferInitiated",
SendMessageError: "SendMessageError",
}
func (e EventCode) String() string {
return Events[e]
}
// Event is a struct containing information about a data transfer event
type Event struct {
Code EventCode // What type of event it is
Message string // Any clarifying information about the event
Timestamp time.Time // when the event happened
}
// Subscriber is a callback that is called when events are emitted
type Subscriber func(event Event, channelState ChannelState)
// Unsubscribe is a function that gets called to unsubscribe from data transfer events
type Unsubscribe func()