You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/app/space.go

133 lines
2.7 KiB

package app
import (
"context"
8 years ago
"reflect"
8 years ago
"v2ray.com/core/common"
"v2ray.com/core/common/event"
)
7 years ago
// Application is a component that runs in Space.
type Application interface {
8 years ago
Interface() interface{}
Start() error
Close()
}
7 years ago
// CreateAppFromConfig creates an Application based on its config. Application must have been registered.
8 years ago
func CreateAppFromConfig(ctx context.Context, config interface{}) (Application, error) {
application, err := common.CreateObject(ctx, config)
if err != nil {
return nil, err
}
8 years ago
switch a := application.(type) {
case Application:
return a, nil
default:
return nil, newError("not an application")
}
}
// A Space contains all apps that may be available in a V2Ray runtime.
type Space interface {
event.Registry
8 years ago
GetApplication(appInterface interface{}) Application
AddApplication(application Application) error
Initialize() error
Start() error
Close()
}
const (
7 years ago
// SpaceInitializing is an event to be fired when Space is being initialized.
SpaceInitializing event.Event = iota
)
type spaceImpl struct {
event.Listener
8 years ago
cache map[reflect.Type]Application
initialized bool
}
9 years ago
7 years ago
// NewSpace creates a new Space.
func NewSpace() Space {
return &spaceImpl{
cache: make(map[reflect.Type]Application),
}
}
func (s *spaceImpl) On(e event.Event, h event.Handler) {
if e == SpaceInitializing && s.initialized {
_ = h(nil) // Ignore error
return
}
s.Listener.On(e, h)
}
8 years ago
func (s *spaceImpl) Initialize() error {
if s.initialized {
return nil
}
8 years ago
s.initialized = true
return s.Fire(SpaceInitializing, nil)
}
8 years ago
func (s *spaceImpl) GetApplication(appInterface interface{}) Application {
if s == nil {
8 years ago
return nil
}
8 years ago
appType := reflect.TypeOf(appInterface)
8 years ago
return s.cache[appType]
}
8 years ago
func (s *spaceImpl) AddApplication(app Application) error {
if s == nil {
return newError("nil space").AtError()
8 years ago
}
8 years ago
appType := reflect.TypeOf(app.Interface())
8 years ago
s.cache[appType] = app
return nil
}
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)
)
8 years ago
func AddApplicationToSpace(ctx context.Context, appConfig interface{}) error {
space := SpaceFromContext(ctx)
if space == nil {
return newError("no space in context").AtError()
8 years ago
}
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)
}