summaryrefslogtreecommitdiffstats
path: root/notFinishedCode/Controller-SecondType.py
blob: 66cdf09f1b724e6bc91a23efcf45811ceb67d283 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import sys
import os
import time
import MySQLdb
import subprocess


import classClient #import class socket
import classDb #import class db
import classPing

from time import sleep
from datetime import datetime


sipNum		= "4661929"
landlnNum	= "076145875681"
unisipNum	= "076120397897"


sipCall		= "sipCall.py"
sipRec 		= "sipReceiver.py"
landlnCall 	= "landlineCall.py"
landlnRec 	= "landlineReceiver.py"
localSipRec 	= "localsipReceiver.py"
rz1Call		= "rz1Call.py"
rz1Rec		= "rz1Receiver.py

o2Call		= "o2Call.py"
o2Rec		= "o2Receiver.py"
vodafoneCall	= "vodafoneCall.py"
vodafoneRec	= "vodafoneReceiver.py"
tmobileCall	= "tmobileCall.py"
tmobilRec	= "tmobileReceiver.py"

# One function for all test case
class TimeoutException(Exception): 
    pass 

def FuncTest(orig, origAdd, dest, destAdd, destNo):


	global repeatTest
	global portOrig
	global resultOrig
	global resultDest
	global testResult
	global x
	global y

	def timeout_handler(signum, frame):
        	raise TimeoutException()

	print "====================== Preparing Test ======================"
	
	try:
		
		if dest =="gsmBox1" or dest =="gsmBox2":

			if dest =="gsmBox1":
				portOrig = 50095

			if 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 " +orig
			subprocess.Popen(args=["gnome-terminal", command])

			sleep(5)
			
			if orig=="SIPCall.py":
				portOrig = 50097				

			if orig=="rz1Call.py":
				portOrig = 50098

			if orig == "landlineCall.py":
				portOrig = 50099

			if orig == "LocalSIPCall.py":
				portOrig = 50100

			#else: #mean external GSM
			#	print "have not yet define"
				#portOrig = 50101

			x = classClient.Connection(origAdd,portOrig)
			print ""
			print "Connecting to Caller handler"
			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:
				y = None
				global portDest

				if dest =="gsmBox1" or dest =="gsmBox2":
	
					if dest =="gsmBox1":
						portDest = 50102

					if dest =="gsmBox2":
						portDest = 50103

					y = classClient.Connection(destAdd,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 " +dest
					subprocess.Popen(args=["gnome-terminal", command])
		
					sleep(2)
		
					if dest=="SIPReceiver.py":
						portDest = 50104

					if dest=="rz1Receiver.py":
						portDest = 50105

					if dest == "landlineReceiver.py":
						portDest = 50106

					if dest == "LocalSIPReceiver.py":
						portDest = 50107

					#mean external GSM
					#print "not define yet"
						#portDest = 50108
					
					y = classClient.Connection(destAdd,portDest)
					print "Connecting to Receiver handler"
					print "Receiver handler status:", y.connect()
					y.sendData('hello Receiver')

				
		    		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")#send message to handler to start the call
					sleep(0.5)
					x.sendData(destNo)
					
					x.closeConnection()
					x.connect()

					y.closeConnection()
					y.connect()

					signal.signal(signal.SIGALRM, timeout_handler) 
		    			signal.alarm(120)

					print "------------ Test Result ------------"
					print ""

					while 1:
						try:
							sleep(0.5)
							resultOrig = x.receiveData()
							resultDest = y.receiveData()

							if resultOrig <> "" or 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")
						sleep(0.5)
						x.sendData(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 <> "" or 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"

		

# Main software

# Check DB connection
db = classDb.DBMySQLConnection('root', 'randompasswordSQL', 'localhost', 'gsmselftesting')
print "Connecting to DB  ", x.connectDB()
print ""
dbStatus = db.connectDB()


if dbStatus == 1:
	
	# Check Ping to the Every Handler
	sipServer = PingClass.Ping('132.230.4.60')
	sipServerStatus = sipServer.ping(3)

	sipGateServer = PingClass.Ping('sipgate.de')
	sipGateServerStatus = sipGateServer.ping(3)

	sipLocalServer = PingClass.Ping('132.230.4.60')
	sipLocalServerStatus = sipLocalServer.ping(3)

# Fetch The task information from DB
	print "do i have anything to do", db.anyTasksToDo()	

	for item in db.tasksList:
		
		taskID = item[0]
		callFrom = item[1]
		callTo = item[2]

		print "Test ID		: " , taskID 
		print "Caller		: " , callFrom
		print "Receiver		: " , callTo

		if callFrom == "sip":
 			if sipServerStatus == 0:
				print "[failed] 500 Server Internal Error"

			else:
			
				if callTo == "gsmrz1":

					FuncTest(sipCall, "localhost", "gsmincom_.py", "localhost","4661473")
					print "Finish test"
					db.addResult(taskID, testResult)
					if repeatTest == "true":
						#select temporary table, if there are have test from gsmrz to sip then skip
							#else, update db, put new data to tasktable. from gsmrz to sip
							# waiting db class  for repeat test from refik which I ask for.
						update
			
				if callTo == "gsmrz2":
	
					if gsmBox1Status == 0:
						print "[failed] 500 Server Internal Error"

					else:
						FuncTest(sipCall, "localhost", "Box1", "132.0.0.2","404")
						db.addResult(taskID, testResult)
						if repeatTest == "true":
						#select temporary table, if there are have test from gsmrz to sip then skip
						#else, update db, put new data to tasktable. from gsmrz to sip
							update

				if callTo == "gsmrz3":

					if gsmBox2Status == 0:
						print "[failed] 500 Server Internal Error"

					else:
						FuncTest(sipCall, "localhost", "Box2", "132.0.0.3","405")
						db.addResult(taskID, testResult)
						if repeatTest == "true":
						#select temporary table, if there are have test from gsmrz to sip then skip
						#else, update db, put new data to tasktable. from gsmrz to sip
							update
				if callTo == "landline":
					if sipGateSeverStatus == 0:
						print "[failed] 500 Server Internal Error"
			
					else:
						FuncTest(sipCall, "localhost", landlnRec, "localhost",landlnNum)
						db.addResult(taskID, testResult)

				if callTo == "localsip":
					if sipLocalSeverStatus == 0:
						print "[failed] 500 Server Internal Error"

					else:
						FuncTest(sipCall, "localhost", localSipRec, "localhost",unisipNum)
						db.addResult(taskID, testResult)

				if callTo == "externalgsm":
					FuncTest(sipCall, "localhost", o2Rec, "localhost","405")
					db.addResult(taskID, testResult)

					FuncTest(sipCall, "localhost", tmobileRec, "localhost","405")
					db.addResult(taskID, testResult)

					FuncTest(sipCall, "localhost", vodafoneRec, "localhost","405")
					db.addResult(taskID, testResult)

					FuncTest(sipCall, "localhost", "xx", "localhost","405")
					db.addResult(taskID, testResult)
		
				

		if callFrom =="gsmrz1":
			
			if callTo =="sip":
				if sipServerStatus == 0:
					print "[failed] 500 Server Internal Error"
					
				else:
					FuncTest("gsmcall_.py", "localhost", sipRec, "localhost",sipNum)
					db.addResult(taskID, testResult)
					sleep(10)
	
			
			if callTo =="gsmrz2":
				if gsmBox1Status == 0:
					print "[failed] 500 Server Internal Error"
		
				else:
					FuncTest("Box1", "132.0.0.2", sipRec, "localhost",sipNum)
					db.addResult(taskID, testResult)

			if callTo =="gsmrz3":
				if gsmBox2Status == 0:
					print "[failed] 500 Server Internal Error"

				else:
					FuncTest("Box2", "132.0.0.3", sipRec, "localhost",sipNum)
					db.addResult(taskID, testResult)
				

	

# will exit if DB connection failed
else:
	sys.exit(5)