space.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package app
  2. type Context interface {
  3. CallerTag() string
  4. }
  5. type contextImpl struct {
  6. callerTag string
  7. }
  8. func (this *contextImpl) CallerTag() string {
  9. return this.callerTag
  10. }
  11. type SpaceController struct {
  12. packetDispatcher PacketDispatcherWithContext
  13. dnsCache DnsCacheWithContext
  14. }
  15. func NewSpaceController() *SpaceController {
  16. return new(SpaceController)
  17. }
  18. func (this *SpaceController) Bind(object interface{}) {
  19. if packetDispatcher, ok := object.(PacketDispatcherWithContext); ok {
  20. this.packetDispatcher = packetDispatcher
  21. }
  22. if dnsCache, ok := object.(DnsCacheWithContext); ok {
  23. this.dnsCache = dnsCache
  24. }
  25. }
  26. func (this *SpaceController) ForContext(tag string) *Space {
  27. return newSpace(this, &contextImpl{callerTag: tag})
  28. }
  29. type Space struct {
  30. packetDispatcher PacketDispatcher
  31. dnsCache DnsCache
  32. }
  33. func newSpace(controller *SpaceController, context Context) *Space {
  34. space := new(Space)
  35. if controller.packetDispatcher != nil {
  36. space.packetDispatcher = &contextedPacketDispatcher{
  37. context: context,
  38. packetDispatcher: controller.packetDispatcher,
  39. }
  40. }
  41. if controller.dnsCache != nil {
  42. space.dnsCache = &contextedDnsCache{
  43. context: context,
  44. dnsCache: controller.dnsCache,
  45. }
  46. }
  47. return space
  48. }
  49. func (this *Space) HasPacketDispatcher() bool {
  50. return this.packetDispatcher != nil
  51. }
  52. func (this *Space) PacketDispatcher() PacketDispatcher {
  53. return this.packetDispatcher
  54. }
  55. func (this *Space) HasDnsCache() bool {
  56. return this.dnsCache != nil
  57. }
  58. func (this *Space) DnsCache() DnsCache {
  59. return this.dnsCache
  60. }