From cc04f21cde23b606c169ae25cfeced9f4684c4e8 Mon Sep 17 00:00:00 2001 From: Refik Hadzialic Date: Fri, 22 Jul 2011 20:41:03 +0200 Subject: Created new clientController and serverHandler! --- notFinishedCode/clientController.py | 177 ++++++++++++++++++++++++++ notFinishedCode/mainController.py | 29 ----- notFinishedCode/mainHandler.py | 133 -------------------- notFinishedCode/serverHandler.py | 245 ++++++++++++++++++++++++++++++++++++ 4 files changed, 422 insertions(+), 162 deletions(-) create mode 100644 notFinishedCode/clientController.py delete mode 100644 notFinishedCode/mainController.py delete mode 100644 notFinishedCode/mainHandler.py create mode 100644 notFinishedCode/serverHandler.py (limited to 'notFinishedCode') diff --git a/notFinishedCode/clientController.py b/notFinishedCode/clientController.py new file mode 100644 index 0000000..61488c9 --- /dev/null +++ b/notFinishedCode/clientController.py @@ -0,0 +1,177 @@ +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 = 1 +################################################## + 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): + + message1 = str(handler1.receiveData(timeout)) + message2 = str(handler2.receiveData(timeout)) + + print 'in receive', message1, message2 + + if message1 == 'HELLO CONTROLLER' and message2 == 'HELLO CONTROLLER' and lastState == 1: + outcome = assignJobs(1,447) + + 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 + + +print 'I am here' +if connectToHandlers() == 1: + print 'connection initialized' + if initTalk() == 1: + receiveMessage(5,1) + receiveMessage(5,1) + receiveMessage(5,1) + receiveMessage(5,1) + lastState = 0 diff --git a/notFinishedCode/mainController.py b/notFinishedCode/mainController.py deleted file mode 100644 index be929f0..0000000 --- a/notFinishedCode/mainController.py +++ /dev/null @@ -1,29 +0,0 @@ -import ClientClass -import DBC -from time import sleep - -#MAIN PART -user = 'root' -passw = 'mysqlpassword' -host = 'localhost' -dbname = 'gsmselftesting' - -database = DBC.DBMySQLConnection(user, passw, host, dbname) -x = ClientClass.Connection('localhost',50008) - - -print x.connect() -x.sendData('HELLO HANDLER') -print x.receiveData() -x.sendData('CALLER|NUMBER') -print x.receiveData() - -sleep(4) -print 'sta sam dobio' -x.sendData('RECEIV|READY') -print x.receiveData() -x.sendData('TERMINATE') -print x.receiveData() - -x.closeConnection() - diff --git a/notFinishedCode/mainHandler.py b/notFinishedCode/mainHandler.py deleted file mode 100644 index 01034a7..0000000 --- a/notFinishedCode/mainHandler.py +++ /dev/null @@ -1,133 +0,0 @@ -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' diff --git a/notFinishedCode/serverHandler.py b/notFinishedCode/serverHandler.py new file mode 100644 index 0000000..f1183ee --- /dev/null +++ b/notFinishedCode/serverHandler.py @@ -0,0 +1,245 @@ +import ServerClass +import GSMClass +import LogFileClass +from time import sleep + +################################ +################################ +######SET THESE PARAMETERS###### +################################ +portListen = 50008 #port number of handler +nameOfLogFile = 'handler.log' #name of the log file +portAddress = '/dev/ttyUSB0' +baudRate = 19200 +################################ +################################ +################################ + +#create the log file class +logger = LogFileClass.Logging(nameOfLogFile) +#use this variable as the error counter +errorCount = 0 +#start logging all events +logger.logEvent('') +#counter how many time we were in the while loop +whileCounter =0 + +#define global varibales +global lastState +lastState = 0 + + + +def initSystem(): + print 'init system' + + global handlerSocket + global gsmDevice + global initTalkVar + global lastState + + global numberToCall + + initTalkVar = 0 #variable used to know if we initialized the start talk + lastState = 0 #variable used to know + numberToCall = '000' #number to call + + + handlerSocket = ServerClass.ServerHandler(portListen) + logger.logEvent('LISTEN ON PORT: '+str(portListen)) + + #add this if you need it + gsmDevice = GSMClass.serialPort(portAddress, baudRate, 20) + initDevice = gsmDevice.portInit() + ######################################################## + + #add nice formating to the log file :) + anyConnection = handlerSocket.openSocket() + + + if anyConnection == 1 and initDevice == 1: + logger.logEvent('CONNECTION ESTABLISHED AND DEVICE WORKING: ' + str(handlerSocket.connectedTo())) + return 1 + + elif anyConnection == 1 and initDevice == 0: + logger.logEvent('$connection established but device not working: ' + str(handlerSocket.connectedTo())) + return 2 + else: + logger.logEvent('$no connection') + return 0 + +def receiveMessage(timeout): + + message = str(handlerSocket.receiveData(timeout)) + + print 'in receive message', message, lastState + + if message == 'HELLO HANDLER' and lastState == 0: + outcome = initTalk() + if outcome == 1: + logger.logEvent('TALK INITIALIZATION SENT') + else: + logger.logEvent('$talk initialization not sent: ' + str(outcome)) + + elif message == 'RECEIVER' and lastState == 1: + outcome = initReceiver() + if outcome == 1: + logger.logEvent('RECEIVER READY SENT') + else: + logger.logEvent('$receiver ready not sent: ' + str(outcome)) + + elif message == 'RECEIVE START' and lastState == 2: + outcome = receiveStart() + if outcome == 1: + logger.logEvent('RECEIVE STATUS REPORTED') + else: + logger.logEvent('$receive status not reported: ' + str(outcome)) + + elif message[0:6] == 'CALLER' and lastState == 1: + outcome = initCaller() + + global numberToCall + numberToCall = message[7:] + if outcome == 1: + logger.logEvent('CALLER READY SENT') + else: + logger.logEvent('$caller ready not sent: ' + str(outcome)) + + elif message == 'CALL START' and lastState == 4: + outcome = callStart(numberToCall) + if outcome == 1: + logger.logEvent('CALLER STATUS SENT') + else: + logger.logEvent('$caller status not sent: ' + str(outcome)) + + elif message == 'TERMINATE CONNECTION' and (lastState == 5 or lastState == 3): + outcome = terminateConnection() + if outcome == 1: + logger.logEvent('TERMINATED THE CONNECTION AFTER TEST') + else: + logger.logEvent('$connection could not be terminated after the test: ' + str(outcome)) + + elif message == 'TERMINATE CONNECTION': + outcome = terminateConnection() + if outcome == 1: + logger.logEvent('TERMINATED THE CONNECTION IN MIDDLE. IN STATE: ' + str(lastState) ) + else: + logger.logEvent('$connection could not be terminated in middle: ' + str(outcome) + ' in state: ' + str(lastState)) + else: + outcome = other() + logger.logEvent('other appeared') + +########INIT TALK PART######## +def initTalk(): + print 'init talk' + + #initialize the talk between handler and controller + global lastState + lastState = 1 + sendMessage = handlerSocket.sendData('HELLO CONTROLLER') + return sendMessage +############################## + + +########RECEIVE PART######## +def initReceiver(): + print 'initReceiver' + + #init function to initialize the receiver + global lastState + lastState = 2 + sendMessage = handlerSocket.sendData('RECEIVER READY') + return sendMessage + +def receiveStart(): + print 'receiveStart' + + #wait for a call and report if you received it and it was successfull or not + global lastState + lastState = 3 + receiveCall = gsmDevice.receiveCall() + + if receiveCall == 1: + callSuccess = 'CALL OK' + else: + callSuccess = 'CALL NOT OK' + + tryHangUp = gsmDevice.hangUp() + + sendMessage = handlerSocket.sendData(callSuccess) + return sendMessage +############################ + + +########CALL PART######## +def initCaller(): + print 'initCaller' + + #initialize caller here + global lastState + lastState = 4 + sendMessage = handlerSocket.sendData('CALLER READY') + return sendMessage + +def callStart(numberToCall): + print 'initCaller' + + #call the number here + global lastState + lastState = 5 + + callSuccess = 'CALL NOT OK' + + tryCall = gsmDevice.callNumber(numberToCall) + if tryCall != 1: + callSuccess = 'CALL NOT OK' + else: + sleep(2) + activeCall = gsmDevice.currentCall() + counter = 0 + while(activeCall!=1): + sleep(1) + activeCall = gsmDevice.currentCall() + if counter == 6: + break + counter += 1 + + if activeCall == 1: + callSuccess = 'CALL OK' + else: + callSuccess = 'CALL NOT OK' + + tryHangUp = gsmDevice.hangUp() + + return handlerSocket.sendData(callSuccess) +######################### + + +########TERMINATE PART######## +def terminateConnection(): + print 'terminate connection' + + close = handlerSocket.closeConnection() + return close +############################## + +########TERMINATE PART######## +def other(): + print 'other' + global lastState + + close = handlerSocket.closeConnection() + lastState = 8 + return 1 +############################## +while 1: + test = initSystem() + if test == 1: + print 'initialized system' + receiveMessage(5) + receiveMessage(5) + receiveMessage(5) + receiveMessage(5) + + del handlerSocket + del gsmDevice -- cgit v1.2.3-55-g7522