summaryrefslogtreecommitdiffstats
path: root/Controller/Controller.py
blob: f977cf382cb853281a407bc7fc800b51e98f95ca (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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import sys
import os
import re
import time
import MySQLdb
import subprocess
from time import sleep
from datetime import datetime


stop = "False"
status = ""
dbSStatus = ""

global idTask
global sip2GsmRz
global sip2LocSip
global sip2LnLine
global sip2ExtGsm
global gsmRz2Gsmrz
global gsmRz2ExtGsmRz
global gsmRz2Sip
global gsmRz2LnLine
global smRz2LocSip
global extGsm2Sip
global extGsm2GsmRz
global lnLine2GsmRz
global lnLine2Sip
global timeStamp
global status



#function to check DBconnection
def FuncDbConnect():

 global dbStatus
 try:
    	mydb = MySQLdb.connect('localhost', 'root', 'randompasswordSQL','GSMTesting');  	
	db=mydb.cursor()
	dbStatus = "Database connection establish"

 except MySQLdb.Error, e:
	print "Error %d: %s" % (e.args[0],e.args[1])


#function to ping
def FuncPing(ip):

 global status
 lifeline = re.compile(r"(\d) received")
 report = ("No response","Partial Response","Alive")

 A=1,2,3
 for host in A:
   
   pingaling = os.popen("ping -q -c2 "+ip,"r")

   while 1:
      line = pingaling.readline()
      if not line: break
      igot = re.findall(lifeline,line)
      if igot:
	       
	status= report[int(igot[0])]

# class for socket connection

# class for socket connection

class Connection:
	def __init__(self, h, p):
		self.host = h
		self.port = p
		self.s = None
		self.connected = 0

	def connect(self):
		self.s = None
		
		for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
			af, socktype, proto, canonname, sa = res
		    	try:
		        	self.s = socket.socket(af, socktype, proto)
		    	except socket.error, msg:
		        	self.s = None
				self.connected = 0
		        	continue
		    	try:
		        	self.s.connect(sa)
		    	except socket.error, msg:
		        	self.s.close()
				self.connected = 0
		        	self.s = None
		        	continue
		    	break
		if self.s is None:
			self.connected = 0
			return '810 General socket layer error: Could not open socket'
		else: 
			self.connected = 1
			return '800 Socket connected'

	def send_data(self, data):
		if self.connected == 1:
			self.s.send(data)

	def receive_data(self):
		if self.connected == 1:
			return self.s.recv(1024)
		else:
			return '812 Socket is not connected.'

	def close_connection(self):
		if self.connected == 1:
			self.s.close()
			self.connected = 0
		return 'Closed'


# all Test case function

# Begin test case SIP to GSM RZ

#def FuncSip2GsmRz(idTask):
def FuncSip2GsmRz():
	
	#first make test from sip to gsm1 rz
	try:
		# open SIP caller handler
		command="--command=python SIPCall.py"
		subprocess.Popen(args=["gnome-terminal", command])

		sleep(5)
		x = Connection('localhost',50099)
		print "SIP handler", x.connect()
		x.send_data('hello SIP')

		# wait respond from handler
		while 1:
			sipHandler = x.receive_data() 
			if sipHandler <> "":
				break
			#think about time out		

		if sipHandler == "ready":
			print "sip handler ready"
			try:
				command="--command=python gsm1rz.py "
				subprocess.Popen(args=["gnome-terminal", command])
		
				sleep(5)
				y = Connection('localhost',50096)
				print "GSM handler", y.connect()
				y.send_data('hello GSM 1 RZ, caller number')

				#wait respond from gsm handler
				while 1:
					gsmHandler = y.receive_data()
					if gsmHandler <> "":
						break
					#should have timeout = think about timeout

				if gsmHandler == "ready":
					
					x.send_data("start")
					sleep(2)
					x.send_data("destination number")
					#send destination number to sip caller and signal to start the call

					#wait respond from both of handler
					while 1:
						resultSip = X.receive_data()
						resultGsm1Rz = y.receive_data()
						if resultSip <> "" or resultGsm1Rz <> "":
							break

					#if failed, try one more time
					if resultSip == "failed" or resultGsm1Rz == "failed":
						y.send_data('hello GSM 1 RZ, caller number')
						x.send_data("start + destination number")
						while 1:
							resultSip = X.receive_data()
							resultGsm1Rz = y.receive_data()
							if resultSip <> "" or resultGsm1Rz <> "":
								break
						
						#if still failed, save to db and update variable gsmRz2Sip == "1", so we make automatic test vice versa
						if resultSip == "failed" or resultGsm1Rz == "failed":
							db.execute ("update result table")
							gsmRz2Sip = "1"

					else: #save to database, tell handler to terminated
						db.execute ("update result table")
						y.send_data("terminated") # signal GSM1 RZ to terminate

				else:
					statusTest = "606 General Handler Error: GSM handler no respond"
					#tell SIP handler to terminate
					x.send_data("terminated")
					
				y.close_connection()

			except ValueError:    
				print "601 General Handler Error: Could not open GSM handler"
		else:
			statusTest = "607 General Handler Error: SIP handler no respond"
		
		x.close_connection()
	
	except ValueError:    
		print "602 General Handler Error: Could not open SIP handler"

	
	#second make test from sip to gsm2 rz
	#third make test from sip to gsm3 rz
		
# Begin test case SIP to Local SIP

def FuncSip2Local():
	
	#first make test from sip to gsm1 rz
	try:
		# open SIP caller handler
		command="--command=python SIPCall.py"
		subprocess.Popen(args=["gnome-terminal", command])

		sleep(5)
		x = Connection('localhost',50099)
		print "SIP handler", x.connect()
		x.send_data('hello SIP')

		# wait respond from handler
		while 1:
			sipHandler = x.receive_data() 
			if sipHandler <> "":
				break
			#think about time out		

		if sipHandler == "ready":
			print "sip handler ready"
			try:
				command="--command=python gsm1rz.py "
				subprocess.Popen(args=["gnome-terminal", command])
		
				sleep(5)
				y = Connection('localhost',50089)
				print "GSM handler", y.connect()
				y.send_data('hello Local SIP, caller number')

				#wait respond from gsm handler
				while 1:
					localHandler = y.receive_data()
					if localHandler <> "":
						break
					#should have timeout = think about timeout

				if localHandler == "ready":
					
					x.send_data("start")
					sleep(2)
					x.send_data("destination number")
					#send destination number to sip caller and signal to start the call

					#wait respond from both of handler
					while 1:
						resultSip = X.receive_data()
						resultLocal = y.receive_data()
						if resultSip <> "" or resultlocal <> "":
							break

					#if failed, try one more time
					if resultSip == "failed" or resultLocal == "failed":
						y.send_data('hello GSM 1 RZ, caller number')
						x.send_data("start + destination number")
						while 1:
							resultSip = X.receive_data()
							resultGsm1Rz = y.receive_data()
							if resultSip <> "" or resultLocal <> "":
								break
						
						#if still failed, save to db and update variable local2Sip == "1", so we make automatic test vice versa
						if resultSip == "failed" or resultLocal == "failed":
							db.execute ("update result table")
							#local2Sip = "1" should make another test which call local sip

					else: #save to database, tell handler to terminated
						db.execute ("update result table")
						y.send_data("terminated") # signal GSM1 RZ to terminate

				else:
					statusTest = "608 General Handler Error: Local SIP handler no respond"
					#tell SIP handler to terminate
					x.send_data("terminated")
					
				y.close_connection()

			except ValueError:    
				print "603 General Handler Error: Could not open Local SIP handler"
		else:
			statusTest = "607 General Handler Error: SIP handler no respond"
		
		x.close_connection()
	
	except ValueError:    
		print "602 General Handler Error: Could not open SIP handler"

# Begin test case SIP to Landline

# Begin test case SIP to Ext. GSM

# Begin test case GSM 1 RZ to GSM 2 RZ

# Begin test case GSM 1 RZ to GSM 3 RZ

# Begin test case GSM 2 RZ to GSM 3 RZ

# Begin test case GSM RZ to Ext. GSM

# Begin test case GSM RZ to SIP, if already have SIP - GSM RZ dont do this test. (we can easy update variable gsm2Sip when SIP to gsm rz test succed from 1 to 0 because 1 mean do the test) but if the result failed, do it.

# Begin test case GSM RZ to Landline

# Begin test case GSM RZ to Local SIP

# Begin test case Ext. GSM to SIP, if already have SIP to Ext. GSM don do this. but if the result failed, do it.

# Begin test case Ext. GSM - GSM RZ, if already have GSM to Ext. GSM test, dont do this. but if the result failed, do it.

# Begin test case Landline to GSM RZ, if already have GSM RZ to Landline, dont do this. but if the result failed, do it.

# Begin test case Landline to SIP, if already have SIP to Landline, Dont do this. but if the result failed, do it.

# Main software



# Check DB connection
FuncDbConnect()
if dbStatus = "Database connection establish":
	
	FuncPing("132.230.4.8")
	sipServerStatus = status

	FuncPing("132.230.4.8")
	sipLocalStatus = status
	
	FuncPing("132.230.4.8")
	sipGateStatus = status

	FuncPing("132.230.4.8")
	btsBox1Status = status # it will be on building 101

	FuncPing("132.230.4.8")
	btsBox2Status = status # it will be on the building 52

	FuncPing("132.230.4.8")
	btsBox3Status = status # dont need, since we put the device on RZ building.just direct connect to the server

# Fetch The task information from DB
	db.execute ("Select IDTask, SIP2GSMRZ, SIP2LocSIP, SIP2Lnline, SIP2ExtGSM, GSMRZ2GSMRZ, GSMRZ2ExtGSM, GSMRZ2SIP, GSMRZ2Lnline, GSMRZ2LocSIP, ExtGSM2SIP, ExtGSM2GSMRZ, Lnline2GSMRZ, Lnline2SIP, timeStamp, status from TaskInformation")
	rows = db.fetchall()	

	for row in rows:
		idTask=row[0]
		sip2GsmRz=row[1]
		sip2LocSip=row[2]
		sip2LnLine=row[3]
		sip2ExtGsm=row[4]
		gsmRz2Gsmrz=row[5]
		gsmRz2ExtGsmRz=row[6]
		gsmRz2Sip=row[7]
		gsmRz2LnLine=row[8]
		gsmRz2LocSip=row[9]
		extGsm2Sip=row[10]
		extGsm2GsmRz=row[11]
		lnLine2GsmRz=row[12]
		lnLine2Sip=row[13]
		timeStamp=row [14]
		status=row[15]

	if sip2Gsmrz == "1" and sipServerStatus <> "No response":
		#call Function for Sip to Gsm
		FuncSip2GsmRz(idTask)

	if sip2LocSip == "1" and sipServerStatus <> "No response" and sipLocalStatus <> "No response":
		# call function for sip to local sip
	if sip2LnLine == "1" and sipServerStatus <> "No response" and sipGateStatus <> "No response":
	
	if sip2ExtGsm == "1" and sipServerStatus <> "No response":

	if gsmRz2GsmRz == "1":
		if btsBox1Status <> "No response":
			if btsBox2Status <> "No response":
				#call function gsm 1 rz to gsm 2 rz
			if btsBox3Status <> "No response":
				#call function gsm 1 rz to gsm 3 rz
		if btsBox2Status <> "No response" and btsBox3Status <> "No response":
			# call function gsm 2 rz to gsm 3 rz

	if gsmRz2ExtGsm == "1":
		if btsBox1Status <> "No response": # I'm not sure with this part, maybe better we make 1 script which can cover all the test to external GSM
			#call function gsm 1 rz to o2
			#call function gsm 1 rz to vodaphone
			#call function gsm 1 rz to Tmobile
			#call function gsm 1 rz to Eplus
		if btsBox2Status <> "No response":
			#call function gsm 2 rz to o2
			#call function gsm 2 rz to vodaphone
			#call function gsm 2 rz to Tmobile
			#call function gsm 2 rz to Eplus
		if btsBox3Status <> "No response":
			#call function gsm 3 rz to o2
			#call function gsm 3 rz to vodaphone
			#call function gsm 3 rz to Tmobile
			#call function gsm 3 rz to Eplus
	
	if gsmRz2SIP == "1":
		if btsBox1Status <> "No response": 
			#call function gsm 1 rz to SIP

		if btsBox2Status <> "No response":
			#call function gsm 2 rz to SIP

		if btsBox3Status <> "No response":
			#call function gsm 3 rz to SIP

	if gsmRz2LnLine == "1" and sipGateStatus <> "No response":
		if btsBox1Status <> "No response": 
			#call function gsm 1 rz to landline

		if btsBox2Status <> "No response":
			#call function gsm 2 rz to Landline

		if btsBox3Status <> "No response":
			#call function gsm 3 rz to Landline

	if gsmRz2LocSip == "1" and sipLocalStatus <> "No response":
		if btsBox1Status <> "No response": 
			#call function gsm 1 rz to local SIP

		if btsBox2Status <> "No response":
			#call function gsm 2 rz to local SIP

		if btsBox3Status <> "No response":
			#call function gsm 3 rz to local SIP
	
	if extGsm2SIP == "1" and sipServerStatus <> "No response":
		#call function o2 to SIP
		#call function vodaphone to SIP
		#call function Tmobile to SIP
		#call function Eplus to SIP
	
	if extGsm2GsmRz == "1":
		if btsBox1Status <> "No response": 
			#call function o2 to GSM1 RZ
			#call function vodaphone to GSM1 RZ
			#call function Tmobile to GSM1 RZ
			#call function Eplus to GSM1 RZ
		if btsBox2Status <> "No response":
			#call function o2 to GSM2 RZ
			#call function vodaphone to GSM2 RZ
			#call function Tmobile to GSM2 RZ
			#call function Eplus to GSM2 RZ
		if btsBox3Status <> "No response":
			#call function o2 to GSM3 RZ
			#call function vodaphone to GSM3 RZ
			#call function Tmobile to GSM3 RZ
			#call function Eplus to GSM3 RZ
		
	if lnLine2GsmRz == "1" and sipGateStatus <> "No response":
		
		if btsBox1Status <> "No response": 
			#call function landline to GSM1 RZ

		if btsBox2Status <> "No response":
			#call function landline to GSM2 RZ

		if btsBox3Status <> "No response":
			#call function landline to GSM3 RZ

	if lnLine2Sip == "1" and sipGateStatus <> "No response":
		if sipServerStatus <> "No response":
			#call function landline to SIP	

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