Skip to content

Commit

Permalink
Send worker lib
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Nov 12, 2024
1 parent 1795e7f commit 0f55f9b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
IceCandidate = WebrtcIce
TerminateSession PT = 204
AppVideoChange PT = 150
LibNewGameList PT = 205
)

func (p PT) String() string {
Expand Down Expand Up @@ -125,6 +126,8 @@ func (p PT) String() string {
return "TerminateSession"
case AppVideoChange:
return "AppVideoChange"
case LibNewGameList:
return "LibNewGameList"
default:
return "Unknown"
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/api/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ type (
S int `json:"s"`
A float32 `json:"a"`
}

LibGameListInfo struct {
T int
List []GameInfo
}
)
9 changes: 9 additions & 0 deletions pkg/coordinator/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func (w *Worker) HandleRequests(users HasUserRegistry) chan struct{} {
w.log.Error().Err(err).Send()
return api.ErrMalformed
}
case api.LibNewGameList:
inf := api.Unwrap[api.LibGameListInfo](payload)
if inf == nil {
return api.ErrMalformed
}
if err := w.HandleLibGameList(*inf); err != nil {
w.log.Error().Err(err).Send()
return api.ErrMalformed
}
default:
w.log.Warn().Msgf("Unknown packet: %+v", p)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/coordinator/workerhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ func (w *Worker) HandleIceCandidate(rq api.WebrtcIceCandidateRequest[com.Uid], u
}
return nil
}

func (w *Worker) HandleLibGameList(inf api.LibGameListInfo) error {
w.log.Info().Msgf("Oh, lib: %v", inf)
return nil
}
11 changes: 11 additions & 0 deletions pkg/worker/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,14 @@ func (c *coordinator) CloseRoom(id string) { c.Notify(api.CloseRoom, id) }
func (c *coordinator) IceCandidate(candidate string, sessionId com.Uid) {
c.Notify(api.WebrtcIce, api.WebrtcIceCandidateRequest[com.Uid]{Stateful: api.Stateful[com.Uid]{Id: sessionId}, Candidate: candidate})
}

func (c *coordinator) SendLibrary(w *Worker) {
games := w.lib.GetAll()

var gg = make([]api.GameInfo, len(games))
for i, g := range games {
gg[i] = api.GameInfo(g)
}

c.Notify(api.LibNewGameList, api.LibGameListInfo{T: 1, List: gg})
}
15 changes: 14 additions & 1 deletion pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/giongto35/cloud-game/v3/pkg/config"
"github.com/giongto35/cloud-game/v3/pkg/games"
"github.com/giongto35/cloud-game/v3/pkg/logger"
"github.com/giongto35/cloud-game/v3/pkg/monitoring"
"github.com/giongto35/cloud-game/v3/pkg/network"
Expand All @@ -19,6 +20,7 @@ type Worker struct {
address string
conf config.WorkerConfig
cord *coordinator
lib games.GameLibrary
log *logger.Logger
mana *caged.Manager
router *room.GameRouter
Expand All @@ -34,7 +36,17 @@ func New(conf config.WorkerConfig, log *logger.Logger) (*Worker, error) {
if err := manager.Load(caged.Libretro, conf); err != nil {
return nil, fmt.Errorf("couldn't cage libretro: %v", err)
}
worker := &Worker{conf: conf, log: log, mana: manager, router: room.NewGameRouter()}

library := games.NewLib(conf.Library, conf.Emulator, log)
library.Scan()

worker := &Worker{
conf: conf,
lib: library,
log: log,
mana: manager,
router: room.NewGameRouter(),
}

h, err := httpx.NewServer(
conf.Worker.GetAddr(),
Expand Down Expand Up @@ -109,6 +121,7 @@ func (w *Worker) Start(done chan struct{}) {
w.cord = cord
w.cord.log.Info().Msgf("Connected to the coordinator %v", remoteAddr)
wait := w.cord.HandleRequests(w)
w.cord.SendLibrary(w)
<-wait
retry.SuccessCheck()
}
Expand Down

0 comments on commit 0f55f9b

Please sign in to comment.