From 40eec6687af9efdfd2bf63587aae236d0539ec1e Mon Sep 17 00:00:00 2001 From: Valentin Samir Date: Sun, 29 Dec 2013 13:45:20 +0100 Subject: [PATCH] =?UTF-8?q?[sip/billing]=20Ajout=20de=20commentaires=20(et?= =?UTF-8?q?=20on=20appel=20hangup=20via=20AMI=20et=20pas=20l'AGI=20qui=20e?= =?UTF-8?q?st=20bloqu=C3=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sip/agi/billing | 22 ++++++++++++++++++++-- sip/asterisk.py | 2 ++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/sip/agi/billing b/sip/agi/billing index 1a429f48..cb0e9c0d 100755 --- a/sip/agi/billing +++ b/sip/agi/billing @@ -10,6 +10,12 @@ from threading import Thread import gestion.secrets_new as secrets from sip.asterisk import AGI, Manager, NullRecv + +# Pour quitter, j'utilise os._exit(0) qui quitte un peut violement (comme un kill -9), sinon les threads ne se termine pas. +# En vrai, il faudrait appeler os.exit (qui lève une exception dans le thread où il est appelé), rattraper l'exception, +# faire se terminer tous les autres threads, puis quitter, mais j'ai la flemme. + +## Redirection de stderr vers le fichier /tmp/breakage.log pour débugguer, c'est bien pratique se = os.open("/tmp/breakage.log", os.O_WRONLY|os.O_APPEND|os.O_CREAT) os.dup2(se, sys.stderr.fileno()) os.close(se) @@ -23,7 +29,7 @@ def hangup_callback(manager, params): def unreachble_callback(manager, params): agi=manager.agi() if params['Peer'] == "SIP/%s" % agi['callerid'] and params['PeerStatus'] in ['Unregistered','Unreachable']: - agi.hangup(agi['channel']) + manager.hangup(agi['channel']) os._exit(0) def start_billing(agi, src, dest): @@ -45,24 +51,36 @@ def dial(agi, to): os._exit(0) if __name__ == '__main__' : + # initialisation d'un AMI manager=Manager("billing", secrets.get('asterisk_billig_passwd') , server="127.0.0.1", event=True, auto_connect=False, timeout=10) syslog.openlog('billing') + # initialisation d'un AGI (on lit des paramètres sur stdin, donc c'est bloquant jusqu'à la lecture d'une ligne vide) agi=manager.agi() + # On lance le decompte du temps lorsque l'on passe à l'état Up. manager.register_events_callback('Newstate', Newstate_callback) + # On coupe la communication si le client se déconnecte ou devient injoiniable (ce qui l'empêche de raccrocher lui même). manager.register_events_callback('PeerStatus', unreachble_callback) + # Des choses à faire lorsque l'un des deux participant raccroche. manager.register_events_callback('Hangup', hangup_callback) + # On récupère le domain sip de la destination SIPDOMAIN=agi['SIPDOMAIN'] + syslog.syslog('%r' % SIPDOMAIN) + # Si le domaine est absent ou local, alors c'est un appel vers le RTC, on passe par la ligne SIP ovh if not SIPDOMAIN or SIPDOMAIN in ['crans.org']: SIPDOMAIN='forfait-ovh' - t=Thread(target=dial, args=(agi, "SIP/%s@%s" % (agi['extension'], SIPDOMAIN))) + agi.answer() + # On compose le numéro dans un thread a part puisque cela est bloquant jusqu'à ce que quelqu'un raccroche. On limite la communication à 30min (1800000 milliseconde) + t=Thread(target=dial, args=(agi, "SIP/%s@%s,60,L(1800000:60000:20000)" % (agi['extension'], SIPDOMAIN))) t.start() + # On se connecte a l'AMI et on attent de recevoir des events while True: manager.connect() try: while True: manager.process_events() + # Si le thread éxécutant dial est mort, c'est que quelqu'un à raccrocher, on quitte. if not t.isAlive(): os._exit(0) except (socket.error, NullRecv): diff --git a/sip/asterisk.py b/sip/asterisk.py index 7338bd90..951e1f0f 100644 --- a/sip/asterisk.py +++ b/sip/asterisk.py @@ -455,6 +455,8 @@ class AGI(object): self.command("exec", app, *params) def dial(self, to): self.launch_app("dial", to) + def answer(self): + self.launch_app("Answer") def goto(self, arg): self.launch_app("goto", arg)