2015-12-05 21:55:45 +00:00
|
|
|
package app
|
|
|
|
|
2016-01-31 16:01:28 +00:00
|
|
|
type ID int
|
|
|
|
|
2015-12-11 11:01:20 +00:00
|
|
|
// Context of a function call from proxy to app.
|
2015-12-10 22:55:39 +00:00
|
|
|
type Context interface {
|
|
|
|
CallerTag() string
|
|
|
|
}
|
|
|
|
|
2016-05-18 06:05:52 +00:00
|
|
|
type Caller interface {
|
|
|
|
Tag() string
|
|
|
|
}
|
|
|
|
|
2015-12-11 11:01:20 +00:00
|
|
|
// 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 {
|
2016-01-31 16:01:28 +00:00
|
|
|
HasApp(ID) bool
|
|
|
|
GetApp(ID) interface{}
|
2016-05-18 06:05:52 +00:00
|
|
|
BindApp(ID, interface{})
|
2016-01-31 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type spaceImpl struct {
|
|
|
|
cache map[ID]interface{}
|
|
|
|
}
|
2015-12-11 14:56:10 +00:00
|
|
|
|
2016-05-18 06:05:52 +00:00
|
|
|
func NewSpace() Space {
|
|
|
|
return &spaceImpl{
|
2016-01-31 16:01:28 +00:00
|
|
|
cache: make(map[ID]interface{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-05-18 06:05:52 +00:00
|
|
|
func (this *spaceImpl) BindApp(id ID, object interface{}) {
|
|
|
|
this.cache[id] = object
|
2015-12-05 21:55:45 +00:00
|
|
|
}
|