http-boot-server/bootserver/services/services.go
Melora Hugues 0b6bbce7d5
All checks were successful
continuous-integration/drone/push Build is passing
Add udp listener
2023-07-29 21:23:36 +02:00

59 lines
1.4 KiB
Go

package services
import (
"errors"
"git.faercol.me/faercol/http-boot-server/bootserver/bootoption"
"github.com/google/uuid"
)
var ErrUnknownClient = errors.New("unknown client")
var ErrUnselectedBootOption = errors.New("unselected boot option")
var ErrUnknownBootOption = errors.New("unknown boot option")
type ClientHandlerService struct {
clients map[uuid.UUID]*bootoption.Client
}
func NewClientHandlerService() *ClientHandlerService {
return &ClientHandlerService{
clients: make(map[uuid.UUID]*bootoption.Client),
}
}
func (chs *ClientHandlerService) AddClient(client *bootoption.Client) {
chs.clients[client.ID] = client
}
func (chs *ClientHandlerService) GetClientSelectedBootOption(client uuid.UUID) (*bootoption.EFIApp, error) {
clientDetails, ok := chs.clients[client]
if !ok {
return nil, ErrUnknownClient
}
if clientDetails.SelectedOption == "" {
return nil, ErrUnselectedBootOption
}
for _, o := range clientDetails.Options {
if o.Name == clientDetails.SelectedOption {
return &o, nil
}
}
return nil, ErrUnknownBootOption
}
func (chs *ClientHandlerService) SetClientBootOption(client uuid.UUID, option string) error {
clientDetails, ok := chs.clients[client]
if !ok {
return ErrUnknownClient
}
for _, o := range clientDetails.Options {
if o.Name == option {
clientDetails.SelectedOption = option
return nil
}
}
return ErrUnknownBootOption
}