mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-16 13:49:53 +05:00
141 lines
3.9 KiB
Go
141 lines
3.9 KiB
Go
/*
|
|
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 <http://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/cgrates/birpc"
|
|
"github.com/cgrates/cgrates/agents"
|
|
"github.com/cgrates/cgrates/config"
|
|
"github.com/cgrates/cgrates/engine"
|
|
"github.com/cgrates/cgrates/servmanager"
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
// NewSIPAgent returns the sip Agent
|
|
func NewSIPAgent(cfg *config.CGRConfig,
|
|
connMgr *engine.ConnManager,
|
|
srvIndexer *servmanager.ServiceIndexer) *SIPAgent {
|
|
return &SIPAgent{
|
|
cfg: cfg,
|
|
connMgr: connMgr,
|
|
srvIndexer: srvIndexer,
|
|
stateDeps: NewStateDependencies([]string{utils.StateServiceUP}),
|
|
}
|
|
}
|
|
|
|
// SIPAgent implements Agent interface
|
|
type SIPAgent struct {
|
|
sync.RWMutex
|
|
cfg *config.CGRConfig
|
|
|
|
sip *agents.SIPAgent
|
|
connMgr *engine.ConnManager
|
|
|
|
oldListen string
|
|
|
|
intRPCconn birpc.ClientConnector // expose API methods over internal connection
|
|
srvIndexer *servmanager.ServiceIndexer // access directly services from here
|
|
stateDeps *StateDependencies // channel subscriptions for state changes
|
|
}
|
|
|
|
// Start should handle the sercive start
|
|
func (sip *SIPAgent) Start(shutdown chan struct{}) (err error) {
|
|
if sip.IsRunning() {
|
|
return utils.ErrServiceAlreadyRunning
|
|
}
|
|
|
|
fs := sip.srvIndexer.GetService(utils.FilterS).(*FilterService)
|
|
if utils.StructChanTimeout(fs.StateChan(utils.StateServiceUP), sip.cfg.GeneralCfg().ConnectTimeout) {
|
|
return utils.NewServiceStateTimeoutError(utils.SIPAgent, utils.FilterS, utils.StateServiceUP)
|
|
}
|
|
|
|
sip.Lock()
|
|
defer sip.Unlock()
|
|
sip.oldListen = sip.cfg.SIPAgentCfg().Listen
|
|
sip.sip, err = agents.NewSIPAgent(sip.connMgr, sip.cfg, fs.FilterS())
|
|
if err != nil {
|
|
utils.Logger.Err(fmt.Sprintf("<%s> error: %s!",
|
|
utils.SIPAgent, err))
|
|
return
|
|
}
|
|
go sip.listenAndServe(shutdown)
|
|
close(sip.stateDeps.StateChan(utils.StateServiceUP))
|
|
return
|
|
}
|
|
|
|
func (sip *SIPAgent) listenAndServe(shutdown chan struct{}) {
|
|
if err := sip.sip.ListenAndServe(); err != nil {
|
|
utils.Logger.Err(fmt.Sprintf("<%s> error: <%s>", utils.SIPAgent, err.Error()))
|
|
close(shutdown) // stop the engine here
|
|
}
|
|
}
|
|
|
|
// Reload handles the change of config
|
|
func (sip *SIPAgent) Reload(shutdown chan struct{}) (err error) {
|
|
if sip.oldListen == sip.cfg.SIPAgentCfg().Listen {
|
|
return
|
|
}
|
|
sip.Lock()
|
|
sip.sip.Shutdown()
|
|
sip.oldListen = sip.cfg.SIPAgentCfg().Listen
|
|
sip.sip.InitStopChan()
|
|
sip.Unlock()
|
|
go sip.listenAndServe(shutdown)
|
|
return
|
|
}
|
|
|
|
// Shutdown stops the service
|
|
func (sip *SIPAgent) Shutdown() (err error) {
|
|
sip.Lock()
|
|
defer sip.Unlock()
|
|
sip.sip.Shutdown()
|
|
sip.sip = nil
|
|
return
|
|
}
|
|
|
|
// IsRunning returns if the service is running
|
|
func (sip *SIPAgent) IsRunning() bool {
|
|
sip.RLock()
|
|
defer sip.RUnlock()
|
|
return sip.sip != nil
|
|
}
|
|
|
|
// ServiceName returns the service name
|
|
func (sip *SIPAgent) ServiceName() string {
|
|
return utils.SIPAgent
|
|
}
|
|
|
|
// ShouldRun returns if the service should be running
|
|
func (sip *SIPAgent) ShouldRun() bool {
|
|
return sip.cfg.SIPAgentCfg().Enabled
|
|
}
|
|
|
|
// StateChan returns signaling channel of specific state
|
|
func (sip *SIPAgent) StateChan(stateID string) chan struct{} {
|
|
return sip.stateDeps.StateChan(stateID)
|
|
}
|
|
|
|
// IntRPCConn returns the internal connection used by RPCClient
|
|
func (sip *SIPAgent) IntRPCConn() birpc.ClientConnector {
|
|
return sip.intRPCconn
|
|
}
|