summaryrefslogtreecommitdiffstats
path: root/For Weekly Test/Advance/ControllerClass.py
blob: 6e1de89140b5aec48831324ef5c139dfc4c072a1 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import sys
import os
import subprocess
import SSHTunnelBoxClass
import ClientClass
import random

import LogFileClass
logger = LogFileClass.Logging('TestProcessLog.log')

from time import sleep


class doTheTest:

	def __init__(self, callFrom, callPortName, accCaller, callTo, destPortName, destNo, accDest):
	
		self.callFrom 		= callFrom
		self.dest           	= callTo
		self.destNo 		= destNo
		self.accDest		= accDest
		self.accCaller		= accCaller
		self.callPortName	= callPortName
		self.destPortName	= destPortName
		self.portCaller		= None
		self.portDest		= None
		self.resultCaller	= None
		self.resultDest		= None
		self.testResult		= None

	def FuncTest(self):

		logger.logEvent('')
		
		self.initCaller()
		
		if self.callFrom =="GSMRZ3" or self.callFrom =="GSMRZ2": # wait until ssh connection establish
			sleep(5)
		else:
			sleep(2)
		self.callerGreeting()

		if self.connected == 'OK':
		
			self.caller.sendData('CALLER|'+self.destNo)
			callerHandler = self.caller.receiveData(10) 
		
			if callerHandler == "CALLER READY":
				logger.logEvent('Caller handler : Ready')
				
				self.initReceiver()	
				if self.dest =="GSMRZ3" or self.dest =="GSMRZ2": # wait until ssh connection establish
					sleep(5)
				else:			
					sleep(2)
				self.receiverGreeting()	
				
				if self.connected == 'OK':	
			
					self.receiver.sendData('RECEIVER')
					destHandler = self.receiver.receiveData(10)
	
					if destHandler == 'RECEIVER READY':
						logger.logEvent('Receiver handler : Ready')

						self.startCall()
						self.waitingFeedback()

					else:
						self.testResult = 604
						logger.logEvent('604 General Handler Error: Destination handler no respond timeout')
						self.caller.sendData('TERMINATE CONNECTION')				
						self.receiver.closeConnection()
	
				else:    
					logger.logEvent('998 General Handler Error: Could not connect Destination handler')
					self.testResult = 998
					self.caller.sendData('TERMINATE CONNECTION')
					self.caller.closeConnection()
					self.initCancelTest()
			else:
				self.testResult = 605
				logger.logEvent('605 General Handler Error: caller handler no respond timeout')

			self.caller.sendData('TERMINATE CONNECTION')
			self.caller.closeConnection()
			self.initCancelTest()
		else:    
			self.testResult = 999
			logger.logEvent('999 General Handler Error: Could not connect to Caller handler')


	def initCancelTest(self):
		#close SSH connection when using gsmBox and destination doesnt respond. to make sure SSH connection are terminate
	        if self.callFrom[0:5] == 'GSMRZ':
            		if self.callFrom != 'GSMRZ1':
				# close SSH tunneling
                		self.boxCaller.killTunneling()

	# waiting results state
	def waitingFeedback(self):
		logger.logEvent('Waiting Feedback')
		self.resultDest = self.receiver.receiveData(15)
		self.resultCaller = self.caller.receiveData(15)
		#print 'result '+self.resultCaller+'--'+self.resultDest
		if self.resultCaller <> 'CALL OK' or self.resultDest <> 'CALL OK':

			logger.logEvent('Test Failed')
			self.testResult = 486
			self.initTerminate()
		
		else:
			logger.logEvent('Test Succeed')
			self.testResult = 200
			self.initTerminate()

	#send start call message to caller
	def startCall(self):
		logger.logEvent('Start Call')
		self.receiver.sendData('RECEIVE START')
		self.caller.sendData('CALL START')
    
    	def initAccount(self, account):

        	accConf = account
        	self.username = accConf[0:accConf.find(':')]

	        line = accConf[accConf.find(':')+1:]
	        self.password = line[0:line.find(':')]

	        newLine = line[line.find(':')+1:]
	        self.server = newLine[0:newLine.find(':')]

	# define the caller configuration such as port name and port caller.
	def initCaller(self):
		logger.logEvent('init Caller')
		logger.logEvent(self.callFrom)
        	self.portCaller = random.randint(30000,60000)
        
        	if self.callFrom[0:4] == 'GSMR':
            		if self.callFrom =="GSMRZ1":
                		self.initGSM(self.portCaller, self.callPortName, self.callFrom)
            		else:
                		self.initAccount(self.accCaller)
				#open SSH tunneling
                		self.boxCaller = SSHTunnelBoxClass.SSHTunneling(self.portCaller, 50008, self.server, self.username, self.password)
                		self.boxCaller.startTunneling()


        	elif self.callFrom[0:4] == 'GSME':
            		self.initGSM(self.portCaller, self.callPortName, self.callFrom)

		else:
			subprocess.Popen(args=["gnome-terminal", '--command=python SIPHandler.py '+self.accCaller+ ' ' +str(self.portCaller)])

	# define the destination configuration such as port name and port caller.
	def initReceiver(self):
		logger.logEvent('init Receiver')
		logger.logEvent(self.dest)
        	self.portDest = random.randint(30000,60000)

	        if self.dest[0:4] == 'GSMR':
			if self.dest =="GSMRZ1":
        	        	self.initGSM(self.portDest, self.destPortName, self.dest)
            		else:
                		self.initAccount(self.accDest)
				#open SSH tunneling
                		self.boxDest = SSHTunnelBoxClass.SSHTunneling(self.portDest, 50008, self.server, self.username, self.password)
                		self.boxDest.startTunneling()

	        elif self.dest[0:4] == 'GSME':
        	    self.initGSM(self.portDest, self.destPortName, self.dest)

		else:
			self.portDest = 50100
			subprocess.Popen(args=['gnome-terminal', '--command=python SIPHandler.py '+self.accDest+ ' ' +str(self.portDest)])

	# send terminate message to Handlers
	def initTerminate(self):
		self.caller.sendData('TERMINATE CONNECTION')
		self.receiver.sendData('TERMINATE CONNECTION')
        	if self.callFrom[0:5] == 'GSMRZ':
            		if self.callFrom != 'GSMRZ1':
				# close SSH tunneling
                		self.boxCaller.killTunneling()
		if self.dest[0:5] == 'GSMRZ':
            		if self.dest != 'GSMRZ1':
				# close SSH tunneling
                		self.boxDest.killTunneling()
        	self.receiver.closeConnection()
            	self.caller.closeConnection()
                    
	def callerGreeting(self): # send greeting message to the caller handler
		self.connected 	= None
		#open connection to the Handler
		self.caller = ClientClass.Connection('localhost',self.portCaller)
		self.caller.connect()

		if self.caller.connected == 1:
			logger.logEvent('Connected to Caller Handler')
			self.caller.sendData('HELLO HANDLER')
			if self.caller.receiveData(30) == 'HELLO CONTROLLER':
				logger.logEvent('Caller Handler respond')
				self.connected = 'OK'
		else:
			logger.logEvent('Cannt connect to Caller')
			self.connected = 'NOT OK'

	def receiverGreeting(self): # send greeting message to the destination handler
		self.connected 	= None
		#open connection to the Handler
		self.receiver = ClientClass.Connection('localhost', self.portDest)
		self.receiver.connect()

		if self.receiver.connected == 1:
			logger.logEvent('Connected to Receiver Handler')
			self.receiver.sendData('HELLO HANDLER')
			if self.receiver.receiveData(30) == 'HELLO CONTROLLER':
				logger.logEvent('Receiver Handler respond')
				self.connected = 'OK'
		else:
			logger.logEvent('Cannt connect to Receiver')
			self.connected = 'NOT OK'

	def initGSM(self, portCommunication, portDevice, handler):
		#open GSM Handler
		subprocess.Popen(args=["gnome-terminal", '--command=python GSMHandler.py '+str(portCommunication)+ ' ' +str(portDevice)+' '+str(handler)])