Skip to content

Commit

Permalink
refactor: state, bus, main
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Apr 25, 2022
1 parent 9a5edf0 commit 08c0d1a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
12 changes: 3 additions & 9 deletions core/bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bus

import (
"context"
"fmt"

"github.com/ItsNotGoodName/reciva-web-remote/core/radio"
"github.com/ItsNotGoodName/reciva-web-remote/core/state"
Expand Down Expand Up @@ -32,7 +31,6 @@ func (bs *BusServiceImpl) stateGet(ctx context.Context, uuid string) (*state.Sta
}

func (bs *BusServiceImpl) Handle(ctx context.Context, readC <-chan Command, writeC chan<- Command) {
stateUUID := ""
stateSub, stateUnsub := make(<-chan state.Message), func() {}
defer stateUnsub()

Expand All @@ -50,9 +48,9 @@ func (bs *BusServiceImpl) Handle(ctx context.Context, readC <-chan Command, writ
case dto := <-readC:
switch dto.Type {
case TypeStateSubscribe:
stateUUID = fmt.Sprint(dto.Slug)
if stateUUID == "" {
writeCommand(ctx, writeC, newErrorCommand(fmt.Errorf("invalid uuid")))
stateUUID, err := parseStateSubscribe(dto.Slug)
if err != nil {
writeCommand(ctx, writeC, newErrorCommand(err))
continue
}

Expand All @@ -72,10 +70,6 @@ func (bs *BusServiceImpl) Handle(ctx context.Context, readC <-chan Command, writ
case TypeStateUnsubscribe:
// Unsubscribe from radio
stateUnsub()
select {
case <-stateSub:
default:
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions core/bus/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bus

import (
"context"
"fmt"

"github.com/ItsNotGoodName/reciva-web-remote/core/state"
)
Expand All @@ -27,6 +28,15 @@ const (
TypeStateUnsubscribe = Type("state.unsubscribe")
)

func parseStateSubscribe(slug interface{}) (string, error) {
uuid := fmt.Sprint(slug)
if uuid == "" {
return "", fmt.Errorf("invalid uuid")
}

return uuid, nil
}

func newErrorCommand(err error) Command {
return Command{
Type: TypeError,
Expand Down
23 changes: 14 additions & 9 deletions core/pubsub/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

type StatePub struct {
subMapMu sync.Mutex
subsMap map[*chan state.Message]string
subsMapMu sync.Mutex
subsMap map[*chan state.Message]string
}

func NewStatePub() *StatePub {
Expand All @@ -20,27 +20,32 @@ func NewStatePub() *StatePub {
func (sp *StatePub) Subscribe(uuid string) (<-chan state.Message, func()) {
sub := make(chan state.Message, 1)

sp.subMapMu.Lock()
sp.subsMapMu.Lock()

sp.subsMap[&sub] = uuid

sp.subMapMu.Unlock()
sp.subsMapMu.Unlock()

return sub, sp.unsubscribeFunc(&sub)
}

func (sp *StatePub) unsubscribeFunc(sub *chan state.Message) func() {
return func() {
sp.subMapMu.Lock()
sp.subsMapMu.Lock()

delete(sp.subsMap, sub)

sp.subMapMu.Unlock()
sp.subsMapMu.Unlock()

select {
case <-*sub:
default:
}
}
}

func (sp *StatePub) Publish(s state.State, changed int) {
sp.subMapMu.Lock()
sp.subsMapMu.Lock()

msg := state.Message{
State: s,
Expand All @@ -55,13 +60,13 @@ func (sp *StatePub) Publish(s state.State, changed int) {
default:
select {
case old := <-*sub:
msg.Changed |= old.Changed
msg.Changed = state.MergeChanged(old.Changed, msg.Changed)
*sub <- msg
case *sub <- msg:
}
}
}
}

sp.subMapMu.Unlock()
sp.subsMapMu.Unlock()
}
4 changes: 4 additions & 0 deletions core/state/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func IsChangedAll(changed int) bool {
return changed&ChangedAll == ChangedAll
}

func MergeChanged(changed int, other int) int {
return changed | other
}

func ValidPresetNumber(s *State, preset int) error {
if preset < 1 || preset > len(s.Presets) {
return fmt.Errorf("invalid preset number: %d", preset)
Expand Down
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ func main() {
return
}

// Backgrounds
var backgrounds []background.Background

// Right
presetStore, err := file.NewPresetStore(cfg.ConfigFile)
if err != nil {
log.Fatalln("Failed to create preset store:", err)
log.Fatalln("main.main: failed to create preset store:", err)
}

// Core
Expand All @@ -58,14 +55,18 @@ func main() {
statePub := pubsub.NewStatePub()
runService := radio.NewRunService(middlewareAndPresetStore, middlewarePub, radioService, statePub)
createService := radio.NewCreateService(upnpsub.NewControlPoint(upnpsub.WithPort(cfg.CPort)), runService)
backgrounds = append(backgrounds, createService)
hubService := radio.NewHubService(createService)
backgrounds = append(backgrounds, hubService)
busService := bus.New(hubService, radioService, statePub)

// Left
router := router.New(cfg.PortStr, presenter.New(json.Render), web.FS(), hubService, radioService, busService, middlewareAndPresetStore)
backgrounds = append(backgrounds, router)

// Backgrounds
backgrounds := []background.Background{
createService,
hubService,
router,
}

// Run backgrounds
background.Run(interrupt.Context(), backgrounds)
Expand Down

0 comments on commit 08c0d1a

Please sign in to comment.