v2ray-core/app/pubsub/pubsub.go

46 lines
1006 B
Go
Raw Normal View History

2015-12-11 14:56:10 +00:00
package pubsub
import (
"github.com/v2ray/v2ray-core/app"
)
2016-01-31 16:01:28 +00:00
const (
APP_ID = app.ID(3)
)
2015-12-11 14:56:10 +00:00
2016-01-31 16:01:28 +00:00
type PubsubMessage []byte
type TopicHandler func(PubsubMessage)
2015-12-11 14:56:10 +00:00
2016-01-31 16:01:28 +00:00
type Pubsub interface {
Publish(topic string, message PubsubMessage)
Subscribe(topic string, handler TopicHandler)
2015-12-11 14:56:10 +00:00
}
2016-01-31 16:01:28 +00:00
type pubsubWithContext interface {
Publish(context app.Context, topic string, message PubsubMessage)
Subscribe(context app.Context, topic string, handler TopicHandler)
2015-12-11 14:56:10 +00:00
}
2016-01-31 16:01:28 +00:00
type contextedPubsub struct {
context app.Context
pubsub pubsubWithContext
2015-12-11 14:56:10 +00:00
}
2016-01-31 16:01:28 +00:00
func (this *contextedPubsub) Publish(topic string, message PubsubMessage) {
this.pubsub.Publish(this.context, topic, message)
2015-12-11 14:56:10 +00:00
}
2016-01-31 16:01:28 +00:00
func (this *contextedPubsub) Subscribe(topic string, handler TopicHandler) {
this.pubsub.Subscribe(this.context, topic, handler)
2015-12-11 14:56:10 +00:00
}
2016-01-31 16:01:28 +00:00
func init() {
app.RegisterApp(APP_ID, func(context app.Context, obj interface{}) interface{} {
pubsub := obj.(pubsubWithContext)
return &contextedPubsub{
context: context,
pubsub: pubsub,
}
})
2015-12-11 14:56:10 +00:00
}