2019-02-20 20:54:46 +00:00
|
|
|
package gun
|
|
|
|
|
2019-02-22 06:46:19 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2019-02-22 09:23:14 +00:00
|
|
|
"errors"
|
2019-02-22 06:46:19 +00:00
|
|
|
"sync"
|
|
|
|
)
|
2019-02-20 20:54:46 +00:00
|
|
|
|
|
|
|
type Scoped struct {
|
2019-02-22 09:23:14 +00:00
|
|
|
gun *Gun
|
|
|
|
|
|
|
|
parent *Scoped
|
|
|
|
field string
|
|
|
|
cachedParentSoul string
|
|
|
|
cachedParentSoulLock sync.RWMutex
|
2019-02-22 06:46:19 +00:00
|
|
|
|
|
|
|
valueChansToListeners map[<-chan *ValueFetch]*messageIDListener
|
|
|
|
valueChansToListenersLock sync.Mutex
|
2019-02-20 20:54:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 06:46:19 +00:00
|
|
|
type messageIDListener struct {
|
|
|
|
id string
|
|
|
|
values chan *ValueFetch
|
2019-02-22 09:23:14 +00:00
|
|
|
receivedMessages chan *MessageReceived
|
2019-02-20 20:54:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 09:23:14 +00:00
|
|
|
func newScoped(gun *Gun, parent *Scoped, field string) *Scoped {
|
2019-02-22 06:46:19 +00:00
|
|
|
return &Scoped{
|
2019-02-22 19:51:50 +00:00
|
|
|
gun: gun,
|
|
|
|
parent: parent,
|
|
|
|
field: field,
|
|
|
|
valueChansToListeners: map[<-chan *ValueFetch]*messageIDListener{},
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ValueFetch struct {
|
|
|
|
// This can be a context error on cancelation
|
|
|
|
Err error
|
|
|
|
Field string
|
2019-02-22 09:23:14 +00:00
|
|
|
// Nil if the value doesn't exist or there's an error
|
|
|
|
Value *ValueWithState
|
2019-02-22 06:46:19 +00:00
|
|
|
// Nil when local and sometimes on error
|
2019-02-22 18:40:02 +00:00
|
|
|
peer *gunPeer
|
2019-02-20 20:54:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 09:23:14 +00:00
|
|
|
var ErrNotObject = errors.New("Scoped value not an object")
|
|
|
|
var ErrLookupOnTopLevel = errors.New("Cannot do lookup on top level")
|
|
|
|
|
|
|
|
// Empty string if doesn't exist, ErrNotObject if self or parent not an object
|
|
|
|
func (s *Scoped) Soul(ctx context.Context) (string, error) {
|
|
|
|
s.cachedParentSoulLock.RLock()
|
|
|
|
cachedParentSoul := s.cachedParentSoul
|
|
|
|
s.cachedParentSoulLock.RUnlock()
|
|
|
|
if cachedParentSoul != "" {
|
|
|
|
return cachedParentSoul, nil
|
|
|
|
} else if v := s.Val(ctx); v.Err != nil {
|
|
|
|
return "", v.Err
|
|
|
|
} else if v.Value == nil {
|
|
|
|
return "", nil
|
|
|
|
} else if rel, ok := v.Value.Value.(ValueRelation); !ok {
|
|
|
|
return "", ErrNotObject
|
|
|
|
} else {
|
|
|
|
s.cachedParentSoulLock.Lock()
|
|
|
|
s.cachedParentSoul = string(rel)
|
|
|
|
s.cachedParentSoulLock.Unlock()
|
|
|
|
return string(rel), nil
|
|
|
|
}
|
2019-02-20 20:54:46 +00:00
|
|
|
}
|
2019-02-22 06:46:19 +00:00
|
|
|
|
|
|
|
func (s *Scoped) Val(ctx context.Context) *ValueFetch {
|
|
|
|
// Try local before remote
|
|
|
|
if v := s.ValLocal(ctx); v != nil {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return s.ValRemote(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scoped) ValLocal(ctx context.Context) *ValueFetch {
|
2019-02-22 09:23:14 +00:00
|
|
|
// If there is no parent, this is just the relation
|
|
|
|
if s.parent == nil {
|
|
|
|
return &ValueFetch{Field: s.field, Value: &ValueWithState{Value: ValueRelation(s.field)}}
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
2019-02-22 09:23:14 +00:00
|
|
|
v := &ValueFetch{Field: s.field}
|
|
|
|
// Need parent soul for lookup
|
|
|
|
var parentSoul string
|
|
|
|
if parentSoul, v.Err = s.parent.Soul(ctx); v.Err == nil {
|
|
|
|
if v.Value, v.Err = s.gun.storage.Get(ctx, parentSoul, s.field); v.Err == ErrStorageNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scoped) ValRemote(ctx context.Context) *ValueFetch {
|
2019-02-22 09:23:14 +00:00
|
|
|
if s.parent == nil {
|
|
|
|
return &ValueFetch{Err: ErrLookupOnTopLevel, Field: s.field}
|
|
|
|
}
|
2019-02-22 06:46:19 +00:00
|
|
|
ch := s.OnRemote(ctx)
|
|
|
|
defer s.Off(ch)
|
|
|
|
return <-ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scoped) On(ctx context.Context) <-chan *ValueFetch {
|
|
|
|
ch := make(chan *ValueFetch, 1)
|
2019-02-22 09:23:14 +00:00
|
|
|
if s.parent == nil {
|
|
|
|
ch <- &ValueFetch{Err: ErrLookupOnTopLevel, Field: s.field}
|
|
|
|
} else {
|
|
|
|
if v := s.ValLocal(ctx); v != nil {
|
|
|
|
ch <- v
|
|
|
|
}
|
|
|
|
go s.onRemote(ctx, ch)
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scoped) OnRemote(ctx context.Context) <-chan *ValueFetch {
|
|
|
|
ch := make(chan *ValueFetch, 1)
|
2019-02-22 09:23:14 +00:00
|
|
|
if s.parent == nil {
|
|
|
|
ch <- &ValueFetch{Err: ErrLookupOnTopLevel, Field: s.field}
|
|
|
|
} else {
|
|
|
|
go s.onRemote(ctx, ch)
|
|
|
|
}
|
2019-02-22 06:46:19 +00:00
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scoped) onRemote(ctx context.Context, ch chan *ValueFetch) {
|
2019-02-22 09:23:14 +00:00
|
|
|
if s.parent == nil {
|
|
|
|
panic("No parent")
|
|
|
|
}
|
|
|
|
// We have to get the parent soul first
|
|
|
|
parentSoul, err := s.parent.Soul(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ch <- &ValueFetch{Err: ErrLookupOnTopLevel, Field: s.field}
|
|
|
|
return
|
|
|
|
}
|
2019-02-22 06:46:19 +00:00
|
|
|
// Create get request
|
|
|
|
req := &Message{
|
|
|
|
ID: randString(9),
|
2019-02-22 09:23:14 +00:00
|
|
|
Get: &MessageGetRequest{Soul: parentSoul, Field: s.field},
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
|
|
|
// Make a chan to listen for received messages and link it to
|
|
|
|
// the given one so we can turn it "off". Off will close this
|
|
|
|
// chan.
|
2019-02-22 09:23:14 +00:00
|
|
|
msgCh := make(chan *MessageReceived)
|
2019-02-22 06:46:19 +00:00
|
|
|
s.valueChansToListenersLock.Lock()
|
|
|
|
s.valueChansToListeners[ch] = &messageIDListener{req.ID, ch, msgCh}
|
|
|
|
s.valueChansToListenersLock.Unlock()
|
|
|
|
// Listen for responses to this get
|
|
|
|
s.gun.RegisterMessageIDPutListener(req.ID, msgCh)
|
|
|
|
// TODO: only for children: s.gun.RegisterValueIDPutListener(s.id, msgCh)
|
|
|
|
// Handle received messages turning them to value fetches
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
ch <- &ValueFetch{Err: ctx.Err(), Field: s.field}
|
|
|
|
s.Off(ch)
|
|
|
|
return
|
|
|
|
case msg, ok := <-msgCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-02-22 18:40:02 +00:00
|
|
|
f := &ValueFetch{Field: s.field, peer: msg.peer}
|
2019-02-22 09:23:14 +00:00
|
|
|
// We asked for a single field, should only get that field or it doesn't exist
|
|
|
|
if n := msg.Put[parentSoul]; n != nil && n.Values[s.field] != nil {
|
|
|
|
f.Value = &ValueWithState{n.Values[s.field], n.State[s.field]}
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
2019-02-22 09:23:14 +00:00
|
|
|
// TODO: conflict resolution and defer
|
|
|
|
// TODO: dedupe
|
|
|
|
// TODO: store and cache
|
|
|
|
safeValueFetchSend(ch, f)
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Send async, sending back errors
|
|
|
|
go func() {
|
|
|
|
for peerErr := range s.gun.Send(ctx, req) {
|
|
|
|
safeValueFetchSend(ch, &ValueFetch{
|
|
|
|
Err: peerErr.Err,
|
|
|
|
Field: s.field,
|
2019-02-22 18:40:02 +00:00
|
|
|
peer: peerErr.peer,
|
2019-02-22 06:46:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scoped) Off(ch <-chan *ValueFetch) bool {
|
|
|
|
s.valueChansToListenersLock.Lock()
|
|
|
|
l := s.valueChansToListeners[ch]
|
|
|
|
delete(s.valueChansToListeners, ch)
|
|
|
|
s.valueChansToListenersLock.Unlock()
|
|
|
|
if l != nil {
|
|
|
|
// Unregister the chan
|
|
|
|
s.gun.UnregisterMessageIDPutListener(l.id)
|
|
|
|
// Close the message chan and the value chan
|
|
|
|
close(l.receivedMessages)
|
|
|
|
close(l.values)
|
|
|
|
}
|
|
|
|
return l != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scoped) Scoped(ctx context.Context, key string, children ...string) *Scoped {
|
2019-02-22 09:23:14 +00:00
|
|
|
ret := newScoped(s.gun, s, key)
|
|
|
|
for _, child := range children {
|
|
|
|
ret = newScoped(s.gun, ret, child)
|
|
|
|
}
|
|
|
|
return ret
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func safeValueFetchSend(ch chan<- *ValueFetch, f *ValueFetch) {
|
|
|
|
// Due to the fact that we may send on a closed channel here, we ignore the panic
|
|
|
|
defer func() { recover() }()
|
|
|
|
ch <- f
|
|
|
|
}
|