From 8fc3df772fbd34209bdfd4a1424a8cbb1fd43581 Mon Sep 17 00:00:00 2001 From: triatmoko Date: Wed, 29 Jun 2011 19:08:07 +0200 Subject: put last version of SIP Handler which is working with our socket communication. --- Handler/SIPCall.py | 189 +++++++++++++++++++++++++++++++++++++++++++++++++ Handler/SIPIncoming.py | 171 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 360 insertions(+) create mode 100644 Handler/SIPCall.py create mode 100644 Handler/SIPIncoming.py diff --git a/Handler/SIPCall.py b/Handler/SIPCall.py new file mode 100644 index 0000000..45fdca2 --- /dev/null +++ b/Handler/SIPCall.py @@ -0,0 +1,189 @@ +import sys +import pjsua as pj +import socket +import time +import MySQLdb +import subprocess +from time import sleep +from datetime import datetime +import string + + + +HOST = None +PORT = 50099 +s = None + +stop ="false" +server = "132.230.4.8" +username = "mpselftest1" +password = "1mpselftest" + +LOG_LEVEL=2 +current_call = None +success = "" + + + + +# Logging callback +def log_cb(level, str, len): + print str, "SIP log" + + +# Callback to receive events from account +class MyAccountCallback(pj.AccountCallback): + + def __init__(self, account=None): + pj.AccountCallback.__init__(self, account) + + # Notification on incoming call## + + + +# Callback to receive events from Call +class MyCallCallback(pj.CallCallback): + + def __init__(self, call=None): + pj.CallCallback.__init__(self, call) + + # Notification when call state has changed + def on_state(self): + global current_call + global success + + if self.call.info().state_text <> "DISCONNCTD": + if self.call.info().state_text == "CONNECTING": + print "CONNECTING CALL" + #print "isine opo:", self.call.info().state_text + print "CALL CONFIRMED and ESTABLISH", self.call.info().state_text + sleep(5) + current_call.hangup() + success = "true" + connection.send(success) + else: + sleep(1) + #print "" + + if self.call.info().last_reason <> "": + + if self.call.info().last_reason == "Busy Here": + + print "Destination Number is Busy or offline" + print "" + success = "false" + connection.send(success) + + + #print self.call.info().state_text + + if self.call.info().state == pj.CallState.DISCONNECTED: + current_call = None + print 'Current call Disconnected' + + +def make_call(uri): + try: + print "Making call to", uri + cb=MyCallCallback() + return acc.make_call(uri, cb) + except pj.Error, e: + print "Exception: " + str(e) + return None + + + + +lib = pj.Lib() +while stop <> "true": + + + for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): + af, socktype, proto, canonname, sa = res + try: + s = socket.socket(af, socktype, proto) + except socket.error, msg: + s = None + continue + + try: + s.bind(sa) + s.listen(1) + except socket.error, msg: + s.close() + s = None + continue + break + + if s is None: + print 'could not open socket' + sys.exit(1) + + + lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb)) + transport = lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(0)) + + print "\nListening on", transport.info().host, + print "port", transport.info().port, "\n" + lib.start() + lib.set_null_snd_dev() + + try: + acc_cfg = pj.AccountConfig(server, username, password) + acc = lib.create_account(acc_cfg, cb=MyAccountCallback()) + + if acc.info().reg_status < 700: + + my_sip_uri = "sip:" + transport.info().host + ":" + str(transport.info().port) + + connection, addr = s.accept() + print 'Connected by', addr + connection.send('I am ready') + + while 1: + try: + data = connection.recv(1024) + + if data == "start": + + while 1: + + num = connection.recv(1024) + if num <> "": + + number = "sip:"+num+"@132.230.4.8" + current_call = make_call(number) + break + + if data == "terminated": + stop = "true" + break + + except ValueError: + print "error when sending message" + connection.close() + + connection.close() + + + else: + Regis_status= "Bad" + print "error when register" + + lib.destroy() + lib = None + acc = None + + + connection.close() + except ValueError: + print "Exception: " + str(e) + +print "Goodbye" +sleep(3) +acc.delete() +lib.destroy() +connection.close() +lib = None +acc = None + diff --git a/Handler/SIPIncoming.py b/Handler/SIPIncoming.py new file mode 100644 index 0000000..14b7837 --- /dev/null +++ b/Handler/SIPIncoming.py @@ -0,0 +1,171 @@ +import sys +import pjsua as pj +import socket +import time + +from time import sleep +from datetime import datetime + + +HOST = None +PORT = 50020 +s = None + +stop ="false" +server = "132.230.4.8" +username = "mpselftest2" +password = "2mpselftest" + +LOG_LEVEL=2 +current_call = None +success = "" +number = "" + + + +# Logging callback +def log_cb(level, str, len): + print str, "I am here" + + +# Callback to receive events from account +class MyAccountCallback(pj.AccountCallback): + + def __init__(self, account=None): + pj.AccountCallback.__init__(self, account) + + # Notification on incoming call + def on_incoming_call(self, call): + global current_call + global number + if current_call: + call.answer(486, "Busy") + return + + number = call.info().remote_uri + + current_call = call + + call_cb = MyCallCallback(current_call) + current_call.set_callback(call_cb) + + current_call.answer(180) + current_call.answer(200) + print "accept call" + sleep(5) + current_call.hangup() + + +# Callback to receive events from Call +class MyCallCallback(pj.CallCallback): + + def __init__(self, call=None): + pj.CallCallback.__init__(self, call) + + # Notification when call state has changed + def on_state(self): + global current_call + + if self.call.info().state_text == "CONNECTING": + success = "true" + #connection.send(success) + + if self.call.info().state == pj.CallState.DISCONNECTED: + current_call = None + print 'Current call is', current_call + + # Notification when call's media state has changed. + def on_media_state(self): + if self.call.info().media_state == pj.MediaState.ACTIVE: + # Connect the call to sound device + call_slot = self.call.info().conf_slot + pj.Lib.instance().conf_connect(call_slot, 0) + pj.Lib.instance().conf_connect(0, call_slot) + #connection.send('establish connection') + + print "Media is now active" + + + else: + print "Media is inactive" + + + +lib = pj.Lib() + +try: + for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): + af, socktype, proto, canonname, sa = res + try: + s = socket.socket(af, socktype, proto) + except socket.error, msg: + s = None + continue + + try: + s.bind(sa) + s.listen(1) + except socket.error, msg: + s.close() + s = None + continue + break + + if s is None: + print 'could not open socket' + sys.exit(1) + + lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb)) + + + transport = lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(0)) + print "\nListening on", transport.info().host, + print "port", transport.info().port, "\n" + + # Start the library + lib.start() + lib.set_null_snd_dev() + + # Create local account + acc_cfg = pj.AccountConfig(server, username, password) + acc = lib.create_account(acc_cfg, cb=MyAccountCallback()) + + if acc.info().reg_status < 700: + + my_sip_uri = "sip:" + transport.info().host + ":" + str(transport.info().port) + + #connection, addr = s.accept() + #print 'Connected by', addr + #connection.send('I am ready') + + while stop <> "true": + + #data = connection.recv(1024) + #if data == "terminated": + # break + + if current_call == None: + continue + else: + print "Incoming call from :", number + sleep(4) + + + # Shutdown the library + transport = None + acc.delete() + acc = None + lib.destroy() + lib = None + else: + Regis_status= "Bad" + print "error when register" + + lib.destroy() + lib = None + acc = None + +except pj.Error, e: + print "Exception: " + str(e) + lib.destroy() + lib = None -- cgit v1.2.3-55-g7522