2016-03-29 00:28:57 +00:00
|
|
|
package networkdb
|
|
|
|
|
2017-01-31 17:13:08 +00:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/docker/go-events"
|
|
|
|
)
|
2016-03-29 00:28:57 +00:00
|
|
|
|
|
|
|
type opType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
opCreate opType = 1 + iota
|
|
|
|
opUpdate
|
|
|
|
opDelete
|
|
|
|
)
|
|
|
|
|
|
|
|
type event struct {
|
|
|
|
Table string
|
|
|
|
NetworkID string
|
|
|
|
Key string
|
|
|
|
Value []byte
|
|
|
|
}
|
|
|
|
|
2017-01-31 17:13:08 +00:00
|
|
|
// NodeTable represents table event for node join and leave
|
|
|
|
const NodeTable = "NodeTable"
|
|
|
|
|
|
|
|
// NodeAddr represents the value carried for node event in NodeTable
|
|
|
|
type NodeAddr struct {
|
|
|
|
Addr net.IP
|
|
|
|
}
|
|
|
|
|
2016-03-29 00:28:57 +00:00
|
|
|
// CreateEvent generates a table entry create event to the watchers
|
|
|
|
type CreateEvent event
|
|
|
|
|
|
|
|
// UpdateEvent generates a table entry update event to the watchers
|
|
|
|
type UpdateEvent event
|
|
|
|
|
|
|
|
// DeleteEvent generates a table entry delete event to the watchers
|
|
|
|
type DeleteEvent event
|
|
|
|
|
|
|
|
// Watch creates a watcher with filters for a particular table or
|
2023-07-02 15:24:42 +00:00
|
|
|
// network or any combination of the tuple. If any of the
|
2016-03-29 00:28:57 +00:00
|
|
|
// filter is an empty string it acts as a wildcard for that
|
|
|
|
// field. Watch returns a channel of events, where the events will be
|
|
|
|
// sent.
|
2023-07-02 15:24:42 +00:00
|
|
|
func (nDB *NetworkDB) Watch(tname, nid string) (*events.Channel, func()) {
|
2016-03-29 00:28:57 +00:00
|
|
|
var matcher events.Matcher
|
|
|
|
|
2023-07-02 15:24:42 +00:00
|
|
|
if tname != "" || nid != "" {
|
2016-03-29 00:28:57 +00:00
|
|
|
matcher = events.MatcherFunc(func(ev events.Event) bool {
|
|
|
|
var evt event
|
|
|
|
switch ev := ev.(type) {
|
|
|
|
case CreateEvent:
|
|
|
|
evt = event(ev)
|
|
|
|
case UpdateEvent:
|
|
|
|
evt = event(ev)
|
|
|
|
case DeleteEvent:
|
|
|
|
evt = event(ev)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tname != "" && evt.Table != tname {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if nid != "" && evt.NetworkID != nid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := events.NewChannel(0)
|
|
|
|
sink := events.Sink(events.NewQueue(ch))
|
|
|
|
|
|
|
|
if matcher != nil {
|
|
|
|
sink = events.NewFilter(sink, matcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.broadcaster.Add(sink)
|
2017-05-31 16:49:55 +00:00
|
|
|
return ch, func() {
|
2016-03-29 00:28:57 +00:00
|
|
|
nDB.broadcaster.Remove(sink)
|
|
|
|
ch.Close()
|
|
|
|
sink.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeEvent(op opType, tname, nid, key string, value []byte) events.Event {
|
|
|
|
ev := event{
|
|
|
|
Table: tname,
|
|
|
|
NetworkID: nid,
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch op {
|
|
|
|
case opCreate:
|
|
|
|
return CreateEvent(ev)
|
|
|
|
case opUpdate:
|
|
|
|
return UpdateEvent(ev)
|
|
|
|
case opDelete:
|
|
|
|
return DeleteEvent(ev)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|