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
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple load function, unmarshal the buffer int from json
|
2023-08-31 19:50:32 -06:00
|
|
|
func (ext *ListenerExt) DeserializeListenerExt(ctx *Context, data []byte) error {
|
2023-08-11 13:01:32 -06:00
|
|
|
err := json.Unmarshal(data, &ext.Buffer)
|
|
|
|
ext.Chan = make(chan Signal, ext.Buffer)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (listener *ListenerExt) Type() ExtType {
|
|
|
|
return ListenerExtType
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the signal to the channel, logging an overflow if it occurs
|
|
|
|
func (ext *ListenerExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) Messages {
|
2023-09-02 17:30:52 -06:00
|
|
|
ctx.Log.Logf("listener", "LISTENER_PROCESS: %s - %+v", node.ID, reflect.TypeOf(signal))
|
2023-08-11 13:01:32 -06:00
|
|
|
select {
|
|
|
|
case ext.Chan <- signal:
|
|
|
|
default:
|
|
|
|
ctx.Log.Logf("listener", "LISTENER_OVERFLOW: %s", node.ID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-31 19:50:32 -06:00
|
|
|
func (ext *ListenerExt) MarshalBinary() ([]byte, error) {
|
2023-08-11 13:01:32 -06:00
|
|
|
return json.Marshal(ext.Buffer)
|
|
|
|
}
|