2023-08-11 13:01:32 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2023-09-02 17:30:52 -06:00
|
|
|
"reflect"
|
2023-08-11 13:01:32 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Listener extension provides a channel that can receive signals on a different thread
|
|
|
|
type ListenerExt struct {
|
2023-09-12 19:00:48 -06:00
|
|
|
Buffer int `gv:"buffer"`
|
2023-08-11 13:01:32 -06:00
|
|
|
Chan chan Signal
|
|
|
|
}
|
|
|
|
|
2024-03-03 16:37:03 -07:00
|
|
|
func (ext *ListenerExt) Load(ctx *Context, node *Node) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *ListenerExt) Unload(ctx *Context, node *Node) {
|
|
|
|
}
|
|
|
|
|
2023-09-12 19:00:48 -06:00
|
|
|
func (ext *ListenerExt) PostDeserialize(ctx *Context) error {
|
|
|
|
ext.Chan = make(chan Signal, ext.Buffer)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 13:01:32 -06:00
|
|
|
// Create a new listener extension with a given buffer size
|
|
|
|
func NewListenerExt(buffer int) *ListenerExt {
|
|
|
|
return &ListenerExt{
|
|
|
|
Buffer: buffer,
|
|
|
|
Chan: make(chan Signal, buffer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the signal to the channel, logging an overflow if it occurs
|
2023-10-07 23:00:07 -06:00
|
|
|
func (ext *ListenerExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) (Messages, Changes) {
|
2023-11-04 18:56:35 -06:00
|
|
|
ctx.Log.Logf("listener", "%s - %+v", node.ID, reflect.TypeOf(signal))
|
|
|
|
ctx.Log.Logf("listener_debug", "%s->%s - %+v", source, node.ID, signal)
|
2023-08-11 13:01:32 -06:00
|
|
|
select {
|
|
|
|
case ext.Chan <- signal:
|
|
|
|
default:
|
|
|
|
ctx.Log.Logf("listener", "LISTENER_OVERFLOW: %s", node.ID)
|
|
|
|
}
|
2023-10-15 20:35:02 -06:00
|
|
|
switch sig := signal.(type) {
|
|
|
|
case *StatusSignal:
|
2023-11-04 23:21:43 -06:00
|
|
|
ctx.Log.Logf("listener_status", "%s - %+v", sig.Source, sig.Changes)
|
2023-10-15 20:35:02 -06:00
|
|
|
}
|
2023-10-07 23:00:07 -06:00
|
|
|
return nil, nil
|
2023-08-11 13:01:32 -06:00
|
|
|
}
|