|
@ -6,12 +6,15 @@ import ( |
|
|
"git.aiterp.net/lucifer3/server/device" |
|
|
"git.aiterp.net/lucifer3/server/device" |
|
|
"git.aiterp.net/lucifer3/server/events" |
|
|
"git.aiterp.net/lucifer3/server/events" |
|
|
"github.com/gobwas/glob" |
|
|
"github.com/gobwas/glob" |
|
|
|
|
|
"sort" |
|
|
|
|
|
"strings" |
|
|
"sync" |
|
|
"sync" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func NewResolver() *Resolver { |
|
|
func NewResolver() *Resolver { |
|
|
return &Resolver{ |
|
|
return &Resolver{ |
|
|
pointers: make([]*device.Pointer, 0, 128), |
|
|
pointers: make([]*device.Pointer, 0, 128), |
|
|
|
|
|
patternCache: make(map[string]glob.Glob), |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -20,25 +23,12 @@ func NewResolver() *Resolver { |
|
|
type Resolver struct { |
|
|
type Resolver struct { |
|
|
mu sync.Mutex |
|
|
mu sync.Mutex |
|
|
pointers []*device.Pointer |
|
|
pointers []*device.Pointer |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *Resolver) ensure(id string) *device.Pointer { |
|
|
|
|
|
for _, ptr := range r.pointers { |
|
|
|
|
|
if ptr.ID == id { |
|
|
|
|
|
return ptr |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
r.pointers = append(r.pointers, &device.Pointer{ |
|
|
|
|
|
ID: id, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
return r.pointers[len(r.pointers)-1] |
|
|
|
|
|
|
|
|
patternCache map[string]glob.Glob |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *Resolver) resolve(pattern string) []*device.Pointer { |
|
|
func (r *Resolver) resolve(pattern string) []*device.Pointer { |
|
|
g, err := glob.Compile(pattern, ':') |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
|
g := r.patternToGlob(pattern) |
|
|
|
|
|
if g == nil { |
|
|
return []*device.Pointer{} |
|
|
return []*device.Pointer{} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -53,14 +43,14 @@ func (r *Resolver) resolve(pattern string) []*device.Pointer { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *Resolver) Resolve(pattern string) []device.Pointer { |
|
|
func (r *Resolver) Resolve(pattern string) []device.Pointer { |
|
|
g, err := glob.Compile(pattern, ':') |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return []device.Pointer{} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
r.mu.Lock() |
|
|
r.mu.Lock() |
|
|
defer r.mu.Unlock() |
|
|
defer r.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
g := r.patternToGlob(pattern) |
|
|
|
|
|
if g == nil { |
|
|
|
|
|
return []device.Pointer{} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
res := make([]device.Pointer, 0, 32) |
|
|
res := make([]device.Pointer, 0, 32) |
|
|
for _, ptr := range r.pointers { |
|
|
for _, ptr := range r.pointers { |
|
|
if ptr.Matches(g) { |
|
|
if ptr.Matches(g) { |
|
@ -83,6 +73,7 @@ func (r *Resolver) HandleEvent(_ *lucifer3.EventBus, event lucifer3.Event) { |
|
|
ptr := r.ensure(event.ID) |
|
|
ptr := r.ensure(event.ID) |
|
|
if event.InternalName != "" && ptr.Name() == "" { |
|
|
if event.InternalName != "" && ptr.Name() == "" { |
|
|
ptr.AddAlias("lucifer:name:" + event.InternalName) |
|
|
ptr.AddAlias("lucifer:name:" + event.InternalName) |
|
|
|
|
|
r.sortPointers() |
|
|
} |
|
|
} |
|
|
r.mu.Unlock() |
|
|
r.mu.Unlock() |
|
|
} |
|
|
} |
|
@ -95,13 +86,81 @@ func (r *Resolver) HandleCommand(_ *lucifer3.EventBus, command lucifer3.Command) |
|
|
for _, ptr := range r.resolve(command.Match) { |
|
|
for _, ptr := range r.resolve(command.Match) { |
|
|
ptr.AddAlias(command.Alias) |
|
|
ptr.AddAlias(command.Alias) |
|
|
} |
|
|
} |
|
|
|
|
|
if strings.HasPrefix(command.Alias, "lucifer:name:") { |
|
|
|
|
|
r.sortPointers() |
|
|
|
|
|
} |
|
|
r.mu.Unlock() |
|
|
r.mu.Unlock() |
|
|
|
|
|
|
|
|
case commands.RemoveAlias: |
|
|
case commands.RemoveAlias: |
|
|
r.mu.Lock() |
|
|
r.mu.Lock() |
|
|
for _, ptr := range r.resolve(command.Match) { |
|
|
for _, ptr := range r.resolve(command.Match) { |
|
|
ptr.AddAlias(command.Alias) |
|
|
|
|
|
|
|
|
ptr.RemoveAlias(command.Alias) |
|
|
|
|
|
} |
|
|
|
|
|
if strings.HasPrefix(command.Alias, "lucifer:name:") { |
|
|
|
|
|
r.sortPointers() |
|
|
} |
|
|
} |
|
|
r.mu.Unlock() |
|
|
r.mu.Unlock() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *Resolver) ensure(id string) *device.Pointer { |
|
|
|
|
|
for _, ptr := range r.pointers { |
|
|
|
|
|
if ptr.ID == id { |
|
|
|
|
|
return ptr |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
r.pointers = append(r.pointers, &device.Pointer{ |
|
|
|
|
|
ID: id, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
return r.pointers[len(r.pointers)-1] |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *Resolver) patternToGlob(pattern string) glob.Glob { |
|
|
|
|
|
if cached, ok := r.patternCache[pattern]; ok { |
|
|
|
|
|
return cached |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
g, err := glob.Compile(pattern, ':') |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Keep this in bounds. Random cache deletions after 128 length.
|
|
|
|
|
|
if len(r.patternCache) > 128 { |
|
|
|
|
|
i := 64 |
|
|
|
|
|
for k := range r.patternCache { |
|
|
|
|
|
if i == 0 { |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
delete(r.patternCache, k) |
|
|
|
|
|
i -= 1 |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Cache it
|
|
|
|
|
|
r.patternCache[pattern] = g |
|
|
|
|
|
|
|
|
|
|
|
return g |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *Resolver) sortPointers() { |
|
|
|
|
|
sort.Slice(r.pointers, func(i, j int) bool { |
|
|
|
|
|
ni := r.pointers[i].Name() |
|
|
|
|
|
nj := r.pointers[j].Name() |
|
|
|
|
|
|
|
|
|
|
|
if ni == nj { |
|
|
|
|
|
return r.pointers[i].ID < r.pointers[j].ID |
|
|
|
|
|
} |
|
|
|
|
|
if ni == "" { |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
if nj == "" { |
|
|
|
|
|
return true |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ni < nj |
|
|
|
|
|
}) |
|
|
|
|
|
} |