def read(self, hospital_address, hospital_port): """ Function for a patient to read all of their data. :param hospital_address: Hospital address :param hospital_port: Hospital port :return: nothing """ if self.card: all_records = [] socket = self.send_msg_get_socket( patient_msg.read_msg(self.card.uid), hospital_address, hospital_port) if isinstance(socket, int): return "No records were retrieved due to socket error" data = socket.recv(MESSAGE_SIZE) if data: responses = constants.deserialize(data) for response in responses: if isinstance(response, int): socket.close() return all_records if response.get(patient_msg.RESPONSE): print( crypto.decrypt(response.get(patient_msg.BLOCK), self.card.priv_key)) all_records.append( crypto.decrypt(response.get(patient_msg.BLOCK), self.card.priv_key)) num_blocks = response.get(patient_msg.NUM_BLOCKS) # Special casing the first response before reading the rest. for i in range(1, num_blocks): data = socket.recv(MESSAGE_SIZE) if data: responses = constants.deserialize(data) for response in responses: print( crypto.decrypt( response.get(patient_msg.BLOCK), self.card.priv_key)) all_records.append( crypto.decrypt( response.get(patient_msg.BLOCK), self.card.priv_key)) else: print("No records were retrieved.") return "No records were retrieved" if all_records: return all_records else: return "No records found" socket.close() else: print("ERROR: Must register with a hospital first") return "ERROR: Must register with a hospital first before reading records"
def read_patient_record(self, card_path, hospital_address, hospital_port): """ Function to handle patient issuing request for physician to read their medical data. :param card_path: Patient card :param hospital_address: Hospital address :param hospital_port: Hospital port :return: boolean, decrypted medical records """ records = [] card = card_helper.get_card_object(card_path) if card == None: return False, records if not self.hospital_affiliation_valid(card.hospital_name): return False, records # Copied patient code. socket = self.send_msg_get_socket(patient_msg.read_msg(card.uid), hospital_address, hospital_port) if isinstance(socket, int): return False, records data = socket.recv(MESSAGE_SIZE) if data: responses = constants.deserialize(data) for response in responses: if isinstance(response, int): socket.close() return False, records if response.get(patient_msg.RESPONSE): r = crypto.decrypt(response.get(patient_msg.BLOCK), card.priv_key) print(r) records.append(r) num_blocks = response.get(patient_msg.NUM_BLOCKS) # Special casing the first response before reading the rest. for i in range(1, num_blocks): data = socket.recv(MESSAGE_SIZE) if data: responses = constants.deserialize(data) for response in responses: r = crypto.decrypt( response.get(patient_msg.BLOCK), card.priv_key) print(r) records.append(r) # print(crypto.decrypt(response.get(patient_msg.BLOCK), card.priv_key)) else: print("No records were retrieved.") socket.close() return False, records socket.close() return True, records
def send_msg(self, msg, address, port): """ Helper function to send a message to the given address. :param: msg: Message to send :param: address: Address to send to :param: port: Port client is listening on :return: response to message """ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((address, port)) # Send a message. s.send(msg) # Wait to receive response. data = s.recv(MESSAGE_SIZE) if data: messages = constants.deserialize(data) for message in messages: s.close() return message except Exception, e: print(e) #print("ERROR: unable to send request %s" %(msg)) s.close() return ERROR
def listen_on_socket(c): while True: try: data = c.recv(MESSAGE_SIZE) if data: messages = constants.deserialize(data) for message in messages: print(message) else: return clean_up(c) except Exception, e: print(e) return clean_up(c)
def send_msg(self, msg, address, port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((address, port)) # Send a message. s.send(msg) # Wait to receive response. data = s.recv(MESSAGE_SIZE) if data: messages = constants.deserialize(data) for message in messages: s.close() return message except Exception, e: print(e) #print("ERROR: unable to send request %s" %(msg)) s.close() return ERROR
def listen_on_socket(c): while True: try: data = c.recv(MESSAGE_SIZE) if data: messages = constants.deserialize(data) for message in messages: response = handle_message(message) # This is a special case since we may be sending multiple blocks of encrypted data. if message.get(TYPE) == patient_msg.READ: if response == None: # No data found for patient. c.send(patient_msg.read_response_msg("", 0, False)) else: # Send all of the blocks. for r in response: c.send(patient_msg.read_response_msg(r, len(response), True)) else: c.send(response) else: return clean_up(c) except Exception, e: print(e) return clean_up(c)