summaryrefslogblamecommitdiffstats
path: root/Code/Server-Code/initTestClass.py
blob: ffbc05f0e65db52e76cd4cc6d12f5571961be53c (plain) (tree)
1
2
3
4
5
6
7
8
9
10






                         
          

                      
                  
 


                                         







                                                  
                                                     




                                                                                    

                                                                                                                                                  









                                                                
                                                                                                                                           
















                                                                          








                                                           
 








                                                                                   
 
                                                                    
                                                      
                        
                                                                                 




                                                                             
 
                                                                    

                                                                                                                                     





                                                                                                             

                                                                                                                         
                                                                                                                                                                        
                                                                           
 
                                                                                              
                                                        



                                                                         
 
                                                
                                                                                   
                                                                              
 


                                                                                  
                                                               
                                                         
                             
                                                  
                                                 
                                             

                                         




                                             

                                  


                
import sys
import subprocess, signal
import os
import ControllerClass
import DbClass
import PingClass
import random
import csv
from time import sleep

class initTesting:

	def __init__(self):
		self.messageList = list()

	def pings(self,IP):

		server = PingClass.Ping(IP)
		self.serverStatus = server.ping(2)
		return self.serverStatus 
	
	def initDB(self):
    
		self.db = DbClass.DBMySQLConnection()
        	self.db.connectDB()
        	self.dbStatus = self.db.connectDB()

	def initaccount(self,account,handler): 
		if handler == 'sip' or handler == 'unisip' or handler == 'landline':
			if account[1] != '' or account[2] != '' or account[3] != '' or account[4] != '': 
				# checking available sip account, is there enough information about the account such as username, password,server 
				self.status = 1
			else:
				self.status = 0
		else:
			if account[0] != '' or account[1] != '':
				self.status = 1
			else:
				self.status = 0


	#kill process to make sure, that the handler is Terminate incase handler having problem receiving terminate message from controller
	def killProc(self):
		# define process name of the Handler
		procNameDest = 'GSM Handler'
		procNameCall = 'SIP Handler'

		p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
		out, err = p.communicate()

		#search process name and kill it.
		for line in out.splitlines():
			if procNameDest in line:
				pid = int(line.split(None, 1)[0])
				os.kill(pid, signal.SIGKILL)
		for line in out.splitlines():
			if procNameCall in line:
				pid = int(line.split(None, 1)[0])
				os.kill(pid, signal.SIGKILL)
	def writeToFile(self):
		try:
			string = '--'
			with open('handler.txt', 'w') as F:
    				writer = csv.writer(F)
    				writer.writerow([string])
			F.close()
		except ValueError:
			print "can't write to file"

	def initTest(self, callFrom, callTo):

    		self.initDB() # open database connection
    
    		if self.dbStatus != 0: # if connection to db establish, do the test

			#fetch device account detail from database
                    	dest = self.db.deviceAddress(str(callTo))
                    	caller = self.db.deviceAddress(str(callFrom))

			# check that the caller server allive or not
			if self.pings(caller[4]) <> 0:
			
				# check that the destination server allive or not
				if self.pings(dest[4]) <> 0:

					self.initaccount(caller,callFrom)
					if self.status == 1:
						self.initaccount(dest,callTo)

						if self.status == 1:
							#string = 'Account:username:password:server:ipaddress:portaddress:portnumber'

        						callPortName = caller[0]
        						accCaller = caller[2]+':'+caller[3]+':'+caller[4]+':'
        
        						destPortName = dest[0]
        						destNo = dest[1]
        						accDest = dest[2]+':'+dest[3]+':'+dest[4]+':'
        					
							# create controller class object and send the handler information
        						makeTest = ControllerClass.controller(callFrom, callPortName, accCaller, callTo, destPortName, destNo, accDest)	
        						makeTest.FuncTest()

                                			self.result = str(makeTest.testResult)
							
						else:
							self.result = 100
					else:
        					self.result = 100

					sleep(1)
					#make sure the handler have been terminated
        				self.killProc() # kill all the handler

					self.db.closeDBConn() #close db connection
					sleep(1)
				else:
					#destintaion server die
					self.result = 501
			else:
				#caller server die
				self.result = 500
    			self.db.closeDBConn()
    		else:
        		self.result = 333
    		try:
			self.writeToFile()
			self.db.closeDBConn()
		except ValueError:
			message = "Error"
    		return self.result