Wrap ModuleManager to ModuleAPI interface

This commit is contained in:
ChronosX88 2020-02-12 21:51:23 +04:00
parent b4ec836327
commit c0ed5c7acf
7 changed files with 27 additions and 16 deletions

View File

@ -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))
}
}

View File

@ -5,7 +5,7 @@ const (
)
type Module interface {
Initialize(moduleAPI *ModuleManager)
Initialize(moduleAPI ModuleAPI)
Name() string
Version() string
}

View File

@ -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]

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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)

View File

@ -16,7 +16,7 @@ func NewRouter() (*Router, error) {
return nil, err
}
r := &Router{
moduleManager: mm,
moduleManager: mm.(*ModuleManager),
}
return r, nil
}