summaryrefslogblamecommitdiffstats
path: root/For Weekly Test/07-07-2011/sipReceiver.py
blob: 6404f106ad920279cb65077ab79640b8e718e06f (plain) (tree)
















































































































































































                                                                                              
import sys
import pjsua as pj
import socket
import time
import os
import string
import classServer

from time import sleep
from datetime import datetime

stop ="false"
server = "132.230.4.8"
username = "mpselftest2"
password = "2mpselftest"

LOG_LEVEL=2
current_call = None
success = ""
global accept
accept = None


# 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)
	print "accept call"
	if current_call <> None:
		accept = True
	if accept == True:
		sleep(2)
		current_call.answer(200)
		sleep(3)
		current_call.hangup()
		accept = "true"
		server.sendData('success')
		
	#server.sendData(success)
		
	


        
# 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":
		print self.call.info().state_text
		success = "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)
	    #server.sendData('establish connection')

            print "Media is now active"
	    

        else:
            print "Media is inactive"


        
lib = pj.Lib()

try:
	#open socket connection to the controller
	server = classServer.ServerHandler(50104)
	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()
	
	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("132.230.4.8", "mpselftest2", "2mpselftest")
    	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)

		server.sendData('ready')#send message to controller that the handler ready

    		while stop <> "true":
			
			data = server.receiveData()#waiting message from controller
			if data == "terminated":
				break
	
			if current_call == None:
				accept = None
				continue
			else:
				print "Incoming call from :", number
				
				sleep(4)
        

    # Shutdown the library
    		transport = None
    		acc.delete()
    		acc = None
    		lib.destroy()
    		lib = None
		server.closeConnection()
		del server
	else:
		Regis_status= "Bad"
		print "error when register"

		lib.destroy()
		lib = None
    		acc = None
		server.closeConnection()
		del server

except pj.Error, e:
    print "Exception: " + str(e)
    lib.destroy()
    lib = None
    server.closeConnection()
    del server