Skip to content

Commit d974065

Browse files
committed
first version: v0.0.1 Alpha
1 parent 7efcb7a commit d974065

File tree

7 files changed

+510
-23
lines changed

7 files changed

+510
-23
lines changed

.gitignore

+27-23
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ $RECYCLE.BIN/
4646
.LSOverride
4747

4848
# Icon must end with two \r
49-
Icon
49+
Icon
50+
5051

5152
# Thumbnails
5253
._*
@@ -485,31 +486,34 @@ FodyWeavers.xsd
485486
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
486487
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
487488

489+
# Whole .idea
490+
.idea
491+
488492
# User-specific stuff
489-
.idea/**/workspace.xml
490-
.idea/**/tasks.xml
491-
.idea/**/usage.statistics.xml
492-
.idea/**/dictionaries
493-
.idea/**/shelf
493+
#.idea/**/workspace.xml
494+
#.idea/**/tasks.xml
495+
#.idea/**/usage.statistics.xml
496+
#.idea/**/dictionaries
497+
#.idea/**/shelf
494498

495499
# AWS User-specific
496-
.idea/**/aws.xml
500+
#.idea/**/aws.xml
497501

498502
# Generated files
499-
.idea/**/contentModel.xml
503+
#.idea/**/contentModel.xml
500504

501505
# Sensitive or high-churn files
502-
.idea/**/dataSources/
503-
.idea/**/dataSources.ids
504-
.idea/**/dataSources.local.xml
505-
.idea/**/sqlDataSources.xml
506-
.idea/**/dynamic.xml
507-
.idea/**/uiDesigner.xml
508-
.idea/**/dbnavigator.xml
506+
#.idea/**/dataSources/
507+
#.idea/**/dataSources.ids
508+
#.idea/**/dataSources.local.xml
509+
#.idea/**/sqlDataSources.xml
510+
#.idea/**/dynamic.xml
511+
#.idea/**/uiDesigner.xml
512+
#.idea/**/dbnavigator.xml
509513

510514
# Gradle
511-
.idea/**/gradle.xml
512-
.idea/**/libraries
515+
#.idea/**/gradle.xml
516+
#.idea/**/libraries
513517

514518
# Gradle and Maven with auto-import
515519
# When using Gradle or Maven with auto-import, you should exclude module files,
@@ -528,7 +532,7 @@ FodyWeavers.xsd
528532
cmake-build-*/
529533

530534
# Mongo Explorer plugin
531-
.idea/**/mongoSettings.xml
535+
#.idea/**/mongoSettings.xml
532536

533537
# File-based project format
534538
*.iws
@@ -537,16 +541,16 @@ cmake-build-*/
537541
out/
538542

539543
# mpeltonen/sbt-idea plugin
540-
.idea_modules/
544+
#.idea_modules/
541545

542546
# JIRA plugin
543547
atlassian-ide-plugin.xml
544548

545549
# Cursive Clojure plugin
546-
.idea/replstate.xml
550+
#.idea/replstate.xml
547551

548552
# SonarLint plugin
549-
.idea/sonarlint/
553+
#.idea/sonarlint/
550554

551555
# Crashlytics plugin (for Android Studio and IntelliJ)
552556
com_crashlytics_export_strings.xml
@@ -555,8 +559,8 @@ crashlytics-build.properties
555559
fabric.properties
556560

557561
# Editor-based Rest Client
558-
.idea/httpRequests
562+
#.idea/httpRequests
559563

560564
# Android studio 3.1+ serialized cache file
561-
.idea/caches/build_file_checksums.ser
565+
#.idea/caches/build_file_checksums.ser
562566

