summaryrefslogblamecommitdiffstats
path: root/For Weekly Test/20-07-2011/landlineCall.py
blob: e25f2515243fc8cd19497d5854ba86a11a1abdc0 (plain) (tree)
1
2
3
4
5
6
7
8
9

                  
                  
                      

 

             
                   












                                                


                            

                                      
 

                                   
 





                                                                                       
                        





                                                               
        


                                                                       
 
                   
 
        
 

                                     
 
                       
                             


                   
              


                   


                   
                                                                  

                                                                                     













                                                                        

                                
                                                




                                                  






                                                                                             
 




















                                                                                                                                                

                                                                                
                                                                          

                                               






                                                                   









                                                                                                                



































                                                                                                   
import sys
import pjsua as pj
import classServer
from time import sleep


<<<<<<< HEAD
LOG_LEVEL = 2
current_call = None
is486 = False
isConfirmed = False
isRinging = False
isConnected = False
isStopped = False
firstTime502 = True
firstTime100 = True
firstTime408 = True
TIMEOUT_LIMIT = 30

# Logging callback
=======
>>>>>>> 3c2c4f14c0dd26f03e5f664d91d51c62b617de7d
def log_cb(level, str, len):
    print str, "SIP log"

# Receive events from Call
class MyCallCallback(pj.CallCallback):

	def on_state(self):
		global current_call

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

		if self.call.info().last_reason == "Busy Here":
			server.sendData('486')
        
        	if self.call.info().state == pj.CallState.DISCONNECTED:
			current_call = None


def make_call(uri):

    try:

	cb=MyCallCallback()
        return acc.make_call(uri, cb)

    except pj.Error, e:
        print "408 " + str(e)
        return None
        

lib = pj.Lib()
stop =False
current_call = None

while stop <> True:


	lib.init(log_cfg = pj.LogConfig(level=1, callback=log_cb))
	transport = lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(0))

<<<<<<< HEAD
	if acc.info().reg_status == 502:
		if firstTime502: 		
			print "CONNECTION ERROR!!"
			print "Please check your internet connection!\n"
			firstTime502 = False
		
		acc.delete()
		lib.destroy()
		lib = None
		acc = None
#		server.sendData('NOT ready')
		continue
=======
	lib.start()
	lib.set_null_snd_dev()	
>>>>>>> 3c2c4f14c0dd26f03e5f664d91d51c62b617de7d
	
	
	server = classServer.ServerHandler(50099) 
	tried = server.openSocket()

<<<<<<< HEAD
	while acc.info().reg_status == 408:
		if firstTime408:		
			print "408: REGISTRATION FAILED DUE TO TIMEOUT!!"
			print "I will try to check again if I can be successful to register!"
			firstTime408 = False
#			server.sendData('NOT ready')


	if acc.info().reg_status == 200:			
		print ("REGISTRATION IS SUCCESSFUL") #server.sendData('ready')		
			
		while 1:
#			try:	
				print "Waiting for server(at the moment YOU!) to send command here:" #This line should be deleted further!	
				startStop = sys.stdin.readline().rstrip("\r\n")                      #startStop = server.receiveData()
				
				if startStop == "start":
					timeCounterRinging = 0
					firstTimeRinging = True							
						
					while 1:
						#number = server.receiveData()
						print "Please enter the number to call:" #This line should be deleted further!
						number = sys.stdin.readline().rstrip("\r\n")#This line should be deleted further!
						if number <> "":
							numberToCall = "sip:"+number+"@"+server					
							current_call = make_call(numberToCall) 
=======
	try:
		acc_cfg = pj.AccountConfig("sipgate.de","1289459","MMW9AX")	
		acc = lib.create_account(acc_cfg, cb=pj.AccountCallback())
			
		if acc.info().reg_status < 700:

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

			while 1:
				try:
					data = server.receiveData()
					event = data[0:5]
					num = data[6:]
					if event == "start" and num <> "":
														
						server.closeConnection()
						tried = server.openSocket()
>>>>>>> 3c2c4f14c0dd26f03e5f664d91d51c62b617de7d
		
						number = "sip:"+num+"@sipgate.de"
						current_call = make_call(number)

					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