修改模块树状设计

This commit is contained in:
boyce
2019-01-31 14:10:49 +08:00
parent 236d472481
commit 32b3e1e548
2 changed files with 57 additions and 31 deletions

View File

@@ -56,6 +56,14 @@ type CTestData struct {
Ddd string Ddd string
} }
type CTestModule struct {
service.BaseModule
}
func (ws *CTestModule) DoSomething() {
fmt.Printf("CTestModule do some thing!")
}
func (ws *CTest) RPC_LogTicker2(args *CTestData, quo *CTestData) error { func (ws *CTest) RPC_LogTicker2(args *CTestData, quo *CTestData) error {
*quo = *args *quo = *args
@@ -83,6 +91,14 @@ func (ws *CTest) OnRun() error {
fmt.Print(err, test2) fmt.Print(err, test2)
//} //}
//模块的示例
testModule := CTestModule{}
testModule.SetModuleType(1)
ws.AddModule(&testModule)
pTmpModule := ws.GetModuleByType(1)
pTmpModuleTest := pTmpModule.(*CTestModule)
pTmpModuleTest.DoSomething()
return nil return nil
} }

View File

@@ -18,10 +18,12 @@ type MethodInfo struct {
} }
type IBaseModule interface { type IBaseModule interface {
SetModuleName(modulename string) bool SetModuleType(moduleType uint32)
GetModuleName() string GetModuleType() uint32
OnInit() error OnInit() error
OnRun() error OnRun() error
AddModule(module IBaseModule) bool
GetModuleByType(moduleType uint32) IBaseModule
} }
type IService interface { type IService interface {
@@ -42,8 +44,6 @@ type IService interface {
GetServiceId() int GetServiceId() int
IsTimeOutTick(microSecond int64) bool IsTimeOutTick(microSecond int64) bool
AddModule(module IBaseModule, autorun bool) bool
GetModule(module string) IBaseModule
GetStatus() int GetStatus() int
} }
@@ -62,6 +62,8 @@ func InitLog() {
} }
type BaseService struct { type BaseService struct {
BaseModule
serviceid int serviceid int
servicename string servicename string
servicetype int servicetype int
@@ -69,21 +71,11 @@ type BaseService struct {
tickTime int64 tickTime int64
statTm int64 statTm int64
Status int Status int
modulelist []IBaseModule
} }
type BaseModule struct { type BaseModule struct {
modulename string moduleType uint32
} mapModule map[uint32]IBaseModule
func (slf *BaseModule) SetModuleName(modulename string) bool {
slf.modulename = modulename
return true
}
func (slf *BaseModule) GetModuleName() string {
return slf.modulename
} }
func (slf *BaseService) GetServiceId() int { func (slf *BaseService) GetServiceId() int {
@@ -178,28 +170,46 @@ func (slf *BaseService) IsTimeOutTick(microSecond int64) bool {
} }
func (slf *BaseService) AddModule(module IBaseModule, autorun bool) bool { func (slf *BaseModule) SetModuleType(moduleType uint32) {
typename := fmt.Sprintf("%v", reflect.TypeOf(module)) slf.moduleType = moduleType
parts := strings.Split(typename, ".") }
if len(parts) < 2 {
func (slf *BaseModule) GetModuleType() uint32 {
return slf.moduleType
}
//OnInit() error
//OnRun() error
func (slf *BaseModule) AddModule(module IBaseModule) bool {
if module.GetModuleType() == 0 {
return false return false
} }
module.SetModuleName(parts[1])
slf.modulelist = append(slf.modulelist, module) if slf.mapModule == nil {
module.OnInit() slf.mapModule = make(map[uint32]IBaseModule)
if autorun == true {
go module.OnRun()
} }
_, ok := slf.mapModule[module.GetModuleType()]
if ok == true {
return false
}
slf.mapModule[module.GetModuleType()] = module
return true return true
} }
func (slf *BaseService) GetModule(module string) IBaseModule { func (slf *BaseModule) GetModuleByType(moduleType uint32) IBaseModule {
for _, v := range slf.modulelist { ret, ok := slf.mapModule[moduleType]
if v.GetModuleName() == module { if ok == false {
return v return nil
}
} }
return nil return ret
}
func (slf *BaseModule) OnInit() error {
return fmt.Errorf("not implement OnInit moduletype %d ", slf.GetModuleType())
}
func (slf *BaseModule) OnRun() error {
return fmt.Errorf("not implement OnRun moduletype %d ", slf.GetModuleType())
} }