mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
SM-Asterisk - connection and listening mechanism implementation
This commit is contained in:
@@ -341,7 +341,7 @@ const CGRATES_CFG_JSON = `
|
||||
"min_call_duration": "0s", // only authorize calls with allowed duration higher than this
|
||||
"max_call_duration": "3h", // maximum call duration a prepaid call can last
|
||||
"asterisk_conns":[ // instantiate connections to multiple Asterisk servers
|
||||
{"address": "127.0.0.1:8088", "user": "cgrates", "password": "CGRateS.org", "reconnects": 5}
|
||||
{"address": "127.0.0.1:8088", "user": "cgrates", "password": "CGRateS.org", "connect_attempts": 3,"reconnects": 5}
|
||||
],
|
||||
},
|
||||
|
||||
|
||||
@@ -529,10 +529,11 @@ func TestSmAsteriskJsonCfg(t *testing.T) {
|
||||
Max_call_duration: utils.StringPointer("3h"),
|
||||
Asterisk_conns: &[]*AstConnJsonCfg{
|
||||
&AstConnJsonCfg{
|
||||
Address: utils.StringPointer("127.0.0.1:8088"),
|
||||
User: utils.StringPointer("cgrates"),
|
||||
Password: utils.StringPointer("CGRateS.org"),
|
||||
Reconnects: utils.IntPointer(5),
|
||||
Address: utils.StringPointer("127.0.0.1:8088"),
|
||||
User: utils.StringPointer("cgrates"),
|
||||
Password: utils.StringPointer("CGRateS.org"),
|
||||
Connect_attempts: utils.IntPointer(3),
|
||||
Reconnects: utils.IntPointer(5),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -215,10 +215,11 @@ type HaPoolJsonCfg struct {
|
||||
}
|
||||
|
||||
type AstConnJsonCfg struct {
|
||||
Address *string
|
||||
User *string
|
||||
Password *string
|
||||
Reconnects *int
|
||||
Address *string
|
||||
User *string
|
||||
Password *string
|
||||
Connect_attempts *int
|
||||
Reconnects *int
|
||||
}
|
||||
|
||||
type SMAsteriskJsonCfg struct {
|
||||
|
||||
@@ -446,10 +446,11 @@ func NewDefaultAsteriskConnCfg() *AsteriskConnCfg {
|
||||
}
|
||||
|
||||
type AsteriskConnCfg struct {
|
||||
Address string
|
||||
User string
|
||||
Password string
|
||||
Reconnects int
|
||||
Address string
|
||||
User string
|
||||
Password string
|
||||
ConnectAttempts int
|
||||
Reconnects int
|
||||
}
|
||||
|
||||
func (aConnCfg *AsteriskConnCfg) loadFromJsonCfg(jsnCfg *AstConnJsonCfg) error {
|
||||
@@ -462,6 +463,9 @@ func (aConnCfg *AsteriskConnCfg) loadFromJsonCfg(jsnCfg *AstConnJsonCfg) error {
|
||||
if jsnCfg.Password != nil {
|
||||
aConnCfg.Password = *jsnCfg.Password
|
||||
}
|
||||
if jsnCfg.Connect_attempts != nil {
|
||||
aConnCfg.ConnectAttempts = *jsnCfg.Connect_attempts
|
||||
}
|
||||
if jsnCfg.Reconnects != nil {
|
||||
aConnCfg.Reconnects = *jsnCfg.Reconnects
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
[general]
|
||||
enabled = yes
|
||||
allowed_origins = http://cgrates.org
|
||||
|
||||
[cgrates]
|
||||
type = user
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[internal]
|
||||
exten => _1XXX,1,NoOp()
|
||||
;same => n,Stasis(cgrates_auth)
|
||||
same => n,Stasis(cgrates_auth)
|
||||
same => n,Dial(PJSIP/${EXTEN},30)
|
||||
same => n,Hangup()
|
||||
|
||||
|
||||
2
glide.lock
generated
2
glide.lock
generated
@@ -8,7 +8,7 @@ imports:
|
||||
- name: github.com/cenk/rpc2
|
||||
version: 7ab76d2e88c77ca1a715756036d8264b2886acd2
|
||||
- name: github.com/cgrates/aringo
|
||||
version: fd546dbf48a7901754d414a345dc8d5572cb7c48
|
||||
version: af32eb4cfc6bfa2b73812221c099455dac3b603c
|
||||
- name: github.com/cgrates/fsock
|
||||
version: a8ffdbdfc8440016df008ba91e6f05f806d7a69f
|
||||
- name: github.com/cgrates/kamevapi
|
||||
|
||||
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package sessionmanager
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/cgrates/aringo"
|
||||
@@ -38,11 +39,16 @@ type SMAsterisk struct {
|
||||
astConnIdx int
|
||||
smg rpcclient.RpcClientConnection
|
||||
astConn *aringo.ARInGO
|
||||
astEvChan chan *json.RawMessage
|
||||
astErrChan chan error
|
||||
}
|
||||
|
||||
func (sma *SMAsterisk) connectAsterisk() error {
|
||||
connCfg := sma.cgrCfg.SMAsteriskCfg().AsteriskConns[sma.astConnIdx]
|
||||
_, err := aringo.NewARInGO(fmt.Sprintf("ws://%s/ari/events?api_key=%s:%s&app=%s", connCfg.Address, connCfg.User, connCfg.Password, CGRAuthAPP), connCfg.Reconnects)
|
||||
sma.astEvChan = make(chan *json.RawMessage)
|
||||
sma.astErrChan = make(chan error)
|
||||
_, err := aringo.NewARInGO(fmt.Sprintf("ws://%s/ari/events?api_key=%s:%s&app=%s", connCfg.Address, connCfg.User, connCfg.Password, CGRAuthAPP), "http://cgrates.org",
|
||||
sma.astEvChan, sma.astErrChan, connCfg.ConnectAttempts, connCfg.Reconnects)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -50,11 +56,19 @@ func (sma *SMAsterisk) connectAsterisk() error {
|
||||
}
|
||||
|
||||
// Called to start the service
|
||||
func (sma *SMAsterisk) ListenAndServe() error {
|
||||
func (sma *SMAsterisk) ListenAndServe() (err error) {
|
||||
if err := sma.connectAsterisk(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
for {
|
||||
select {
|
||||
case err = <-sma.astErrChan:
|
||||
return err
|
||||
case astRawEv := <-sma.astEvChan:
|
||||
fmt.Printf("<SMAsterisk> Received raw event: %+v\n", astRawEv)
|
||||
}
|
||||
}
|
||||
panic("<SMAsterisk> ListenAndServe out of select")
|
||||
}
|
||||
|
||||
// Called to shutdown the service
|
||||
|
||||
Reference in New Issue
Block a user