summaryrefslogblamecommitdiffstats
path: root/notFinishedCode/mainHandler.py
blob: 01034a719b9cb0b326cdffc446203b52fc470037 (plain) (tree)




































































































































                                                                                                            
import ServerClass
import LogFileClass


errorCount = 0 #will be used as an error counter
errorOpinion = 0 #your opinion of the error

logger = LogFileClass.Logging('handlerOfSystem.log')
logger.logEvent('')

while 1:
	###########################+++CHANGE PORT NUMBER+++###########################
	handler = ServerClass.ServerHandler(50008) #define the port on which the handler will work
        openSocket = handler.openSocket() #try to open the socket and wait till somebody sends you a request
	logger.logEvent('Tried to open socket: ' + str(openSocket))
	
	if openSocket == 1: #we have an active connection here
		if handler.connected == 1: #check still once for the connection
			
			#closeConnection = handler.closeConnection()
			#logger.logEvent('Tried to close the socket: ' + str(closeConnection))


			###########HELLO MESSAGE###############
			#receive the first message
			helloMsg = handler.receiveData() #receive the hello message from main controller
			if helloMsg == 'HELLO HANDLER':
				handler.sendData('HELLO CONTROLLER')
			else:
				logger.logEvent('First message was not hello but: ' + str(helloMsg))
			
			#this variable defines the handler is, receiver or caller 
			iAmReceiver = -1  #1 for receiver, 0 for caller, -1 for not defined



			#########FIRST COMMAND#################
			#receive command what to be
			commandMsg = handler.receiveData()
			if commandMsg[0:6] == 'CALLER':

				#execute stuff for caller
				iAmReceiver = 0
				handler.sendData('CALLER|READY')
			elif commandMsg[0:6] == 'RECEIV':

				#execute stuff for receiver
				iAmReceiver = 1
				handler.sendData('RECEIV|READY')
			else:
				#do something else
				handler.sendData('SHIT')



			##########SECOND COMMAND###############
			#receive command what to do next		
			commandMsg = handler.receiveData()
			if commandMsg[0:12] == 'CALLER|READY':
				if iAmReceiver == 1:
	
					#if received call then 
					handler.sendData('RECEIV|YESREC|' + str(errorOpinion))

					#if I didn't get the call
				#	handler.sendData('RECEIV|NOTREC|' + str(errorOpinion))
				else:
					#a missunderstanding happened
					handler.sendData('MISUND1')

			elif commandMsg[0:12] == 'RECEIV|READY':
				if iAmReceiver == 0:

					#if called successfully
					handler.sendData('CALLER|YESCAL|' + str(errorOpinion))

					#if my call didn't work
				#	handler.sendData('CALLER|NOTCAL|' + str(errorOpinion))

				else:
					#a missunderstanding happened
					handler.sendData('MISUND2')



			###########TERMINATE COMMAND###########
			commandMsg = handler.receiveData()
			if commandMsg[0:9] == 'TERMINATE':
				handler.sendData('TERMINATE')

			else:
				#do something else
				handler.sendData('SHIT')




		else:
			print 'The connection is not working properly!'	
			logger.logEvent('Not connected, handler.connected='+str(handler.connedted))
	else:	
		errorCount+=1
		if errorCount >=1:
			if handler.error != 'No error':
                		logger.logEvent(str(handler.error))
                		if handler.error == '[Errno 98] Address already in use':
        	                	#print 'one should try to kill the port'
	                        	print handler.killPort()
					logger.logEvent('Try to kill port')

		
		logger.logEvent('Couldn''t open the socket: ' + str(openSocket))
	try:
		handler.closeConnection()        
		del handler
	except:
		del handler
		

#        if server.connected == 1:
#                test = server.receiveData()
#                print test

#        if server.connected == 1:
#                if server.sendData(test) == 1:
#                        print 'data sent successfully'

#        if server.connected == 1:
#                print server.receiveData()

#        close = server.closeConnection()
#        if close == 1:
#                print 'Closed connection successfully'