import sys import os import time import MySQLdb import subprocess import signal import classClient import classDb import classPing from time import sleep class TimeoutException(Exception): pass class test: def __init__(self, orig, origAdd, dest, destAdd, destNo): self.orig = orig self.origAdd = origAdd self.dest = dest self.destAdd = destAdd self.destNo = destNo self.repeatTest = None self.portOrig = None self.resultOrig = None self.resultDest = None self.testResult = None self.x = None self.y = None def FuncTest(self): def timeout_handler(signum, frame): raise TimeoutException() print "" print "====================== Preparing Test ======================" try: if self.dest =="gsmBox1" or self.dest =="gsmBox2": if self.dest =="gsmBox1": portOrig = 50095 if self.dest =="gsmBox2": portOrig = 50096 print "" print "Connecting to Caller handler" print "Caller handler status :", x.connect() x.sendData('hello server please wakeup the handler and be caller') else: # open SIP caller handler command="--command=python " +self.orig subprocess.Popen(args=["gnome-terminal", command]) sleep(5) if self.orig=="sipCall.py": self.portOrig = 50097 if self.orig=="rz1Call.py": self.portOrig = 50098 if self.orig == "landlineCall.py": self.portOrig = 50099 if self.orig == "LocalSIPCall.py": self.portOrig = 50100 #else: #mean external GSM # print "have not yet define" #portOrig = 50101 x = classClient.Connection(self.origAdd,self.portOrig) print "" print "Connecting to Caller handler", self.origAdd,self.portOrig print "Caller handler status:", x.connect() x.sendData('hello Handler') # wait respond from origin handler signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(60) while 1: try: origHandler = x.receiveData() if origHandler <> "": break except TimeoutException: origHandler = "failed" break if origHandler == "ready": print "Caller handler : Ready" print "" try: print self.dest if self.dest =="gsmBox1" or self.dest =="gsmBox2": if self.dest =="gsmBox1": self.portDest = 50102 if self.dest =="gsmBox2": self.portDest = 50103 y = classClient.Connection(self.destAdd, self.portDest) print "Connecting to Receiver handler" print "Receiver handler status: ", y.connect() y.sendData('hello server please wakeup the handler and be receiver') else: command="--command=python " +self.dest subprocess.Popen(args=["gnome-terminal", command]) sleep(2) if self.dest=="SIPReceiver.py": self.portDest = 50104 if self.dest=="gsmincom_.py": self.portDest = 50105 if self.dest == "landlineReceiver.py": self.portDest = 50106 if self.dest == "LocalSIPReceiver.py": self.portDest = 50107 #mean external GSM #print "not define yet" #portDest = 50108 y = classClient.Connection(self.destAdd, self.portDest) print "Connecting to Receiver handler xx", self.destAdd, self.portDest, self.dest print "Receiver handler status:", y.connect() signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(60) while 1: try: destHandler = y.receiveData() if destHandler <> "": break except TimeoutException: destHandler = "failed" break if destHandler == "ready": print "Receiver handler : Ready" print "" x.sendData("start|"+self.destNo)#send message to handler to start the call x.closeConnection() x.connect() y.closeConnection() y.connect() signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(1200) print "------------ Test Result ------------" print "" while 1: try: sleep(0.5) resultOrig = x.receiveData() resultDest = y.receiveData() if resultOrig <> "" and resultDest <> "": break except TimeoutException: resultOrig = 486 resultDest = 486 break #if failed, tell everybody to try one more time if resultOrig == 486 or resultDest == 486: x.sendData("start|"+self.destNo) x.closeConnection() x.connect() y.closeConnection() y.connect() signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(120) while 1: try: sleep(0.5) resultOrig = x.receiveData() resultDest = y.receiveData() if resultOrig <> "" and resultDest <> "": break except TimeoutException: resultOrig = 486 resultDest = 486 break #if still failed, we make automatic test vice versa if resultOrig == 486 or resultDest == 486: testResult = 486 repeatTest = True print "Second test result origin: ", resultOrig print "Second test result Destination: ", resultDest else: testResult = 200 print "Second test result origin: ", resultOrig print "Second test result Destination: ", resultDest x.sendData('487') y.sendData('487') else: testResult = 200 print "Test Result origin: ", resultOrig print "Test Result Destination: ", resultDest x.sendData('487') y.sendData('487') else: testResult = 604 print "604 General Handler Error: Destination handler no respond" #tell Caller handler to terminate because receiver doesnt work x.sendData('487') y.closeConnection() except ValueError: print "601 General Handler Error: Could not open Destination handler" testResult = "601" else: testResult = 605 print "605 General Handler Error: Origin handler no respond" x.closeConnection() except ValueError: testResult = 602 print "602 General Handler Error: Could not open Origin handler"