package emlid import ( "log" "os" "time" "forge.cadoles.com/Pyxis/golang-socketio" ) // Logger is a logger implementation for the ReachRS client type Logger interface { Printf(format string, args ...interface{}) } // Options are ReachRS client configuration options type Options struct { Endpoint string PingInterval time.Duration PingTimeout time.Duration ReceiveTimeout time.Duration SendTimeout time.Duration BufferSize int Logger Logger } // OptionFunc is an option configuration for the ReachRS client type OptionFunc func(opts *Options) // WithEndpoint configures the client to target the given host and port func WithEndpoint(host string, port int) OptionFunc { return func(opts *Options) { opts.Endpoint = gosocketio.GetUrl(host, port, false) } } // WithPingInterval configures the client to use the given ping interval func WithPingInterval(interval time.Duration) OptionFunc { return func(opts *Options) { opts.PingInterval = interval } } // WithPingTimeout configures the client to use the given ping timeout func WithPingTimeout(timeout time.Duration) OptionFunc { return func(opts *Options) { opts.PingTimeout = timeout } } // WithReceiveTimeout configures the client to use the given receive timeout func WithReceiveTimeout(timeout time.Duration) OptionFunc { return func(opts *Options) { opts.ReceiveTimeout = timeout } } // WithSendTimeout configures the client to use the given send timeout func WithSendTimeout(timeout time.Duration) OptionFunc { return func(opts *Options) { opts.SendTimeout = timeout } } // WithBufferSize configures the client to use the given buffer size func WithBufferSize(size int) OptionFunc { return func(opts *Options) { opts.BufferSize = size } } // WithLogger configures the client to use the given logger func WithLogger(logger Logger) OptionFunc { return func(opts *Options) { opts.Logger = logger } } // WithStandardLogger configures the client to use the given standard logger func WithStandardLogger() OptionFunc { return func(opts *Options) { logger := log.New(os.Stdout, "[reachrs] ", log.LstdFlags) opts.Logger = logger } } // DefaultOptions returns the default values for the client options func DefaultOptions() *Options { opts := &Options{} defaults := []OptionFunc{ WithEndpoint("192.168.42.1", 80), WithPingInterval(30 * time.Second), WithPingTimeout(60 * time.Second), WithReceiveTimeout(60 * time.Second), WithSendTimeout(60 * time.Second), WithBufferSize(1024 * 32), } for _, o := range defaults { o(opts) } return opts }