summaryrefslogtreecommitdiffstats
path: root/Code/Server-Code/initTestClass.py
blob: 2a75192ceb8c7c811817362b8bd2586b70e8c206 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sys
import subprocess, signal
import os
import ControllerClass
import DbClass
import PingClass
import random
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