From ba4b084085746646df78ce3fc03d6e40987c7f84 Mon Sep 17 00:00:00 2001 From: boyce Date: Tue, 12 Feb 2019 12:03:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A8=A1=E5=9D=97=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/Service.go | 75 +++++++++++++++++++--------------- service/servicemanager.go | 8 +--- sysservice/httpserverervice.go | 4 +- sysservice/wsserverservice.go | 4 +- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/service/Service.go b/service/Service.go index 795440e..d2fd4eb 100644 --- a/service/Service.go +++ b/service/Service.go @@ -20,8 +20,10 @@ type MethodInfo struct { type IModule interface { SetModuleType(moduleType uint32) GetModuleType() uint32 + RunModule(module IModule, exit chan bool, pwaitGroup *sync.WaitGroup) error + InitModule(module IModule) error OnInit() error - OnRun() error + OnRun() bool AddModule(module IModule) bool GetModuleByType(moduleType uint32) IModule GetOwnerService() IService @@ -30,11 +32,9 @@ type IModule interface { type IService interface { Init(Iservice IService, servicetype int) error - Run(service IService, exit chan bool, pwaitGroup *sync.WaitGroup) error OnInit() error OnEndInit() error - OnRun() error - OnRunLoop() error + OnRun() bool OnDestory() error OnFetchService(iservice IService) error OnSetupService(iservice IService) //其他服务被安装 @@ -70,9 +70,7 @@ type BaseService struct { servicename string servicetype int - tickTime int64 - statTm int64 - Status int + Status int } type BaseModule struct { @@ -80,6 +78,7 @@ type BaseModule struct { mapModule map[uint32]IModule ownerService IService + tickTime int64 } func (slf *BaseService) GetServiceId() int { @@ -135,30 +134,6 @@ func (slf *BaseService) Init(iservice IService, servicetype int) error { return nil } -func (slf *BaseService) OnRunLoop() error { - return fmt.Errorf("None Loop") -} - -func (slf *BaseService) Run(service IService, exit chan bool, pwaitGroup *sync.WaitGroup) error { - defer pwaitGroup.Done() - service.OnRun() - for { - select { - case <-exit: - fmt.Println("stopping...") - return nil - default: - } - slf.tickTime = time.Now().UnixNano() / 1e6 - if service.OnRunLoop() != nil { - break - } - slf.tickTime = time.Now().UnixNano() / 1e6 - } - - return nil -} - func (slf *BaseService) RPC_CheckServiceTickTimeOut(microSecond int64) error { if slf.IsTimeOutTick(microSecond) == true { @@ -213,12 +188,13 @@ func (slf *BaseModule) GetModuleByType(moduleType uint32) IModule { 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()) +func (slf *BaseModule) OnRun() bool { + return false } func (slf *BaseModule) GetOwnerService() IService { @@ -228,3 +204,36 @@ func (slf *BaseModule) GetOwnerService() IService { func (slf *BaseModule) SetOwnerService(iservice IService) { slf.ownerService = iservice } + +func (slf *BaseModule) InitModule(module IModule) error { + module.OnInit() + for _, subModule := range slf.mapModule { + go subModule.OnInit() + } + + return nil +} + +func (slf *BaseModule) RunModule(module IModule, exit chan bool, pwaitGroup *sync.WaitGroup) error { + //运行所有子模块 + for _, subModule := range slf.mapModule { + go subModule.RunModule(subModule, exit, pwaitGroup) + } + + pwaitGroup.Add(1) + defer pwaitGroup.Done() + for { + select { + case <-exit: + fmt.Println("stopping module %s...", fmt.Sprintf("%T", slf)) + return nil + default: + } + if module.OnRun() == false { + break + } + slf.tickTime = time.Now().UnixNano() / 1e6 + } + + return nil +} diff --git a/service/servicemanager.go b/service/servicemanager.go index 913d698..ca16d4d 100644 --- a/service/servicemanager.go +++ b/service/servicemanager.go @@ -14,7 +14,6 @@ type IServiceManager interface { } type CServiceManager struct { - //serviceList []IService genserviceid int localserviceMap map[string]IService } @@ -47,9 +46,7 @@ func (slf *CServiceManager) FetchService(s FetchService) IService { func (slf *CServiceManager) Init() bool { for _, s := range slf.localserviceMap { - if s.OnInit() != nil { - return false - } + (s.(IModule)).InitModule(s.(IModule)) } // 初始化结束 @@ -64,8 +61,7 @@ func (slf *CServiceManager) Init() bool { func (slf *CServiceManager) Start(exit chan bool, pwaitGroup *sync.WaitGroup) bool { for _, s := range slf.localserviceMap { - pwaitGroup.Add(1) - go s.Run(s, exit, pwaitGroup) + (s.(IModule)).RunModule(s.(IModule), exit, pwaitGroup) } pwaitGroup.Add(1) diff --git a/sysservice/httpserverervice.go b/sysservice/httpserverervice.go index 6cbec41..19389a0 100644 --- a/sysservice/httpserverervice.go +++ b/sysservice/httpserverervice.go @@ -51,10 +51,10 @@ func (slf *HttpServerService) initRouterHandler() http.Handler { return cors.Handler(r) } -func (slf *HttpServerService) OnRun() error { +func (slf *HttpServerService) OnRun() bool { slf.httpserver.Start() - return nil + return false } func NewHttpServerService(port uint16) *HttpServerService { diff --git a/sysservice/wsserverservice.go b/sysservice/wsserverservice.go index 21a18d2..f401c3c 100644 --- a/sysservice/wsserverservice.go +++ b/sysservice/wsserverservice.go @@ -21,10 +21,10 @@ func (ws *WSServerService) OnInit() error { return nil } -func (ws *WSServerService) OnRun() error { +func (ws *WSServerService) OnRun() bool { ws.wsserver.Start() - return nil + return false } func NewWSServerService(pattern string, port uint16, messageReciver network.IMessageReceiver, bEnableCompression bool) *WSServerService {