bbgo_origin/pkg/bbgo/notifier.go

73 lines
1.9 KiB
Go
Raw Normal View History

2020-07-13 05:25:48 +00:00
package bbgo
2022-06-09 07:49:13 +00:00
import (
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/util"
2022-06-09 07:49:13 +00:00
)
2020-07-16 07:36:02 +00:00
type Notifier interface {
NotifyTo(channel string, obj interface{}, args ...interface{})
Notify(obj interface{}, args ...interface{})
2020-07-16 07:36:02 +00:00
}
type NullNotifier struct{}
func (n *NullNotifier) NotifyTo(channel string, obj interface{}, args ...interface{}) {}
2020-10-22 02:47:54 +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
}
// RouteSymbol routes symbol name to channel
2020-10-27 01:24:59 +00:00
func (m *Notifiability) RouteSymbol(symbol string) (channel string, ok bool) {
if m.SymbolChannelRouter != nil {
return m.SymbolChannelRouter.Route(symbol)
}
return "", false
2020-10-27 01:24:59 +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) {
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) {
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)
}
func (m *Notifiability) Notify(obj interface{}, args ...interface{}) {
if str, ok := obj.(string); ok {
simpleArgs := util.FilterSimpleArgs(args)
2022-06-09 04:31:50 +00:00
logrus.Infof(str, simpleArgs...)
}
2020-10-22 02:54:03 +00:00
for _, n := range m.notifiers {
n.Notify(obj, args...)
2020-10-22 06:45:05 +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 {
n.NotifyTo(channel, obj, args...)
2020-10-22 02:54:03 +00:00
}
}
2022-06-09 04:31:50 +00:00