From c0ed5c7acf28a75f2c0f174a0be4160747e7e37c Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 12 Feb 2020 21:51:23 +0400 Subject: [PATCH] Wrap ModuleManager to ModuleAPI interface --- core/globals.go | 6 +++--- core/module.go | 2 +- core/module_manager.go | 16 +++++++++++++--- core/module_rpc_receiver.go | 2 +- core/module_rpc_sender.go | 13 +++++++------ core/plugin_manager.go | 2 +- core/router.go | 2 +- 7 files changed, 27 insertions(+), 16 deletions(-) diff --git a/core/globals.go b/core/globals.go index 9f7a902..83fb485 100644 --- a/core/globals.go +++ b/core/globals.go @@ -5,7 +5,7 @@ import ( ) var ( - moduleMgr *ModuleManager + moduleMgr ModuleAPI router *Router authManager *AuthManager serverDomain string @@ -31,11 +31,11 @@ func InitializeContext(sDomain string, pluginsDirPath string, enabledPlugins []s serverDomain = sDomain for _, v := range BuiltinPlugins { - go v.Initialize(moduleMgr) // Initialize builtin plugins + go v.Initialize(ModuleAPI(moduleMgr)) // Initialize builtin plugins } pluginManager = NewPluginManager() for _, v := range enabledPlugins { - pluginManager.StartPlugin(pluginsDirPath, v, moduleMgr) + pluginManager.StartPlugin(pluginsDirPath, v, moduleMgr.(*ModuleManager)) } } diff --git a/core/module.go b/core/module.go index 952003a..a25135c 100644 --- a/core/module.go +++ b/core/module.go @@ -5,7 +5,7 @@ const ( ) type Module interface { - Initialize(moduleAPI *ModuleManager) + Initialize(moduleAPI ModuleAPI) Name() string Version() string } diff --git a/core/module_manager.go b/core/module_manager.go index d111488..19f7749 100644 --- a/core/module_manager.go +++ b/core/module_manager.go @@ -19,7 +19,17 @@ type ModuleManager struct { coreEventHandlers map[string][]func(sourceModuleName string, event map[string]interface{}) } -func NewModuleManager() (*ModuleManager, error) { +type ModuleAPI interface { + Hook(messageType string, anonymousAllowed bool, handlerFunc func(origin *OriginC2S, message models.BaseMessage)) + HookInternalEvent(eventName string, handlerFunc func(sourceModuleName string, event map[string]interface{})) + Unhook(messageType string, handlerFunc func(origin *OriginC2S, message models.BaseMessage)) + UnhookInternalEvent(eventName string, handlerFunc func(sourceModuleName string, event map[string]interface{})) + FireEvent(sourceModuleName string, eventName string, eventPayload map[string]interface{}) + GenerateToken(entityID, deviceID string, tokenExpireTimeDuration time.Duration) (string, error) + GetServerDomain() string +} + +func NewModuleManager() (ModuleAPI, error) { var mm = &ModuleManager{ c2sMessageHandlers: make(map[string][]*C2SMessageHandler), coreEventHandlers: make(map[string][]func(sourceModuleName string, event map[string]interface{})), @@ -36,7 +46,7 @@ func (mm *ModuleManager) Hook(messageType string, anonymousAllowed bool, handler mm.moduleMutex.Unlock() } -func (mm *ModuleManager) HookcoreEvent(eventName string, handlerFunc func(sourceModuleName string, event map[string]interface{})) { +func (mm *ModuleManager) HookInternalEvent(eventName string, handlerFunc func(sourceModuleName string, event map[string]interface{})) { mm.moduleMutex.Lock() mm.coreEventHandlers[eventName] = append(mm.coreEventHandlers[eventName], handlerFunc) mm.moduleMutex.Unlock() @@ -59,7 +69,7 @@ func (mm *ModuleManager) Unhook(messageType string, handlerFunc func(origin *Ori } } -func (mm *ModuleManager) UnhookcoreEvent(eventName string, handlerFunc func(sourceModuleName string, event map[string]interface{})) { +func (mm *ModuleManager) UnhookInternalEvent(eventName string, handlerFunc func(sourceModuleName string, event map[string]interface{})) { mm.moduleMutex.Lock() defer mm.moduleMutex.Unlock() var handlers = mm.coreEventHandlers[eventName] diff --git a/core/module_rpc_receiver.go b/core/module_rpc_receiver.go index eabfcca..45c183d 100644 --- a/core/module_rpc_receiver.go +++ b/core/module_rpc_receiver.go @@ -32,7 +32,7 @@ func (p *greeterServer) Version(nothing interface{}, result *string) error { } // StartTime calls the plugin implementation to initialize plugin -func (p *greeterServer) Initialize(moduleAPI *ModuleManager, emptyResult interface{}) error { +func (p *greeterServer) Initialize(moduleAPI ModuleAPI, emptyResult interface{}) error { p.Module.Initialize(moduleAPI) return nil } diff --git a/core/module_rpc_sender.go b/core/module_rpc_sender.go index 0dae804..151e975 100644 --- a/core/module_rpc_sender.go +++ b/core/module_rpc_sender.go @@ -1,9 +1,9 @@ package core import ( - "log" "net/rpc" + "github.com/google/logger" "github.com/hashicorp/go-plugin" ) @@ -22,7 +22,7 @@ func (p *moduleClient) Name() string { var resp string err := p.Client.Call("Plugin.Name", new(interface{}), &resp) if err != nil { - log.Fatal(err) // FIXME + logger.Fatal(err) // FIXME } return resp } @@ -32,17 +32,18 @@ func (p *moduleClient) Version() string { var resp string err := p.Client.Call("Plugin.Version", new(interface{}), &resp) if err != nil { - log.Fatal(err) // FIXME + logger.Fatal(err) // FIXME } return resp } // StartTime initiates an RPC call to the plugin for initializing -func (p *moduleClient) Initialize(moduleAPI *ModuleManager) { +func (p *moduleClient) Initialize(moduleAPI ModuleAPI) { + var resp interface{} err := p.Client.Call("Plugin.Initialize", map[string]interface{}{ "moduleAPI": moduleAPI, - }, nil) + }, &resp) if err != nil { - log.Fatal(err) // FIXME + logger.Fatal(err) // FIXME } } diff --git a/core/plugin_manager.go b/core/plugin_manager.go index a38e0b8..e67a604 100644 --- a/core/plugin_manager.go +++ b/core/plugin_manager.go @@ -15,7 +15,7 @@ func NewPluginManager() *PluginManager { return &PluginManager{} } -func (p *PluginManager) StartPlugin(pluginsDirPath, pluginFile string, moduleManager *ModuleManager) error { +func (p *PluginManager) StartPlugin(pluginsDirPath, pluginFile string, moduleManager ModuleAPI) error { pluginsDirectory, _ := filepath.Abs(filepath.Dir(pluginsDirPath)) pluginFile = filepath.Join(pluginsDirectory, pluginFile) diff --git a/core/router.go b/core/router.go index 3247cb9..ecfa8dd 100644 --- a/core/router.go +++ b/core/router.go @@ -16,7 +16,7 @@ func NewRouter() (*Router, error) { return nil, err } r := &Router{ - moduleManager: mm, + moduleManager: mm.(*ModuleManager), } return r, nil }