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