2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-07-25 12:50:14 +00:00
|
|
|
|
2014-07-13 19:07:24 +00:00
|
|
|
// Package events provides event subscription and polling functionality.
|
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-04-22 22:54:31 +00:00
|
|
|
stdsync "sync"
|
2014-07-13 19:07:24 +00:00
|
|
|
"time"
|
2015-04-22 22:54:31 +00:00
|
|
|
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-07-13 19:07:24 +00:00
|
|
|
)
|
|
|
|
|
2015-01-18 01:12:06 +00:00
|
|
|
type EventType int
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
const (
|
2014-10-06 22:03:24 +00:00
|
|
|
Ping EventType = 1 << iota
|
2014-07-17 11:38:36 +00:00
|
|
|
Starting
|
2014-07-13 19:07:24 +00:00
|
|
|
StartupComplete
|
2014-09-28 11:00:38 +00:00
|
|
|
DeviceDiscovered
|
|
|
|
DeviceConnected
|
|
|
|
DeviceDisconnected
|
|
|
|
DeviceRejected
|
2015-08-23 19:56:10 +00:00
|
|
|
DevicePaused
|
|
|
|
DeviceResumed
|
2014-07-13 19:07:24 +00:00
|
|
|
LocalIndexUpdated
|
|
|
|
RemoteIndexUpdated
|
|
|
|
ItemStarted
|
2015-02-01 17:31:19 +00:00
|
|
|
ItemFinished
|
2014-07-17 11:38:36 +00:00
|
|
|
StateChanged
|
2014-09-28 11:00:38 +00:00
|
|
|
FolderRejected
|
2014-09-06 15:31:23 +00:00
|
|
|
ConfigSaved
|
2014-11-16 23:18:59 +00:00
|
|
|
DownloadProgress
|
2015-03-26 22:26:51 +00:00
|
|
|
FolderSummary
|
|
|
|
FolderCompletion
|
2015-06-26 11:31:30 +00:00
|
|
|
FolderErrors
|
2015-08-26 22:49:06 +00:00
|
|
|
FolderScanProgress
|
2015-09-12 19:59:15 +00:00
|
|
|
ExternalPortMappingChanged
|
2015-09-20 13:30:25 +00:00
|
|
|
RelayStateChanged
|
2015-11-08 20:05:36 +00:00
|
|
|
LoginAttempt
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2014-10-06 22:03:24 +00:00
|
|
|
AllEvents = (1 << iota) - 1
|
2014-07-13 19:07:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (t EventType) String() string {
|
|
|
|
switch t {
|
|
|
|
case Ping:
|
|
|
|
return "Ping"
|
2014-07-17 11:38:36 +00:00
|
|
|
case Starting:
|
|
|
|
return "Starting"
|
2014-07-13 19:07:24 +00:00
|
|
|
case StartupComplete:
|
|
|
|
return "StartupComplete"
|
2014-09-28 11:00:38 +00:00
|
|
|
case DeviceDiscovered:
|
|
|
|
return "DeviceDiscovered"
|
|
|
|
case DeviceConnected:
|
|
|
|
return "DeviceConnected"
|
|
|
|
case DeviceDisconnected:
|
|
|
|
return "DeviceDisconnected"
|
|
|
|
case DeviceRejected:
|
|
|
|
return "DeviceRejected"
|
2014-07-13 19:07:24 +00:00
|
|
|
case LocalIndexUpdated:
|
|
|
|
return "LocalIndexUpdated"
|
|
|
|
case RemoteIndexUpdated:
|
|
|
|
return "RemoteIndexUpdated"
|
|
|
|
case ItemStarted:
|
|
|
|
return "ItemStarted"
|
2015-02-01 17:31:19 +00:00
|
|
|
case ItemFinished:
|
|
|
|
return "ItemFinished"
|
2014-07-17 11:38:36 +00:00
|
|
|
case StateChanged:
|
|
|
|
return "StateChanged"
|
2014-09-28 11:00:38 +00:00
|
|
|
case FolderRejected:
|
|
|
|
return "FolderRejected"
|
2014-09-06 15:31:23 +00:00
|
|
|
case ConfigSaved:
|
|
|
|
return "ConfigSaved"
|
2014-11-16 23:18:59 +00:00
|
|
|
case DownloadProgress:
|
|
|
|
return "DownloadProgress"
|
2015-03-26 22:26:51 +00:00
|
|
|
case FolderSummary:
|
|
|
|
return "FolderSummary"
|
|
|
|
case FolderCompletion:
|
|
|
|
return "FolderCompletion"
|
2015-06-26 11:31:30 +00:00
|
|
|
case FolderErrors:
|
|
|
|
return "FolderErrors"
|
2015-08-23 19:56:10 +00:00
|
|
|
case DevicePaused:
|
|
|
|
return "DevicePaused"
|
|
|
|
case DeviceResumed:
|
|
|
|
return "DeviceResumed"
|
2015-08-26 22:49:06 +00:00
|
|
|
case FolderScanProgress:
|
|
|
|
return "FolderScanProgress"
|
2015-09-12 19:59:15 +00:00
|
|
|
case ExternalPortMappingChanged:
|
|
|
|
return "ExternalPortMappingChanged"
|
2015-09-20 13:30:25 +00:00
|
|
|
case RelayStateChanged:
|
|
|
|
return "RelayStateChanged"
|
2015-11-08 20:05:36 +00:00
|
|
|
case LoginAttempt:
|
|
|
|
return "LoginAttempt"
|
2014-07-13 19:07:24 +00:00
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t EventType) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(t.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const BufferSize = 64
|
|
|
|
|
|
|
|
type Logger struct {
|
2015-09-29 15:17:09 +00:00
|
|
|
subs []*Subscription
|
2014-12-08 15:36:15 +00:00
|
|
|
nextID int
|
2014-07-13 19:07:24 +00:00
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type Event struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Type EventType `json:"type"`
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Subscription struct {
|
2015-05-23 18:38:41 +00:00
|
|
|
mask EventType
|
|
|
|
events chan Event
|
|
|
|
timeout *time.Timer
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var Default = NewLogger()
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrTimeout = errors.New("timeout")
|
|
|
|
ErrClosed = errors.New("closed")
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewLogger() *Logger {
|
|
|
|
return &Logger{
|
2015-04-22 22:54:31 +00:00
|
|
|
mutex: sync.NewMutex(),
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Log(t EventType, data interface{}) {
|
|
|
|
l.mutex.Lock()
|
2015-10-03 15:25:21 +00:00
|
|
|
dl.Debugln("log", l.nextID, t, data)
|
2015-09-29 15:17:09 +00:00
|
|
|
l.nextID++
|
2014-07-13 19:07:24 +00:00
|
|
|
e := Event{
|
2014-12-08 15:36:15 +00:00
|
|
|
ID: l.nextID,
|
2014-07-13 19:07:24 +00:00
|
|
|
Time: time.Now(),
|
|
|
|
Type: t,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
for _, s := range l.subs {
|
|
|
|
if s.mask&t != 0 {
|
|
|
|
select {
|
|
|
|
case s.events <- e:
|
|
|
|
default:
|
2014-10-06 22:03:24 +00:00
|
|
|
// if s.events is not ready, drop the event
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Subscribe(mask EventType) *Subscription {
|
|
|
|
l.mutex.Lock()
|
2015-10-03 15:25:21 +00:00
|
|
|
dl.Debugln("subscribe", mask)
|
2015-11-17 11:03:18 +00:00
|
|
|
|
2014-07-13 19:07:24 +00:00
|
|
|
s := &Subscription{
|
2015-05-23 18:38:41 +00:00
|
|
|
mask: mask,
|
|
|
|
events: make(chan Event, BufferSize),
|
|
|
|
timeout: time.NewTimer(0),
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
2015-11-17 11:03:18 +00:00
|
|
|
|
|
|
|
// We need to create the timeout timer in the stopped, non-fired state so
|
|
|
|
// that Subscription.Poll() can safely reset it and select on the timeout
|
|
|
|
// channel. This ensures the timer is stopped and the channel drained.
|
|
|
|
if !s.timeout.Stop() {
|
|
|
|
<-s.timeout.C
|
|
|
|
}
|
|
|
|
|
2015-09-29 15:17:09 +00:00
|
|
|
l.subs = append(l.subs, s)
|
2014-07-13 19:07:24 +00:00
|
|
|
l.mutex.Unlock()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Unsubscribe(s *Subscription) {
|
|
|
|
l.mutex.Lock()
|
2015-10-03 15:25:21 +00:00
|
|
|
dl.Debugln("unsubscribe")
|
2015-09-29 15:17:09 +00:00
|
|
|
for i, ss := range l.subs {
|
|
|
|
if s == ss {
|
|
|
|
last := len(l.subs) - 1
|
|
|
|
l.subs[i] = l.subs[last]
|
|
|
|
l.subs[last] = nil
|
|
|
|
l.subs = l.subs[:last]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-07-13 19:07:24 +00:00
|
|
|
close(s.events)
|
|
|
|
l.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-05-23 18:38:41 +00:00
|
|
|
// Poll returns an event from the subscription or an error if the poll times
|
|
|
|
// out of the event channel is closed. Poll should not be called concurrently
|
|
|
|
// from multiple goroutines for a single subscription.
|
2014-07-13 19:07:24 +00:00
|
|
|
func (s *Subscription) Poll(timeout time.Duration) (Event, error) {
|
2015-10-03 15:25:21 +00:00
|
|
|
dl.Debugln("poll", timeout)
|
2014-07-25 12:50:14 +00:00
|
|
|
|
2015-11-17 11:03:18 +00:00
|
|
|
s.timeout.Reset(timeout)
|
2015-08-24 07:38:39 +00:00
|
|
|
|
2014-07-13 19:07:24 +00:00
|
|
|
select {
|
|
|
|
case e, ok := <-s.events:
|
|
|
|
if !ok {
|
|
|
|
return e, ErrClosed
|
|
|
|
}
|
2015-11-17 11:03:18 +00:00
|
|
|
if !s.timeout.Stop() {
|
|
|
|
// The timeout must be stopped and possibly drained to be ready
|
|
|
|
// for reuse in the next call.
|
|
|
|
<-s.timeout.C
|
|
|
|
}
|
2014-07-13 19:07:24 +00:00
|
|
|
return e, nil
|
2015-05-23 18:38:41 +00:00
|
|
|
case <-s.timeout.C:
|
2014-07-13 19:07:24 +00:00
|
|
|
return Event{}, ErrTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-26 23:12:12 +00:00
|
|
|
func (s *Subscription) C() <-chan Event {
|
|
|
|
return s.events
|
|
|
|
}
|
|
|
|
|
2014-07-13 19:07:24 +00:00
|
|
|
type BufferedSubscription struct {
|
|
|
|
sub *Subscription
|
|
|
|
buf []Event
|
|
|
|
next int
|
|
|
|
cur int
|
|
|
|
mut sync.Mutex
|
2015-04-22 22:54:31 +00:00
|
|
|
cond *stdsync.Cond
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBufferedSubscription(s *Subscription, size int) *BufferedSubscription {
|
|
|
|
bs := &BufferedSubscription{
|
|
|
|
sub: s,
|
|
|
|
buf: make([]Event, size),
|
2015-04-22 22:54:31 +00:00
|
|
|
mut: sync.NewMutex(),
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
2015-04-22 22:54:31 +00:00
|
|
|
bs.cond = stdsync.NewCond(bs.mut)
|
2014-07-13 19:07:24 +00:00
|
|
|
go bs.pollingLoop()
|
|
|
|
return bs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BufferedSubscription) pollingLoop() {
|
|
|
|
for {
|
|
|
|
ev, err := s.sub.Poll(60 * time.Second)
|
|
|
|
if err == ErrTimeout {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err == ErrClosed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic("unexpected error: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mut.Lock()
|
|
|
|
s.buf[s.next] = ev
|
|
|
|
s.next = (s.next + 1) % len(s.buf)
|
|
|
|
s.cur = ev.ID
|
|
|
|
s.cond.Broadcast()
|
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BufferedSubscription) Since(id int, into []Event) []Event {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
|
|
|
for id >= s.cur {
|
|
|
|
s.cond.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := s.next; i < len(s.buf); i++ {
|
|
|
|
if s.buf[i].ID > id {
|
|
|
|
into = append(into, s.buf[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < s.next; i++ {
|
|
|
|
if s.buf[i].ID > id {
|
|
|
|
into = append(into, s.buf[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return into
|
|
|
|
}
|
2015-05-27 09:14:39 +00:00
|
|
|
|
|
|
|
// Error returns a string pointer suitable for JSON marshalling errors. It
|
2015-11-12 02:20:34 +00:00
|
|
|
// retains the "null on success" semantics, but ensures the error result is a
|
2015-05-27 09:14:39 +00:00
|
|
|
// string regardless of the underlying concrete error type.
|
|
|
|
func Error(err error) *string {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
str := err.Error()
|
|
|
|
return &str
|
|
|
|
}
|