/* Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments Copyright (C) ITsysCOM GmbH This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see */ package services import ( "fmt" "sync" "github.com/cgrates/birpc/context" "github.com/cgrates/cgrates/apis" "github.com/cgrates/birpc" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/cores" "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/servmanager" "github.com/cgrates/cgrates/utils" ) // NewRouteService returns the Route Service func NewRouteService(cfg *config.CGRConfig, dm *DataDBService, cacheS *CacheService, filterSChan chan *engine.FilterS, server *cores.Server, internalRouteSChan chan birpc.ClientConnector, connMgr *engine.ConnManager, anz *AnalyzerService, srvDep map[string]*sync.WaitGroup) servmanager.Service { return &RouteService{ connChan: internalRouteSChan, cfg: cfg, dm: dm, cacheS: cacheS, filterSChan: filterSChan, server: server, connMgr: connMgr, anz: anz, srvDep: srvDep, } } // RouteService implements Service interface type RouteService struct { sync.RWMutex cfg *config.CGRConfig dm *DataDBService cacheS *CacheService filterSChan chan *engine.FilterS server *cores.Server connMgr *engine.ConnManager routeS *engine.RouteService connChan chan birpc.ClientConnector anz *AnalyzerService srvDep map[string]*sync.WaitGroup } // Start should handle the sercive start func (routeS *RouteService) Start(ctx *context.Context, _ context.CancelFunc) (err error) { if routeS.IsRunning() { return utils.ErrServiceAlreadyRunning } if err = routeS.cacheS.WaitToPrecache(ctx, utils.CacheRouteProfiles, utils.CacheRouteFilterIndexes); err != nil { return } var filterS *engine.FilterS if filterS, err = waitForFilterS(ctx, routeS.filterSChan); err != nil { return } var datadb *engine.DataManager if datadb, err = routeS.dm.WaitForDM(ctx); err != nil { return } routeS.Lock() defer routeS.Unlock() routeS.routeS = engine.NewRouteService(datadb, filterS, routeS.cfg, routeS.connMgr) utils.Logger.Info(fmt.Sprintf("<%s> starting <%s> subsystem", utils.CoreS, utils.RouteS)) srv, _ := birpc.NewService(apis.NewRouteSv1(routeS.routeS), "", false) if !routeS.cfg.DispatcherSCfg().Enabled { routeS.server.RpcRegister(srv) } routeS.connChan <- routeS.anz.GetInternalCodec(srv, utils.RouteS) return } // Reload handles the change of config func (routeS *RouteService) Reload(*context.Context, context.CancelFunc) (err error) { return } // Shutdown stops the service func (routeS *RouteService) Shutdown() (err error) { routeS.Lock() defer routeS.Unlock() routeS.routeS.Shutdown() //we don't verify the error because shutdown never returns an error routeS.routeS = nil <-routeS.connChan routeS.server.RpcUnregisterName(utils.RouteSv1) return } // IsRunning returns if the service is running func (routeS *RouteService) IsRunning() bool { routeS.RLock() defer routeS.RUnlock() return routeS != nil && routeS.routeS != nil } // ServiceName returns the service name func (routeS *RouteService) ServiceName() string { return utils.RouteS } // ShouldRun returns if the service should be running func (routeS *RouteService) ShouldRun() bool { return routeS.cfg.RouteSCfg().Enabled }