summaryrefslogblamecommitdiffstats
path: root/For Weekly Test/15-07-2011/LandlineCall.py
blob: a0b96e5be57860b6e6131bbe2a501382e6e49173 (plain) (tree)





































































































































































                                                                                                      
import sys
import pjsua as pj

import classServer

from time import sleep
from datetime import datetime
import string


stop =False


LOG_LEVEL=2
current_call = None
status = None


# Logging
def log_cb(level, str, len):
    print str, "SIP log"


# Receive events from account
class MyAccountCallback(pj.AccountCallback):

    def __init__(self, account=None):
        pj.AccountCallback.__init__(self, account)

        
# Receive events from Call
class MyCallCallback(pj.CallCallback):
   
    def __init__(self, call=None):
        pj.CallCallback.__init__(self, call)

    
    def on_state(self):
        global current_call
	global status

	if self.call.info().state <> pj.CallState.DISCONNECTED:
		if self.call.info().state_text == "CONNECTING":
			print "CONNECTING CALL"
			
			print "CALL CONFIRMED and ESTABLISH", self.call.info().state_text
			sleep(2)
			current_call.hangup()
			status = 200
			server.sendData(status)
		else:
			sleep(0.5)


	if self.call.info().last_reason <> "":
		
		if self.call.info().last_reason == "Busy Here":
			
			print "486 Busy Here"
			print ""
			status = 486
			server.sendData(status)
        
        if self.call.info().state == pj.CallState.DISCONNECTED:
            current_call = None
            print 'Current call Disconnected'

#Function to make call
def make_call(uri):
    try:
        print "Making call to", uri
	cb=MyCallCallback()
        return acc.make_call(uri, cb)
    except pj.Error, e:
        print "408 Request Time-out (Couldn't find the user in time) " + str(e)
        return None
        



lib = pj.Lib()
while stop <> True:


	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()	
	
	
	server = classServer.ServerHandler(50097) 
	tried = server.openSocket()

	if server.error != 'No error':
		print server.error
		if server.error == '[Errno 98] Address already in use':
			print 'one should try to kill the port'
			print server.killPort()
			server.closeConection()

	try:
		acc_cfg = pj.AccountConfig("132.230.4.8","mpselftest1","1mpselftest")	
		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)
			print my_sip_uri

			server.sendData('ready')
			print "Status: ", server.connected

			while 1:
				try:
					data = server.receiveData()
					
					if data == "start":
						
						while 1:

							num = server.receiveData()
							if num <> "":
								
								number = "sip:"+num+"@132.230.4.8"
								current_call = make_call(number)
								break

					if data == "487":
						stop = True
						break
	
				except ValueError:
					print "813 General socket layer error: Failed send message"
					server.closeConnection()
					del server 

			server.closeConnection()
			del server 
					
		else:
			Regis_status= "Bad"
			print "488 Not Acceptable Here"

			lib.destroy()
			lib = None
    			acc = None


		server.closeConnection()	
		del server 
	except ValueError:    
		print "401 Unauthorized " + str(e)

print "Goodbye"
sleep(3)
acc.delete()
lib.destroy()
server.closeConnection()
del server 
lib = None
acc = None