Skip to content

Commit

Permalink
first version: v0.0.1 Alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
bigsk05 committed Nov 20, 2024
1 parent 7efcb7a commit d974065
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 23 deletions.
50 changes: 27 additions & 23 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ $RECYCLE.BIN/
.LSOverride

# Icon must end with two \r
Icon
Icon


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

# Whole .idea
.idea

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
#.idea/**/workspace.xml
#.idea/**/tasks.xml
#.idea/**/usage.statistics.xml
#.idea/**/dictionaries
#.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml
#.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml
#.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
#.idea/**/dataSources/
#.idea/**/dataSources.ids
#.idea/**/dataSources.local.xml
#.idea/**/sqlDataSources.xml
#.idea/**/dynamic.xml
#.idea/**/uiDesigner.xml
#.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries
#.idea/**/gradle.xml
#.idea/**/libraries

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

# Mongo Explorer plugin
.idea/**/mongoSettings.xml
#.idea/**/mongoSettings.xml

# File-based project format
*.iws
Expand All @@ -537,16 +541,16 @@ cmake-build-*/
out/

# mpeltonen/sbt-idea plugin
.idea_modules/
#.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml
#.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/
#.idea/sonarlint/

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

# Editor-based Rest Client
.idea/httpRequests
#.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
#.idea/caches/build_file_checksums.ser

73 changes: 73 additions & 0 deletions controller.go
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
}
71 changes: 71 additions & 0 deletions controller_test.go
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
}
}
5 changes: 5 additions & 0 deletions go.mod
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
2 changes: 2 additions & 0 deletions go.sum
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=
Loading

0 comments on commit d974065

Please sign in to comment.