From 038bbe5916c36ebbc166bfa5f9147846be27a3c7 Mon Sep 17 00:00:00 2001 From: Esmail Fadae Date: Thu, 27 Feb 2014 00:29:16 -0500 Subject: [PATCH] C1: Testable. --- atmPython/client/myClient.py | 77 ++++---- atmPython/client/myClientReceive.py | 69 +++++-- atmPython/client/myClientSend.py | 292 ++++++++++++++-------------- atmPython/messages.proto | 10 +- atmPython/messages_pb2.py | 20 +- atmPython/server/myServer.py | 116 +++++------ atmPython/server/myServerReceive.py | 25 ++- atmPython/server/myServerSend.py | 25 +-- 8 files changed, 348 insertions(+), 286 deletions(-) diff --git a/atmPython/client/myClient.py b/atmPython/client/myClient.py index 6bb1803..a1602a1 100644 --- a/atmPython/client/myClient.py +++ b/atmPython/client/myClient.py @@ -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 ''' @@ -41,6 +33,8 @@ def getInput(): return netBuffer def processInput(netBuffer, mySocket): + request_serviced = True + #create if netBuffer == str(1): create_request(mySocket) @@ -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... @@ -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') @@ -94,23 +100,28 @@ def getResponse(mySocket): if __name__ == '__main__': if(len(sys.argv) != 3): - print "ERROR: Usage 'python myClient.py '" - sys.exit() - + # Use defaults + myHost = 'localhost' + myPort = '8080' +# print "ERROR: Usage 'python myClient.py '" +# 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() \ No newline at end of file diff --git a/atmPython/client/myClientReceive.py b/atmPython/client/myClientReceive.py index e7feb2f..250c022 100644 --- a/atmPython/client/myClientReceive.py +++ b/atmPython/client/myClientReceive.py @@ -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() @@ -52,4 +72,19 @@ def end_session_success(conn,netBuffer): #handle invalid opcodes def unknown_opcode(conn): print "ERROR: INCORRECT OPCODE" - return \ No newline at end of file + 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 + } \ No newline at end of file diff --git a/atmPython/client/myClientSend.py b/atmPython/client/myClientSend.py index e71debf..6c795f7 100644 --- a/atmPython/client/myClientSend.py +++ b/atmPython/client/myClientSend.py @@ -7,161 +7,167 @@ from struct import pack from sys import maxint, exit -#create new account +# Import the interface to our GPB messages +import sys +sys.path.append('./..') +import messages_pb2 + +sys.path.append('./../server') +from myServerSend import insert_checksum + +# create new account def create_request(conn): + print "CREATING AN ACCOUNT \n" + print "enter a starting balance:" + message = messages_pb2.ClientRequest() + message.version = '\x01' + message.opcode = '\x10' + while True: + try: + netBuffer = int(raw_input('>> ')) + except ValueError: + continue + if(netBuffer >= 0 and netBuffer < maxint): + message.bal = netBuffer + break - print "CREATING AN ACCOUNT \n" - print "enter a starting balance:" - buffer = new message_pb2.User - buffer.version_num = 'x\01' - buffer.opcode = 'x\10' - while True: - try: - netBuffer = int(raw_input('>> ')) - except ValueError: - continue - if(netBuffer >= 0 and netBuffer < maxint): - buffer.bal = netbuffer - break - - print "enter a an account number 1-100(input 0 for a random number):" - while True: - try: - netBuffer = int(raw_input('>> ')) - except ValueError: - continue - - if(netBuffer > 0 and netBuffer <= 100): - buffer.account_num = netBuffer - break - elif(netBuffer == 0): - act = -1 - break - - send_buffer(buffer, conn) - return + print "enter a an account number 1-100(input 0 for a random number):" + while True: + try: + netBuffer = int(raw_input('>> ')) + except ValueError: + continue + + if(netBuffer > 0 and netBuffer <= 100): + message.act = netBuffer + break + elif(netBuffer == 0): + message.act = -1 + break + + send_buffer(message, conn) + return -#delete an existing account +# delete an existing account def delete_request(conn): - print "DELETING AN ACCOUNT \n" - buffer = new message_pb2.User - buffer.version_num = 'x\01' - buffer.opcode = 'x\20' - print "enter a an account number 1-100:" - while True: - try: - netBuffer = int(raw_input('>> ')) - except ValueError: - continue - - if(netBuffer > 0 and netBuffer <= 100): - buffer.account_num = netBuffer - break + print "DELETING AN ACCOUNT \n" + message = messages_pb2.ClientRequest() + message.version = '\x01' + message.opcode = '\x20' + print "enter a an account number 1-100:" + while True: + try: + netBuffer = int(raw_input('>> ')) + except ValueError: + continue + + if(netBuffer > 0 and netBuffer <= 100): + message.act = netBuffer + break + + send_buffer(message, conn) + return - send_buffer(buffer, conn) - return - -#deposit to an existing account +# deposit to an existing account def deposit_request(conn): - print "DEPOSITING SOME DOUGH \n" - buffer = new message_pb2.User - buffer.version_num = 'x\01' - buffer.opcode = 'x\30' - print "enter a an account number 1-100:" - while True: - try: - netBuffer = int(raw_input('>> ')) - except ValueError: - continue - - if(netBuffer > 0 and netBuffer <= 100): - buffer.account_num = netbuffer - break - print "enter an amount to deposit:" - while True: - try: - netBuffer = int(raw_input('>> ')) - except ValueError: - continue - if(netBuffer >= 0 and netBuffer < maxint): - buffer.bal = netbuffer - break - - send_buffer(buffer, conn) - return + print "DEPOSITING SOME DOUGH \n" + message = messages_pb2.ClientRequest() + message.version = '\x01' + message.opcode = '\x30' + print "enter a an account number 1-100:" + while True: + try: + netBuffer = int(raw_input('>> ')) + except ValueError: + continue + + if(netBuffer > 0 and netBuffer <= 100): + message.act = netBuffer + break + print "enter an amount to deposit:" + while True: + try: + netBuffer = int(raw_input('>> ')) + except ValueError: + continue + if(netBuffer >= 0 and netBuffer < maxint): + message.bal = netBuffer + break + + send_buffer(message, conn) + return -#withdraw from an existing account +# withdraw from an existing account def withdraw_request(conn): - print "WITHDRAWING SOME DOUGH \n" - buffer = new message_pb2.User - buffer.version_num = 'x\01' - buffer.opcode = 'x\40' - print "enter a an account number 1-100:" - while True: - try: - netBuffer = int(raw_input('>> ')) - except ValueError: - continue - - if(netBuffer > 0 and netBuffer <= 100): - buffer.account_num = netbuffer - break - - print "enter an amount to withdraw:" - while True: - try: - netBuffer = int(raw_input('>> ')) - except ValueError: - continue - if(netBuffer >= 0 and netBuffer < maxint): - buffer.bal = netbuffer - break + print "WITHDRAWING SOME DOUGH \n" + message = messages_pb2.ClientRequest() + message.version = '\x01' + message.opcode = '\x40' + print "enter a an account number 1-100:" + while True: + try: + netBuffer = int(raw_input('>> ')) + except ValueError: + continue + + if(netBuffer > 0 and netBuffer <= 100): + message.act = netBuffer + break - send_buffer(buffer, conn) - return + print "enter an amount to withdraw:" + while True: + try: + netBuffer = int(raw_input('>> ')) + except ValueError: + continue + if(netBuffer >= 0 and netBuffer < maxint): + message.bal = netBuffer + break + + send_buffer(message, conn) + return -#withdraw from an existing account +# withdraw from an existing account def balance_request(conn): - print "CHECKING THE BALANCE OF AN ACCOUNT \n" - buffer = new message_pb2.User - buffer.version_num = 'x\01' - buffer.opcode = 'x\50' - print "enter a an account number 1-100:" - while True: - try: - netBuffer = int(raw_input('>> ')) - except ValueError: - continue - - if(netBuffer > 0 and netBuffer <= 100): - buffer.account_num = netBuffer - break - - send_buffer(buffer, conn) - return + print "CHECKING THE BALANCE OF AN ACCOUNT \n" + message = messages_pb2.ClientRequest() + message.version = '\x01' + message.opcode = '\x50' + print "enter a an account number 1-100:" + while True: + try: + netBuffer = int(raw_input('>> ')) + except ValueError: + continue + + if(netBuffer > 0 and netBuffer <= 100): + message.act = netBuffer + break + + send_buffer(message, conn) + return -#end a session +# end a session def end_session(conn): - buffer = new message_pb2.User - buffer.version_num = 'x\01' - buffer.opcode = 'x\60' - send_buffer(buffer, conn) - return - + message = messages_pb2.ClientRequest() + message.version = '\x01' + message.opcode = '\x60' + send_buffer(message, conn) + return + def send_message(message, conn): - try: - conn.send(message) - except: - #close the client if the connection is down - print "ERROR: connection down" - exit() - return + try: + conn.send(message) + except: + # close the client if the connection is down + print "ERROR: connection down" + exit() + return -#function to encrypt message and create a hash and then send it to the server -def send_buffer(buffer, conn): - buffer.checksum = md5.new(buffer.SerializeToString) - buffer_string = buffer.SerializeToString - length = len(buffer_string) - send_message(pack('!I', length),conn) - send_message(buffer_string, conn) - return \ No newline at end of file +# function to encrypt message and create a hash and then send it to the server +def send_buffer(message, conn): + insert_checksum(message) + message_string = message.SerializeToString() + length = len(message_string) + send_message(pack('!I', length)+message_string, conn) + return diff --git a/atmPython/messages.proto b/atmPython/messages.proto index 8ce23cd..ed64e42 100644 --- a/atmPython/messages.proto +++ b/atmPython/messages.proto @@ -1,17 +1,17 @@ package atm_message; message ClientRequest { - required string version = 1; + required bytes version = 1; required bytes checksum = 2; - required string opcode = 3; - required int32 act = 4; + required bytes opcode = 3; + optional int32 act = 4; optional int32 bal = 5; } message ServerResponse { - required string version = 1; + required bytes version = 1; required bytes checksum = 2; - required string opcode = 3; + required bytes opcode = 3; optional int32 act = 4; optional int32 bal = 5; optional string error_message = 6; diff --git a/atmPython/messages_pb2.py b/atmPython/messages_pb2.py index 1524ccb..a54a084 100644 --- a/atmPython/messages_pb2.py +++ b/atmPython/messages_pb2.py @@ -13,7 +13,7 @@ DESCRIPTOR = _descriptor.FileDescriptor( name='messages.proto', package='atm_message', - serialized_pb='\n\x0emessages.proto\x12\x0b\x61tm_message\"\\\n\rClientRequest\x12\x0f\n\x07version\x18\x01 \x02(\t\x12\x10\n\x08\x63hecksum\x18\x02 \x02(\x0c\x12\x0e\n\x06opcode\x18\x03 \x02(\t\x12\x0b\n\x03\x61\x63t\x18\x04 \x02(\x05\x12\x0b\n\x03\x62\x61l\x18\x05 \x01(\x05\"t\n\x0eServerResponse\x12\x0f\n\x07version\x18\x01 \x02(\t\x12\x10\n\x08\x63hecksum\x18\x02 \x02(\x0c\x12\x0e\n\x06opcode\x18\x03 \x02(\t\x12\x0b\n\x03\x61\x63t\x18\x04 \x01(\x05\x12\x0b\n\x03\x62\x61l\x18\x05 \x01(\x05\x12\x15\n\rerror_message\x18\x06 \x01(\t') + serialized_pb='\n\x0emessages.proto\x12\x0b\x61tm_message\"\\\n\rClientRequest\x12\x0f\n\x07version\x18\x01 \x02(\x0c\x12\x10\n\x08\x63hecksum\x18\x02 \x02(\x0c\x12\x0e\n\x06opcode\x18\x03 \x02(\x0c\x12\x0b\n\x03\x61\x63t\x18\x04 \x01(\x05\x12\x0b\n\x03\x62\x61l\x18\x05 \x01(\x05\"t\n\x0eServerResponse\x12\x0f\n\x07version\x18\x01 \x02(\x0c\x12\x10\n\x08\x63hecksum\x18\x02 \x02(\x0c\x12\x0e\n\x06opcode\x18\x03 \x02(\x0c\x12\x0b\n\x03\x61\x63t\x18\x04 \x01(\x05\x12\x0b\n\x03\x62\x61l\x18\x05 \x01(\x05\x12\x15\n\rerror_message\x18\x06 \x01(\t') @@ -27,8 +27,8 @@ fields=[ _descriptor.FieldDescriptor( name='version', full_name='atm_message.ClientRequest.version', index=0, - number=1, type=9, cpp_type=9, label=2, - has_default_value=False, default_value=unicode("", "utf-8"), + number=1, type=12, cpp_type=9, label=2, + has_default_value=False, default_value="", message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), @@ -41,14 +41,14 @@ options=None), _descriptor.FieldDescriptor( name='opcode', full_name='atm_message.ClientRequest.opcode', index=2, - number=3, type=9, cpp_type=9, label=2, - has_default_value=False, default_value=unicode("", "utf-8"), + number=3, type=12, cpp_type=9, label=2, + has_default_value=False, default_value="", message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), _descriptor.FieldDescriptor( name='act', full_name='atm_message.ClientRequest.act', index=3, - number=4, type=5, cpp_type=1, label=2, + number=4, type=5, cpp_type=1, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, @@ -83,8 +83,8 @@ fields=[ _descriptor.FieldDescriptor( name='version', full_name='atm_message.ServerResponse.version', index=0, - number=1, type=9, cpp_type=9, label=2, - has_default_value=False, default_value=unicode("", "utf-8"), + number=1, type=12, cpp_type=9, label=2, + has_default_value=False, default_value="", message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), @@ -97,8 +97,8 @@ options=None), _descriptor.FieldDescriptor( name='opcode', full_name='atm_message.ServerResponse.opcode', index=2, - number=3, type=9, cpp_type=9, label=2, - has_default_value=False, default_value=unicode("", "utf-8"), + number=3, type=12, cpp_type=9, label=2, + has_default_value=False, default_value="", message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), diff --git a/atmPython/server/myServer.py b/atmPython/server/myServer.py index 9bb85bc..9c51112 100644 --- a/atmPython/server/myServer.py +++ b/atmPython/server/myServer.py @@ -8,27 +8,25 @@ import socket import struct -import myServerReceive +from myServerReceive import opcodes import myServerSend from myServerSend import unknown_opcode import thread -import hashlib # Import the interface to our GPB messages import sys sys.path.append('./..') import messages_pb2 +import hashlib +def verify_checksum(message): + received_checksum = message.checksum + message.checksum = '' + computed_checksum = hashlib.sha256(message.SerializeToString()).digest() + message.checksum = received_checksum + return received_checksum == computed_checksum version = '\x01' -#opcode associations -opcodes = {'\x10': myServerReceive.create_request, - '\x20': myServerReceive.delete_request, - '\x30': myServerReceive.deposit_request, - '\x40': myServerReceive.withdraw_request, - '\x50': myServerReceive.balance_request, - '\x60': myServerReceive.end_session - } def recordConnect(log, addr): print 'Opened connection with ' + addr @@ -37,44 +35,45 @@ def recordConnect(log, addr): #thread for handling clients def handler(conn,lock, myData): - #keep track of erroneous opcodes - second_attempt = 0 - while True: - #retrieve header - try: - netbuffer = conn.recv( 1024 ) - except: - #close the thread if the connection is down - thread.exit() - #if we receive a message... - if len(netbuffer) >= 4: - # Get the GPB message length - length = struct.unpack('!I',netbuffer[0:4]) - # Populate the message with the data received - message = messages_pb2.ClientRequest() - message.ParseFromString(netbuffer[4:]) - # Back up the received checksum and prepare for verification - received_checksum = message.checksum - message.checksum = '' - computed_checksum = hashlib.sha256(message.SerializeToString()).digest() - - #only allow correct version numbers and buffers that are of the appropriate length - if (message.version== version) and (len(netbuffer) == length + 4) and (received_checksum == computed_checksum): - opcode = message.opcode - #try to send packet to correct handler - try: - opcodes[opcode](conn,message,myData,lock) - #catch unhandled opcodes - except KeyError: - if(second_attempt): - #disconnect the client - myServerSend.end_session_success(conn) - conn.close() - return - else: - #send incorrect opcode message - second_attempt = 1 - unknown_opcode(conn) + #keep track of erroneous opcodes + second_attempt = 0 + while True: + #retrieve header + try: + netbuffer = conn.recv( 1024 ) + except: + #close the thread if the connection is down + thread.exit() + #if we receive a message... + if len(netbuffer) >= 4: + # Get the GPB message length + length = struct.unpack('!I',netbuffer[0:4])[0] + + if len(netbuffer) == 4: + # Only received the length so far + netbuffer += mySocket.recv( 1024 ) + + #only allow correct version numbers and buffers that are of the appropriate length + if (len(netbuffer) == (length + 4)): + # Populate the message with the data received + message = messages_pb2.ClientRequest() + message.ParseFromString(netbuffer[4:4+length]) + if (message.version== version) and verify_checksum(message): + opcode = message.opcode + #try to send packet to correct handler + try: + opcodes[opcode](conn,message,myData,lock) + #catch unhandled opcodes + except KeyError: + if(second_attempt): + #disconnect the client + myServerSend.end_session_success(conn) + conn.close() + return + else: + #send incorrect opcode message + second_attempt = 1 + unknown_opcode(conn) if __name__ == '__main__': @@ -89,13 +88,16 @@ def handler(conn,lock, myData): mySocket.listen(5) #param represents the number of queued connections #listening for connections - while True: - #This is the simple way to start this; we could also do a SELECT - conn, address = mySocket.accept() - #log connection - recordConnect(log, str(address)) - #start a new thread - lock = thread.allocate_lock() - thread.start_new_thread(handler, (conn, lock, myData)) - + try: + while True: + #This is the simple way to start this; we could also do a SELECT + conn, address = mySocket.accept() + #log connection + recordConnect(log, str(address)) + #start a new thread + lock = thread.allocate_lock() + thread.start_new_thread(handler, (conn, lock, myData)) + except: + mySocket.close() + log.close() \ No newline at end of file diff --git a/atmPython/server/myServerReceive.py b/atmPython/server/myServerReceive.py index 0a26200..be3e2a2 100644 --- a/atmPython/server/myServerReceive.py +++ b/atmPython/server/myServerReceive.py @@ -10,14 +10,23 @@ sys.path.append('./..') import messages_pb2 + # 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() - values[0] = message.act - # The create, withdraw, and deposit messages additionally include an amount - if message.opcode in ['\x10', '\x30', '\x40']: - values[1] = message.bal + # Use a slightly weird method of referencing opcodes by the functions that handle them so the code is less opaque + if opcodes[message.opcode] == end_session: + None + + else: + values.append(message.act) + + # The create, withdraw, and deposit messages additionally include an amount + if opcodes[message.opcode] in [create_request, deposit_request, withdraw_request]: + values.append(message.bal) + + return values #create new account def create_request(conn,message,myData,lock): @@ -197,4 +206,12 @@ def end_session(conn,message,myData,lock): return +#opcode associations +opcodes = {'\x10': create_request, + '\x20': delete_request, + '\x30': deposit_request, + '\x40': withdraw_request, + '\x50': balance_request, + '\x60': end_session + } diff --git a/atmPython/server/myServerSend.py b/atmPython/server/myServerSend.py index b9b89ab..7f43300 100644 --- a/atmPython/server/myServerSend.py +++ b/atmPython/server/myServerSend.py @@ -42,8 +42,7 @@ def general_failure(conn, err_type, reason): message_string = message.SerializeToString() length = len(message_string) - conn.send(struct.pack('!I',length)) - conn.send(message_string) + conn.send(struct.pack('!I',length)+message_string) return #create new account @@ -58,13 +57,11 @@ def create_success(conn,act): # Send message_string = message.SerializeToString() length = len(message_string) - conn.send(struct.pack('!I',length)) - conn.send(message_string) + conn.send(struct.pack('!I',length)+message_string) return #delete an existing account def delete_success(conn): - conn.send('\x01\x00\x00\x00\x00\x21') # Create a message to be transmitted message = messages_pb2.ServerResponse() message.version = '\x01' @@ -74,8 +71,7 @@ def delete_success(conn): # Send message_string = message.SerializeToString() length = len(message_string) - conn.send(struct.pack('!I',length)) - conn.send(message_string) + conn.send(struct.pack('!I',length)+message_string) return #deposit to an existing account @@ -90,8 +86,7 @@ def deposit_success(conn,bal): # Send message_string = message.SerializeToString() length = len(message_string) - conn.send(struct.pack('!I',length)) - conn.send(message_string) + conn.send(struct.pack('!I',length)+message_string) return #withdraw from an existing account @@ -106,8 +101,7 @@ def withdraw_success(conn,bal): # Send message_string = message.SerializeToString() length = len(message_string) - conn.send(struct.pack('!I',length)) - conn.send(message_string) + conn.send(struct.pack('!I',length)+message_string) return #withdraw from an existing account @@ -122,8 +116,7 @@ def balance_success(conn,bal): # Send message_string = message.SerializeToString() length = len(message_string) - conn.send(struct.pack('!I',length)) - conn.send(message_string) + conn.send(struct.pack('!I',length)+message_string) return #end a session @@ -137,8 +130,7 @@ def end_session_success(conn): # Send message_string = message.SerializeToString() length = len(message_string) - conn.send(struct.pack('!I',length)) - conn.send(message_string) + conn.send(struct.pack('!I',length)+message_string) return #handle invalid opcodes @@ -152,7 +144,6 @@ def unknown_opcode(conn): # Send message_string = message.SerializeToString() length = len(message_string) - conn.send(struct.pack('!I',length)) - conn.send(message_string) + conn.send(struct.pack('!I',length)+message_string) return \ No newline at end of file