-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
73 lines (63 loc) · 1.46 KB
/
controller.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
package richkago
import "sync"
type Controller struct {
paused bool
excepted bool
totalSize int64
downloadedSize int64
downloadedSlices map[string]int64
mu sync.Mutex
}
// NewController build a new controller
func NewController() *Controller {
return &Controller{
downloadedSlices: make(map[string]int64),
}
}
// UpdateProgress update progress into controller
func (c *Controller) UpdateProgress(size int64, chunkID string) {
// Get lock
c.mu.Lock()
defer c.mu.Unlock()
if chunkID == "" && len(c.downloadedSlices) == 0 {
// Init variable
c.downloadedSize = size
} else {
// Update progress
c.downloadedSlices[chunkID] = size
c.downloadedSize = 0
// Sum up
for _, v := range c.downloadedSlices {
c.downloadedSize += v
}
}
}
// Pause pause a progress
func (c *Controller) Pause() {
c.paused = true
}
// Unpause unpause a progress
func (c *Controller) Unpause() {
c.paused = false
}
// Status gets a status of a controller
func (c *Controller) Status() int {
if c.downloadedSize == 0 && !c.excepted {
return -1 // Not started
} else if c.paused {
return -2 // Paused
} else if c.excepted {
return -3 // Excepted
} else if c.downloadedSize == c.totalSize {
return 0 // Done
} else {
return 1 // Downloading
}
}
// Progress gets progress of a controller
func (c *Controller) Progress() float64 {
if c.totalSize == 0 {
return -1
}
return float64(c.downloadedSize) / float64(c.totalSize) * 100
}