summaryrefslogtreecommitdiffstats
path: root/Code/Server-Code/ControllerClass.py
blob: 4ed0d9d252822ee8a194f948b72ce95abb723653 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import sys
import os
import subprocess
import SSHTunnelBoxClass
import ClientClass
import random
import csv

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

from time import sleep


class controller:

	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(' -- -X- --')
		
		self.initCaller()
		
		if self.callFrom =="GSMRZ3" or self.callFrom =="GSMRZ2": # wait until ssh connection establish
			if self.continues == 1:
				#putting 6 seconds sleep to prevent unsuccess login using SSH since we have problem with duration time loging into beagle board
				sleep(6)
				self.callerGreeting()
			else:
				self.connected = 'NOT OK'
		else:
			#waiting 2 seconds if doesnt use ssh connection until handler ready
			sleep(2)
			self.callerGreeting()

		logger.logEvent('Status Handler Connection	:'+self.connected)
		if self.connected == 'OK':
		
			self.caller.sendData('CALLER|'+self.destNo)
			callerHandler = self.caller.receiveData(25)
			#waiting ready message from caller
		
			if callerHandler == "CALLER READY":
				logger.logEvent('Caller handler : Ready')
				self.initReceiver()	
				if self.dest =="GSMRZ3" or self.dest =="GSMRZ2": # wait until ssh connection establish
					if self.continues == 1:
					#putting 6 seconds sleep to prevent unsuccess login using SSH since we have problem with duration time loging into beagle board
						sleep(6)
						self.receiverGreeting()
					else:
						self.connected = 'NOT OK'
				else:	
					#waiting 2 seconds if doesnt use ssh connection until handler ready		
					sleep(2)
					self.receiverGreeting()
	
				logger.logEvent('Status Handler Connection	:'+self.connected)
				if self.connected == 'OK':	
			
					self.receiver.sendData('RECEIVER')
					destHandler = self.receiver.receiveData(25)
					#waiting ready message from destination
				
					if destHandler == 'RECEIVER READY':
						logger.logEvent('Receiver handler : Ready')
						self.startCall()
						self.waitingFeedback()

					#Device destination having error after handler ready to receive call.
					elif destHandler == 'DEVICE NOT READY':
						self.testResult == 802
						logger.logEvent('802 General Device Error: Destination device no respond timeout')
						self.initTerminate()
					else:
						self.testResult = 604
						logger.logEvent('604 General Handler Error: Destination handler no respond timeout')
						self.initTerminate()

				#can connect to handler but device destination not ready to do the test.
				elif self.connected == 'DEVICE NOT READY':
						self.testResult = 802
						logger.logEvent('802 General Device Error: Destination device no respond timeout')
						self.initTerminate()
				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()

		#can connect to handler but device caller not ready to do the test.
		elif self.connected == 'DEVICE NOT READY':
			self.testResult = 801
			self.caller.sendData('TERMINATE CONNECTION')
			self.caller.closeConnection()
			logger.logEvent('802 General Device Error: Caller device no respond timeout')			
			self.initCancelTest()
		else:    
			self.testResult = 999
			logger.logEvent('999 General Handler Error: Could not connect to Caller handler')

	def writeToFile(self, AccountInfo):
		try:
			with open('handler.txt', 'w') as F:
    				writer = csv.writer(F)
    				writer.writerow([AccountInfo])
			F.close()
		except ValueError:
			print "can't write to file"

	def initCancelTest(self):
		#close SSH connection when using gsmBox and destination doesnt respond. to make sure SSH connection are terminate
		logger.logEvent('init Cancel test')
	        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(20)
		self.resultCaller = self.caller.receiveData(20)
		#print 'result '+self.resultCaller+'--'+self.resultDest
		if self.resultCaller == 'DEVICE NOT READY':
			logger.logEvent('Task :'+self.callFrom+' - '+self.dest)
			logger.logEvent('Caller DEVICE NOT READY')
			self.testResult = 801
			self.initTerminate()

		elif self.dest == 'DEVICE NOT READY':
			logger.logEvent('Task :'+self.callFrom+' - '+self.dest)
			logger.logEvent('Caller DEVICE NOT READY')
			self.testResult = 802
			self.initTerminate()

		elif self.resultCaller == 'CALL OK' and self.resultDest =='CALL OK':
			logger.logEvent('Task :'+self.callFrom+' - '+self.dest)
			logger.logEvent('Test Succeed')
			self.testResult = 200
			self.initTerminate()
		else:
			#build specially only for Eplus card. since they use prepaid card.
			if self.dest == 'GSMExt.Eplus':
				if self.resultCaller == 'CALL OK' and self.resultDest <> 'TIME OUT':
					logger.logEvent('Task :'+self.callFrom+' - '+self.dest)
					logger.logEvent('Test Failed - Eplus No credit on Eplus')
					self.testResult = 402
					self.initTerminate()
				else:
					logger.logEvent('Test Failed')
					logger.logEvent('Task :'+self.callFrom+' - '+self.dest)
					self.testResult = 486
					self.initTerminate()

			else:
				#one or both of the handler send un success test. assign failed to this test
				if self.resultCaller <> 'CALL OK' or self.resultDest <> 'CALL OK':
					logger.logEvent('Task :'+self.callFrom+' - '+self.dest)
					logger.logEvent('Test Failed')
					self.testResult = 486
					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, handler, PortName, portCom):

        	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(':')]

		textFile = 'Account:'+str(self.username)+':'+str(self.password)+':'+str(self.server)+':'+str(handler)+':'+str(PortName)+':'+str(portCom)
		self.writeToFile(textFile)

	# 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.initAccount(self.accCaller,self.callFrom, self.callPortName,self.portCaller)
                		self.initGSM(self.portCaller, self.callPortName, self.callFrom)
            		else:
                		self.initAccount(self.accCaller, self.callFrom, self.callPortName,self.portCaller)
				#open SSH tunneling

                		self.boxCaller = SSHTunnelBoxClass.SSHTunneling(self.portCaller, 50008, self.server, self.username, self.password)
				status = self.boxCaller.startTunneling()
				logger.logEvent('SSH Status	:'+str(status))
				
				#check whether the SSH tunneling succes or not, 0 is failed!          
                		if status!= 0:
					self.continues = 1
				else:
					self.continues = 0
        	elif self.callFrom[0:4] == 'GSME':
			self.initAccount(self.accCaller,self.callFrom, self.callPortName,self.portCaller)
            		self.initGSM(self.portCaller, self.callPortName, self.callFrom)

		else:
			#open the SIP handler
			self.initAccount(self.accCaller,self.callFrom, self.callPortName,self.portCaller)
			script = 'SIPHandler.py'
			subprocess.Popen(['python',script], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

	# 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.initAccount(self.accDest, self.dest, self.destPortName,self.portDest)
        	        	self.initGSM(self.portDest, self.destPortName, self.dest)
            		else:
                		self.initAccount(self.accDest, self.dest, self.destPortName,self.portDest)
				#open SSH tunneling

                		self.boxDest = SSHTunnelBoxClass.SSHTunneling(self.portDest, 50008, self.server, self.username, self.password)
                		status = self.boxDest.startTunneling()
				
				#check whether the SSH tunneling succes or not, 0 is failed!
				logger.logEvent('SSH Status	:'+str(status))
                		if status!= 0:
					self.continues = 1
				else:
					self.continues = 0

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

		else:
			#self.portDest = 50100
			self.initAccount(self.accDest, self.dest, self.destPortName,self.portDest)
			script = 'SIPHandler.py'
			subprocess.Popen(['python',script], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

	# send terminate message to Handlers
	def initTerminate(self):
		logger.logEvent('TERMINATE CONNECTION')
		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()

		#close port communication
        	self.receiver.closeConnection()
            	self.caller.closeConnection()
                    
	def callerGreeting(self): # send greeting message to the caller handler
		logger.logEvent('Caller Greeting')
		self.connected 	= None
		#open connection to the Handler
		self.caller = ClientClass.Connection('localhost',self.portCaller)
		self.caller.connect()

		if self.caller.connected == 1:
			#connection establish and send hallo message to handler
			logger.logEvent('Connected to Caller Handler')
			self.caller.sendData('HELLO HANDLER')
			#waiting handler respond
			message = self.caller.receiveData(20)

			if message == 'HELLO CONTROLLER':
				# caller handler send hello message
				logger.logEvent('Caller Handler respond')
				self.connected = 'OK'
			elif message == 'DEVICE NOT READY':
				# the handler found the device not ready to making test.
				logger.logEvent('Connect to Caller but device doesnt work')
				self.connected = 'DEVICE NOT READY'
			else:
				logger.logEvent('Cannt connect to Caller')
				self.connected = 'NOT OK'
		else:
			#can't connect to caller handler
			logger.logEvent('Cannt connect to Caller')
			self.connected = 'NOT OK'

	def receiverGreeting(self): # send greeting message to the destination handler
		logger.logEvent('Receiver Greeting')
		self.connected 	= None
		
		#open connection to the Handler
		self.receiver = ClientClass.Connection('localhost', self.portDest)
		self.receiver.connect()
		
		if self.receiver.connected == 1:
			#connection establish and send hallo message to handler
			logger.logEvent('Connected to Receiver Handler')
			self.receiver.sendData('HELLO HANDLER')
			#waiting handler respond
			message = self.receiver.receiveData(20)
			
			# destination handler send hello message
			if message == 'HELLO CONTROLLER':
				logger.logEvent('Receiver Handler respond')
				self.connected = 'OK'

			# the handler found the device not ready to making test.
			elif message == 'DEVICE NOT READY':
				logger.logEvent('Connect to Caller but device doesnt work')
				self.connected = 'DEVICE NOT READY'
			else:
				logger.logEvent('receiver handler not respond')
				self.connected = 'NOT OK'
		else:
			#can't connect to destintaion handler
			logger.logEvent('Cannt connect to Receiver')
			self.connected = 'NOT OK'

	def initGSM(self, portCommunication, portDevice, handler):
		#open GSM Handler
		logger.logEvent('Init GSM')
		script = 'GSMHandler.py'
		subprocess.Popen(['python',script], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)