diff --git a/icsapp/execsvc.go b/icsapp/execsvc.go index d4718c2..b893d9b 100644 --- a/icsapp/execsvc.go +++ b/icsapp/execsvc.go @@ -1,7 +1,7 @@ package icsapp import ( - "strings" + // "strings" "time" "gitlab.com/ics_cinnamon/voicegateway/icsconf" @@ -57,20 +57,20 @@ func (exe IcsExec) Execute() *icserror.IcsError { ///////////////////////////////////////////////////////////////////// //start bot-command TCP listener - cmdDone := make(chan *icserror.IcsError) - bcValue := strings.ToUpper(exe.config.CommandConfig.Value) - if strings.Compare("TRUE", bcValue) == 0 { - go func() { - cmdErr := sm.RunBotCommandMNG() - if cmdErr != nil { - cmdDone <- cmdErr - return - } - //defer sm.Close() + // cmdDone := make(chan *icserror.IcsError) + // bcValue := strings.ToUpper(exe.config.CommandConfig.Value) + // if strings.Compare("TRUE", bcValue) == 0 { + // go func() { + // cmdErr := sm.RunBotCommandMNG() + // if cmdErr != nil { + // cmdDone <- cmdErr + // return + // } + // //defer sm.Close() - //cmdDone <- nil - }() - } + // //cmdDone <- nil + // }() + // } select { case err := <-smDone: @@ -79,12 +79,12 @@ func (exe IcsExec) Execute() *icserror.IcsError { //err.Print() return err } - case err := <-cmdDone: - l.Printf(icslog.LOG_LEVEL_INFO, -1, "Closed BotCommand manager: %s", err) - if err != nil { - //err.Print() - return err - } + // case err := <-cmdDone: + // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Closed BotCommand manager: %s", err) + // if err != nil { + // //err.Print() + // return err + // } } } diff --git a/icsconf/icsconf.go b/icsconf/icsconf.go index ebcf097..268dfda 100644 --- a/icsconf/icsconf.go +++ b/icsconf/icsconf.go @@ -11,7 +11,7 @@ import ( ) type IcsConfig struct { - XMLName xml.Name `xml:"ICSVG"` + XMLName xml.Name `xml:"ICSVSIM"` Version string `xml:"version,attr"` InfoConfig InfoConfig `xml:"INFO"` LicenseConfig LicenseConfig `xml:"LICENSE"` @@ -23,6 +23,8 @@ type IcsConfig struct { VoiceAgentConfig VoiceAgentConfig `xml:"VOICEAGENT"` PbxConfig PbxConfig `xml:"PBX"` SimLoopCount int `xml:"SIMLOOP"` + Representative Representative `xml:"REPRESENTATIVE"` + CallEndInfo CallEndInfo `xml:"CALLENDINFO"` HomeDir string } @@ -105,6 +107,16 @@ type SIPConfig struct { SIPProxy string `xml:"proxy,attr"` } +type Representative struct { + Value bool `xml:"value,attr"` + Name string `xml:"name,attr"` +} + +type CallEndInfo struct { + Value bool `xml:"value,attr"` + Loop int `xml:"loopCount,attr"` +} + type PbxConfig struct { PbxIp string `xml:"IP"` PbxPort int `xml:"PORT"` diff --git a/icslog/icslog.go b/icslog/icslog.go index 26f8449..ab6ec1e 100644 --- a/icslog/icslog.go +++ b/icslog/icslog.go @@ -130,7 +130,7 @@ func NewIcsLog(conf *icsconf.LogConfig, level int, output int, path string, disk //make log file - icsvg.log-yyyymmdd yyyy, mm, dd := gIcsLog.CurrentDate.Date() - gIcsLog.LogFileName = fmt.Sprintf("%s/icsvg.log-%d%02d%02d", gIcsLog.Path, yyyy, mm, dd) + gIcsLog.LogFileName = fmt.Sprintf("%s/icsvsim.log-%d%02d%02d", gIcsLog.Path, yyyy, mm, dd) stat, err := os.Stat(gIcsLog.LogFileName) if err == nil { diff --git a/icspacketparser/icssipparser.go b/icspacketparser/icssipparser.go index d39f5f0..c0f8876 100644 --- a/icspacketparser/icssipparser.go +++ b/icspacketparser/icssipparser.go @@ -1,6 +1,7 @@ package icspacketparser import ( + "fmt" "strconv" "strings" @@ -63,6 +64,7 @@ type SIP struct { MaxForwards int64 Contact string To string + ToRep string // ToTag string From string // FromTag string @@ -80,6 +82,7 @@ type SIP struct { ResType string XAICall string + Media string // direction bool @@ -340,6 +343,8 @@ func (s *SIP) setSipStruct(name string, value string) (icserr *icserror.IcsError return nil case "X-AICALL": s.XAICall = value + case "MEDIA": + s.Media = value default: values := strings.Split(value, ";") @@ -355,6 +360,21 @@ func (s *SIP) setSipStruct(name string, value string) (icserr *icserror.IcsError return nil } +// representative +func (s *SIP) SetTo(name string) { + name1 := strings.SplitN(s.To, "@", 2) + tmp := "", s.AgentName) + if conf.Representative.Value { + inviteMethod = fmt.Sprintf("sip:%s@192.168.0.83;transport=%s", "AISB", transport) + s.uri = fmt.Sprintf("sip:%s@%s", "AISB", ip) + to = fmt.Sprintf("", "AISB") + } reqInvite := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_INVITE, inviteMethod) via := fmt.Sprintf("SIP/2.0/UDP 192.168.0.83:5090;branch=%s", sipasm.GenerateBranch()) maxforwards := "70" from := fmt.Sprintf(";tag=%s", inviteto, ip, sipasm.GenerateTag()) - s.uri = fmt.Sprintf("sip:%s@%s", s.AgentName, ip) l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set URI [%s]", s.uri) - to := fmt.Sprintf("", s.AgentName) postfix := "192.168.0.83" var callid string if len(s.callID) != 0 { @@ -331,7 +338,12 @@ func (s *IcsSession) RequestInvite(inviteto string) { s.callID = callid - s.SetAgentStatus(STATUS_AGENT_CALLING) + if conf.Representative.Value { + s.SetAgentStatus(STATUS_AGENT_READY) + } else { + s.SetAgentStatus(STATUS_AGENT_CALLING) + } + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent Data(%d) [%s]->[%s]>\n%s", wlen, @@ -342,6 +354,93 @@ func (s *IcsSession) RequestInvite(inviteto string) { inviteFunc() } +// pass invite for sim +func (s *IcsSession) PassInvite(sip *icspacketparser.SIP) *icserror.IcsError { + l := icslog.GetIcsLog() + conf := icsconf.GetIcsConfig() + + // TODO - send bye + defer func() { + if err := recover(); err != nil { + l.Printf(icslog.LOG_LEVEL_WARN, s.ID, "%s \n%s", + icserror.ICSERRNETNotConnectError.GetMessage(), debug.Stack()) + } + }() + + ip := conf.InfoConfig.ServerIP + port := conf.SIPConfig.Port + + fmt.Println("sip.Media ", sip.Media) + mediaPort, _ := strconv.Atoi(sip.Media) + + proxy := conf.SIPConfig.SIPProxy + transport := conf.SIPConfig.Transport + format := conf.AgentConfig[s.ID].MediaConfig.Format + + inviteMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "AISB", proxy, transport) + reqInvite := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_INVITE, inviteMethod) + + via := fmt.Sprintf("SIP/2.0/UDP %s;rport;branch=%s", ip, sipasm.GenerateBranch()) + maxforwards := "70" + from := fmt.Sprintf(";tag=%s", s.AgentName, ip, sipasm.GenerateTag()) + + s.uri = fmt.Sprintf("sip:%s@%s", s.AgentName, ip) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set URI [%s]", s.uri) + + to := fmt.Sprintf("", "01025670081", ip) + //to := fmt.Sprintf("", s.AgentName, ip) + // postfix := fmt.Sprintf("@%s", ip) + var callid string + callid = uuid.New().String() + // if len(s.callID) != 0 { + // callid = s.callID + // //if len(s.regiCallID) != 0 { + // //callid = s.regiCallID + // } else { + // callid = sipasm.GenerateCallID(postfix) + // } + cseq := fmt.Sprintf("%d INVITE", s.Cseq) + s.Cseq++ + contact := fmt.Sprintf("", s.AgentName, ip, port, transport) + //expires := fmt.Sprintf("%d", conf.AgentConfig[s.ID].RegisterConfig.RegisterExpire) + userAgent := conf.InfoConfig.Product + allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" + supported := "timer,path,replaces" + + formats := strings.Split(format, " ") + reqInvite.SDP = sipasm.NewSDPMessage(ip, mediaPort, formats[:], 0) + sdpLength := fmt.Sprintf("%d", len(reqInvite.SDP.String())) + + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, via) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, from) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, to) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, callid) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, cseq) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) + //reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_EXPIRES, expires) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_TYPE, "application/sdp") + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, sdpLength) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) + + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "sip 내용 전달! %+v", sip) + // sendINVITE := fmt.Sprintf("%+v",sip) + wlen, werr := (*s.sipNeter).WriteSIP([]byte(reqInvite.String())) + if werr != nil { + l.Print(icslog.LOG_LEVEL_FATAL, s.ID, werr.GetMessage()) + s.SetAgentStatus(STATUS_AGENT_READY) + return werr + } + fmt.Println(wlen) + + + s.RemoveSession() + return nil +} + //response invite func (s *IcsSession) ProcInvite(sip *icspacketparser.SIP) *icserror.IcsError { l := icslog.GetIcsLog() @@ -413,6 +512,7 @@ func (s *IcsSession) ProcInvite(sip *icspacketparser.SIP) *icserror.IcsError { remoteAddr := icsnet.NewNetAddrWithIPAddr(remoteAddrs) //connect to remote RTP s.rtpMediaNeter.SetRemoteAddr(&remoteAddr) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set media remote address - %s(%s)", remoteAddr, s.rtpMediaNeter.RemoteAddr()) /* cerr := s.rtpMediaNeter.Connect(nil, &remoteAddr) @@ -708,6 +808,7 @@ func (s *IcsSession) ProcInvite(sip *icspacketparser.SIP) *icserror.IcsError { func (s *IcsSession) ProcACKInvite(sip *icspacketparser.SIP) *icserror.IcsError { l := icslog.GetIcsLog() conf := icsconf.GetIcsConfig() + s.InviteSIP = sip /////////////////////////////////////////// /////////////////////////////////////////// @@ -748,7 +849,11 @@ func (s *IcsSession) ProcACKInvite(sip *icspacketparser.SIP) *icserror.IcsError transport := conf.SIPConfig.Transport format := conf.AgentConfig[s.ID].MediaConfig.Format + if conf.Representative.Value {// 대표번호일때 강제로 콜링으로 변환 + s.SetAgentStatus(STATUS_AGENT_CALLING) + } status := s.GetAgentStatus() + fmt.Println("BOT STATUS ", status == STATUS_AGENT_CALLING) if status == STATUS_AGENT_CALLING { ackMethod := fmt.Sprintf("sip:01025670081@%s;transport=%s", "192.168.0.222:5090", transport) @@ -941,6 +1046,8 @@ func (s *IcsSession) ProcACKInvite(sip *icspacketparser.SIP) *icserror.IcsError /////////////////////////////////////////// /////////////////////////////////////////// + fmt.Println(s) + fmt.Println("loop count ", s.simLoopCount) l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent Data(%d) [%s]->[%s]>\n%s", wlen, (*s.sipNeter).LocalAddr().String(), @@ -1044,6 +1151,8 @@ func (s *IcsSession) ProcBye(sip *icspacketparser.SIP) *icserror.IcsError { //send call signal-end to VoiceAgent ///////////////////////////////////////////// //set call signal addr + // fmt.Println(s) + fmt.Println(s) csraddr := icsnet.NewNetAddrWithIPPort(s.AgentInfo.IP, s.AgentInfo.Port) //csladdr := icsnet.NewNetAddrWithIPPort(conf.VoiceAgentConfig.MyAddr.ServerIP, conf.VoiceAgentConfig.MyAddr.ServerPort) /* @@ -1132,40 +1241,18 @@ func (s *IcsSession) SendRequestInvite() { fmt.Println("######### sim loop count!! ", conf.SimLoopCount) 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!!!!!!!", s.AgentName, s.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") - // fmt.Printf("Sent Call Start sip(%s) count (%d)\n\n", s.AgentName, s.simLoopCount) - // l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent Call Start sip(%s) count (%d)", s.AgentName, s.simLoopCount) - // tmp := "SIP/2.0 200 OK\r\nVia: SIP/2.0/UDP 192.168.0.83;received=192.168.0.21;rport=5090;branch=N2IwOGJjNmYtMTY5Zi00Y2NkLThjYzAtYWVhOTg3OGMyYTdj\r\nFrom: ;tag=MTY1MDk1NTEwNTY4NzgzNjI3NQ--\r\nTo: ;tag=1c1466330427\r\nCall-ID: 6a0b9cc3-e0c6-48da-a4f8-e7c40003bab2@192.168.0.21\r\nCSeq: 2 OPTIONS\r\nContact: \r\nServer: M800B/v.7.20A.204.759\r\nContent-Length: 0\r\n" - // sendVal := []string{fmt.Sprintf(tmp, s.AgentName, s.AgentName)} - // address, err := net.ResolveUDPAddr("udp", "192.168.0.83:5090") - // if err != nil { - // fmt.Printf("ERROR!: %s\n", err) - // } - // conn, err := net.DialUDP("udp", nil, address) - // if err != nil { - // l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "connect err", err) - // fmt.Println("## connect err", err) - // } - - // l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "send val - %s", []string(sendVal)) - // for _, elem := range sendVal[:] { - // sent, err := conn.Write([]byte(elem)) - // if err != nil { - // fmt.Printf("sent %d-%s\n", sent, elem) - // } - - // time.Sleep(time.Millisecond * 100) - // } s.simLoopCount += 1 } else { - l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Send Call Start Sip(%s) END!!!!!!!", s.AgentName, s.simLoopCount) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Send Call Start Sip(%s) END!!!!!!!\r\n", s.AgentName, s.simLoopCount) } } func (s *IcsSession) ProcSIP20Bye(sip *icspacketparser.SIP) *icserror.IcsError { l := icslog.GetIcsLog() conf := icsconf.GetIcsConfig() + // conf := icsconf.GetIcsConfig() //ip := conf.InfoConfig.ServerIP //mediaPort := conf.AgentConfig[s.ID].MediaConfig.Port //sipPort := conf.SIPConfig.Port @@ -1173,57 +1260,67 @@ func (s *IcsSession) ProcSIP20Bye(sip *icspacketparser.SIP) *icserror.IcsError { //format := conf.AgentConfig[s.ID].MediaConfig.Format l.Print(icslog.LOG_LEVEL_INFO, s.ID, "SIP2.0 BYE") - //////////////////////////////////////// - //send call signal-end to VoiceAgent - ///////////////////////////////////////////// - //set call signal addr - csraddr := icsnet.NewNetAddrWithIPPort(s.AgentInfo.IP, s.AgentInfo.Port) - //csladdr := icsnet.NewNetAddrWithIPPort(conf.VoiceAgentConfig.MyAddr.ServerIP, conf.VoiceAgentConfig.MyAddr.ServerPort) - /* - csraddr := icsnet.NewNetAddrWithIPPort(conf.VoiceAgentConfig.AgentInfo[s.ID].IP, conf.VoiceAgentConfig.AgentInfo[s.ID].Port) - csladdr := icsnet.NewNetAddrWithIPPort(conf.VoiceAgentConfig.MyAddr.ServerIP, conf.VoiceAgentConfig.MyAddr.ServerPort) - */ + s.RemoveSession() + // //////////////////////////////////////// + // //send call signal-end to VoiceAgent + // ///////////////////////////////////////////// + // //set call signal addr + // csraddr := icsnet.NewNetAddrWithIPPort(s.AgentInfo.IP, s.AgentInfo.Port) + // //csladdr := icsnet.NewNetAddrWithIPPort(conf.VoiceAgentConfig.MyAddr.ServerIP, conf.VoiceAgentConfig.MyAddr.ServerPort) + // /* + // csraddr := icsnet.NewNetAddrWithIPPort(conf.VoiceAgentConfig.AgentInfo[s.ID].IP, conf.VoiceAgentConfig.AgentInfo[s.ID].Port) + // csladdr := icsnet.NewNetAddrWithIPPort(conf.VoiceAgentConfig.MyAddr.ServerIP, conf.VoiceAgentConfig.MyAddr.ServerPort) + // */ + + // defer func() { + // if err := recover(); err != nil { + // l.Printf(icslog.LOG_LEVEL_WARN, s.ID, "%s %s\n%s", + // icserror.ICSERRNETNotConnectError.GetMessage(), csraddr, debug.Stack()) + // } + // }() + // var inout string = "O" + // s.SetDirection(true) + // if s.GetDirection() { + // inout = "I" + // } + // callsignal := writecallsignal.NewCallSignalData() + // s.StartTimeStamp = time.Now().UnixNano() + // s.EndTimeStamp = time.Now().UnixNano() + // s.ChannelID = conf.GetChannelID() + // s.ServerID = conf.GetServerID() + // 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] + // ////////// + // 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) + // callsignal.SetData(s.ChannelID, s.ServerID, s.Station, s.CustID, s.StartTimeStamp, s.EndTimeStamp, "E", inout) + // t, wlen, werr := icsnet.SendCallSignal(nil, &csraddr, callsignal.GetData()) + // //t, wlen, werr := icsnet.SendCallSignal(&csladdr, &csraddr, callsignal.GetData()) + // defer t.Close() + // if werr != nil { + // l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Failed to send Call-End signal to VoiceAgent(%s)", werr.GetError()) + // } else { + // l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent Call-End signal to VoiceAgent(%d)", wlen) + // } + // //close voiceneter(voice connection to voice agent) + // if s.VoiceNeter != nil { + // s.VoiceNeter.Close() + // } - defer func() { - if err := recover(); err != nil { - l.Printf(icslog.LOG_LEVEL_WARN, s.ID, "%s %s\n%s", - icserror.ICSERRNETNotConnectError.GetMessage(), csraddr, debug.Stack()) - } - }() - var inout string = "O" - s.SetDirection(true) - if s.GetDirection() { - inout = "I" - } - callsignal := writecallsignal.NewCallSignalData() - s.StartTimeStamp = time.Now().UnixNano() - s.EndTimeStamp = time.Now().UnixNano() - s.ChannelID = conf.GetChannelID() - s.ServerID = conf.GetServerID() - 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] - ////////// - 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) - callsignal.SetData(s.ChannelID, s.ServerID, s.Station, s.CustID, s.StartTimeStamp, s.EndTimeStamp, "E", inout) - t, wlen, werr := icsnet.SendCallSignal(nil, &csraddr, callsignal.GetData()) - //t, wlen, werr := icsnet.SendCallSignal(&csladdr, &csraddr, callsignal.GetData()) - defer t.Close() - if werr != nil { - l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Failed to send Call-End signal to VoiceAgent(%s)", werr.GetError()) + // s.RemoveSession() + fmt.Println("######### sim loop count!! ", conf.SimLoopCount) + 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.simLoopCount += 1 } else { - l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent Call-End signal to VoiceAgent(%d)", wlen) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Send Call Start Sip(%s) END!!!!!!!\r\n", s.AgentName, s.simLoopCount) } - //close voiceneter(voice connection to voice agent) - if s.VoiceNeter != nil { - s.VoiceNeter.Close() - } - - s.RemoveSession() return nil } @@ -1231,7 +1328,7 @@ func (s *IcsSession) ProcSIP20Bye(sip *icspacketparser.SIP) *icserror.IcsError { //rtp sender callback timer function func (s *IcsSession) SendRTPCB(t *icscbtimer.IcsCBTimer) { l := icslog.GetIcsLog() - //conf := icsconf.GetIcsConfig() + conf := icsconf.GetIcsConfig() //format := conf.AgentConfig[s.ID].MediaConfig.Format defer func() { @@ -1270,7 +1367,7 @@ func (s *IcsSession) SendRTPCB(t *icscbtimer.IcsCBTimer) { // voiceFilePath := "/home/leejj9612/sim/voice/9001-RX-1648533187911907029.pcm" - voiceFilePath := "/home/leejj9612/sim/voice/Sample.pcm" + voiceFilePath := "/home/icsvsim/voice/Sample.pcm" readPcmData, ferr := ioutil.ReadFile(voiceFilePath) if ferr != nil { fmt.Println("Read Voice File error ", ferr) @@ -1283,6 +1380,7 @@ func (s *IcsSession) SendRTPCB(t *icscbtimer.IcsCBTimer) { pcmDataLen := len(readPcmData) pcmData := make([]byte, pcmDataLen) copy(pcmData, readPcmData[44:]) //cut off wave header + // fmt.Printf("@@@@@ Read Data len: %d\r\n@@@@@ data: %+v\r\n@@@@@ copyData: %+v \r\n\r\n", pcmDataLen, readPcmData, pcmData) //pcmData := s.tts[44:] //cut off wave header //fmt.Println("pcmData len>>>", pcmDataLen, len(s.tts)) s.m.Unlock() @@ -1297,85 +1395,175 @@ func (s *IcsSession) SendRTPCB(t *icscbtimer.IcsCBTimer) { } //defer s.TxConverter.Close() + fmt.Printf("@@@@@@@@@@@@@@@@@ pcmDataLen : %d, loop Count: %d \r\n", pcmDataLen, conf.CallEndInfo.Loop) //fmt.Println(">>>", s.payloadType, icspacketparser.RTPPayloadInfo[s.payloadType].PSize) var rtp *icsrtp.RTP - for { - iter := 0 - totalSentLen := 0 - offset := 0 - psize := icspacketparser.RTPPayloadInfo[s.payloadType].PSize - ts := s.rtpTS - seq := s.rtpSeq - for t1 := range t.GetTick() { - if pcmDataLen < offset+psize*2 { - //fmt.Println("BREAK!", pcmDataLen, offset+psize*2) - break - } - payload, convErr := s.TxConverter.Encode(pcmData[offset : offset+psize*2]) - //payload, convErr := s.TxConverter.Encode(s.tts[offset : offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) - if convErr != nil { - l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Encoding Error - %s", convErr.GetError()) - return - } - - //fmt.Printf("1048@@@@ %+v\n", s.rtpMediaNeter) - /* - wflen, wferr := ttsfile1.Write(payload) - if wferr != nil { - fmt.Println("file.write error", wferr, wflen) - os.Exit(1) + if conf.CallEndInfo.Value { + for lc := 0; lc < conf.CallEndInfo.Loop; lc++ { + fmt.Printf("@@@@@@@@@@@@@@@@@@@@@@ %d ", lc) + iter := 0 + totalSentLen := 0 + offset := 0 + psize := icspacketparser.RTPPayloadInfo[s.payloadType].PSize + ts := s.rtpTS + seq := s.rtpSeq + for t1 := range t.GetTick() { + if pcmDataLen < offset+psize { + //fmt.Println("BREAK!", pcmDataLen, offset+psize*2) + break } - ttsfile1.Sync() - */ + /* + uplus에서 샘플 음성으로 pcma를 사용(이미 인코딩 되있는 음성파일이라 읽어서 160씩 보내주면됨) + - 인코딩 할 필요 없는 파일이라 인코딩 제거 + - pcma여서 160으로 고정해서 전송(다른 경우는 추후 진행) + */ + payload := pcmData[offset : offset+psize] + //payload, convErr := s.TxConverter.Encode(s.tts[offset : offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) + // if convErr != nil { + // l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Encoding Error - %s", convErr.GetError()) + // return + // } + + //fmt.Printf("1048@@@@ %+v\n", s.rtpMediaNeter) + /* + wflen, wferr := ttsfile1.Write(payload) + if wferr != nil { + fmt.Println("file.write error", wferr, wflen) + os.Exit(1) + } + ttsfile1.Sync() + */ - if iter == 0 { - if offset+psize >= pcmDataLen { - //if offset+psize >= alawDataLen { - offset = 0 + if iter == 0 { + if offset+psize >= pcmDataLen { + //if offset+psize >= alawDataLen { + offset = 0 + } + rtp = icsrtp.NewRTP(1, //set mark bit + int(s.payloadType), + int(seq), + int(ts), + int(s.rtpSSRC), + payload) + //alawData[offset:offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) + totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize + offset += psize + //offset += icspacketparser.RTPPayloadInfo[s.payloadType].PSize + } else { + if offset+psize >= pcmDataLen { + //if offset+psize >= alawDataLen { + offset = 0 + } + rtp = icsrtp.NewRTP(0, + int(s.payloadType), + int(seq), + int(ts), + int(s.rtpSSRC), + payload) + //alawData[offset:offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) + totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize + offset += psize + //offset += icspacketparser.RTPPayloadInfo[s.payloadType].PSize } - rtp = icsrtp.NewRTP(1, //set mark bit - int(s.payloadType), - int(seq), - int(ts), - int(s.rtpSSRC), - payload) - //alawData[offset:offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) - totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize - offset += psize * 2 - //offset += icspacketparser.RTPPayloadInfo[s.payloadType].PSize - } else { - if offset+psize >= pcmDataLen { - //if offset+psize >= alawDataLen { - offset = 0 + iter++ + ts += uint32(psize) + seq++ + s.txCnt++ + + //fmt.Printf("1095@@@@ %+v\n", s.rtpMediaNeter) + wlen, werr := s.rtpMediaNeter.WriteRTP(rtp.Byte()) + if werr != nil { + fmt.Println(t1, wlen, werr) } - rtp = icsrtp.NewRTP(0, - int(s.payloadType), - int(seq), - int(ts), - int(s.rtpSSRC), - payload) - //alawData[offset:offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) - totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize - offset += psize * 2 - //offset += icspacketparser.RTPPayloadInfo[s.payloadType].PSize - } - iter++ - ts += uint32(psize) - seq++ - s.txCnt++ + //fmt.Printf("wlen: %d\r", totalSentLen) + //l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent RTP(%d) to %s", wlen, s.rtpMediaNeter.RemoteAddr().String()) - //fmt.Printf("1095@@@@ %+v\n", s.rtpMediaNeter) - wlen, werr := s.rtpMediaNeter.WriteRTP(rtp.Byte()) - if werr != nil { - fmt.Println(t1, wlen, werr) } - //fmt.Printf("wlen: %d\r", totalSentLen) - //l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent RTP(%d) to %s", wlen, s.rtpMediaNeter.RemoteAddr().String()) + // fmt.Printf("Sent RTP(%d) to [%s] [%s]\r\n", totalSentLen, s.rtpMediaNeter.RemoteAddr().String(), s.AgentName ) + // l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent RTP(%d) to %s", totalSentLen, s.rtpMediaNeter.RemoteAddr().String()) + time.Sleep(time.Second) + } + s.RequestBYE(s.InviteSIP) + } else { + for { + iter := 0 + totalSentLen := 0 + offset := 0 + psize := icspacketparser.RTPPayloadInfo[s.payloadType].PSize + ts := s.rtpTS + seq := s.rtpSeq + for t1 := range t.GetTick() { + if pcmDataLen < offset+psize*2 { + //fmt.Println("BREAK!", pcmDataLen, offset+psize*2) + break + } + payload, convErr := s.TxConverter.Encode(pcmData[offset : offset+psize*2]) + //payload, convErr := s.TxConverter.Encode(s.tts[offset : offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) + if convErr != nil { + l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Encoding Error - %s", convErr.GetError()) + return + } + + //fmt.Printf("1048@@@@ %+v\n", s.rtpMediaNeter) + /* + wflen, wferr := ttsfile1.Write(payload) + if wferr != nil { + fmt.Println("file.write error", wferr, wflen) + os.Exit(1) + } + ttsfile1.Sync() + */ + + if iter == 0 { + if offset+psize >= pcmDataLen { + //if offset+psize >= alawDataLen { + offset = 0 + } + rtp = icsrtp.NewRTP(1, //set mark bit + int(s.payloadType), + int(seq), + int(ts), + int(s.rtpSSRC), + payload) + //alawData[offset:offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) + totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize + offset += psize * 2 + //offset += icspacketparser.RTPPayloadInfo[s.payloadType].PSize + } else { + if offset+psize >= pcmDataLen { + //if offset+psize >= alawDataLen { + offset = 0 + } + rtp = icsrtp.NewRTP(0, + int(s.payloadType), + int(seq), + int(ts), + int(s.rtpSSRC), + payload) + //alawData[offset:offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) + totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize + offset += psize * 2 + //offset += icspacketparser.RTPPayloadInfo[s.payloadType].PSize + } + iter++ + ts += uint32(psize) + seq++ + s.txCnt++ + //fmt.Printf("1095@@@@ %+v\n", s.rtpMediaNeter) + wlen, werr := s.rtpMediaNeter.WriteRTP(rtp.Byte()) + if werr != nil { + fmt.Println(t1, wlen, werr) + } + //fmt.Printf("wlen: %d\r", totalSentLen) + //l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent RTP(%d) to %s", wlen, s.rtpMediaNeter.RemoteAddr().String()) + + } + // fmt.Printf("Sent RTP(%d) to [%s] [%s]\r\n", totalSentLen, s.rtpMediaNeter.RemoteAddr().String(), s.AgentName ) + // l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent RTP(%d) to %s", totalSentLen, s.rtpMediaNeter.RemoteAddr().String()) + time.Sleep(time.Second) + // s.RequestBYE(s.InviteSIP) } - // fmt.Printf("Sent RTP(%d) to [%s] [%s]\r\n", totalSentLen, s.rtpMediaNeter.RemoteAddr().String(), s.AgentName ) - // l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent RTP(%d) to %s", totalSentLen, s.rtpMediaNeter.RemoteAddr().String()) - time.Sleep(time.Second) } } @@ -1727,10 +1915,79 @@ func (s *IcsSession) ProcReferNotify200ok(sip *icspacketparser.SIP) { optfunc() } +func (s *IcsSession) RequestBYESIM() { + l := icslog.GetIcsLog() + conf := icsconf.GetIcsConfig() + ip := conf.InfoConfig.ServerIP + port := conf.SIPConfig.Port + transport := conf.SIPConfig.Transport + // contact := fmt.Sprintf("", s.AgentName, ip, port, transport) + // fmt.Println(">>>contact", inviteSIP.Contact) + // contactName1 := strings.SplitN(inviteSIP.Contact, "sip:", 2) + // contactName := strings.SplitN(contactName1[1], "@", 2) + + optfunc := func() { + optMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "AISB", conf.SIPConfig.SIPProxy, transport) + reqOpt := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_BYE, optMethod) + + via := fmt.Sprintf("SIP/2.0/UDP %s:%d;branch=%s", ip, port, sipasm.GenerateBranch()) + maxforwards := "70" + // from := strings.SplitN(s.CustID, ": ", 2) + from := s.AgentName + // from := fmt.Sprintf(";tag=%s", s.AgentName, ip, sipasm.GenerateTag()) + + s.uri = fmt.Sprintf("sip:%s@%s", s.AgentName, ip) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set URI [%s]", s.uri) + + // to := strings.SplitN(, ": ", 2) + to := s.CustID + //to := fmt.Sprintf("", s.AgentName, ip) + callid := s.callID + cseq := fmt.Sprintf("%d BYE", s.Cseq) + s.Cseq++ + //referto := fmt.Sprintf("") + contact := fmt.Sprintf("", s.AgentName, ip, port, transport) + userAgent := conf.InfoConfig.Product + allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" + supported := "timer,path,replaces" + contentLength := "0" + + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, via) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, to[0]) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, from[0]) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, callid) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, cseq) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) + //reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_REFER_TO, referto) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, contentLength) + reqOpt.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) + + wlen, werr := (*s.sipNeter).WriteSIP([]byte(reqOpt.String())) + if werr != nil { + l.Print(icslog.LOG_LEVEL_FATAL, s.ID, werr.GetMessage()) + return + } + + //l.Printf(icslog.LOG_LEVEL_WARN, s.ID, "BYE CALL STACK\n%s", debug.Stack()) + + l.Printf(icslog.LOG_LEVEL_DEBUG, s.ID, "Sent Data(%d) [%s]->[%s]>\n%s", + wlen, + (*s.sipNeter).LocalAddr().String(), + (*s.sipNeter).RemoteAddr().String(), + reqOpt.String()) + } + optfunc() +} + //request bye func (s *IcsSession) RequestBYE(inviteSIP *icspacketparser.SIP) { l := icslog.GetIcsLog() conf := icsconf.GetIcsConfig() + fmt.Printf("inviteSIP %+v\r\n", inviteSIP) ip := conf.InfoConfig.ServerIP port := conf.SIPConfig.Port transport := conf.SIPConfig.Transport @@ -1793,6 +2050,66 @@ func (s *IcsSession) RequestBYE(inviteSIP *icspacketparser.SIP) { optfunc() } +func (s *IcsSession) Res486Ack(sip *icspacketparser.SIP) *icserror.IcsError { + l := icslog.GetIcsLog() + conf := icsconf.GetIcsConfig() + + ip := conf.InfoConfig.ServerIP + //mediaPort := conf.AgentConfig[s.ID].MediaConfig.Port + sipPort := conf.SIPConfig.Port + transport := conf.SIPConfig.Transport + + ackMethod := fmt.Sprintf("sip:01025670081@%s;transport=%s", "192.168.0.222:5090", transport) + + reqAck := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_ACK, ackMethod) + + //via := fmt.Sprintf("SIP/2.0/UDP %s;rport;branch=%s", ip, sipasm.GenerateBranch()) + maxforwards := "70" + //from := fmt.Sprintf(";tag=%s", s.AgentName, ip, sipasm.GenerateTag()) + + s.uri = fmt.Sprintf("sip:%s@%s", s.AgentName, ip) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set URI [%s]", s.uri) + + //to := fmt.Sprintf("", "01025670081", "192.168.0.221") + //to := fmt.Sprintf("", "01025670081", ip) + //to := fmt.Sprintf("", s.AgentName, ip) + //cseq := fmt.Sprintf("%d INVITE", s.Cseq) + contact := fmt.Sprintf("", s.AgentName, 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 := (*s.sipNeter).WriteSIP([]byte(reqAck.String())) + if werr != nil { + l.Print(icslog.LOG_LEVEL_FATAL, s.ID, werr.GetMessage()) + s.SetAgentStatus(STATUS_AGENT_READY) + return werr + } + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent Data(%d) [%s]->[%s]>\n%s", + wlen, + (*s.sipNeter).LocalAddr().String(), + (*s.sipNeter).RemoteAddr().String(), + reqAck.String()) + + return nil +} + func (s *IcsSession) ResError(sip *icspacketparser.SIP, errNum int) { var resError *sipasm.SIPMessage l := icslog.GetIcsLog() diff --git a/icssessionmanager/csprocessor.go b/icssessionmanager/csprocessor.go index be8bfe7..a743f56 100644 --- a/icssessionmanager/csprocessor.go +++ b/icssessionmanager/csprocessor.go @@ -30,7 +30,8 @@ func (s *IcsSession) Processing(sip *parser.SIP) *icserror.IcsError { l.Printf(icslog.LOG_LEVEL_DEBUG2, s.ID, "INVITE") //time.Sleep(3000 * time.Millisecond) //return nil - return s.ProcInvite(sip) + return s.PassInvite(sip) + // return s.ProcInvite(sip) case parser.ICS_SIP_METHOD_PUBLISH: case parser.ICS_SIP_METHOD_OPTIONS: diff --git a/icssessionmanager/icssessionmanager.go b/icssessionmanager/icssessionmanager.go index 0b14856..2b26cb5 100644 --- a/icssessionmanager/icssessionmanager.go +++ b/icssessionmanager/icssessionmanager.go @@ -8,6 +8,7 @@ import ( "strings" "sync" "time" + // "strconv" "gitlab.com/ics_cinnamon/voicegateway/icscbtimer" "gitlab.com/ics_cinnamon/voicegateway/icsconf" @@ -18,6 +19,7 @@ import ( "gitlab.com/ics_cinnamon/voicegateway/icspacketparser" "gitlab.com/ics_cinnamon/voicegateway/icssvc" "gitlab.com/ics_cinnamon/voicegateway/recorddata" + "gitlab.com/ics_cinnamon/voicegateway/sipasm" ) type SessionManager struct { @@ -255,82 +257,205 @@ func (sm *SessionManager) Run() (icserr *icserror.IcsError) { l.Printf(icslog.LOG_LEVEL_INFO, -1, "icspacketparser.ICS_SIP_METHOD_NOT_FOUND-%s", string(data)) continue } else { - if SessionAvailableSipMethod(&sip) { - l.Printf(icslog.LOG_LEVEL_DEBUG, -1, "Session Processing Method-[%s, %s, %s]", - 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()) - continue + // fmt.Println("메서드", sip.Method.String()) + // if sip.Method == icspacketparser.ICS_SIP_METHOD_INVITE { + // // conf := icsconf.GetIcsConfig() + + // // TODO - send bye + // // defer func() { + // // if err := recover(); err != nil { + // // l.Printf(icslog.LOG_LEVEL_WARN, =1, "%s \n%s", + // // icserror.ICSERRNETNotConnectError.GetMessage(), debug.Stack()) + // // } + // // }() + + + // l.Printf(icslog.LOG_LEVEL_INFO, -1, "sip 내용 전달! %+v", sip) + // // ip := strings.Split(conf.SIPConfig.SIPProxy, ":")[0] + // // port, cerr := strconv.Atoi(strings.Split(conf.SIPConfig.SIPProxy, ":")[1]) + // // if cerr != nil { + // // fmt.Println("cerr err") + // // } + // // iport := fmt.Sprintf("%s:%d", ip, port) + + + // // fmt.Println(ip, port) + + // sipPort := conf.SIPConfig.Port + // // transport := conf.SIPConfig.Transport + // sipProxy := conf.SIPConfig.SIPProxy + + // remoteaddr := icsnet.NewNetAddrWithIPAddr(sipProxy) + // localAddrStr := fmt.Sprintf("192.168.0.4:%d", sipPort) + // localAddr := icsnet.NewNetAddrWithIPAddr(localAddrStr) + + // t := icsnet.NewUDP(&localAddr, &remoteaddr) + // // fmt.Println("local ", t.LocalAddr().String()) + // fmt.Println("remote ", t.RemoteAddr().String()) + // // sendSIP := []string{data[:]} + + // // for _, elem := range data { + // sent, err := t.WriteSIP([]byte(data)) + // if err != nil { + // fmt.Println(err) + // } + // // } + + // // wlen, werr := t.WriteSIP([]byte(data)) + // // if werr != nil { + // // l.Print(icslog.LOG_LEVEL_FATAL, -1, werr.GetMessage()) + // // // s.SetAgentStatus(STATUS_AGENT_READY) + // // // return werr + // // } + // fmt.Printf("send data \r\n%s, \r\n %d\r\n", string(data), sent) + + // } else { + if SessionAvailableSipMethod(&sip) { + l.Printf(icslog.LOG_LEVEL_DEBUG, -1, "Session Processing Method-[%s, %s, %s]", + 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()) + ////////////////////////////////////////////// + } + 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 { + s.SetCallID(sip.GetCallID()) + } + // 대표번호 + if sip.Method == icspacketparser.ICS_SIP_METHOD_SIP20 || strings.SplitN(sip.Cseq, " ", 2)[1] == "INVITE" { + s.SetCallID(sip.GetCallID()) + // representative + if conf.Representative.Value { + sip.SetToRep() + sip.SetTo(s.AgentName) + // if sip.Source == "486" { + // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") + // fmt.Println("##### All Agent Clling!!!!!! ") + // s.Res486Ack(&sip) + // continue + // } + } + } + + if s.CheckAutomata(&sip) { + s.SetSessionMethod(sip) + } else { + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Not Allowed Method(%d)", s.MethodAutomata) + //TODO : response 400 error + continue + } + + // if conf.Representative.Value && sip.Source == "486" { + // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") + // fmt.Println("##### All Agent Clling!!!!!! ") + // s.Res486Ack(&sip) + // continue + // } + + //if cancel ACK or bye res, remove the session + if s.MethodAutomata == ICS_SIP_AUTOMATA_CANCEL && sip.Method == icspacketparser.ICS_SIP_METHOD_ACK { + s.Stop() + serr := s.RemoveSession() + if serr != nil { + l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "RemoveSession Error %s", serr.GetMessage()) + + } else { + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Session Removed") + } + 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 { - s.SetCallID(sip.GetCallID()) + + //post the sip event to session + h := icsevent.NewEventH() + evt, evtErr := h.AllocEvent(sip) + if evtErr != nil { + return evtErr } - - if s.CheckAutomata(&sip) { - s.SetSessionMethod(sip) + perr := h.PostEvent(s.ID, evt) + //perr := h.PostEvent(int(s.GetSessionID()), evt) + if perr == nil { + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Post SIP event[%d] to Session[%03d]", evt.ID, s.ID) } else { - l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Not Allowed Method(%d)", s.MethodAutomata) - //TODO : response 400 error - continue + l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Failed to post SIP event[%d] to Session[%03d] - %s", + evt.ID, s.ID, perr.GetError()) } - - //if cancel ACK or bye res, remove the session - if s.MethodAutomata == ICS_SIP_AUTOMATA_CANCEL && sip.Method == icspacketparser.ICS_SIP_METHOD_ACK { - s.Stop() - serr := s.RemoveSession() - if serr != nil { - l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "RemoveSession Error %s", serr.GetMessage()) - + } else { // no session + switch sip.Method { + case icspacketparser.ICS_SIP_METHOD_OPTIONS: + l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) + case icspacketparser.ICS_SIP_METHOD_SIP20: + if strings.Contains(strings.ToUpper(sip.Cseq), "OPTIONS") { + //l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) } else { - l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Session Removed") + l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) } - continue - } - } - - //post the sip event to session - h := icsevent.NewEventH() - evt, evtErr := h.AllocEvent(sip) - if evtErr != nil { - return evtErr - } - perr := h.PostEvent(s.ID, evt) - //perr := h.PostEvent(int(s.GetSessionID()), evt) - if perr == nil { - l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Post SIP event[%d] to Session[%03d]", evt.ID, s.ID) - } else { - l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Failed to post SIP event[%d] to Session[%03d] - %s", - evt.ID, s.ID, perr.GetError()) - } - } else { // no session - switch sip.Method { - case icspacketparser.ICS_SIP_METHOD_OPTIONS: - l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) - case icspacketparser.ICS_SIP_METHOD_SIP20: - if strings.Contains(strings.ToUpper(sip.Cseq), "OPTIONS") { - //l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) - } else { + default: l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) } - default: - l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) } - } - + // } } data = nil diff --git a/icssessionmanager/sessionoperator.go b/icssessionmanager/sessionoperator.go index 57b6ee0..dbcf516 100644 --- a/icssessionmanager/sessionoperator.go +++ b/icssessionmanager/sessionoperator.go @@ -78,6 +78,39 @@ func findSessionWithAgentName(agentName string) *IcsSession { return nil } +// representative +func findSessionWithSessionStatus(callID string) *IcsSession { + conf := icsconf.GetIcsConfig() + sessions := getSessionInstance(nil) + for idx, session := range sessions { + 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() + return session + } + session.m.Unlock() + } + + return nil +} + +func findSessionWithSessionStatusForINVITE() *IcsSession { + conf := icsconf.GetIcsConfig() + sessions := getSessionInstance(nil) + for idx, session := range sessions { + session.m.Lock() + if strings.Compare(conf.AgentConfig[idx].Value, "true") == 0 && session.agentStatus == 64 { + session.m.Unlock() + return session + } + session.m.Unlock() + } + + return nil +} + func findSessionWithAgentName2(agentName string) *IcsSession { sessions := getSessionInstance(nil) for _, session := range sessions { @@ -110,6 +143,7 @@ func FindSessionID(sip icspacketparser.SIP) (IcsSessionID, *icserror.IcsError) { func FindSession(data interface{}) (*IcsSession, *icserror.IcsError) { //func FindSession(sip icspacketparser.SIP) (*IcsSession, *icserror.IcsError) { l := icslog.GetIcsLog() + conf := icsconf.GetIcsConfig() defer func() { if err := recover(); err != nil { l.Printf(icslog.LOG_LEVEL_WARN, -1, "%s\n%s", @@ -130,26 +164,36 @@ func FindSession(data interface{}) (*IcsSession, *icserror.IcsError) { } */ - if len(agentname) > 0 { - l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "FindSession>findSessionWithAgentName> agent name[%s]", agentname) - session := findSessionWithAgentName(agentname) - if session != nil { - return session, nil + if conf.Representative.Value && sip.Method == icspacketparser.ICS_SIP_METHOD_SIP20 && strings.SplitN(sip.Cseq, " ", 2)[1] == "INVITE" { + if len(agentname) > 0 { + l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "FindSession>findSessionWithAgentName> agent name[%s]", agentname) + session := findSessionWithSessionStatus(callID) + if session != nil { + return session, nil + } } - } - if len(callID) > 0 { - l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "FindSession>findSessionWithCallID> callid [%s]", callID) - session := findSessionWithCallID(callID) - if session != nil { - return session, nil + } else { + if len(callID) > 0 { + l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "FindSession>findSessionWithCallID> callid [%s]", callID) + session := findSessionWithCallID(callID) + if session != nil { + return session, nil + } } - } - if len(uri) > 0 { - l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "FindSession>findSessionWithFromURI> uri [%s]", uri) - //fmt.Println("findsession", uri) - session := findSessionWithFromURI(uri) - if session != nil { - return session, nil + if len(agentname) > 0 { + l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "FindSession>findSessionWithAgentName> agent name[%s]", agentname) + session := findSessionWithAgentName(agentname) + if session != nil { + return session, nil + } + } + if len(uri) > 0 { + l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "FindSession>findSessionWithFromURI> uri [%s]", uri) + //fmt.Println("findsession", uri) + session := findSessionWithFromURI(uri) + if session != nil { + return session, nil + } } } case icspacketparser.RTP: @@ -335,6 +379,7 @@ func (s *IcsSession) RemoveSession() *icserror.IcsError { s.referto = "" s.SetAgentStatus(STATUS_AGENT_READY) s.SetRegisterStatus(STATUS_REGISTER_READY) + fmt.Println("REMOVE SESSION STATUS: ", s.GetAgentStatus()) s.m.Unlock() return nil diff --git a/icsutil/daemon.go b/icsutil/daemon.go index 8f69d68..31aacdb 100644 --- a/icsutil/daemon.go +++ b/icsutil/daemon.go @@ -69,7 +69,7 @@ func WritePID(pid int) *icserror.IcsError { var homeDir string for _, e := range os.Environ() { s := strings.SplitN(e, "=", 2) - if strings.Compare(s[0], "ICSVG_ROOT") == 0 { + if strings.Compare(s[0], "ICSVSIM_ROOT") == 0 { homeDir = s[1] //service.SetHomeDir(s[1]) isStop = true @@ -93,7 +93,7 @@ func CheckPID() bool { var homeDir string for _, e := range os.Environ() { s := strings.SplitN(e, "=", 2) - if strings.Compare(s[0], "ICSVG_ROOT") == 0 { + if strings.Compare(s[0], "ICSVSIM_ROOT") == 0 { homeDir = s[1] //service.SetHomeDir(s[1]) isStop = true @@ -192,7 +192,7 @@ func PWritePID(pid int) *icserror.IcsError { var homeDir string for _, e := range os.Environ() { s := strings.SplitN(e, "=", 2) - if strings.Compare(s[0], "ICSVG_ROOT") == 0 { + if strings.Compare(s[0], "ICSVSIM_ROOT") == 0 { homeDir = s[1] //service.SetHomeDir(s[1]) isStop = true @@ -216,7 +216,7 @@ func PCheckPID() bool { var homeDir string for _, e := range os.Environ() { s := strings.SplitN(e, "=", 2) - if strings.Compare(s[0], "ICSVG_ROOT") == 0 { + if strings.Compare(s[0], "ICSVSIM_ROOT") == 0 { homeDir = s[1] //service.SetHomeDir(s[1]) isStop = true diff --git a/loader/loader.go b/loader/loader.go index ba8d39a..eb5ea7b 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -22,7 +22,7 @@ func main() { var homeDir string for _, e := range os.Environ() { s := strings.SplitN(e, "=", 2) - if strings.Compare(s[0], "ICSVG_ROOT") == 0 { + if strings.Compare(s[0], "ICSVSIM_ROOT") == 0 { homeDir = s[1] //service.SetHomeDir(s[1]) isStop = true @@ -35,7 +35,7 @@ func main() { } //configuration - configFile := fmt.Sprintf("%s/config/icsvg.xml", homeDir) + configFile := fmt.Sprintf("%s/config/icsvsim.xml", homeDir) conf, confErr := icsconf.OpenConfig(configFile, homeDir) if confErr != nil { confErr.PrintWithCaller(0) diff --git a/voicegateway b/voicegateway deleted file mode 100755 index 3c09112..0000000 Binary files a/voicegateway and /dev/null differ diff --git a/voicegatewayCheck/voicegatewayCheck.go b/voicegatewayCheck/voicegatewayCheck.go index e13f528..1c5744d 100644 --- a/voicegatewayCheck/voicegatewayCheck.go +++ b/voicegatewayCheck/voicegatewayCheck.go @@ -20,7 +20,7 @@ func main() { for _, e := range os.Environ() { s := strings.SplitN(e, "=", 2) - if strings.Compare(s[0], "ICSVG_ROOT") == 0 { + if strings.Compare(s[0], "ICSVSIM_ROOT") == 0 { homeDir = s[1] //service.SetHomeDir(s[1]) isStop = true @@ -33,7 +33,7 @@ func main() { } //configuration - configFile := fmt.Sprintf("%s/config/icsvg.xml", homeDir) + configFile := fmt.Sprintf("%s/config/icsvsim.xml", homeDir) //configFile := fmt.Sprintf("%s/config/icsvc.xml", service.GetHomeDir()) //fmt.Println("Config file:", configFile) conf, confErr := icsconf.OpenConfig(configFile, homeDir) @@ -57,7 +57,7 @@ func main() { if d1 != d2 || y1 != y2 || m1 != m2 { icsLog.M.Lock() - icsLog.LogFileName = fmt.Sprintf("%s/icsvg.log-%d%02d%02d", icsLog.Path, y2, m2, d2) // file name change + icsLog.LogFileName = fmt.Sprintf("%s/icsvsim.log-%d%02d%02d", icsLog.Path, y2, m2, d2) // file name change var oerr error icsLog.LogFile, oerr = os.OpenFile(icsLog.LogFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if oerr != nil { diff --git a/voicegateway.go b/voicegatewaysim.go similarity index 95% rename from voicegateway.go rename to voicegatewaysim.go index 4801b5a..5fa7205 100644 --- a/voicegateway.go +++ b/voicegatewaysim.go @@ -20,7 +20,7 @@ func main() { /////////////////////////////////// //Demonize service - //check Voice Gateway pid file - ICSVG_ROOT/voicegateway.pid + //check Voice Gateway pid file - ICSVSIM_ROOT/voicegateway.pid //if already running, terminate self. /* if icsutil.CheckPID() { @@ -48,7 +48,7 @@ func main() { var homeDir string for _, e := range os.Environ() { s := strings.SplitN(e, "=", 2) - if strings.Compare(s[0], "ICSVG_ROOT") == 0 { + if strings.Compare(s[0], "ICSVSIM_ROOT") == 0 { homeDir = s[1] //service.SetHomeDir(s[1]) isStop = true @@ -61,7 +61,7 @@ func main() { } //configuration - configFile := fmt.Sprintf("%s/config/icsvg.xml", homeDir) + configFile := fmt.Sprintf("%s/config/icsvsim.xml", homeDir) //configFile := fmt.Sprintf("%s/config/icsvg.xml", service.GetHomeDir()) //fmt.Println("Config file:", configFile) conf, confErr := icsconf.OpenConfig(configFile, homeDir)