Pjsip:用戶註冊多線程實例

# -*- coding: cp936 -*-
import pjsua as pj
import threading,time
import sys


def log_cb(level, str, len):
    print str,


class TestAccountCallback(pj.AccountCallback):
    iRegStatus = 0


    def __init__(self, account=None):
        pj.AccountCallback.__init__(self, account)
        self.iRegStatus = 0
        
    def on_reg_state(self):
        self.iRegStatus = self.account.info().reg_status
        


class SipRegister(threading.Thread):
    def __init__(self,lib,Domain,UserName,UserPsw):
        threading.Thread.__init__(self)
        self.lib = lib
        self.Domain = Domain
        self.UserName = UserName
        self.UserPsw = UserPsw
        self.Acc = None
        self.Acc_cb = None
        
    def run(self):
        self.lib.thread_register("Test")
        iTickCount = 0
        iCode = 0
        self.Register()
        iCode = self.Acc_cb.iRegStatus
        while iCode < 200 and iTickCount < 320:
            time.sleep(0.1)
            iCode = self.Acc_cb.iRegStatus
            iTickCount += 1
        
        if iCode == 200:
            print "Register Ok"
        else:
            print "Register Fail"
    def Register(self):
        global mutex
        try:
            mutex.acquire()
            self.Acc = self.lib.create_account(pj.AccountConfig(self.Domain,self.UserName,self.UserPsw))
            self.Acc_cb = TestAccountCallback(self.Acc)
            self.Acc.set_callback(self.Acc_cb)
            mutex.release()
        except pj.Error, e:
            print "Exception: " + str(e)
            return None
            
if __name__ == "__main__":
    mutex = threading.Lock()


    print "\n*****************************************************************"
    print "*                                                               *"
    print "*                    Created by Jason 2013.05                   *"
    print "*                                                               *"
    print "*****************************************************************\n"


    lib = pj.Lib()
    lib.init(log_cfg = pj.LogConfig(level=0, callback=log_cb, console_level=0))  
    lib.start()   
    transport = lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(5060))
    for i in range(3):
        SipRegister(lib,"192.168.28.108",str(3601+i),"123456").start()
        time.sleep(1)
        
    print "Press any key to exit!"
    sys.stdin.readline().rstrip("\r\n")
    transport = None
    lib.destroy()
    lib = None
    print "\n***************************Exit**************************"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章