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