From d65897c2d07713409624ca96d1af75a035b4917e Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 9 Sep 2016 17:34:28 +0200 Subject: [PATCH] SM-Asterisk - connection and listening mechanism implementation --- config/config_defaults.go | 2 +- config/config_json_test.go | 9 +++++---- config/libconfig_json.go | 9 +++++---- config/smconfig.go | 12 +++++++---- .../asterisk/etc/asterisk/ari.conf | 1 + .../asterisk/etc/asterisk/extensions.conf | 2 +- glide.lock | 2 +- sessionmanager/smasterisk.go | 20 ++++++++++++++++--- 8 files changed, 39 insertions(+), 18 deletions(-) diff --git a/config/config_defaults.go b/config/config_defaults.go index d7b6469d5..ccf6042df 100644 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -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} ], }, diff --git a/config/config_json_test.go b/config/config_json_test.go index b90464109..ee4997639 100644 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -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), }, }, } diff --git a/config/libconfig_json.go b/config/libconfig_json.go index a0c743e85..710b36998 100644 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -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 { diff --git a/config/smconfig.go b/config/smconfig.go index b3738e92e..164d6c6f5 100644 --- a/config/smconfig.go +++ b/config/smconfig.go @@ -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 } diff --git a/data/tutorials/asterisk_events/asterisk/etc/asterisk/ari.conf b/data/tutorials/asterisk_events/asterisk/etc/asterisk/ari.conf index 77a5128ca..f78a801e7 100755 --- a/data/tutorials/asterisk_events/asterisk/etc/asterisk/ari.conf +++ b/data/tutorials/asterisk_events/asterisk/etc/asterisk/ari.conf @@ -1,5 +1,6 @@ [general] enabled = yes +allowed_origins = http://cgrates.org [cgrates] type = user diff --git a/data/tutorials/asterisk_events/asterisk/etc/asterisk/extensions.conf b/data/tutorials/asterisk_events/asterisk/etc/asterisk/extensions.conf index 96fc6ba55..734d8fb41 100755 --- a/data/tutorials/asterisk_events/asterisk/etc/asterisk/extensions.conf +++ b/data/tutorials/asterisk_events/asterisk/etc/asterisk/extensions.conf @@ -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() diff --git a/glide.lock b/glide.lock index 0908cbd03..c4c8d85c1 100644 --- a/glide.lock +++ b/glide.lock @@ -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 diff --git a/sessionmanager/smasterisk.go b/sessionmanager/smasterisk.go index b85211aa7..20e2b9920 100644 --- a/sessionmanager/smasterisk.go +++ b/sessionmanager/smasterisk.go @@ -18,6 +18,7 @@ along with this program. If not, see 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(" Received raw event: %+v\n", astRawEv) + } + } + panic(" ListenAndServe out of select") } // Called to shutdown the service