1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/app/space.go

107 lines
2.3 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 (
"github.com/golang/protobuf/proto"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
"v2ray.com/core/common/serial"
)
2016-01-31 11:01:28 -05:00
2016-05-18 11:12:04 -04:00
type Application interface {
}
2017-01-06 09:32:36 -05:00
type InitializationCallback func() error
2016-10-16 10:04:30 -04:00
type ApplicationFactory interface {
Create(space Space, config interface{}) (Application, error)
2017-01-06 09:32:36 -05:00
}
type AppGetter interface {
GetApp(name string) Application
2016-10-16 10:04:30 -04:00
}
var (
2016-10-17 08:35:13 -04:00
applicationFactoryCache = make(map[string]ApplicationFactory)
2016-10-16 10:04:30 -04:00
)
2017-01-06 09:32:36 -05:00
func RegisterApplicationFactory(defaultConfig proto.Message, factory ApplicationFactory) error {
if defaultConfig == nil {
return errors.New("Space: config is nil.")
}
name := serial.GetMessageType(defaultConfig)
if len(name) == 0 {
return errors.New("Space: cannot get config type.")
}
2016-10-16 10:04:30 -04:00
applicationFactoryCache[name] = factory
return nil
}
2016-05-18 11:12:04 -04: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-06 09:32:36 -05:00
AddApp(config proto.Message) error
AddAppLegacy(name string, app Application)
2016-05-18 11:12:04 -04:00
Initialize() error
2017-01-06 09:32:36 -05:00
OnInitialize(InitializationCallback)
2016-01-31 11:01:28 -05:00
}
type spaceImpl struct {
2017-01-06 09:32:36 -05:00
initialized bool
cache map[string]Application
appInit []InitializationCallback
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{
2017-01-06 09:32:36 -05:00
cache: make(map[string]Application),
appInit: make([]InitializationCallback, 0, 32),
2016-05-18 11:12:04 -04:00
}
}
2017-01-06 09:32:36 -05:00
func (v *spaceImpl) OnInitialize(f InitializationCallback) {
if v.initialized {
if err := f(); err != nil {
log.Error("Space: error after space initialization: ", err)
}
} else {
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 {
2017-01-06 09:32:36 -05:00
if err := f(); err != nil {
return err
}
2016-01-31 11:01:28 -05:00
}
2017-01-06 09:34:21 -05:00
v.appInit = nil
2017-01-06 09:32:36 -05:00
v.initialized = true
2016-05-18 11:12:04 -04:00
return nil
2016-01-31 11:01:28 -05:00
}
2017-01-06 09:32:36 -05:00
func (v *spaceImpl) GetApp(configType string) Application {
obj, found := v.cache[configType]
2016-01-31 11:01:28 -05:00
if !found {
return nil
}
return obj
}
2017-01-06 09:32:36 -05:00
func (v *spaceImpl) AddApp(config proto.Message) error {
configName := serial.GetMessageType(config)
factory, found := applicationFactoryCache[configName]
2016-10-16 10:04:30 -04:00
if !found {
2017-01-06 09:32:36 -05:00
return errors.New("Space: app not registered: ", configName)
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
}
2017-01-06 09:32:36 -05:00
v.cache[configName] = app
2016-10-16 10:04:30 -04:00
return nil
}
2017-01-06 09:32:36 -05:00
func (v *spaceImpl) AddAppLegacy(name string, application Application) {
v.cache[name] = application
}