1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-02 08:46:04 -04:00
v2fly/app/space.go
2015-12-10 23:55:39 +01:00

75 lines
1.5 KiB
Go

package app
type Context interface {
CallerTag() string
}
type contextImpl struct {
callerTag string
}
func (this *contextImpl) CallerTag() string {
return this.callerTag
}
type SpaceController struct {
packetDispatcher PacketDispatcherWithContext
dnsCache DnsCacheWithContext
}
func NewSpaceController() *SpaceController {
return new(SpaceController)
}
func (this *SpaceController) Bind(object interface{}) {
if packetDispatcher, ok := object.(PacketDispatcherWithContext); ok {
this.packetDispatcher = packetDispatcher
}
if dnsCache, ok := object.(DnsCacheWithContext); ok {
this.dnsCache = dnsCache
}
}
func (this *SpaceController) ForContext(tag string) *Space {
return newSpace(this, &contextImpl{callerTag: tag})
}
type Space struct {
packetDispatcher PacketDispatcher
dnsCache DnsCache
}
func newSpace(controller *SpaceController, context Context) *Space {
space := new(Space)
if controller.packetDispatcher != nil {
space.packetDispatcher = &contextedPacketDispatcher{
context: context,
packetDispatcher: controller.packetDispatcher,
}
}
if controller.dnsCache != nil {
space.dnsCache = &contextedDnsCache{
context: context,
dnsCache: controller.dnsCache,
}
}
return space
}
func (this *Space) HasPacketDispatcher() bool {
return this.packetDispatcher != nil
}
func (this *Space) PacketDispatcher() PacketDispatcher {
return this.packetDispatcher
}
func (this *Space) HasDnsCache() bool {
return this.dnsCache != nil
}
func (this *Space) DnsCache() DnsCache {
return this.dnsCache
}