controller.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package richkago
2+
3+
import "sync"
4+
5+
type Controller struct {
6+
paused bool
7+
excepted bool
8+
totalSize int64
9+
downloadedSize int64
10+
downloadedSlices map[string]int64
11+
mu sync.Mutex
12+
}
13+
14+
// NewController build a new controller
15+
func NewController() *Controller {
16+
return &Controller{
17+
downloadedSlices: make(map[string]int64),
18+
}
19+
}
20+
21+
// UpdateProgress update progress into controller
22+
func (c *Controller) UpdateProgress(size int64, chunkID string) {
23+
// Get lock
24+
c.mu.Lock()
25+
defer c.mu.Unlock()
26+
27+
if chunkID == "" && len(c.downloadedSlices) == 0 {
28+
// Init variable
29+
c.downloadedSize = size
30+
} else {
31+
// Update progress
32+
c.downloadedSlices[chunkID] = size
33+
c.downloadedSize = 0
34+
// Sum up
35+
for _, v := range c.downloadedSlices {
36+
c.downloadedSize += v
37+
}
38+
}
39+
}
40+
41+
// Pause pause a progress
42+
func (c *Controller) Pause() {
43+
c.paused = true
44+
}
45+
46+
// Unpause unpause a progress
47+
func (c *Controller) Unpause() {
48+
c.paused = false
49+
}
50+
51+
// Status gets a status of a controller
52+
func (c *Controller) Status() int {
53+
if c.downloadedSize == 0 && !c.excepted {
54+
return -1 // Not started
55+
} else if c.paused {
56+
return -2 // Paused
57+
} else if c.excepted {
58+
return -3 // Excepted
59+
} else if c.downloadedSize == c.totalSize {
60+
return 0 // Done
61+
} else {
62+
return 1 // Downloading
63+
}
64+
}
65+
66+
// Progress gets progress of a controller
67+
func (c *Controller) Progress() float64 {
68+
if c.totalSize == 0 {
69+
return -1
70+
}
71+
72+
return float64(c.downloadedSize) / float64(c.totalSize) * 100
73+
}

controller_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package richkago
2+
3+
import "testing"
4+
5+
// TestNewController test example that builds a new controller
6+
func TestNewController(t *testing.T) {
7+
controller := NewController()
8+
if controller == nil {
9+
t.Error("controller is nil")
10+
return
11+
}
12+
}
13+
14+
// TestController_UpdateProgress test example that updates progress
15+
func TestController_UpdateProgress(t *testing.T) {
16+
controller := NewController()
17+
controller.totalSize = 1000
18+
19+
controller.UpdateProgress(100, "1")
20+
if controller.Progress() != 10 {
21+
t.Error("progress is wrong", controller.Progress())
22+
return
23+
}
24+
25+
if controller.Status() != 1 {
26+
t.Error("status is wrong", controller.Status())
27+
return
28+
}
29+
}
30+
31+
// TestController_Pause test example that pause a progress
32+
func TestController_Pause(t *testing.T) {
33+
controller := NewController()
34+
controller.totalSize = 1000
35+
36+
controller.UpdateProgress(100, "1")
37+
if controller.Progress() != 10 {
38+
t.Error("progress is wrong", controller.Progress())
39+
return
40+
}
41+
42+
controller.Pause()
43+
if controller.Status() != -2 {
44+
t.Error("status is wrong", controller.Status())
45+
return
46+
}
47+
}
48+
49+
// TestController_Unpause test example that unpause a progress
50+
func TestController_Unpause(t *testing.T) {
51+
controller := NewController()
52+
controller.totalSize = 1000
53+
54+
controller.UpdateProgress(100, "1")
55+
if controller.Progress() != 10 {
56+
t.Error("progress is wrong", controller.Progress())
57+
return
58+
}
59+
60+
controller.Pause()
61+
if controller.Status() != -2 {
62+
t.Error("status is wrong", controller.Status())
63+
return
64+
}
65+
66+
controller.Unpause()
67+
if controller.Status() != 1 {
68+
t.Error("status is wrong", controller.Status())
69+
return
70+
}
71+
}

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/ghinknet/richkago
2+
3+
go 1.23.2
4+
5+
require golang.org/x/sync v0.9.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
2+
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=

0 commit comments

Comments
 (0)