v2ray-core/app/space.go

131 lines
2.6 KiB
Go
Raw Normal View History

2015-12-05 21:55:45 +00:00
package app
2017-01-06 14:32:36 +00:00
import (
"context"
2017-01-13 12:41:40 +00:00
"reflect"
2017-01-13 12:41:40 +00:00
"v2ray.com/core/common"
2017-01-06 14:32:36 +00:00
)
2016-01-31 16:01:28 +00:00
2016-05-18 15:12:04 +00:00
type Application interface {
2017-01-13 12:41:40 +00:00
Interface() interface{}
2017-02-01 20:35:40 +00:00
Start() error
Close()
2016-05-18 15:12:04 +00:00
}
2017-01-06 14:32:36 +00:00
type InitializationCallback func() error
2017-01-13 12:41:40 +00:00
func CreateAppFromConfig(ctx context.Context, config interface{}) (Application, error) {
application, err := common.CreateObject(ctx, config)
if err != nil {
return nil, err
2017-01-06 14:32:36 +00:00
}
2017-01-13 12:41:40 +00:00
switch a := application.(type) {
case Application:
return a, nil
default:
2017-04-08 23:43:25 +00:00
return nil, newError("App: Not an application.")
2017-01-06 14:32:36 +00:00
}
2016-10-16 14:04:30 +00:00
}
2016-05-18 15:12:04 +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 {
2017-01-13 12:41:40 +00:00
GetApplication(appInterface interface{}) Application
AddApplication(application Application) error
2016-05-18 15:12:04 +00:00
Initialize() error
2017-01-06 14:32:36 +00:00
OnInitialize(InitializationCallback)
2017-02-01 20:35:40 +00:00
Start() error
Close()
2016-01-31 16:01:28 +00:00
}
type spaceImpl struct {
2017-01-06 14:32:36 +00:00
initialized bool
2017-01-13 12:41:40 +00:00
cache map[reflect.Type]Application
2017-01-06 14:32:36 +00:00
appInit []InitializationCallback
2016-01-31 16:01:28 +00:00
}
2015-12-11 14:56:10 +00:00
2016-05-18 06:05:52 +00:00
func NewSpace() Space {
return &spaceImpl{
2017-01-13 12:41:40 +00:00
cache: make(map[reflect.Type]Application),
2017-01-06 14:32:36 +00:00
appInit: make([]InitializationCallback, 0, 32),
2016-05-18 15:12:04 +00:00
}
}
2017-01-06 14:32:36 +00:00
func (v *spaceImpl) OnInitialize(f InitializationCallback) {
if v.initialized {
2017-02-01 20:35:40 +00:00
f()
2017-01-06 14:32:36 +00:00
} else {
v.appInit = append(v.appInit, f)
}
2016-05-18 15:12:04 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *spaceImpl) Initialize() error {
for _, f := range v.appInit {
2017-01-06 14:32:36 +00:00
if err := f(); err != nil {
return err
}
2016-01-31 16:01:28 +00:00
}
2017-01-06 14:34:21 +00:00
v.appInit = nil
2017-01-06 14:32:36 +00:00
v.initialized = true
2016-05-18 15:12:04 +00:00
return nil
2016-01-31 16:01:28 +00:00
}
2017-01-13 12:41:40 +00:00
func (v *spaceImpl) GetApplication(appInterface interface{}) Application {
2017-01-13 13:33:28 +00:00
if v == nil {
return nil
}
2017-01-13 12:41:40 +00:00
appType := reflect.TypeOf(appInterface)
return v.cache[appType]
2016-01-31 16:01:28 +00:00
}
2017-01-13 12:41:40 +00:00
func (v *spaceImpl) AddApplication(app Application) error {
2017-01-13 13:33:28 +00:00
if v == nil {
2017-04-08 23:43:25 +00:00
return newError("App: Nil space.")
2017-01-13 13:33:28 +00:00
}
2017-01-13 12:41:40 +00:00
appType := reflect.TypeOf(app.Interface())
v.cache[appType] = app
2016-10-16 14:04:30 +00:00
return nil
}
2017-01-06 14:32:36 +00:00
2017-02-01 20:35:40 +00:00
func (s *spaceImpl) Start() error {
for _, app := range s.cache {
if err := app.Start(); err != nil {
return err
}
}
return nil
}
func (s *spaceImpl) Close() {
for _, app := range s.cache {
app.Close()
}
}
type contextKey int
const (
spaceKey = contextKey(0)
)
2017-01-13 12:41:40 +00:00
func AddApplicationToSpace(ctx context.Context, appConfig interface{}) error {
space := SpaceFromContext(ctx)
if space == nil {
2017-04-08 23:43:25 +00:00
return newError("App: No space in context.")
2017-01-13 12:41:40 +00:00
}
application, err := CreateAppFromConfig(ctx, appConfig)
if err != nil {
return err
}
return space.AddApplication(application)
}
func SpaceFromContext(ctx context.Context) Space {
return ctx.Value(spaceKey).(Space)
}
func ContextWithSpace(ctx context.Context, space Space) context.Context {
return context.WithValue(ctx, spaceKey, space)
}