2015-04-26 21:33:35 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Broker interface {
|
|
|
|
Address() string
|
|
|
|
Connect() error
|
|
|
|
Disconnect() error
|
|
|
|
Init() error
|
|
|
|
Publish(string, []byte) error
|
|
|
|
Subscribe(string, func(*Message)) (Subscriber, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Id string
|
|
|
|
Timestamp int64
|
|
|
|
Topic string
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type Subscriber interface {
|
|
|
|
Topic() string
|
|
|
|
Unsubscribe() error
|
|
|
|
}
|
|
|
|
|
2015-05-16 02:34:02 +03:00
|
|
|
type options struct{}
|
|
|
|
|
|
|
|
type Options func(*options)
|
|
|
|
|
2015-04-26 21:33:35 +03:00
|
|
|
var (
|
|
|
|
Address string
|
|
|
|
Id string
|
|
|
|
DefaultBroker Broker
|
|
|
|
)
|
|
|
|
|
|
|
|
func Init() error {
|
|
|
|
if len(Id) == 0 {
|
|
|
|
Id = "broker-" + uuid.NewUUID().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if DefaultBroker == nil {
|
2015-05-16 02:34:02 +03:00
|
|
|
DefaultBroker = NewHttpBroker([]string{Address})
|
2015-04-26 21:33:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return DefaultBroker.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Connect() error {
|
|
|
|
return DefaultBroker.Connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Disconnect() error {
|
|
|
|
return DefaultBroker.Disconnect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Publish(topic string, data []byte) error {
|
|
|
|
return DefaultBroker.Publish(topic, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Subscribe(topic string, function func(*Message)) (Subscriber, error) {
|
|
|
|
return DefaultBroker.Subscribe(topic, function)
|
|
|
|
}
|