diff --git a/icssessionmanager/agentoperator.go b/icssessionmanager/agentoperator.go index 9e6a9bd..9fe36fe 100644 --- a/icssessionmanager/agentoperator.go +++ b/icssessionmanager/agentoperator.go @@ -283,10 +283,10 @@ func (s *IcsSession) RequestInvite(inviteto string) { inviteMethod = fmt.Sprintf("sip:%s@%s;transport=%s", "AISB", ip, transport) s.uri = fmt.Sprintf("sip:%s@%s", "AISB", ip) to = fmt.Sprintf("", "AISB", ip) - agentName := s.AgentName - agentSplit := agentName[len(agentName)-2:len(agentName)] - telNo := fmt.Sprintf("010243427%s", agentSplit) - from = fmt.Sprintf(";tag=%s", telNo, ip, sipasm.GenerateTag()) + // agentName := s.AgentName + // agentSplit := agentName[len(agentName)-2:len(agentName)] + // telNo := fmt.Sprintf("010243427%s", agentSplit) + from = fmt.Sprintf(";tag=%s", inviteto, ip, sipasm.GenerateTag()) } reqInvite := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_INVITE, inviteMethod) @@ -394,7 +394,11 @@ func (s *IcsSession) PassInvite(sip *icspacketparser.SIP) *icserror.IcsError { maxforwards := "70" agentName := s.AgentName agentSplit := agentName[len(agentName)-2:len(agentName)] - telNo := fmt.Sprintf("010243427%s", agentSplit) + cust1 := strings.SplitN(sip.From, "@", 2) + cust2 := strings.SplitN(cust1[0], ":", 2) + + telNo := fmt.Sprintf("%s%s", cust2[1][0:9], agentSplit) + s.CustID = telNo from := fmt.Sprintf(";tag=%s", telNo, ip, sipasm.GenerateTag()) s.uri = fmt.Sprintf("sip:%s@%s", s.AgentName, ip) @@ -850,9 +854,9 @@ func (s *IcsSession) ProcACKInvite(sip *icspacketparser.SIP) *icserror.IcsError name1 := strings.SplitN(sip.To, "@", 2) name2 := strings.SplitN(name1[0], ":", 2) ac := FindAgentConfig(name2[1]) - s.m.Lock() - s.callID = sip.CallID - s.m.Unlock() + // s.m.Lock() + // s.callID = sip.CallID + // s.m.Unlock() if ac == nil { l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Could not found Agent Name - %s", icserror.ICSERRUnkown.GetMessage()) @@ -903,6 +907,12 @@ func (s *IcsSession) ProcACKInvite(sip *icspacketparser.SIP) *icserror.IcsError s.uri = fmt.Sprintf("sip:%s@%s", s.AgentName, ip) l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set URI [%s]", s.uri) + from := fmt.Sprintf(";tag=%s", s.CustID, ip, sipasm.GenerateTag()) + to := fmt.Sprintf("", s.CustID, ip) + if s.InviteSIP != nil { + from = s.InviteSIP.From + to = s.InviteSIP.To + } //to := fmt.Sprintf("", "01025670081", "192.168.0.221") //to := fmt.Sprintf("", "01025670081", ip) //to := fmt.Sprintf("", s.AgentName, ip) @@ -954,8 +964,8 @@ func (s *IcsSession) ProcACKInvite(sip *icspacketparser.SIP) *icserror.IcsError reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, viav) } reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, sip.From) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, sip.To) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, from) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, to) reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, sip.CallID) reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, sip.Cseq) reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) @@ -1215,9 +1225,9 @@ func (s *IcsSession) ProcBye(sip *icspacketparser.SIP) *icserror.IcsError { station := conf.AgentConfig[s.ID].Name s.Station = station /////////// - cust1 := strings.SplitN(sip.From, "@", 2) - cust2 := strings.SplitN(cust1[0], ":", 2) - s.CustID = cust2[1] + // cust1 := strings.SplitN(sip.From, "@", 2) + // cust2 := strings.SplitN(cust1[0], ":", 2) + // s.CustID = cust2[1] ////////// l.Printf(icslog.LOG_LEVEL_DEBUG2, s.ID, ">>>>>channelid: %s, serverid: %d, station: %s, Custom: %s, starttimestamp: %d, START, inout: %s\n", s.ChannelID, s.ServerID, s.Station, s.CustID, s.StartTimeStamp, inout) @@ -1280,7 +1290,7 @@ func (s *IcsSession) SendRequestInvite() { if s.simLoopCount < conf.SimLoopCount { l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Send Call Start Sip(%s) count %d!!!!!!!\r\n", s.AgentName, s.simLoopCount) time.Sleep(time.Second*1) - s.RequestInvite("01024342788") + s.RequestInvite(s.CustID) s.simLoopCount += 1 } else { l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Send Call Start Sip(%s) END %d !!!!!!!\r\n", s.AgentName, s.simLoopCount) @@ -1354,7 +1364,7 @@ func (s *IcsSession) ProcSIP20Bye(sip *icspacketparser.SIP) *icserror.IcsError { // fmt.Printf("Send Call Start Sip(%s) count %d!!!!!!!", s.AgentName, s.simLoopCount) if s.simLoopCount < conf.SimLoopCount { l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Send Call Start Sip(%s) count %d!!!!!!!\r\n", s.AgentName, s.simLoopCount) - s.RequestInvite("01024342788") + s.RequestInvite(s.CustID) s.simLoopCount += 1 } else { l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Send Call Start Sip(%s) END!!!!!!!\r\n", s.AgentName, s.simLoopCount) @@ -2044,14 +2054,14 @@ func (s *IcsSession) RequestBYE(inviteSIP *icspacketparser.SIP) { via := fmt.Sprintf("SIP/2.0/UDP %s:%d;branch=%s", ip, port, sipasm.GenerateBranch()) maxforwards := "70" - from := fmt.Sprintf(";tag=%s", "AISB", ip, sipasm.GenerateTag()) + from := fmt.Sprintf(";tag=%s", s.CustID, ip, sipasm.GenerateTag()) // from := strings.SplitN(inviteSIP.From, ": ", 2) //from := fmt.Sprintf(";tag=%s", s.AgentName, ip, sipasm.GenerateTag()) s.uri = fmt.Sprintf("sip:%s@%s", "AISB", ip) l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set URI [%s]", s.uri) - to := fmt.Sprintf("", "01024342788", ip) + to := fmt.Sprintf("", s.CustID, ip) if s.InviteSIP != nil { from = s.InviteSIP.From to = s.InviteSIP.To diff --git a/icssessionmanager/icssessionmanager.go b/icssessionmanager/icssessionmanager.go index ac815eb..6f9c427 100644 --- a/icssessionmanager/icssessionmanager.go +++ b/icssessionmanager/icssessionmanager.go @@ -41,6 +41,12 @@ var bigSession []*IcsSession var onceSession sync.Once var channelNum int +var m *sync.Mutex + +func init() { + m = &sync.Mutex{} +} + func getSessionInstance(SIPNeter *icsnet.IcsNeter) []*IcsSession { //l := icslog.GetIcsLog() @@ -315,68 +321,117 @@ func (sm *SessionManager) Run() (icserr *icserror.IcsError) { sip.Method, sip.ResType, sip.Cseq) s, serr := FindSession(sip) - if serr != nil { //not found session, create new - name1 := strings.SplitN(sip.To, "@", 2) - name2 := strings.SplitN(name1[0], ":", 2) - agentname := name2[1] - s, serr = AllocSession(agentname, sip.GetCallID()) - if serr != nil { - l.Printf(icslog.LOG_LEVEL_ERROR, -1, "Licensed Session number[%d:%s] is EXCEEDED-%s", - conf.GetChannelNum(), agentname, serr.GetMessage()) - if conf.Representative.Value && sip.Source == "486" { - l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") - sipPort := conf.SIPConfig.Port - // sipProxy := conf.SIPConfig.SIPProxy - transport := conf.SIPConfig.Transport - // remoteaddr := icsnet.NewNetAddrWithIPAddr(sipProxy) - // localAddrStr := fmt.Sprintf("0.0.0.0:%d", sipPort) - // localAddr := icsnet.NewNetAddrWithIPAddr(localAddrStr) - ////////////////////////////////////////////// - ip := conf.InfoConfig.ServerIP - ackMethod := fmt.Sprintf("sip:01025670081@%s;transport=%s", "192.168.0.222:5090", transport) - reqAck := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_ACK, ackMethod) - maxforwards := "70" - // s.uri = fmt.Sprintf("sip:%s@%s", conf.Representative.Name, ip) - // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Set URI [%s]", s.uri) - contact := fmt.Sprintf("", conf.Representative.Name, ip, sipPort, transport) - userAgent := conf.InfoConfig.Product - allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" - supported := "timer,path,replaces" - - for _, viav := range sip.Via { - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, viav) - } - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, sip.From) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, sip.To) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, sip.CallID) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, sip.Cseq) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, "0") - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) - - // wlen, werr := (*s.sipNeter).WriteSIPTo([]byte(reqAck.String()), s.remoteSIPAddr) - wlen, werr := (sm.SIPNeter).WriteSIP([]byte(reqAck.String())) - if werr != nil { - l.Print(icslog.LOG_LEVEL_FATAL, s.ID, werr.GetMessage()) - return werr - } - l.Printf(icslog.LOG_LEVEL_INFO, -1, "Sent Data(%d) [%s]->[%s]>\n%s", - wlen, - (sm.SIPNeter).LocalAddr().String(), - (sm.SIPNeter).RemoteAddr().String(), - reqAck.String()) - ////////////////////////////////////////////// + if serr != nil { //not found session + // name1 := strings.SplitN(sip.To, "@", 2) + // name2 := strings.SplitN(name1[0], ":", 2) + // agentname := name2[1] + if conf.Representative.Value && sip.Source == "486" { + l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") + sipPort := conf.SIPConfig.Port + // sipProxy := conf.SIPConfig.SIPProxy + transport := conf.SIPConfig.Transport + // remoteaddr := icsnet.NewNetAddrWithIPAddr(sipProxy) + // localAddrStr := fmt.Sprintf("0.0.0.0:%d", sipPort) + // localAddr := icsnet.NewNetAddrWithIPAddr(localAddrStr) + ////////////////////////////////////////////// + ip := conf.InfoConfig.ServerIP + ackMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "192.168.0.222:5090", "AISB",transport) + reqAck := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_ACK, ackMethod) + maxforwards := "70" + // s.uri = fmt.Sprintf("sip:%s@%s", conf.Representative.Name, ip) + // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Set URI [%s]", s.uri) + contact := fmt.Sprintf("", conf.Representative.Name, ip, sipPort, transport) + userAgent := conf.InfoConfig.Product + allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" + supported := "timer,path,replaces" + + for _, viav := range sip.Via { + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, viav) } - continue + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, sip.From) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, sip.To) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, sip.CallID) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, sip.Cseq) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, "0") + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) + + // wlen, werr := (*s.sipNeter).WriteSIPTo([]byte(reqAck.String()), s.remoteSIPAddr) + wlen, werr := (sm.SIPNeter).WriteSIP([]byte(reqAck.String())) + if werr != nil { + l.Print(icslog.LOG_LEVEL_FATAL, -1, werr.GetMessage()) + return werr + } + l.Printf(icslog.LOG_LEVEL_INFO, -1, "Sent Data(%d) [%s]->[%s]>\n%s", + wlen, + (sm.SIPNeter).LocalAddr().String(), + (sm.SIPNeter).RemoteAddr().String(), + reqAck.String()) + ////////////////////////////////////////////// } - l.Printf(icslog.LOG_LEVEL_INFO, -1, "Session Allocated. Session ID[%d] Call ID[%s]", s.ID, sip.GetCallID()) - s.SetSessionMethod(sip) - //session start - s.Start() + continue + // s, serr = AllocSession(agentname, sip.GetCallID()) + // if serr != nil { + // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "Licensed Session number[%d:%s] is EXCEEDED-%s", + // conf.GetChannelNum(), agentname, serr.GetMessage()) + // if conf.Representative.Value && sip.Source == "486" { + // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") + // sipPort := conf.SIPConfig.Port + // // sipProxy := conf.SIPConfig.SIPProxy + // transport := conf.SIPConfig.Transport + // // remoteaddr := icsnet.NewNetAddrWithIPAddr(sipProxy) + // // localAddrStr := fmt.Sprintf("0.0.0.0:%d", sipPort) + // // localAddr := icsnet.NewNetAddrWithIPAddr(localAddrStr) + // ////////////////////////////////////////////// + // ip := conf.InfoConfig.ServerIP + // ackMethod := fmt.Sprintf("sip:01025670081@%s;transport=%s", "192.168.0.222:5090", transport) + // reqAck := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_ACK, ackMethod) + // maxforwards := "70" + // // s.uri = fmt.Sprintf("sip:%s@%s", conf.Representative.Name, ip) + // // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Set URI [%s]", s.uri) + // contact := fmt.Sprintf("", conf.Representative.Name, ip, sipPort, transport) + // userAgent := conf.InfoConfig.Product + // allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" + // supported := "timer,path,replaces" + + // for _, viav := range sip.Via { + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, viav) + // } + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, sip.From) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, sip.To) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, sip.CallID) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, sip.Cseq) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, "0") + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) + + // // wlen, werr := (*s.sipNeter).WriteSIPTo([]byte(reqAck.String()), s.remoteSIPAddr) + // wlen, werr := (sm.SIPNeter).WriteSIP([]byte(reqAck.String())) + // if werr != nil { + // l.Print(icslog.LOG_LEVEL_FATAL, s.ID, werr.GetMessage()) + // return werr + // } + // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Sent Data(%d) [%s]->[%s]>\n%s", + // wlen, + // (sm.SIPNeter).LocalAddr().String(), + // (sm.SIPNeter).RemoteAddr().String(), + // reqAck.String()) + // ////////////////////////////////////////////// + // } + // continue + // } + // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Session Allocated. Session ID[%d] Call ID[%s]", s.ID, sip.GetCallID()) + // s.SetSessionMethod(sip) + // //session start + // s.Start() } else { //found session l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Session found [%s][%s]", sip.GetCallID(), s.callID) if sip.Method == icspacketparser.ICS_SIP_METHOD_INVITE { diff --git a/icssessionmanager/sessionoperator.go b/icssessionmanager/sessionoperator.go index 5c1fce9..9079554 100644 --- a/icssessionmanager/sessionoperator.go +++ b/icssessionmanager/sessionoperator.go @@ -17,62 +17,74 @@ import ( //session operator func findSessionWithCallID(callId string) *IcsSession { sessions := getSessionInstance(nil) + m.Lock() for _, session := range sessions { - session.m.Lock() + // session.m.Lock() // fmt.Printf("findSessionWithCallID %d [%s][%s]\n", iter, session.callID, callId) if strings.Compare(session.callID, callId) == 0 { //if strings.Compare(session.callID, callId) == 0 || strings.Compare(session.regiCallID, callId) == 0 { // fmt.Printf("findSessionWithCallID session found! %d [%s][%s]\n", iter, session.callID, callId) - session.m.Unlock() + // session.m.Unlock() + m.Unlock() return session } - session.m.Unlock() + // session.m.Unlock() } + m.Unlock() return nil } func findSessionWithFromURI(uri string) *IcsSession { sessions := getSessionInstance(nil) + m.Lock() for _, session := range sessions { - session.m.Lock() + // session.m.Lock() //fmt.Println("findSessionWithFromURI", session.uri) if strings.Contains(session.uri, uri) { - session.m.Unlock() + // session.m.Unlock() + m.Unlock() return session } - session.m.Unlock() + // session.m.Unlock() } + m.Unlock() return nil } func findSessionWithRTPPort(srcPort int, dstPort int) *IcsSession { sessions := getSessionInstance(nil) + m.Lock() for _, session := range sessions { - session.m.Lock() + // session.m.Lock() if (session.srcPort == srcPort && session.dstPort == dstPort) || (session.srcPort == dstPort && session.dstPort == srcPort) { - session.m.Unlock() + // session.m.Unlock() + m.Unlock() return session } - session.m.Unlock() + // session.m.Unlock() } + m.Unlock() return nil } func findSessionWithAgentName(agentName string) *IcsSession { sessions := getSessionInstance(nil) + m.Lock() for _, session := range sessions { - session.m.Lock() + // session.m.Lock() //fmt.Printf(">>>>findSessionWithAgentName: name: %s, name2: %s, arg: %s\n", session.AgentName, session.AgentName2, agentName) if strings.Compare(session.AgentName, agentName) == 0 { - session.m.Unlock() + // session.m.Unlock() + m.Unlock() return session } - session.m.Unlock() + // session.m.Unlock() } + m.Unlock() return nil } @@ -81,16 +93,19 @@ func findSessionWithAgentName(agentName string) *IcsSession { func findSessionWithSessionStatus(callID string) *IcsSession { conf := icsconf.GetIcsConfig() sessions := getSessionInstance(nil) + m.Lock() for idx, session := range sessions { - session.m.Lock() + // session.m.Lock() // fmt.Println(session.AgentName, session.agentStatus) if strings.Compare(conf.AgentConfig[idx].Value, "true") == 0 && session.agentStatus == 64 { // session.callID = callID - session.m.Unlock() + // session.m.Unlock() + m.Unlock() return session } - session.m.Unlock() + // session.m.Unlock() } + m.Unlock() return nil } @@ -112,15 +127,18 @@ func findSessionWithSessionStatusForINVITE() *IcsSession { func findSessionWithAgentName2(agentName string) *IcsSession { sessions := getSessionInstance(nil) + m.Lock() for _, session := range sessions { - session.m.Lock() + // session.m.Lock() //fmt.Printf(">>>>findSessionWithAgentName2: name: %s, name2: %s, arg: %s\n", session.AgentName, session.AgentName2, agentName) if strings.Compare(session.AgentName2, agentName) == 0 { - session.m.Unlock() + // session.m.Unlock() + m.Unlock() return session } - session.m.Unlock() + // session.m.Unlock() } + m.Unlock() return nil } @@ -363,7 +381,7 @@ func (s *IcsSession) RemoveSession() *icserror.IcsError { s.AgentName2 = "" */ - s.callID = "" + // s.callID = "" s.tts = nil