2020-07-13 05:25:48 +00:00
|
|
|
package bbgo
|
|
|
|
|
2022-06-09 07:49:13 +00:00
|
|
|
import (
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-06-13 03:20:29 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2022-06-09 07:49:13 +00:00
|
|
|
)
|
2022-06-04 17:09:31 +00:00
|
|
|
|
2020-07-16 07:36:02 +00:00
|
|
|
type Notifier interface {
|
2021-05-12 04:37:48 +00:00
|
|
|
NotifyTo(channel string, obj interface{}, args ...interface{})
|
|
|
|
Notify(obj interface{}, args ...interface{})
|
2020-07-16 07:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NullNotifier struct{}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (n *NullNotifier) NotifyTo(channel string, obj interface{}, args ...interface{}) {}
|
2020-10-22 02:47:54 +00:00
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (n *NullNotifier) Notify(obj interface{}, args ...interface{}) {}
|
2020-10-22 02:54:03 +00:00
|
|
|
|
|
|
|
type Notifiability struct {
|
2020-10-27 01:24:59 +00:00
|
|
|
notifiers []Notifier
|
2021-03-16 10:39:18 +00:00
|
|
|
SessionChannelRouter *PatternChannelRouter `json:"-"`
|
|
|
|
SymbolChannelRouter *PatternChannelRouter `json:"-"`
|
|
|
|
ObjectChannelRouter *ObjectChannelRouter `json:"-"`
|
2020-10-27 01:24:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
// RouteSymbol routes symbol name to channel
|
2020-10-27 01:24:59 +00:00
|
|
|
func (m *Notifiability) RouteSymbol(symbol string) (channel string, ok bool) {
|
2020-11-17 00:19:22 +00:00
|
|
|
if m.SymbolChannelRouter != nil {
|
|
|
|
return m.SymbolChannelRouter.Route(symbol)
|
|
|
|
}
|
|
|
|
return "", false
|
2020-10-27 01:24:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
// RouteSession routes Session name to channel
|
2020-10-27 01:24:59 +00:00
|
|
|
func (m *Notifiability) RouteSession(session string) (channel string, ok bool) {
|
2020-11-17 00:19:22 +00:00
|
|
|
if m.SessionChannelRouter != nil {
|
|
|
|
return m.SessionChannelRouter.Route(session)
|
|
|
|
}
|
|
|
|
return "", false
|
2020-10-27 01:24:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:24:27 +00:00
|
|
|
// RouteObject routes object to channel
|
2020-10-27 01:24:59 +00:00
|
|
|
func (m *Notifiability) RouteObject(obj interface{}) (channel string, ok bool) {
|
2020-11-17 00:19:22 +00:00
|
|
|
if m.ObjectChannelRouter != nil {
|
|
|
|
return m.ObjectChannelRouter.Route(obj)
|
|
|
|
}
|
|
|
|
return "", false
|
2020-10-22 02:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:24:27 +00:00
|
|
|
// AddNotifier adds the notifier that implements the Notifier interface.
|
2020-10-22 02:54:03 +00:00
|
|
|
func (m *Notifiability) AddNotifier(notifier Notifier) {
|
|
|
|
m.notifiers = append(m.notifiers, notifier)
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (m *Notifiability) Notify(obj interface{}, args ...interface{}) {
|
2022-06-04 17:09:31 +00:00
|
|
|
if str, ok := obj.(string); ok {
|
2022-06-13 03:20:29 +00:00
|
|
|
simpleArgs := util.FilterSimpleArgs(args)
|
2022-06-09 04:31:50 +00:00
|
|
|
logrus.Infof(str, simpleArgs...)
|
2022-06-04 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 02:54:03 +00:00
|
|
|
for _, n := range m.notifiers {
|
2021-05-12 04:37:48 +00:00
|
|
|
n.Notify(obj, args...)
|
2020-10-22 06:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (m *Notifiability) NotifyTo(channel string, obj interface{}, args ...interface{}) {
|
2020-10-22 06:45:05 +00:00
|
|
|
for _, n := range m.notifiers {
|
2021-05-12 04:37:48 +00:00
|
|
|
n.NotifyTo(channel, obj, args...)
|
2020-10-22 02:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-09 04:31:50 +00:00
|
|
|
|