import sys import os import re import time import MySQLdb import subprocess import classClient #import class socket import classDb #import class db import classPing from time import sleep from datetime import datetime user = 'root' passw = 'randompasswordSQL' #default password we agree with this password before! host = 'localhost' dbname = 'gsmselftesting' stop = "False" status = "" dbSStatus = "" sipNum = "4661929" landlnNum = "076145875681" sipCall = "sipCall.py" sipRec = "sipReceiver.py" landlnCall = "LandlineCall.py" landlnRec = "LandlineReceiver.py" localSipRec = "localsipReceiver.py" rz1Call = "rz1Call.py" rz1Rec = "rz1Receiver.py" o2Call = "o2Call.py" o2Rec = "o2Receiver.py" vodafoneCall = "vodafoneCall.py" vodafoneRec = "vodafoneReceiver.py" tmobileCall = "tmobileCall.py" tmobilRec = "tmobileReceiver.py" # One function for all test case def FuncTest(orig, origAdd, dest, destAdd, destNo): global repeatTest global portOrig global resultOrig global resultDest global x global y portOrig = None resultOrig= None resultDest = None repeatTest = "" try: if dest =="gsmBox1" or dest =="gsmBox2": if dest =="gsmBox1": portOrig = 50095 if dest =="gsmBox2": portOrig = 50096 print "GSM server handler", x.connect() x.sendData('hello server please wakeup the handler and be caller') else: # open SIP caller handler command="--command=python " +orig subprocess.Popen(args=["gnome-terminal", command]) sleep(5) if orig=="sipCall.py": portOrig = 50097 if orig=="gsmcall_.py": portOrig = 50098 if orig == "landlineCall.py": portOrig = 50099 if orig == "LocalSIPCall.py": portOrig = 50100 #else: #mean external GSM # print "have not yet define" #portOrig = 50101 x = classClient.Connection(origAdd,portOrig) print "" print "Caller handler status:", x.connect() x.sendData('hello Handler') # wait respond from origin handler while 1: origHandler = x.receiveData() if origHandler <> "": break #think about time out if origHandler == "ready": print "Caller handler ready" print "" try: y = None global portDest portDest = None if dest =="gsmBox1" or dest =="gsmBox2": if dest =="gsmBox1": portDest = 50102 if dest =="gsmBox2": portDest = 50103 y = classClient.Connection(destAdd,portDest) print "GSM handler", y.connect() y.sendData('hello server please wakeup the handler and be receiver') else: command="--command=python " +dest subprocess.Popen(args=["gnome-terminal", command]) sleep(2) if dest=="SIPIncoming.py": portDest = 50104 if dest=="gsmincom_.py": portDest = 50105 if dest == "landlineReceiver.py": portDest = 50106 if dest == "LocalSIPReceiver.py": portDest = 50107 #mean external GSM #print "not define yet" #portDest = 50108 y = classClient.Connection(destAdd,portDest) print "Destination handler status:", y.connect() y.sendData('hello Receiver, caller number') #wait respond from destination handler while 1: destHandler = y.receiveData() if destHandler <> "": break #should have timeout = think about timeout if destHandler == "ready": print "Destination handler ready" print "" y.closeConnection() x.sendData("start")#send message to handler to start the call sleep(1) x.sendData(destNo) #for refresh socket, so gsm handler can use socket to communicated with the phone x.closeConnection() print "" #wait respond from both of handler while 1: sleep(5) x.connect() y.connect() sleep(1) resultOrig = x.receiveData() resultDest = y.receiveData() print "", resultDest if resultOrig <> "" and resultDest <> "": break #if failed, try one more time if resultOrig == "failed" or resultDest == "failed": y.sendData('hello Destination Handler') x.sendData("start") sleep(2) x.sendData(destNo) #for refresh socket, so gsm handler can use socket to communicated with the phone #x.closeConnection() #x.connect() while 1: resultOrig = X.receiveData() resultDest = y.receiveData() print "Origin1: ", resultOrig print "Destination1: ", resultDest if resultOrig <> "" or resultDest <> "": break #if still failed, save to db and return repeat test, so we make automatic test vice versa if resultOrig == "failed" or resultDest == "failed": #db.execute ("update result table") repeatTest="true" print "Origin2: ", resultOrig print "Destination1: ", resultDest else: #save to database, tell handler to terminated #db.execute ("update result table") print "Test Result origin: ", resultOrig print "Test Result Destination: ", resultDest x.sendData('terminated') # signal caller to terminate y.sendData('terminated') # signal receiver to terminate else: statusTest = "604 General Handler Error: Destination handler no respond" #tell Caller handler to terminate because receiver doesnt work x.sendData("terminated") y.closeConnection() except ValueError: print "601 General Handler Error: Could not open Destination handler" else: statusTest = "605 General Handler Error: Origin handler no respond" x.closeConnection() except ValueError: print "602 General Handler Error: Could not open Origin handler" # Main software # Check DB connection x = classDb.DBMySQLConnection(user, passw, host, dbname) print "connected to the DB ", x.connectDB() print "" dbStatus = x.connectDB() if dbStatus == 1: # Check Ping to the Every Handler sipServer = classPing.Ping('132.230.4.8') sipServerStatus = sipServer.ping(3) sipGateServer = classPing.Ping('sipgate.de') sipGateServerStatus = sipGateServer.ping(3) sipLocalServer = classPing.Ping('132.230.4.60') sipLocalServerStatus = sipLocalServer.ping(3) #gsmBox1Status = FuncPing("132.230.4.8") #gsmBox2Status = FuncPing("132.230.4.8") # Fetch The task information from DB print "do i have anything to do", x.anyTasksToDo() for item in x.tasksList: taskID = item[0] callFrom = item[1] callTo = item[2] print "call from to ", taskID , callFrom , callTo print sipServerStatus if callFrom == "sip" and sipServerStatus <> 0: if callTo == "gsmrz1": FuncTest(sipCall, "localhost", "gsmincom_.py", "localhost","4661455") print "Finish test" if repeatTest == "true": #select temporary table, if there are have test from gsmrz to sip then skip #else, update db, put new data to tasktable. from gsmrz to sip update if callTo == "gsmrz2": if gsmBox1Status <> 0: FuncTest(sipCall, "localhost", "Box1", "132.0.0.2","404") if repeatTest == "true": #select temporary table, if there are have test from gsmrz to sip then skip #else, update db, put new data to tasktable. from gsmrz to sip update if callTo == "gsmrz3": if gsmBox2Status <> 0: FuncTest(sipCall, "localhost", "Box2", "132.0.0.3","405") if repeatTest == "true": #select temporary table, if there are have test from gsmrz to sip then skip #else, update db, put new data to tasktable. from gsmrz to sip update if callTo == "landline" and sipGateSeverStatus <> 0: FuncTest(sipCall, "localhost", landlnRec, "localhost",landlnNum) if callTo == "localsip" and sipLocalSeverStatus <> 0: FuncTest(sipCall, "localhost", localSipRec, "localhost","405") if callTo == "externalgsm": FuncTest(sipCall, "localhost", o2Rec, "localhost","405") FuncTest(sipCall, "localhost", tmobileRec, "localhost","405") FuncTest(sipCall, "localhost", vodafoneRec, "localhost","405") FuncTest(sipCall, "localhost", "xx", "localhost","405") if callFrom =="gsmrz1": if callTo =="sip" and sipServerStatus <> 0: FuncTest("gsmcall_.py", "localhost", sipRec, "localhost",sipNum) sleep(10) if callTo =="gsmrz2" and gsmBox1Status <> 0: FuncTest("Box1", "132.0.0.2", sipRec, "localhost",sipNum) if callTo =="gsmrz3" and gsmBox2Status <> 0: FuncTest("Box2", "132.0.0.3", sipRec, "localhost",sipNum) # will exit if DB connection failed else: sys.exit(5)