go-emlid/reach/client/protocol/registry.go

55 lines
993 B
Go
Raw Normal View History

2024-07-30 14:28:39 +02:00
package protocol
import (
"context"
"github.com/pkg/errors"
)
type Registry struct {
protocols map[Identifier]Protocol
}
func (r *Registry) Register(protocol Protocol) {
r.protocols[protocol.Identifier()] = protocol
}
func (r *Registry) Availables(ctx context.Context, addr string) ([]Protocol, error) {
availables := make([]Protocol, 0)
for _, proto := range r.protocols {
available, err := proto.Available(ctx, addr)
if err != nil {
return nil, errors.WithStack(err)
}
if !available {
continue
}
availables = append(availables, proto)
}
return availables, nil
}
func NewRegistry() *Registry {
return &Registry{
protocols: make(map[Identifier]Protocol),
}
}
var defaultRegistry = NewRegistry()
func DefaultRegistry() *Registry {
return defaultRegistry
}
func Register(protocol Protocol) {
defaultRegistry.Register(protocol)
}
func Availables(ctx context.Context, addr string) ([]Protocol, error) {
return defaultRegistry.Availables(ctx, addr)
}