summaryrefslogblamecommitdiffstats
path: root/notFinishedCode/clientController.py
blob: 55a867cf8004d2cf5c015ad03bc6f43dedb4d00a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                  
                    







































                                                                            






                                                             

                                                      
                





























                                                                                                        
 









































































                                                                         




                                              



                                            
                                     
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