| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package app
- type ID int
- const (
- PACKET_DISPATCHER = ID(1)
- DNS_CACHE = ID(2)
- PUBSUB = ID(3)
- INBOUND_HANDLER_MANAGER = ID(4)
- )
- // Context of a function call from proxy to app.
- type Context interface {
- CallerTag() string
- }
- // A Space contains all apps that may be available in a V2Ray runtime.
- // Caller must check the availability of an app by calling HasXXX before getting its instance.
- type Space interface {
- HasApp(ID) bool
- GetApp(ID) interface{}
- }
- type ForContextCreator func(Context, interface{}) interface{}
- var (
- metadataCache = make(map[ID]ForContextCreator)
- )
- func RegisterApp(id ID, creator ForContextCreator) {
- // TODO: check id
- metadataCache[id] = creator
- }
- type contextImpl struct {
- callerTag string
- }
- func (this *contextImpl) CallerTag() string {
- return this.callerTag
- }
- type spaceImpl struct {
- cache map[ID]interface{}
- tag string
- }
- func newSpaceImpl(tag string, cache map[ID]interface{}) *spaceImpl {
- space := &spaceImpl{
- tag: tag,
- cache: make(map[ID]interface{}),
- }
- context := &contextImpl{
- callerTag: tag,
- }
- for id, object := range cache {
- creator, found := metadataCache[id]
- if found {
- space.cache[id] = creator(context, object)
- }
- }
- return space
- }
- func (this *spaceImpl) HasApp(id ID) bool {
- _, found := this.cache[id]
- return found
- }
- func (this *spaceImpl) GetApp(id ID) interface{} {
- obj, found := this.cache[id]
- if !found {
- return nil
- }
- return obj
- }
- // A SpaceController is supposed to be used by a shell to create Spaces. It should not be used
- // directly by proxies.
- type SpaceController struct {
- objectCache map[ID]interface{}
- }
- func NewController() *SpaceController {
- return &SpaceController{
- objectCache: make(map[ID]interface{}),
- }
- }
- func (this *SpaceController) Bind(id ID, object interface{}) {
- this.objectCache[id] = object
- }
- func (this *SpaceController) ForContext(tag string) Space {
- return newSpaceImpl(tag, this.objectCache)
- }
|