def list_account(version, server_socket): """ This function is responsible for sending a package to the server to request a list of all the account names that match a given regular expression specified by the user. Arguments: version: the version of the protocol server_socket: the socket through which we talk to the server """ logging.info('list the account(s) that match the criteria') package = protocol_pb2.Client2Server() package.version = version package.opcode = 6 print """ Please enter the criteria: (To list all the accounts, type *; Use text wildcard '_' to match any one character) """ while True: try: acc_src = raw_input('>> ') except ValueError: continue package.msg = acc_src break send_package(server_socket, package.SerializeToString())
def log_in(version, server_socket): """ This function is responsible for sending a package to the server with the message to log in to an account. The user specifies the account name which should be at most 25 characters, otherwise an error message is printed. Arguments: version: the version of the protocol server_socket: the socket through which we talk to the server """ logging.info('client logs in') package = protocol_pb2.Client2Server() package.version = version package.opcode = 2 print 'Please enter your account name:' while True: try: acc_src = raw_input('>> ') except ValueError: continue if (len(acc_src) > 0 and len(acc_src) <= 25): package.msg = acc_src break else: print 'Invalid name. Try again please (Remember: 25 characters max).' continue send_package(server_socket, package.SerializeToString())
def create_approved(version, server_socket): logging.info('client solved the puzzle, do create_account') # print('client solved the puzzle, do create_account') package = protocol_pb2.Client2Server() package.version = version package.opcode = 20 send_package(server_socket, package.SerializeToString())
def handle(self): client_addr = self.client_address logging.info('client connected from: %s', client_addr) print("Server: client connected from " + str(client_addr)) too_many_attempts = 0 # only allow limited mistakes from the client while True: try: self.message = self.request.recv(1024) if len(self.message) == 0: raise KeyboardInterrupt # except: # logging.critical('client connection dropped.') package = protocol_pb2.Client2Server() package.ParseFromString(self.message) # parsing the package using Protocol Buffer if package.opcode == 0: # in some case an empty package may be sent continue print("Server: received a message from " + str(client_addr)) if package.version == VERSION: try: OPCODES[package.opcode](package, self.request, self.server.data, self.server.lock, VERSION) except: logging.critical('unexpected fatal error occurred. Check server request handler.') else: logging.critical('client uses a different version.') except KeyboardInterrupt: logging.critical('Keyboard interrupt on Server.') thread.exit() except: logging.critical('client connection dropped.')
def evil_msg(version, server_socket): # fakemsg = str(time()) package = protocol_pb2.Client2Server() package.version = version package.opcode = 4 # package.msg = fakemsg send_package(server_socket, package.SerializeToString())
def inform_dead(version, server_socket): """ This function informs the server that the client is forced quitted unexpected. Arguments: version: the version of the protocol server_socket: the socket through which we talk to the server """ logging.info('client being forced quitted') package = protocol_pb2.Client2Server() package.version = version package.opcode = 7 send_package(server_socket, package.SerializeToString())
def quit(version, server_socket): """ This function is responsible for sending a package to the server to request a log out for the client. Arguments: version: the version of the protocol server_socket: the socket through which we talk to the server """ logging.info('client wants to quit the system.') package = protocol_pb2.Client2Server() package.version = version package.opcode = 7 send_package(server_socket, package.SerializeToString())
def check_message(version, server_socket): """ This function is responsible for sending a package to the server to request all the messages received during the time that the client was logged out. Arguments: version: the version of the protocol server_socket: the socket through which we talk to the server """ logging.info('client checks unread messages sent to him/her') package = protocol_pb2.Client2Server() package.version = version package.opcode = 4 send_package(server_socket, package.SerializeToString())
def send_message(version, server_socket): """ This function is responsible for sending a package to the server with the message to send a message to an account. The user specifies the account name of the recipient which should be at most 25 characters, otherwise an error message is printed, and the message itself which should be at most 100 characters, otherwise an error message is printed. Arguments: version: the version of the protocol server_socket: the socket through which we talk to the server """ logging.info('client sends a message') package = protocol_pb2.Client2Server() package.version = version package.opcode = 3 print 'Please enter the destination account name:' while True: try: acc_dst = raw_input('>> ') except ValueError: continue if (len(acc_dst) > 0 and len(acc_dst) <= 25): package.receiver = acc_dst break else: print 'Invalid name. Try again please (Remember: 25 characters max).' continue print 'Please enter your message (100 characters max):' while True: try: msg = raw_input('>> ') except ValueError: continue if (len(msg) > 0 and len(msg) <= 100): package.msg = msg break else: print 'Message length is not within the specs. Try again please (Remember: 100 characters max).' continue break send_package(server_socket, package.SerializeToString())
def delete_account(version, server_socket): """ This function is responsible for sending a package to the server to request the deletion of the client's account. Arguments: version: the version of the protocol server_socket: the socket through which we talk to the server """ logging.info('client deletes a register account') package = protocol_pb2.Client2Server() package.version = version package.opcode = 5 print """ Warning: This action is not reversible! You will see all the unread messages, however. """ send_package(server_socket, package.SerializeToString())