2015-12-05 16:55:45 -05:00
|
|
|
package app
|
|
|
|
|
2017-01-04 06:52:24 -05:00
|
|
|
import "v2ray.com/core/common/errors"
|
2016-05-18 11:12:04 -04:00
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
type ID int
|
|
|
|
|
2015-12-11 06:01:20 -05:00
|
|
|
// Context of a function call from proxy to app.
|
2015-12-10 17:55:39 -05:00
|
|
|
type Context interface {
|
|
|
|
CallerTag() string
|
|
|
|
}
|
|
|
|
|
2016-05-18 02:05:52 -04:00
|
|
|
type Caller interface {
|
|
|
|
Tag() string
|
|
|
|
}
|
|
|
|
|
2016-05-18 11:12:04 -04:00
|
|
|
type Application interface {
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApplicationInitializer func() error
|
2016-10-16 10:04:30 -04:00
|
|
|
type ApplicationFactory interface {
|
|
|
|
Create(space Space, config interface{}) (Application, error)
|
|
|
|
AppId() ID
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-10-17 08:35:13 -04:00
|
|
|
applicationFactoryCache = make(map[string]ApplicationFactory)
|
2016-10-16 10:04:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func RegisterApplicationFactory(name string, factory ApplicationFactory) error {
|
|
|
|
applicationFactoryCache[name] = factory
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-18 11:12:04 -04:00
|
|
|
|
2015-12-11 06:01:20 -05: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-05-18 11:12:04 -04:00
|
|
|
Initialize() error
|
|
|
|
InitializeApplication(ApplicationInitializer)
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
HasApp(ID) bool
|
2016-05-18 11:12:04 -04:00
|
|
|
GetApp(ID) Application
|
|
|
|
BindApp(ID, Application)
|
2016-10-16 10:04:30 -04:00
|
|
|
BindFromConfig(name string, config interface{}) error
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type spaceImpl struct {
|
2016-05-22 13:32:28 -04:00
|
|
|
cache map[ID]Application
|
|
|
|
appInit []ApplicationInitializer
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
2015-12-11 09:56:10 -05:00
|
|
|
|
2016-05-18 02:05:52 -04:00
|
|
|
func NewSpace() Space {
|
|
|
|
return &spaceImpl{
|
2016-05-22 13:32:28 -04:00
|
|
|
cache: make(map[ID]Application),
|
|
|
|
appInit: make([]ApplicationInitializer, 0, 32),
|
2016-05-18 11:12:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *spaceImpl) InitializeApplication(f ApplicationInitializer) {
|
|
|
|
v.appInit = append(v.appInit, f)
|
2016-05-18 11:12:04 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *spaceImpl) Initialize() error {
|
|
|
|
for _, f := range v.appInit {
|
2016-05-22 13:32:28 -04:00
|
|
|
err := f()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
2016-05-18 11:12:04 -04:00
|
|
|
return nil
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *spaceImpl) HasApp(id ID) bool {
|
|
|
|
_, found := v.cache[id]
|
2016-01-31 11:01:28 -05:00
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *spaceImpl) GetApp(id ID) Application {
|
|
|
|
obj, found := v.cache[id]
|
2016-01-31 11:01:28 -05:00
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *spaceImpl) BindApp(id ID, application Application) {
|
|
|
|
v.cache[id] = application
|
2015-12-05 16:55:45 -05:00
|
|
|
}
|
2016-10-16 10:04:30 -04:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *spaceImpl) BindFromConfig(name string, config interface{}) error {
|
2016-10-16 10:04:30 -04:00
|
|
|
factory, found := applicationFactoryCache[name]
|
|
|
|
if !found {
|
2016-12-04 03:10:47 -05:00
|
|
|
return errors.New("Space: app not registered: ", name)
|
2016-10-16 10:04:30 -04:00
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
app, err := factory.Create(v, config)
|
2016-10-16 10:04:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
v.BindApp(factory.AppId(), app)
|
2016-10-16 10:04:30 -04:00
|
|
|
return nil
|
|
|
|
}
|