summaryrefslogtreecommitdiffstats
path: root/notFinishedCode/clientController.py
blob: 55a867cf8004d2cf5c015ad03bc6f43dedb4d00a (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
import ClientClass
from time import sleep

########SET VARIABLES########
addressOfHandler1 = '132.230.4.64'
portOfHandler1 = 50008

addressOfHandler2 = 'localhost'
portOfHandler2 = 50008
#############################
def connectToHandlers():
	global lastState
	lastState = 0
#########################################33333####
	global whoIsWho
	whoIsWho = 2
##################################################
	global handler1
	global handler2

	handler1 = ClientClass.Connection(addressOfHandler1, portOfHandler1)
	handler2 = ClientClass.Connection(addressOfHandler2, portOfHandler2)


	tryConn1 = handler1.connect()
	tryConn2 = handler2.connect()

	if tryConn1 == 1 and tryConn2 == 1:
		return 1

	else:
		if tryConn1 == 1 and tryConn2 == 0:
			return 2
		elif tryConn1 == 0 and tryConn2 == 1:
			return 3
		elif tryConn1 == 0 and tryConn2 == 0:
			return 4

		return 5


def initTalk():
	print 'in init'

	global lastState
	lastState = 1
	sendMessage1 = handler1.sendData('HELLO HANDLER')
	sendMessage2 = handler2.sendData('HELLO HANDLER')

	if sendMessage1 == 1 and sendMessage2 == 1:
		return 1
	else:
		return 0

def receiveMessage(timeout, whoIsWho):

	if whoIsWho == 1:
		message1 = str(handler1.receiveData(timeout))
		message2 = str(handler2.receiveData(timeout))
	else:
		message1 = str(handler2.receiveData(timeout))
		message2 = str(handler1.receiveData(timeout))

	if message1 != '' and message2 !='':
		print 'in receive', message1, message2
		
		if message1 == 'HELLO CONTROLLER' and message2 == 'HELLO CONTROLLER' and lastState == 1:
			outcome = assignJobs(0,455)

		elif message1 == 'RECEIVER READY' and message2 == 'CALLER READY' and lastState == 2:
			outcome = startJobs(whoIsWho)

		elif message1 == 'CALL OK' and message2 == 'CALL OK' and lastState == 3:
			outcome = terminateConnection()
			#write to db here
			print 'TEST WAS OK'
			

		elif message1 == 'CALL NOT OK' and message2 == 'CALL NOT OK' and lastState == 3:
			outcome = terminateConnection()
			#write to db here
			print 'BOTH SIDES FAILED'

		elif message1 == 'CALL NOT OK' and message2 == 'CALL OK' and lastState == 3:
			outcome = terminateConnection()
			#write to db here
			print 'ONE SIDE FAILED ', whoIsWho
		
		elif message1 == 'CALL OK' and message2 == 'CALL NOT OK' and lastState == 3:
			outcome = terminateConnection()
			#write to db here
			print 'ONE SIDE FAILED', whoIsWho

		else:
			#abort test since they are not initialized
			outcome = other()

def assignJobs(whoIsWho, number):
	print 'in assign jobs'
	global lastState
	lastState = 2
	if whoIsWho == 1:
		sendMessage1 = handler1.sendData('RECEIVER')
		sendMessage2 = handler2.sendData('CALLER|' + str(number))
	else:
		sendMessage1 = handler1.sendData('CALLER|' + str(number))
		sendMessage2 = handler2.sendData('RECEIVER')

	if sendMessage1 == 1 and sendMessage2 == 1:
		return 1
	else:
		return 0

def startJobs(whoIsWho):
	print 'in start jobs'
	global lastState
	lastState = 3
	if whoIsWho == 1:
		sendMessage1 = handler1.sendData('RECEIVE START')
		sendMessage2 = handler2.sendData('CALL START')
	else:
		sendMessage2 = handler2.sendData('RECEIVE START')
		sendMessage1 = handler1.sendData('CALL START')

	if sendMessage1 == 1 and sendMessage2 == 1:
		return 1
	else:
		return 0

def terminateConnection():
	print 'in terminate connection'

	global lastState
	lastState = 4
	sendMessage1 = handler1.sendData('TERMINATE CONNECTION')
	sendMessage2 = handler2.sendData('TERMINATE CONNECTION')

	if sendMessage1 == 1 and sendMessage2 == 1:
		return 1
	elif sendMessage1 == 1 and sendMessage2 == 0:
		return 2
	elif sendMessage1 == 0 and sendMessage2 == 1:
		return 3
	elif sendMessage1 == 0 and sendMessage2 == 0:
		return 4
	else:
		return 5 

def other():
	print 'in other state'

	global lastState
	lastState = 5
	
	sendMessage1 = handler1.sendData('TERMINATE CONNECTION')
        sendMessage2 = handler2.sendData('TERMINATE CONNECTION')

	close1 = handler1.closeConnection()
	close2 = handler2.closeConnection()

        if sendMessage1 == 1 and sendMessage2 == 1:
                return 1
        elif sendMessage1 == 1 and sendMessage2 == 0:
                return 2
        elif sendMessage1 == 0 and sendMessage2 == 1:
                return 3
        elif sendMessage1 == 0 and sendMessage2 == 0:
                return 4
        else:
                return 5

while 1:
	# print 'I am here'	
	if connectToHandlers() == 1:
		print 'connection initialized'
		if initTalk() == 1:
			receiveMessage(10,2)
			receiveMessage(10,2)
			receiveMessage(10,2)
			receiveMessage(10,2)
			lastState = 0