Skip to content

Commit

Permalink
C1: Testable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Esmail Fadae committed Feb 27, 2014
1 parent d9ad7b4 commit 038bbe5
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 286 deletions.
77 changes: 44 additions & 33 deletions atmPython/client/myClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,16 @@

import socket
from myClientSend import *
from myClientReceive import *
from myClientReceive import opcodes
import sys
from struct import unpack
import struct

# Import the interface to our GPB messages
sys.path.append('./..')
import messages_pb2

from myServer import verify_checksum

#opcode associations; note that these opcodes will be returned by the serverzl;khjapoiwpe
opcodes = {'\x11': create_success,
'\x12': general_failure,
'\x21': delete_success,
'\x22': general_failure,
'\x31': deposit_success,
'\x32': general_failure,
'\x41': withdraw_success,
'\x42': general_failure,
'\x51': balance_success,
'\x52': general_failure,
'\x61': end_session_success,
'\x62': unknown_opcode
}

def getInput():
print '''
Expand All @@ -41,6 +33,8 @@ def getInput():
return netBuffer

def processInput(netBuffer, mySocket):
request_serviced = True

#create
if netBuffer == str(1):
create_request(mySocket)
Expand All @@ -64,8 +58,11 @@ def processInput(netBuffer, mySocket):
#quit
elif netBuffer == str(6):
end_session(mySocket)

else:
request_serviced = False

return
return request_serviced

def getResponse(mySocket):
#wait for server responses...
Expand All @@ -77,15 +74,24 @@ def getResponse(mySocket):
print "ERROR: connection down"
sys.exit()

if len(retBuffer) != 0:
if len(retBuffer) >= 4:
# Get the GPB message length
length = struct.unpack('!I',retBuffer[0:4])[0]

if len(retBuffer) == 4:
# Only received the length so far
retBuffer += mySocket.recv( 1024 )

header = unpack('!cIc',retBuffer[0:6])
if (len(retBuffer) == (length + 4)):
# Populate the message with the data received
message = messages_pb2.ServerResponse()
message.ParseFromString(retBuffer[4:4+length])
#only allow correct version numbers
if header[0] == version:
opcode = header[2]
if (message.version== version) and verify_checksum(message):
opcode = message.opcode
#send packet to correct handler
try:
opcodes[opcode](mySocket,retBuffer)
opcodes[opcode](mySocket,message)
except KeyError:
break
#mySocket.send ('\x01\x01\x02\x03\x53\x10\x12\x34')
Expand All @@ -94,23 +100,28 @@ def getResponse(mySocket):

if __name__ == '__main__':
if(len(sys.argv) != 3):
print "ERROR: Usage 'python myClient.py <host> <port>'"
sys.exit()

# Use defaults
myHost = 'localhost'
myPort = '8080'
# print "ERROR: Usage 'python myClient.py <host> <port>'"
# sys.exit()
else:
#get the address of the server
myHost = sys.argv[1]
myPort = sys.argv[2]
myHost = sys.argv[1]
myPort = sys.argv[2]

mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#mySocket.settimeout(5.)
try:
mySocket.connect ( ( myHost, int(myPort)) )
mySocket.connect ( ( myHost, int(myPort)) )
except:
print "ERROR: could not connect to " + myHost + ":" + myPort
sys.exit()
print "ERROR: could not connect to " + myHost + ":" + myPort
sys.exit()

while True:
netBuffer = getInput()
#menu selection and function priming
processInput(netBuffer, mySocket)
netBuffer = getInput()
#menu selection and function priming
if (processInput(netBuffer, mySocket)):
getResponse(mySocket)

mySocket.close()
69 changes: 52 additions & 17 deletions atmPython/client/myClientReceive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,67 @@
Altered Feb. 20, 2014
'''
from struct import unpack
from sys import exit


# Repackage our GPB message into a list of values so we can reuse this code with only minor modification
def repackage_message(message):
values = list()

# Use a slightly weird method of referencing opcodes by the functions that handle them so the code is less opaque
if opcodes[message.opcode] in [delete_success, end_session_success, unknown_opcode]:
# These messages have no additional data so return an empty list
None

elif opcodes[message.opcode] == create_success:
values.append(message.act)

elif opcodes[message.opcode] == general_failure:
values.append(message.error_message)

# All other server responses include the balance only
else:
values.append(message.bal)

return values

#handle errors from server side.
def general_failure(conn, netBuffer):
values = unpack('!h',netBuffer[6:8])
strlen = values[0]
print "\nERROR: " + netBuffer[8:8+strlen]
return
def general_failure(conn, message):
# No shortcut here
print "\nERROR: " + message.error_message
return

#create new account
def create_success(conn, netBuffer):
values = unpack('!I',netBuffer[6:10])
def create_success(conn, message):
values = repackage_message(message)
print "Account creation successful " + str(values[0])
return

#delete an existing account
def delete_success(conn, netBuffer):
def delete_success(conn, message):
print "Account deletion successful"
return

#deposit to an existing account
def deposit_success(conn,netBuffer):
values = unpack('!I',netBuffer[6:10])
def deposit_success(conn,message):
values = repackage_message(message)
print "Deposit success. The updated balance: " + str(values[0])
return

#withdraw from an existing account
def withdraw_success(conn,netBuffer):
values = unpack('!I',netBuffer[6:10])
def withdraw_success(conn,message):
values = repackage_message(message)
print "Withdrawal success. The updated balance: " + str(values[0])
return

#withdraw from an existing account
def balance_success(conn,netBuffer):
values = unpack('!I',netBuffer[6:10])
def balance_success(conn,message):
values = repackage_message(message)
print "The balance of that account is: " + str(values[0])
return

#end a session
def end_session_success(conn,netBuffer):
def end_session_success(conn,message):
print "SHUTTING DOWN"
conn.close()
exit()
Expand All @@ -52,4 +72,19 @@ def end_session_success(conn,netBuffer):
#handle invalid opcodes
def unknown_opcode(conn):
print "ERROR: INCORRECT OPCODE"
return
return

#opcode associations; note that these opcodes will be returned by the serverzl;khjapoiwpe
opcodes = {'\x11': create_success,
'\x12': general_failure,
'\x21': delete_success,
'\x22': general_failure,
'\x31': deposit_success,
'\x32': general_failure,
'\x41': withdraw_success,
'\x42': general_failure,
'\x51': balance_success,
'\x52': general_failure,
'\x61': end_session_success,
'\x62': unknown_opcode
}
Loading

0 comments on commit 038bbe5

Please sign in to comment.