package broadcast import ( "context" "git.sr.ht/~blallo/logz/interface" ) const ( bufLines = 100 ) type Command struct { CommandType Resp chan any } type CommandType int const ( CommandStart = 1 << iota CommandStop CommandStatus CommandLiveness ) type Radio struct { Program string CmdLine []string runnable Runnable commands chan Command logger logz.Logger } func NewRadio(logger logz.Logger, runnable Runnable) (*Radio, error) { return &Radio{ runnable: runnable, logger: logger, commands: make(chan Command), }, nil } func (r *Radio) Run(ctx context.Context) error { if err := r.runnable.Init(ctx); err != nil { return err } for { select { case <-ctx.Done(): r.logger.Warn(map[string]any{ "msg": "Context terminated. Exiting...", }) return ctx.Err() case cmd := <-r.commands: switch cmd.CommandType { case CommandStart: r.logger.Info(map[string]any{ "msg": "Received Start command", }) cmd.Resp <- r.runnable.Start(ctx) case CommandStop: r.logger.Info(map[string]any{ "msg": "Received Stop command", }) cmd.Resp <- r.runnable.Stop(ctx) case CommandStatus: r.logger.Info(map[string]any{ "msg": "Received Status command", }) cmd.Resp <- r.runnable.Logs(ctx) case CommandLiveness: r.logger.Info(map[string]any{ "msg": "Received Liveness command", }) cmd.Resp <- r.runnable.Liveness(ctx) } } } } func (r *Radio) Start() <-chan any { resp := make(chan any) r.commands <- Command{ CommandType: CommandStart, Resp: resp, } return resp } func (r *Radio) Stop() <-chan any { resp := make(chan any) r.commands <- Command{ CommandType: CommandStop, Resp: resp, } return resp } func (r *Radio) Status() <-chan any { resp := make(chan any) r.commands <- Command{ CommandType: CommandStatus, Resp: resp, } return resp } func (r *Radio) IsAlive() <-chan any { resp := make(chan any) r.commands <- Command{ CommandType: CommandLiveness, Resp: resp, } return resp }