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

63 lines
1.2 KiB
Go

package protocol
import (
"context"
"log/slog"
"net"
"forge.cadoles.com/cadoles/go-emlid/reach/client/logger"
"github.com/pkg/errors"
)
type Identifier string
type DialFunc func(network string, addr string) (net.Conn, error)
type Protocol interface {
Identifier() Identifier
Available(ctx context.Context, addr string) (bool, error)
Operations(addr string) Operations
}
type ProtocolOptions struct {
Logger logger.Logger
Dial DialFunc
}
var DefaultDialFunc = func(network, addr string) (net.Conn, error) {
conn, err := net.Dial(network, addr)
if err != nil {
return nil, errors.WithStack(err)
}
return conn, nil
}
type ProtocolFactory func(opts *ProtocolOptions) (Protocol, error)
type ProtocolOptionFunc func(opts *ProtocolOptions)
func NewProtocolOptions(funcs ...ProtocolOptionFunc) *ProtocolOptions {
opts := &ProtocolOptions{
Logger: slog.Default(),
}
for _, fn := range funcs {
fn(opts)
}
return opts
}
func WithProtocolLogger(logger logger.Logger) ProtocolOptionFunc {
return func(opts *ProtocolOptions) {
opts.Logger = logger
}
}
func WithProtocolDial(dial DialFunc) ProtocolOptionFunc {
return func(opts *ProtocolOptions) {
opts.Dial = dial
}
}