import sys import os import re import time import MySQLdb import subprocess from time import sleep from datetime import datetime stop = "False" status = "" dbSStatus = "" global idTask global sip2GsmRz global sip2LocSip global sip2LnLine global sip2ExtGsm global gsmRz2Gsmrz global gsmRz2ExtGsmRz global gsmRz2Sip global gsmRz2LnLine global smRz2LocSip global extGsm2Sip global extGsm2GsmRz global lnLine2GsmRz global lnLine2Sip global timeStamp global status #function to check DBconnection def FuncDbConnect(): global dbStatus try: mydb = MySQLdb.connect('localhost', 'root', 'randompasswordSQL','GSMTesting'); db=mydb.cursor() dbStatus = "Database connection establish" except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0],e.args[1]) #function to ping def FuncPing(ip): global status lifeline = re.compile(r"(\d) received") report = ("No response","Partial Response","Alive") A=1,2,3 for host in A: pingaling = os.popen("ping -q -c2 "+ip,"r") while 1: line = pingaling.readline() if not line: break igot = re.findall(lifeline,line) if igot: status= report[int(igot[0])] # class for socket connection # class for socket connection class Connection: def __init__(self, h, p): self.host = h self.port = p self.s = None self.connected = 0 def connect(self): self.s = None for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: self.s = socket.socket(af, socktype, proto) except socket.error, msg: self.s = None self.connected = 0 continue try: self.s.connect(sa) except socket.error, msg: self.s.close() self.connected = 0 self.s = None continue break if self.s is None: self.connected = 0 return '810 General socket layer error: Could not open socket' else: self.connected = 1 return '800 Socket connected' def send_data(self, data): if self.connected == 1: self.s.send(data) def receive_data(self): if self.connected == 1: return self.s.recv(1024) else: return '812 Socket is not connected.' def close_connection(self): if self.connected == 1: self.s.close() self.connected = 0 return 'Closed' # all Test case function # Begin test case SIP to GSM RZ #def FuncSip2GsmRz(idTask): def FuncSip2GsmRz(): #first make test from sip to gsm1 rz try: # open SIP caller handler command="--command=python SIPCall.py" subprocess.Popen(args=["gnome-terminal", command]) sleep(5) x = Connection('localhost',50099) print "SIP handler", x.connect() x.send_data('hello SIP') # wait respond from handler while 1: sipHandler = x.receive_data() if sipHandler <> "": break #think about time out if sipHandler == "ready": print "sip handler ready" try: command="--command=python gsm1rz.py " subprocess.Popen(args=["gnome-terminal", command]) sleep(5) y = Connection('localhost',50096) print "GSM handler", y.connect() y.send_data('hello GSM 1 RZ, caller number') #wait respond from gsm handler while 1: gsmHandler = y.receive_data() if gsmHandler <> "": break #should have timeout = think about timeout if gsmHandler == "ready": x.send_data("start") sleep(2) x.send_data("destination number") #send destination number to sip caller and signal to start the call #wait respond from both of handler while 1: resultSip = X.receive_data() resultGsm1Rz = y.receive_data() if resultSip <> "" or resultGsm1Rz <> "": break #if failed, try one more time if resultSip == "failed" or resultGsm1Rz == "failed": y.send_data('hello GSM 1 RZ, caller number') x.send_data("start + destination number") while 1: resultSip = X.receive_data() resultGsm1Rz = y.receive_data() if resultSip <> "" or resultGsm1Rz <> "": break #if still failed, save to db and update variable gsmRz2Sip == "1", so we make automatic test vice versa if resultSip == "failed" or resultGsm1Rz == "failed": db.execute ("update result table") gsmRz2Sip = "1" else: #save to database, tell handler to terminated db.execute ("update result table") y.send_data("terminated") # signal GSM1 RZ to terminate else: statusTest = "606 General Handler Error: GSM handler no respond" #tell SIP handler to terminate x.send_data("terminated") y.close_connection() except ValueError: print "601 General Handler Error: Could not open GSM handler" else: statusTest = "607 General Handler Error: SIP handler no respond" x.close_connection() except ValueError: print "602 General Handler Error: Could not open SIP handler" #second make test from sip to gsm2 rz #third make test from sip to gsm3 rz # Begin test case SIP to Local SIP def FuncSip2Local(): #first make test from sip to gsm1 rz try: # open SIP caller handler command="--command=python SIPCall.py" subprocess.Popen(args=["gnome-terminal", command]) sleep(5) x = Connection('localhost',50099) print "SIP handler", x.connect() x.send_data('hello SIP') # wait respond from handler while 1: sipHandler = x.receive_data() if sipHandler <> "": break #think about time out if sipHandler == "ready": print "sip handler ready" try: command="--command=python gsm1rz.py " subprocess.Popen(args=["gnome-terminal", command]) sleep(5) y = Connection('localhost',50089) print "GSM handler", y.connect() y.send_data('hello Local SIP, caller number') #wait respond from gsm handler while 1: localHandler = y.receive_data() if localHandler <> "": break #should have timeout = think about timeout if localHandler == "ready": x.send_data("start") sleep(2) x.send_data("destination number") #send destination number to sip caller and signal to start the call #wait respond from both of handler while 1: resultSip = X.receive_data() resultLocal = y.receive_data() if resultSip <> "" or resultlocal <> "": break #if failed, try one more time if resultSip == "failed" or resultLocal == "failed": y.send_data('hello GSM 1 RZ, caller number') x.send_data("start + destination number") while 1: resultSip = X.receive_data() resultGsm1Rz = y.receive_data() if resultSip <> "" or resultLocal <> "": break #if still failed, save to db and update variable local2Sip == "1", so we make automatic test vice versa if resultSip == "failed" or resultLocal == "failed": db.execute ("update result table") #local2Sip = "1" should make another test which call local sip else: #save to database, tell handler to terminated db.execute ("update result table") y.send_data("terminated") # signal GSM1 RZ to terminate else: statusTest = "608 General Handler Error: Local SIP handler no respond" #tell SIP handler to terminate x.send_data("terminated") y.close_connection() except ValueError: print "603 General Handler Error: Could not open Local SIP handler" else: statusTest = "607 General Handler Error: SIP handler no respond" x.close_connection() except ValueError: print "602 General Handler Error: Could not open SIP handler" # Begin test case SIP to Landline # Begin test case SIP to Ext. GSM # Begin test case GSM 1 RZ to GSM 2 RZ # Begin test case GSM 1 RZ to GSM 3 RZ # Begin test case GSM 2 RZ to GSM 3 RZ # Begin test case GSM RZ to Ext. GSM # Begin test case GSM RZ to SIP, if already have SIP - GSM RZ dont do this test. (we can easy update variable gsm2Sip when SIP to gsm rz test succed from 1 to 0 because 1 mean do the test) but if the result failed, do it. # Begin test case GSM RZ to Landline # Begin test case GSM RZ to Local SIP # Begin test case Ext. GSM to SIP, if already have SIP to Ext. GSM don do this. but if the result failed, do it. # Begin test case Ext. GSM - GSM RZ, if already have GSM to Ext. GSM test, dont do this. but if the result failed, do it. # Begin test case Landline to GSM RZ, if already have GSM RZ to Landline, dont do this. but if the result failed, do it. # Begin test case Landline to SIP, if already have SIP to Landline, Dont do this. but if the result failed, do it. # Main software # Check DB connection FuncDbConnect() if dbStatus = "Database connection establish": FuncPing("132.230.4.8") sipServerStatus = status FuncPing("132.230.4.8") sipLocalStatus = status FuncPing("132.230.4.8") sipGateStatus = status FuncPing("132.230.4.8") btsBox1Status = status # it will be on building 101 FuncPing("132.230.4.8") btsBox2Status = status # it will be on the building 52 FuncPing("132.230.4.8") btsBox3Status = status # dont need, since we put the device on RZ building.just direct connect to the server # Fetch The task information from DB db.execute ("Select IDTask, SIP2GSMRZ, SIP2LocSIP, SIP2Lnline, SIP2ExtGSM, GSMRZ2GSMRZ, GSMRZ2ExtGSM, GSMRZ2SIP, GSMRZ2Lnline, GSMRZ2LocSIP, ExtGSM2SIP, ExtGSM2GSMRZ, Lnline2GSMRZ, Lnline2SIP, timeStamp, status from TaskInformation") rows = db.fetchall() for row in rows: idTask=row[0] sip2GsmRz=row[1] sip2LocSip=row[2] sip2LnLine=row[3] sip2ExtGsm=row[4] gsmRz2Gsmrz=row[5] gsmRz2ExtGsmRz=row[6] gsmRz2Sip=row[7] gsmRz2LnLine=row[8] gsmRz2LocSip=row[9] extGsm2Sip=row[10] extGsm2GsmRz=row[11] lnLine2GsmRz=row[12] lnLine2Sip=row[13] timeStamp=row [14] status=row[15] if sip2Gsmrz == "1" and sipServerStatus <> "No response": #call Function for Sip to Gsm FuncSip2GsmRz(idTask) if sip2LocSip == "1" and sipServerStatus <> "No response" and sipLocalStatus <> "No response": # call function for sip to local sip if sip2LnLine == "1" and sipServerStatus <> "No response" and sipGateStatus <> "No response": if sip2ExtGsm == "1" and sipServerStatus <> "No response": if gsmRz2GsmRz == "1": if btsBox1Status <> "No response": if btsBox2Status <> "No response": #call function gsm 1 rz to gsm 2 rz if btsBox3Status <> "No response": #call function gsm 1 rz to gsm 3 rz if btsBox2Status <> "No response" and btsBox3Status <> "No response": # call function gsm 2 rz to gsm 3 rz if gsmRz2ExtGsm == "1": if btsBox1Status <> "No response": # I'm not sure with this part, maybe better we make 1 script which can cover all the test to external GSM #call function gsm 1 rz to o2 #call function gsm 1 rz to vodaphone #call function gsm 1 rz to Tmobile #call function gsm 1 rz to Eplus if btsBox2Status <> "No response": #call function gsm 2 rz to o2 #call function gsm 2 rz to vodaphone #call function gsm 2 rz to Tmobile #call function gsm 2 rz to Eplus if btsBox3Status <> "No response": #call function gsm 3 rz to o2 #call function gsm 3 rz to vodaphone #call function gsm 3 rz to Tmobile #call function gsm 3 rz to Eplus if gsmRz2SIP == "1": if btsBox1Status <> "No response": #call function gsm 1 rz to SIP if btsBox2Status <> "No response": #call function gsm 2 rz to SIP if btsBox3Status <> "No response": #call function gsm 3 rz to SIP if gsmRz2LnLine == "1" and sipGateStatus <> "No response": if btsBox1Status <> "No response": #call function gsm 1 rz to landline if btsBox2Status <> "No response": #call function gsm 2 rz to Landline if btsBox3Status <> "No response": #call function gsm 3 rz to Landline if gsmRz2LocSip == "1" and sipLocalStatus <> "No response": if btsBox1Status <> "No response": #call function gsm 1 rz to local SIP if btsBox2Status <> "No response": #call function gsm 2 rz to local SIP if btsBox3Status <> "No response": #call function gsm 3 rz to local SIP if extGsm2SIP == "1" and sipServerStatus <> "No response": #call function o2 to SIP #call function vodaphone to SIP #call function Tmobile to SIP #call function Eplus to SIP if extGsm2GsmRz == "1": if btsBox1Status <> "No response": #call function o2 to GSM1 RZ #call function vodaphone to GSM1 RZ #call function Tmobile to GSM1 RZ #call function Eplus to GSM1 RZ if btsBox2Status <> "No response": #call function o2 to GSM2 RZ #call function vodaphone to GSM2 RZ #call function Tmobile to GSM2 RZ #call function Eplus to GSM2 RZ if btsBox3Status <> "No response": #call function o2 to GSM3 RZ #call function vodaphone to GSM3 RZ #call function Tmobile to GSM3 RZ #call function Eplus to GSM3 RZ if lnLine2GsmRz == "1" and sipGateStatus <> "No response": if btsBox1Status <> "No response": #call function landline to GSM1 RZ if btsBox2Status <> "No response": #call function landline to GSM2 RZ if btsBox3Status <> "No response": #call function landline to GSM3 RZ if lnLine2Sip == "1" and sipGateStatus <> "No response": if sipServerStatus <> "No response": #call function landline to SIP # will exit if DB connection failed else: sys.exit(5)