2018-09-18 18:07:40 +02:00
|
|
|
package reach
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Pyxis/golang-socketio"
|
|
|
|
)
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// Logger is a logger implementation for the ReachRS client
|
2018-09-18 18:07:40 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// OptionFunc is an option configuration for the ReachRS client
|
2018-09-18 18:07:40 +02:00
|
|
|
type OptionFunc func(opts *Options)
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// WithEndpoint configures the client to target the given host and port
|
2018-09-18 18:07:40 +02:00
|
|
|
func WithEndpoint(host string, port int) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Endpoint = gosocketio.GetUrl(host, port, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// WithPingInterval configures the client to use the given ping interval
|
2018-09-18 18:07:40 +02:00
|
|
|
func WithPingInterval(interval time.Duration) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.PingInterval = interval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// WithPingTimeout configures the client to use the given ping timeout
|
2018-09-18 18:07:40 +02:00
|
|
|
func WithPingTimeout(timeout time.Duration) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.PingTimeout = timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// WithReceiveTimeout configures the client to use the given receive timeout
|
2018-09-18 18:07:40 +02:00
|
|
|
func WithReceiveTimeout(timeout time.Duration) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.ReceiveTimeout = timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// WithSendTimeout configures the client to use the given send timeout
|
2018-09-18 18:07:40 +02:00
|
|
|
func WithSendTimeout(timeout time.Duration) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.SendTimeout = timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// WithBufferSize configures the client to use the given buffer size
|
2018-09-18 18:07:40 +02:00
|
|
|
func WithBufferSize(size int) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.BufferSize = size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// WithLogger configures the client to use the given logger
|
2018-09-18 18:07:40 +02:00
|
|
|
func WithLogger(logger Logger) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Logger = logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// WithStandardLogger configures the client to use the given standard logger
|
2018-09-18 18:07:40 +02:00
|
|
|
func WithStandardLogger() OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
logger := log.New(os.Stdout, "[reachrs] ", log.LstdFlags)
|
|
|
|
opts.Logger = logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 10:01:28 +02:00
|
|
|
// DefaultOptions returns the default values for the client options
|
2018-09-18 18:07:40 +02:00
|
|
|
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
|
|
|
|
}
|