-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
510 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package richkago | ||
|
||
import "testing" | ||
|
||
// TestNewController test example that builds a new controller | ||
func TestNewController(t *testing.T) { | ||
controller := NewController() | ||
if controller == nil { | ||
t.Error("controller is nil") | ||
return | ||
} | ||
} | ||
|
||
// TestController_UpdateProgress test example that updates progress | ||
func TestController_UpdateProgress(t *testing.T) { | ||
controller := NewController() | ||
controller.totalSize = 1000 | ||
|
||
controller.UpdateProgress(100, "1") | ||
if controller.Progress() != 10 { | ||
t.Error("progress is wrong", controller.Progress()) | ||
return | ||
} | ||
|
||
if controller.Status() != 1 { | ||
t.Error("status is wrong", controller.Status()) | ||
return | ||
} | ||
} | ||
|
||
// TestController_Pause test example that pause a progress | ||
func TestController_Pause(t *testing.T) { | ||
controller := NewController() | ||
controller.totalSize = 1000 | ||
|
||
controller.UpdateProgress(100, "1") | ||
if controller.Progress() != 10 { | ||
t.Error("progress is wrong", controller.Progress()) | ||
return | ||
} | ||
|
||
controller.Pause() | ||
if controller.Status() != -2 { | ||
t.Error("status is wrong", controller.Status()) | ||
return | ||
} | ||
} | ||
|
||
// TestController_Unpause test example that unpause a progress | ||
func TestController_Unpause(t *testing.T) { | ||
controller := NewController() | ||
controller.totalSize = 1000 | ||
|
||
controller.UpdateProgress(100, "1") | ||
if controller.Progress() != 10 { | ||
t.Error("progress is wrong", controller.Progress()) | ||
return | ||
} | ||
|
||
controller.Pause() | ||
if controller.Status() != -2 { | ||
t.Error("status is wrong", controller.Status()) | ||
return | ||
} | ||
|
||
controller.Unpause() | ||
if controller.Status() != 1 { | ||
t.Error("status is wrong", controller.Status()) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/ghinknet/richkago | ||
|
||
go 1.23.2 | ||
|
||
require golang.org/x/sync v0.9.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= | ||
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= |
Oops, something went wrong.