Skip to content

Commit

Permalink
refactor: radio create and discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Apr 30, 2022
1 parent 2882c12 commit 1896e33
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
}

// Create radio
radio, err := createService.Create(ctx, clients[0])
radio, err := createService.Create(ctx, ctx, clients[0])
if err != nil {
log.Fatalln("failed to create radio:", err)
}
Expand Down
10 changes: 5 additions & 5 deletions core/radio/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (cs *CreateServiceImpl) Background(ctx context.Context, doneC chan<- struct
doneC <- struct{}{}
}

func (cs *CreateServiceImpl) Create(dctx context.Context, client goupnp.ServiceClient) (Radio, error) {
func (cs *CreateServiceImpl) Create(ctx context.Context, dctx context.Context, client goupnp.ServiceClient) (Radio, error) {
// Get UUID
uuid, err := upnp.GetUUID(client)
if err != nil {
Expand All @@ -45,14 +45,14 @@ func (cs *CreateServiceImpl) Create(dctx context.Context, client goupnp.ServiceC
s := state.New(uuid, upnp.GetName(client), upnp.GetModelName(client), upnp.GetModelNumber(client))

// Get and set volume
volume, err := upnp.GetVolume(dctx, client)
volume, err := upnp.GetVolume(ctx, client)
if err != nil {
return Radio{}, err
}
s.SetVolume(volume)

// Get and parse presets count
presetsCount, err := upnp.GetNumberOfPresets(dctx, client)
presetsCount, err := upnp.GetNumberOfPresets(ctx, client)
if err != nil {
return Radio{}, err
}
Expand All @@ -63,7 +63,7 @@ func (cs *CreateServiceImpl) Create(dctx context.Context, client goupnp.ServiceC
// Get and set presets
var presets []state.Preset
for i := 1; i <= presetsCount; i++ {
p, err := upnp.GetPreset(dctx, client, i)
p, err := upnp.GetPreset(ctx, client, i)
if err != nil {
return Radio{}, err
}
Expand All @@ -73,7 +73,7 @@ func (cs *CreateServiceImpl) Create(dctx context.Context, client goupnp.ServiceC
s.SetPresets(presets)

// Get audio sources
audioSources, err := upnp.GetAudioSources(dctx, client)
audioSources, err := upnp.GetAudioSources(ctx, client)
if err != nil {
return Radio{}, err
}
Expand Down
40 changes: 31 additions & 9 deletions core/radio/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"sync"
"time"

"github.com/ItsNotGoodName/reciva-web-remote/core"
"github.com/ItsNotGoodName/reciva-web-remote/core/upnp"
Expand Down Expand Up @@ -59,18 +60,39 @@ func (hs *HubServiceImpl) Background(ctx context.Context, doneC chan<- struct{})
return 0, err
}

// Radio run context
newCtx, newCancel := context.WithCancel(ctx)

// Create radios
var radios []Radio
for _, client := range clients {
radio, err := hs.radioService.Create(newCtx, client)
if err != nil {
fmt.Println("radio.HubService.Background:", err)
continue
}
// Create radios concurrently
radioC := make(chan Radio)
var wg sync.WaitGroup
for i := range clients {
wg.Add(1)
go (func(idx int) {
// Timeout for creating radio
sctx, cancel := context.WithTimeout(ctx, 45*time.Second)
defer cancel()

// Create radio
radio, err := hs.radioService.Create(sctx, newCtx, clients[idx])
if err != nil {
fmt.Println("radio.HubService.Background:", err)
} else {
radioC <- radio
}

wg.Done()
})(i)
}
go (func() {
wg.Wait()
close(radioC)
})()

radios = append(radios, radio)
// Collect radios
var radios []Radio
for r := range radioC {
radios = append(radios, r)
}

// Create radios map
Expand Down
2 changes: 1 addition & 1 deletion core/radio/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type (
}

CreateService interface {
Create(dctx context.Context, client goupnp.ServiceClient) (Radio, error)
Create(ctx context.Context, dctx context.Context, client goupnp.ServiceClient) (Radio, error)
}

RunService interface {
Expand Down

0 comments on commit 1896e33

Please sign in to comment.