-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreceiver.go
60 lines (55 loc) · 1.51 KB
/
receiver.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
package pump
import (
"io"
)
type Receiver struct {
writers map[Object]io.WriterAt
chunkDecoders map[Chunk]*chunkDecoder
finishedChunks map[Chunk]struct{}
}
func NewReceiver() *Receiver {
return &Receiver{
writers: make(map[Object]io.WriterAt),
chunkDecoders: make(map[Chunk]*chunkDecoder),
finishedChunks: make(map[Chunk]struct{}),
}
}
func (rx *Receiver) PrepareForReception(o Object, w io.WriterAt) {
rx.writers[o] = w
}
func (rx *Receiver) Receive(packet Packet) {
if _, registered := rx.writers[packet.Chunk.Object]; !registered {
return
}
if _, alreadyFinished := rx.finishedChunks[packet.Chunk]; alreadyFinished {
return
}
if _, present := rx.chunkDecoders[packet.Chunk]; !present {
rx.chunkDecoders[packet.Chunk] = packet.Chunk.decoder()
}
if rx.chunkDecoders[packet.Chunk].ingest(packet) {
rx.writers[packet.Chunk.Object].WriteAt(rx.chunkDecoders[packet.Chunk].data(), packet.Chunk.Offset)
rx.finishedChunks[packet.Chunk] = struct{}{}
delete(rx.chunkDecoders, packet.Chunk)
}
}
func (rx *Receiver) Idle() bool {
if len(rx.completedObjects()) == len(rx.writers) {
return true
}
return false
}
func (rx *Receiver) completedObjects() (completedObjects []Object) {
for o := range rx.writers {
if o.isCompletedBy(rx.finishedChunkList()) {
completedObjects = append(completedObjects, o)
}
}
return
}
func (rx *Receiver) finishedChunkList() (finishedChunkList []Chunk) {
for c := range rx.finishedChunks {
finishedChunkList = append(finishedChunkList, c)
}
return
}