def get_files(self, folder_type, file_list): ''' Sends a request for a list of files to the memory module. ''' try: # Preperations self.connect('memory') print 'gfs: connected' files = file_list[0] file_list.remove(files) # There's at least one file print 'gfs: 1st' for file_name in file_list: # If there's more than one file... files += '|{}'.format(file_name) print 'gfs: added' message = 'FLS|{}|{}'.format(self.username, folder_type) # Connection set-up self.sock.send(message) print 'gfs: sent message' response = self.sock.recv(len(message) + 5) print 'gfs: got response' response_parts = response.split('|') flag = response_parts[0] response_parts.remove(flag) if flag == 'ACK' and response_parts == message.split('|'): # Files transfer print 'gfs: show time!' file_send(self.sock, files) print 'gfs: sent files' data = file_recv(self.sock) print 'gfs: recived' self.disconnect() return 'SCS', data except Exception, error: print 'ERROR', error return 'WTF', 'WTF'
def do_work(): while True: client_socket, client_addr = q.get() while True: try: req = client_socket.recv(2048) except Exception, e: print 'ERROR',e if e.errno == 10054: # A forced connection close req = '' else: raise print 'req:', req if req == "": client_socket.close() print "Closed connection" # -For The Record- q.task_done() break else: try: data = req.split('|') cmd = data[0] if cmd == 'LUD': # first step - verification db_response = verify(data) print 'database said', db_response if db_response == 'SCS': do = True # All good elif db_response == 'NNM': do = False # Not good else: do = False # Really not good else: do = True # Normal if cmd in TO_MEMORY: # A request for the memory module target_ip = MEMORY_IP target_port = MEMORY_PORT print 'to memory' elif cmd in TO_DATABASE: # A request for the memory module target_ip = DATABASE_IP target_port = DATABASE_PORT print 'to database' else: # An unknown request raise except: # An unknown request client_socket.send('WTF') else: # A known reques if do: # All was good forward_socket = socket.socket() forward_socket.connect((target_ip, target_port)) forward_socket.send(req) if cmd in ('LUD', 'GET'): # The command requests a file module_response = file_recv(forward_socket) file_send(client_socket, module_response) else: # The command requests a simple status response module_response = forward_socket.recv(len(req)+5) client_socket.send(module_response) forward_socket.close() else: # Database said no file_send(client_socket, db_response) # Client expects a file, so we give him a "file"
def set_folder_info(self, folder_type, data): ''' Sends a request to the memory module to update the folder info ''' try: # First step - set up connection self.connect('memory') print 'sfi: connceted' message = 'NUD|{}|{}'.format(self.username, folder_type) print 'sfi: made message' self.sock.send(message) print 'sfi: sent' response = self.sock.recv(len(message) + 5) print 'sfi: recived' response_parts = response.split('|') print 'sfi: split' flag = response_parts[0] response_parts.remove(flag) print 'sfi: flag' if flag == 'ACK' and response_parts == message.split('|'): # Second step - send file file_send(self.sock, data) print 'sfi: sent file' final_response = self.sock.recv(len(message) + 5) print 'sfi: final response' self.disconnect() print 'sfi: disconnected' final_response_parts = final_response.split('|') print 'sfi: final parts' final_flag = final_response_parts[0] final_response_parts.remove(final_flag) print 'sfi: final flag' if final_response_parts == message.split('|'): return final_flag else: raise else: raise except Exception, error: print 'ERROR', error try: self.disconnect() except Exception, error: print 'ERROR', error pass # If it didn't manage do disconnect, then it was already closed.
def update_folder(self, folder_type, data): ''' Sends a request to update the content of a specific folder to the memory module. ''' try: # Connection set-up self.connect('memory') print 'upd: connected' message = 'WRT|{}|{}'.format(self.username, folder_type) print 'upd: message made' self.sock.send(message) print 'upd: sent message' response = self.sock.recv(len(message) + 5) print 'upd: recived resp' response_parts = response.split('|') print 'upd: split' flag = response_parts[0] response_parts.remove(flag) print 'upd: flag: ' + flag if flag == 'ACK' and response_parts == message.split('|'): # File send file_send(self.sock, data) print 'upd: sent file' final_response = self.sock.recv(len(message) + 5) print 'upd: final recived' self.disconnect() print 'upd: disconnected' final_response_parts = final_response.split('|') print 'upd: final split' flag = final_response_parts[0] final_response_parts.remove(flag) print 'upd: final flag' if final_response_parts == message.split('|'): return flag else: raise # Shouldn't get here... else: raise except Exception, error: print 'ERROR', error self.disconnect() return 'WTF'
status, new_data = user.get_files(folder_type, files_list) print 'got files' elif command == "DEL": # A request to delete a file status = user.delete_file(info[0], info[1]) print 'deleted file' new_data = "NONEWDATA" else: # An unknown request raise except Exception, error: # An error occured mid-response-getting print 'ERROR', error status = "WTF" new_data = "NONEWDATA" finally: # Sending the response, or the error notification, back to the client. if new_data != 'NONEWDATA': if command in ("FLS", "GET", "LUD"): # Commands that get a file file_send(target, new_data) else: target.send( '{}|{}|{}'.format(status, data, new_data) ) # Commands that returns a response, other than a status, that isn't a file. # There are no such commands yet, but these lines are here for future development, if there'll be such a thing. else: target.send('{}|{}'.format( status, data)) # Commands that only return a status line print "Sent data to client" def do_work( ): # Both do_work and respond_to_clients are present since at the beginning this module used a diffrent approach for multi-clients support, and when changed to multi-threading we made a simple method for the threads that uses a slightly modified version of the client responding method to complete it. while True: print 'start'