2015-04-26 21:33:35 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
type Broker interface {
|
|
|
|
Address() string
|
|
|
|
Connect() error
|
|
|
|
Disconnect() error
|
|
|
|
Init() error
|
2015-06-12 21:52:27 +03:00
|
|
|
Publish(string, *Message) error
|
|
|
|
Subscribe(string, Handler) (Subscriber, error)
|
2015-04-26 21:33:35 +03:00
|
|
|
}
|
|
|
|
|
2015-06-12 21:52:27 +03:00
|
|
|
type Handler func(*Message)
|
|
|
|
|
2015-04-26 21:33:35 +03:00
|
|
|
type Message struct {
|
2015-06-12 21:52:27 +03:00
|
|
|
Header map[string]string
|
|
|
|
Body []byte
|
2015-04-26 21:33:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Subscriber interface {
|
|
|
|
Topic() string
|
|
|
|
Unsubscribe() error
|
|
|
|
}
|
|
|
|
|
2015-05-16 02:34:02 +03:00
|
|
|
type options struct{}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type Option func(*options)
|
2015-05-16 02:34:02 +03:00
|
|
|
|
2015-04-26 21:33:35 +03:00
|
|
|
var (
|
2015-06-12 21:52:27 +03:00
|
|
|
DefaultBroker Broker = newHttpBroker([]string{})
|
2015-04-26 21:33:35 +03:00
|
|
|
)
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func NewBroker(addrs []string, opt ...Option) Broker {
|
2015-06-12 21:52:27 +03:00
|
|
|
return newHttpBroker(addrs, opt...)
|
2015-05-23 22:04:16 +03:00
|
|
|
}
|
|
|
|
|
2015-04-26 21:33:35 +03:00
|
|
|
func Init() error {
|
|
|
|
return DefaultBroker.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Connect() error {
|
|
|
|
return DefaultBroker.Connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Disconnect() error {
|
|
|
|
return DefaultBroker.Disconnect()
|
|
|
|
}
|
|
|
|
|
2015-06-12 21:52:27 +03:00
|
|
|
func Publish(topic string, msg *Message) error {
|
|
|
|
return DefaultBroker.Publish(topic, msg)
|
2015-04-26 21:33:35 +03:00
|
|
|
}
|
|
|
|
|
2015-06-12 21:52:27 +03:00
|
|
|
func Subscribe(topic string, handler Handler) (Subscriber, error) {
|
|
|
|
return DefaultBroker.Subscribe(topic, handler)
|
2015-04-26 21:33:35 +03:00
|
|
|
}
|