使用 gTaskMg。

Signed-off-by: rick.chan <cy@haoan119.com>
This commit is contained in:
rick.chan 2024-08-02 15:18:11 +08:00
parent 798f3319d4
commit 96e1585a54
7 changed files with 52 additions and 172 deletions

View File

@ -1,12 +1,25 @@
package main
type Config struct{}
import (
"encoding/json"
"os"
"sync"
)
type ConfigJ struct {
Album ConfigAlbum `json:"album,omitempty"`
const (
FILE_CONFIG string = "./config.json"
)
type Config struct {
CfgJson
sync.Mutex
}
type ConfigAlbum struct {
type CfgJson struct {
Album CfgAlbum `json:"album,omitempty"`
}
type CfgAlbum struct {
Folder []AlbumFolder `json:"folder,omitempty"`
Virtual map[string]interface{} `json:"virtual,omitempty"`
}
@ -17,9 +30,36 @@ type AlbumFolder struct {
}
func (my *Config) Init() *Config {
my.Read()
return my
}
func (my *Config) Read() {}
func (my *Config) Read() (*CfgJson, error) {
b, err := os.ReadFile(FILE_CONFIG)
if err != nil {
return nil, err
}
my.Mutex.Lock()
err = json.Unmarshal(b, &my.CfgJson)
my.Mutex.Unlock()
if err != nil {
return nil, err
}
return &my.CfgJson, nil
}
func (my *Config) Write() {}
func (my *Config) Write(cfg *CfgJson) error {
if cfg != nil {
my.Mutex.Lock()
my.CfgJson = *cfg
} else {
my.Mutex.Lock()
}
b, err := json.Marshal(&my.CfgJson)
my.Mutex.Unlock()
if err != nil {
return err
}
return os.WriteFile(FILE_CONFIG, b, 0644)
}

View File

@ -1,85 +0,0 @@
package main
import (
"sync"
)
type GManager struct {
sync.WaitGroup
tlock sync.RWMutex
tasks map[string]*GTask
}
const (
GMSG_EXIT string = "GManager.Exit"
)
func (my *GManager) Init() *GManager {
my.tasks = make(map[string]*GTask)
return my
}
func (my *GManager) RegistTask(task *GTask, name string) {
my.tlock.Lock()
defer my.tlock.Unlock()
my.tasks[name] = task
}
func (my *GManager) CreateTask(name string, qsize int) *GTask {
t := new(GTask).Init(my, qsize)
t.GManager = my
my.RegistTask(t, name)
return t
}
func (my *GManager) Enter() {
my.WaitGroup.Add(1)
}
func (my *GManager) Exit() {
my.WaitGroup.Done()
}
func (my *GManager) Join() {
my.WaitGroup.Wait()
}
func (my *GManager) Broadcast(event interface{}) {
my.tlock.RLock()
defer my.tlock.RUnlock()
// Last to first.
for name := range my.tasks {
t := my.tasks[name]
t.EnQueueSync(event)
}
}
func (my *GManager) BroadcastWithout(event interface{}, without string) {
my.tlock.RLock()
defer my.tlock.RUnlock()
// Last to first.
for name := range my.tasks {
if without == name {
continue
}
t := my.tasks[name]
t.EnQueueSync(event)
}
}
func (my *GManager) ReqExit() {
my.Broadcast(GMSG_EXIT)
}
func (my *GManager) ReqTaskExit(name string) {
t, f := my.tasks[name]
if f {
t.EnQueueSync(GMSG_EXIT)
}
}
func (my *GManager) GetQueue(name string) (GQueue, bool) {
t, f := my.tasks[name]
return t.GQueue, f
}

View File

@ -1,45 +0,0 @@
package main
import (
"errors"
"time"
)
type GQueue chan interface{}
const (
ERR_GQUEUE_TIMEOUT string = "gqueue timeout"
)
func (my *GQueue) Init(size int) *GQueue {
*my = make(chan interface{}, size)
return my
}
func (my *GQueue) EnQueueSync(itm interface{}) {
*my <- itm
}
func (my *GQueue) EnQueue(itm interface{}, timeout time.Duration) error {
select {
case *my <- itm:
return nil
case <-time.After(timeout):
return errors.New(ERR_GQUEUE_TIMEOUT)
}
}
func (my *GQueue) DeQueueSync() interface{} {
itm := <-*my
return itm
}
func (my *GQueue) DeQueue(timeout time.Duration) (interface{}, error) {
var itm interface{}
select {
case itm = <-*my:
return itm, nil
case <-time.After(timeout):
return nil, errors.New(ERR_GQUEUE_TIMEOUT)
}
}

View File

@ -1,35 +0,0 @@
package main
import (
"sync"
)
type GTask struct {
GQueue
sync.WaitGroup
*GManager
}
func (my *GTask) Init(manager *GManager, qsize int) *GTask {
my.GManager = manager
my.GQueue.Init(qsize)
return my
}
func (my *GTask) Enter() {
if my.GManager != nil {
my.GManager.Enter()
}
my.WaitGroup.Add(1)
}
func (my *GTask) Exit() {
my.WaitGroup.Done()
if my.GManager != nil {
my.GManager.Exit()
}
}
func (my *GTask) Join() {
my.WaitGroup.Wait()
}

3
MediaList.go Normal file
View File

@ -0,0 +1,3 @@
package main
type MediaList struct{}

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.22.3
require (
blacktea.vip.cpolar.top/OrgGo/simplelog v1.0.5
fyne.io/fyne/v2 v2.5.0
github.com/lion187chen/gTaskMg v0.0.0-20240802065247-3ec44796cf98
)
require (

View File

@ -3,10 +3,11 @@ package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"github.com/lion187chen/gTaskMg"
)
type MGCore struct {
*GManager
*gTaskMg.GManager
*Loger
fyne.App