mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Cleanup opensipsm with comments, fix opensipsevent_test for GetUUID not anymore considering from and to tags
This commit is contained in:
@@ -80,8 +80,8 @@ func TestOsipsEventGetValues(t *testing.T) {
|
||||
dur, _ := osipsEv.GetDuration(utils.META_DEFAULT)
|
||||
endTime, _ := osipsEv.GetEndTime()
|
||||
if osipsEv.GetName() != "E_ACC_CDR" ||
|
||||
osipsEv.GetCgrId() != utils.Sha1("ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ"+";"+"eb082607"+";"+"4ea9687f", setupTime.UTC().String()) ||
|
||||
osipsEv.GetUUID() != "ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ;eb082607;4ea9687f" ||
|
||||
osipsEv.GetCgrId() != utils.Sha1("ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ", setupTime.UTC().String()) ||
|
||||
osipsEv.GetUUID() != "ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ" ||
|
||||
osipsEv.GetDirection(utils.META_DEFAULT) != utils.OUT ||
|
||||
osipsEv.GetSubject(utils.META_DEFAULT) != "dan" ||
|
||||
osipsEv.GetAccount(utils.META_DEFAULT) != "dan" ||
|
||||
@@ -97,8 +97,8 @@ func TestOsipsEventGetValues(t *testing.T) {
|
||||
osipsEv.GetSupplier(utils.META_DEFAULT) != "supplier3" ||
|
||||
osipsEv.GetOriginatorIP(utils.META_DEFAULT) != "172.16.254.77" {
|
||||
t.Error("GetValues not matching: ", osipsEv.GetName() != "E_ACC_CDR",
|
||||
osipsEv.GetCgrId() != utils.Sha1("ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ"+";"+"eb082607"+";"+"4ea9687f", setupTime.UTC().String()),
|
||||
osipsEv.GetUUID() != "ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ;eb082607;4ea9687f",
|
||||
osipsEv.GetCgrId() != utils.Sha1("ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ", setupTime.UTC().String()),
|
||||
osipsEv.GetUUID() != "ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ",
|
||||
osipsEv.GetDirection(utils.META_DEFAULT) != utils.OUT,
|
||||
osipsEv.GetSubject(utils.META_DEFAULT) != "dan",
|
||||
osipsEv.GetAccount(utils.META_DEFAULT) != "dan",
|
||||
@@ -133,8 +133,9 @@ func TestOsipsEventMissingParameter(t *testing.T) {
|
||||
func TestOsipsEventAsStoredCdr(t *testing.T) {
|
||||
setupTime, _ := utils.ParseTimeDetectLayout("1406370492")
|
||||
answerTime, _ := utils.ParseTimeDetectLayout("1406370499")
|
||||
eStoredCdr := &engine.StoredCdr{CgrId: utils.Sha1("ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ;eb082607;4ea9687f", setupTime.UTC().String()),
|
||||
TOR: utils.VOICE, AccId: "ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ;eb082607;4ea9687f", CdrHost: "172.16.254.77", CdrSource: "OSIPS_E_ACC_CDR", ReqType: utils.META_PREPAID,
|
||||
eStoredCdr := &engine.StoredCdr{CgrId: utils.Sha1("ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ", setupTime.UTC().String()),
|
||||
TOR: utils.VOICE, AccId: "ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ", CdrHost: "172.16.254.77", CdrSource: "OSIPS_E_ACC_CDR",
|
||||
ReqType: utils.META_PREPAID,
|
||||
Direction: utils.OUT, Tenant: "itsyscom.com", Category: "call", Account: "dan", Subject: "dan",
|
||||
Destination: "+4986517174963", SetupTime: setupTime, AnswerTime: answerTime,
|
||||
Usage: time.Duration(20) * time.Second, Supplier: "supplier3", ExtraFields: map[string]string{"extra1": "val1", "extra2": "val2"}, Cost: -1}
|
||||
@@ -151,8 +152,8 @@ func TestOsipsAccMissedToStoredCdr(t *testing.T) {
|
||||
"cgr_account": "1001", "cgr_destination": "1002", utils.CGR_SUPPLIER: "supplier1",
|
||||
"duration": "", "dialog_id": "3547:277000822", "extra1": "val1", "extra2": "val2"}, OriginatorAddress: addr,
|
||||
}}
|
||||
eStoredCdr := &engine.StoredCdr{CgrId: utils.Sha1("27b1e6679ad0109b5d756e42bb4c9c28@0:0:0:0:0:0:0:0;5cb81eaa;", setupTime.UTC().String()),
|
||||
TOR: utils.VOICE, AccId: "27b1e6679ad0109b5d756e42bb4c9c28@0:0:0:0:0:0:0:0;5cb81eaa;", CdrHost: "172.16.254.77", CdrSource: "OSIPS_E_ACC_MISSED_EVENT",
|
||||
eStoredCdr := &engine.StoredCdr{CgrId: utils.Sha1("27b1e6679ad0109b5d756e42bb4c9c28@0:0:0:0:0:0:0:0", setupTime.UTC().String()),
|
||||
TOR: utils.VOICE, AccId: "27b1e6679ad0109b5d756e42bb4c9c28@0:0:0:0:0:0:0:0", CdrHost: "172.16.254.77", CdrSource: "OSIPS_E_ACC_MISSED_EVENT",
|
||||
ReqType: utils.META_PSEUDOPREPAID, Direction: utils.OUT, Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Supplier: "supplier1",
|
||||
Destination: "1002", SetupTime: setupTime, AnswerTime: setupTime,
|
||||
Usage: time.Duration(0), ExtraFields: map[string]string{"extra1": "val1", "extra2": "val2"}, Cost: -1}
|
||||
@@ -176,7 +177,7 @@ func TestOsipsUpdateDurationFromEvent(t *testing.T) {
|
||||
"duration": "", "dialog_id": "3547:277000822"}, OriginatorAddress: addr,
|
||||
}}
|
||||
eOsipsEv := &OsipsEvent{osipsEvent: &osipsdagram.OsipsEvent{Name: "E_ACC_EVENT",
|
||||
AttrValues: map[string]string{"method": "INVITE", "from_tag": "87d02470", "to_tag": "a671a98", "callid": "05dac0aaa716c9814f855f0e8fee6936@0:0:0:0:0:0:0:0",
|
||||
AttrValues: map[string]string{"method": "UPDATE", "from_tag": "87d02470", "to_tag": "a671a98", "callid": "05dac0aaa716c9814f855f0e8fee6936@0:0:0:0:0:0:0:0",
|
||||
"sip_code": "200", "sip_reason": "OK", "time": "1430579770", "cgr_reqtype": utils.META_PREPAID,
|
||||
"cgr_account": "1001", "cgr_destination": "1002", utils.CGR_SUPPLIER: "supplier1",
|
||||
"duration": "27s", "dialog_id": "3547:277000822", "extra1": "val1", "extra2": "val2"}, OriginatorAddress: addr,
|
||||
|
||||
@@ -100,9 +100,10 @@ type OsipsSessionManager struct {
|
||||
stopServing chan struct{} // Stop serving datagrams
|
||||
miConn *osipsdagram.OsipsMiDatagramConnector // Pool of connections used to various OpenSIPS servers, keep reference towards events received so we can issue commands always to the same remote
|
||||
sessions []*Session
|
||||
cdrStartEvents map[string]*OsipsEvent // Used when building CDRs
|
||||
cdrStartEvents map[string]*OsipsEvent // Used when building CDRs, ToDo: secure access to map
|
||||
}
|
||||
|
||||
// Called when firing up the session manager, will stay connected for the duration of the daemon running
|
||||
func (osm *OsipsSessionManager) Connect() (err error) {
|
||||
osm.stopServing = make(chan struct{})
|
||||
if osm.miConn, err = osipsdagram.NewOsipsMiDatagramConnector(osm.cfg.MiAddr, osm.cfg.Reconnects); err != nil {
|
||||
@@ -120,6 +121,8 @@ func (osm *OsipsSessionManager) Connect() (err error) {
|
||||
evsrv.ServeEvents(osm.stopServing) // Will break through stopServing on error in other places
|
||||
return errors.New("<SM-OpenSIPS> Stopped reading events")
|
||||
}
|
||||
|
||||
// Removes a session on call end
|
||||
func (osm *OsipsSessionManager) RemoveSession(uuid string) {
|
||||
for i, ss := range osm.sessions {
|
||||
if ss.eventStart.GetUUID() == uuid {
|
||||
@@ -128,32 +131,39 @@ func (osm *OsipsSessionManager) RemoveSession(uuid string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
func (osm *OsipsSessionManager) MaxDebit(cd *engine.CallDescriptor, cc *engine.CallCost) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DebitInterval will give out the frequence of the debits sent to engine
|
||||
func (osm *OsipsSessionManager) DebitInterval() time.Duration {
|
||||
return osm.cfg.DebitInterval
|
||||
}
|
||||
|
||||
// Returns the connection to local cdr database, used by session to log it's final costs
|
||||
func (osm *OsipsSessionManager) CdrDb() engine.CdrStorage {
|
||||
return osm.cdrDb
|
||||
}
|
||||
func (osm *OsipsSessionManager) DbLogger() engine.LogStorage {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns connection to rater/controller
|
||||
func (osm *OsipsSessionManager) Rater() engine.Connector {
|
||||
return osm.rater
|
||||
}
|
||||
|
||||
// Part of the session manager interface, not really used with OpenSIPS now
|
||||
func (osm *OsipsSessionManager) WarnSessionMinDuration(sessionUuid, connId string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Called on session manager shutdown, could add more cleanup actions in the future
|
||||
func (osm *OsipsSessionManager) Shutdown() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Process the CDR with CDRS component
|
||||
func (osm *OsipsSessionManager) ProcessCdr(storedCdr *engine.StoredCdr) error {
|
||||
var reply string
|
||||
return osm.cdrsrv.ProcessCdr(storedCdr, &reply)
|
||||
}
|
||||
|
||||
// Disconnects the session
|
||||
func (osm *OsipsSessionManager) DisconnectSession(ev engine.Event, connId, notify string) error {
|
||||
sessionIds := ev.GetSessionIds()
|
||||
if len(sessionIds) != 2 {
|
||||
@@ -173,16 +183,6 @@ func (osm *OsipsSessionManager) DisconnectSession(ev engine.Event, connId, notif
|
||||
return nil
|
||||
}
|
||||
|
||||
// Searches and return the session with the specifed uuid
|
||||
func (osm *OsipsSessionManager) getSession(uuid string) *Session {
|
||||
for _, s := range osm.sessions {
|
||||
if s.eventStart.GetUUID() == uuid {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Automatic subscribe to OpenSIPS for events, trigered on Connect or OpenSIPS restart
|
||||
func (osm *OsipsSessionManager) SubscribeEvents(evStop chan struct{}) error {
|
||||
if err := osm.subscribeEvents(); err != nil { // Init subscribe
|
||||
@@ -227,12 +227,14 @@ func (osm *OsipsSessionManager) subscribeEvents() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Triggered opensips_start event
|
||||
func (osm *OsipsSessionManager) onOpensipsStart(cdrDagram *osipsdagram.OsipsEvent) {
|
||||
osm.evSubscribeStop <- struct{}{} // Cancel previous subscribes
|
||||
osm.evSubscribeStop = make(chan struct{}) // Create a fresh communication channel
|
||||
go osm.SubscribeEvents(osm.evSubscribeStop)
|
||||
}
|
||||
|
||||
// Triggered by CDR event
|
||||
func (osm *OsipsSessionManager) onCdr(cdrDagram *osipsdagram.OsipsEvent) {
|
||||
osipsEv, _ := NewOsipsEvent(cdrDagram)
|
||||
if err := osm.ProcessCdr(osipsEv.AsStoredCdr()); err != nil {
|
||||
@@ -240,6 +242,7 @@ func (osm *OsipsSessionManager) onCdr(cdrDagram *osipsdagram.OsipsEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
// Triggered by ACC_EVENT
|
||||
func (osm *OsipsSessionManager) onAccEvent(osipsDgram *osipsdagram.OsipsEvent) {
|
||||
osipsEv, _ := NewOsipsEvent(osipsDgram)
|
||||
if osipsEv.GetReqType(utils.META_DEFAULT) == utils.META_NONE { // Do not process this request
|
||||
@@ -262,6 +265,7 @@ func (osm *OsipsSessionManager) onAccEvent(osipsDgram *osipsdagram.OsipsEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
// Handler of call start event. Mostly starts a session if needed
|
||||
func (osm *OsipsSessionManager) callStart(osipsEv *OsipsEvent) error {
|
||||
if osipsEv.MissingParameter() {
|
||||
if err := osm.DisconnectSession(osipsEv, "", utils.ERR_MANDATORY_IE_MISSING); err != nil {
|
||||
@@ -276,6 +280,7 @@ func (osm *OsipsSessionManager) callStart(osipsEv *OsipsEvent) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handler for callEnd. Mostly removes a session if needed
|
||||
func (osm *OsipsSessionManager) callEnd(osipsEv *OsipsEvent) error {
|
||||
s := osm.getSession(osipsEv.GetUUID())
|
||||
if s == nil { // Not handled by us
|
||||
@@ -327,3 +332,13 @@ func (osm *OsipsSessionManager) processCdrStop(osipsEv *OsipsEvent) error {
|
||||
}
|
||||
return osm.ProcessCdr(osipsEvStart.AsStoredCdr())
|
||||
}
|
||||
|
||||
// Searches and return the session with the specifed uuid
|
||||
func (osm *OsipsSessionManager) getSession(uuid string) *Session {
|
||||
for _, s := range osm.sessions {
|
||||
if s.eventStart.GetUUID() == uuid {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user