1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-30 07:46:41 -04:00
v2fly/app/space.go

133 lines
2.7 KiB
Go
Raw Normal View History

2015-12-05 16:55:45 -05:00
package app
2017-01-06 09:32:36 -05:00
import (
"context"
2017-01-13 07:41:40 -05:00
"reflect"
2017-01-13 07:41:40 -05:00
"v2ray.com/core/common"
2017-11-28 17:41:20 -05:00
"v2ray.com/core/common/event"
2017-01-06 09:32:36 -05:00
)
2016-01-31 11:01:28 -05:00
2017-12-14 09:02:36 -05:00
// Application is a component that runs in Space.
2016-05-18 11:12:04 -04:00
type Application interface {
2017-01-13 07:41:40 -05:00
Interface() interface{}
2017-02-01 15:35:40 -05:00
Start() error
Close()
2016-05-18 11:12:04 -04:00
}
2017-12-14 09:02:36 -05:00
// CreateAppFromConfig creates an Application based on its config. Application must have been registered.
2017-01-13 07:41:40 -05: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 09:32:36 -05:00
}
2017-01-13 07:41:40 -05:00
switch a := application.(type) {
case Application:
return a, nil
default:
2017-04-09 09:04:04 -04:00
return nil, newError("not an application")
2017-01-06 09:32:36 -05:00
}
2016-10-16 10:04:30 -04:00
}
2016-05-18 11:12:04 -04:00
// A Space contains all apps that may be available in a V2Ray runtime.
type Space interface {
2017-11-28 17:41:20 -05:00
event.Registry
2017-01-13 07:41:40 -05:00
GetApplication(appInterface interface{}) Application
AddApplication(application Application) error
2016-05-18 11:12:04 -04:00
Initialize() error
2017-02-01 15:35:40 -05:00
Start() error
Close()
2016-01-31 11:01:28 -05:00
}
2017-11-28 17:41:20 -05:00
const (
2017-11-30 06:55:31 -05:00
// SpaceInitializing is an event to be fired when Space is being initialized.
2017-11-28 17:41:20 -05:00
SpaceInitializing event.Event = iota
)
2016-01-31 11:01:28 -05:00
type spaceImpl struct {
2017-11-28 17:41:20 -05:00
event.Listener
2017-01-13 07:41:40 -05:00
cache map[reflect.Type]Application
2017-11-28 17:41:20 -05:00
initialized bool
2016-01-31 11:01:28 -05:00
}
2015-12-11 09:56:10 -05:00
2017-11-30 06:55:31 -05:00
// NewSpace creates a new Space.
2016-05-18 02:05:52 -04:00
func NewSpace() Space {
return &spaceImpl{
2017-11-28 17:41:20 -05:00
cache: make(map[reflect.Type]Application),
2016-05-18 11:12:04 -04:00
}
}
2017-11-28 17:41:20 -05:00
func (s *spaceImpl) On(e event.Event, h event.Handler) {
if e == SpaceInitializing && s.initialized {
_ = h(nil) // Ignore error
return
2017-01-06 09:32:36 -05:00
}
2017-11-28 17:41:20 -05:00
s.Listener.On(e, h)
2016-05-18 11:12:04 -04:00
}
2017-04-23 13:16:56 -04:00
func (s *spaceImpl) Initialize() error {
2017-11-28 17:41:20 -05:00
if s.initialized {
return nil
2016-01-31 11:01:28 -05:00
}
2017-04-23 13:16:56 -04:00
s.initialized = true
2017-11-28 17:41:20 -05:00
return s.Fire(SpaceInitializing, nil)
2016-01-31 11:01:28 -05:00
}
2017-04-23 13:16:56 -04:00
func (s *spaceImpl) GetApplication(appInterface interface{}) Application {
if s == nil {
2017-01-13 08:33:28 -05:00
return nil
}
2017-01-13 07:41:40 -05:00
appType := reflect.TypeOf(appInterface)
2017-04-23 13:16:56 -04:00
return s.cache[appType]
2016-01-31 11:01:28 -05:00
}
2017-04-23 13:16:56 -04:00
func (s *spaceImpl) AddApplication(app Application) error {
if s == nil {
2017-04-09 09:04:04 -04:00
return newError("nil space").AtError()
2017-01-13 08:33:28 -05:00
}
2017-01-13 07:41:40 -05:00
appType := reflect.TypeOf(app.Interface())
2017-04-23 13:16:56 -04:00
s.cache[appType] = app
2016-10-16 10:04:30 -04:00
return nil
}
2017-01-06 09:32:36 -05:00
2017-02-01 15:35:40 -05: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 07:41:40 -05:00
func AddApplicationToSpace(ctx context.Context, appConfig interface{}) error {
space := SpaceFromContext(ctx)
if space == nil {
2017-04-09 09:04:04 -04:00
return newError("no space in context").AtError()
2017-01-13 07:41:40 -05: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)
}