2020-07-08 22:33:28 +00:00
|
|
|
package publicip
|
|
|
|
|
|
|
|
|
|
import (
|
2020-12-29 02:55:34 +00:00
|
|
|
"net/http"
|
2020-07-08 22:33:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
2021-02-06 11:05:50 -05:00
|
|
|
"github.com/qdm12/gluetun/internal/configuration"
|
2020-12-28 01:51:55 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2021-07-24 19:49:11 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/loopstate"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2021-07-24 19:49:11 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/publicip/state"
|
2020-07-08 22:33:28 +00:00
|
|
|
)
|
|
|
|
|
|
2021-07-24 19:49:11 +00:00
|
|
|
var _ Looper = (*Loop)(nil)
|
|
|
|
|
|
2020-07-08 22:33:28 +00:00
|
|
|
type Looper interface {
|
2021-07-24 19:49:11 +00:00
|
|
|
Runner
|
|
|
|
|
RestartTickerRunner
|
|
|
|
|
loopstate.Getter
|
|
|
|
|
loopstate.Applier
|
2021-07-24 19:49:50 +00:00
|
|
|
SettingsGetSetter
|
2021-09-06 13:28:05 +00:00
|
|
|
GetSetter
|
2020-07-08 22:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-24 19:49:11 +00:00
|
|
|
type Loop struct {
|
|
|
|
|
statusManager loopstate.Manager
|
|
|
|
|
state state.Manager
|
2020-12-28 01:51:55 +00:00
|
|
|
// Objects
|
2021-07-24 19:49:11 +00:00
|
|
|
fetcher Fetcher
|
|
|
|
|
client *http.Client
|
2021-09-23 16:58:21 +00:00
|
|
|
logger Logger
|
2020-12-28 01:51:55 +00:00
|
|
|
// Fixed settings
|
2020-12-29 16:44:35 +00:00
|
|
|
puid int
|
|
|
|
|
pgid int
|
2020-12-28 01:51:55 +00:00
|
|
|
// Internal channels and locks
|
|
|
|
|
start chan struct{}
|
|
|
|
|
running chan models.LoopStatus
|
|
|
|
|
stop chan struct{}
|
|
|
|
|
stopped chan struct{}
|
|
|
|
|
updateTicker chan struct{}
|
2020-12-30 17:22:54 +00:00
|
|
|
backoffTime time.Duration
|
2021-07-26 01:35:43 +00:00
|
|
|
userTrigger bool
|
2020-12-28 01:51:55 +00:00
|
|
|
// Mock functions
|
2021-07-24 19:49:11 +00:00
|
|
|
timeNow func() time.Time
|
2020-07-08 22:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-30 17:22:54 +00:00
|
|
|
const defaultBackoffTime = 5 * time.Second
|
|
|
|
|
|
2021-09-23 16:58:21 +00:00
|
|
|
func NewLoop(client *http.Client, logger Logger,
|
2021-07-24 19:49:11 +00:00
|
|
|
settings configuration.PublicIP, puid, pgid int) *Loop {
|
|
|
|
|
start := make(chan struct{})
|
|
|
|
|
running := make(chan models.LoopStatus)
|
|
|
|
|
stop := make(chan struct{})
|
|
|
|
|
stopped := make(chan struct{})
|
|
|
|
|
updateTicker := make(chan struct{})
|
|
|
|
|
|
|
|
|
|
statusManager := loopstate.New(constants.Stopped, start, running, stop, stopped)
|
|
|
|
|
state := state.New(statusManager, settings, updateTicker)
|
|
|
|
|
|
|
|
|
|
return &Loop{
|
|
|
|
|
statusManager: statusManager,
|
|
|
|
|
state: state,
|
2020-12-28 01:51:55 +00:00
|
|
|
// Objects
|
2021-02-08 00:01:14 +00:00
|
|
|
client: client,
|
2021-07-24 19:49:11 +00:00
|
|
|
fetcher: NewFetch(client),
|
2021-05-12 22:57:15 +00:00
|
|
|
logger: logger,
|
2020-12-29 16:44:35 +00:00
|
|
|
puid: puid,
|
|
|
|
|
pgid: pgid,
|
2021-07-26 01:35:43 +00:00
|
|
|
start: start,
|
|
|
|
|
running: running,
|
|
|
|
|
stop: stop,
|
|
|
|
|
stopped: stopped,
|
|
|
|
|
updateTicker: updateTicker,
|
|
|
|
|
userTrigger: true,
|
2020-12-30 17:22:54 +00:00
|
|
|
backoffTime: defaultBackoffTime,
|
2020-12-28 01:51:55 +00:00
|
|
|
timeNow: time.Now,
|
2020-07-08 22:47:12 +00:00
|
|
|
}
|
|
|
|
|
}
|