summaryrefslogtreecommitdiffstats
path: root/notFinishedCode/mainHandler.py
blob: 01034a719b9cb0b326cdffc446203b52fc470037 (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
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'