forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a540d7d
commit b6ed26a
Showing
8 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/v2ray/v2ray-core/app" | ||
) | ||
|
||
type PubsubWithContext interface { | ||
Publish(context app.Context, topic string, message app.PubsubMessage) | ||
Subscribe(context app.Context, topic string, handler app.TopicHandler) | ||
} | ||
|
||
type contextedPubsub struct { | ||
context app.Context | ||
pubsub PubsubWithContext | ||
} | ||
|
||
func (this *contextedPubsub) Publish(topic string, message app.PubsubMessage) { | ||
this.pubsub.Publish(this.context, topic, message) | ||
} | ||
|
||
func (this *contextedPubsub) Subscribe(topic string, handler app.TopicHandler) { | ||
this.pubsub.Subscribe(this.context, topic, handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package app | ||
|
||
type PubsubMessage []byte | ||
type TopicHandler func(PubsubMessage) | ||
|
||
type Pubsub interface { | ||
Publish(topic string, message PubsubMessage) | ||
Subscribe(topic string, handler TopicHandler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package pubsub | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/v2ray/v2ray-core/app" | ||
"github.com/v2ray/v2ray-core/app/internal" | ||
) | ||
|
||
type TopicHandlerList struct { | ||
sync.RWMutex | ||
handlers []app.TopicHandler | ||
} | ||
|
||
func NewTopicHandlerList(handlers ...app.TopicHandler) *TopicHandlerList { | ||
return &TopicHandlerList{ | ||
handlers: handlers, | ||
} | ||
} | ||
|
||
func (this *TopicHandlerList) Add(handler app.TopicHandler) { | ||
this.Lock() | ||
this.handlers = append(this.handlers, handler) | ||
this.Unlock() | ||
} | ||
|
||
func (this *TopicHandlerList) Dispatch(message app.PubsubMessage) { | ||
this.RLock() | ||
for _, handler := range this.handlers { | ||
go handler(message) | ||
} | ||
this.RUnlock() | ||
} | ||
|
||
type Pubsub struct { | ||
topics map[string]*TopicHandlerList | ||
sync.RWMutex | ||
} | ||
|
||
func New() internal.PubsubWithContext { | ||
return &Pubsub{ | ||
topics: make(map[string]*TopicHandlerList), | ||
} | ||
} | ||
|
||
func (this *Pubsub) Publish(context app.Context, topic string, message app.PubsubMessage) { | ||
this.RLock() | ||
list, found := this.topics[topic] | ||
this.RUnlock() | ||
|
||
if found { | ||
list.Dispatch(message) | ||
} | ||
} | ||
|
||
func (this *Pubsub) Subscribe(context app.Context, topic string, handler app.TopicHandler) { | ||
this.Lock() | ||
defer this.Unlock() | ||
if list, found := this.topics[topic]; found { | ||
list.Add(handler) | ||
} else { | ||
this.topics[topic] = NewTopicHandlerList(handler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package pubsub_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/v2ray/v2ray-core/app" | ||
. "github.com/v2ray/v2ray-core/app/pubsub" | ||
apptesting "github.com/v2ray/v2ray-core/app/testing" | ||
v2testing "github.com/v2ray/v2ray-core/testing" | ||
"github.com/v2ray/v2ray-core/testing/assert" | ||
) | ||
|
||
func TestPubsub(t *testing.T) { | ||
v2testing.Current(t) | ||
|
||
messages := make(map[string]app.PubsubMessage) | ||
|
||
pubsub := New() | ||
pubsub.Subscribe(&apptesting.Context{}, "t1", func(message app.PubsubMessage) { | ||
messages["t1"] = message | ||
}) | ||
|
||
pubsub.Subscribe(&apptesting.Context{}, "t2", func(message app.PubsubMessage) { | ||
messages["t2"] = message | ||
}) | ||
|
||
message := app.PubsubMessage([]byte("This is a pubsub message.")) | ||
pubsub.Publish(&apptesting.Context{}, "t2", message) | ||
<-time.Tick(time.Second) | ||
|
||
_, found := messages["t1"] | ||
assert.Bool(found).IsFalse() | ||
|
||
actualMessage, found := messages["t2"] | ||
assert.Bool(found).IsTrue() | ||
assert.StringLiteral(string(actualMessage)).Equals(string(message)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,7 @@ type Space interface { | |
|
||
HasDnsCache() bool | ||
DnsCache() DnsCache | ||
|
||
HasPubsub() bool | ||
Pubsub() Pubsub | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package command | ||
|
||
type ResponseCmd byte |