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