def display_philosopher_states(self, data): display_state = [] action = values.unserialize(data[0]) #[philosopher_id, action] philosopher_id = action[0] state = action[1] state_string = "" if state == values.STATE_THINKING: state_string = values.STATE_THINKING_STRING elif state == values.STATE_WAITING: state_string = values.STATE_WAITING_STRING elif state == values.STATE_EATING: state_string = values.STATE_EATING_STRING current_time = datetime.datetime.now().strftime( values.DISPLAY_TIME_FORMAT) display_state.append(current_time) for i in range(len(values.forks)): if i == philosopher_id: display_state.append(state_string) else: display_state.append("-" * len(values.forks)) string1 = "" for i in display_state: string1 += " " * values.DISPLAY_COLUMN_OFFSET + str(i) print(string1)
def receive_data(self, connection): while self.is_running: try: data = connection.recv(values.DATA_SIZE) if data: print("data from philosopher : " + str(values.unserialize(data))) philosopher_request = values.unserialize(data) if philosopher_request[1] == values.ACTION_PICK_FORK: is_successful = self.pickfork(philosopher_request, connection) if not is_successful: break elif philosopher_request[1] == values.ACTION_DROP_FORK: self.dropfork(philosopher_request, connection) break except socket.error as e: print("error getting data : " + str(e)) #connection.close() connection.close()
def pickfork(self, philosopher_request, connection): if self.is_dirty: sent_data = values.serialize([ self.id, philosopher_request[0], values.ACTION_PICK_FORK, values.ACTION_FAIL ]) connection.send(sent_data) print("data received at philosopher : ", str(values.unserialize(sent_data))) return False else: # If fork is clean/not being used self.is_dirty = True self.current_philosopher = philosopher_request[0] sent_data = self.serialize([ self.id, self.current_philosopher, values.ACTION_PICK_FORK, values.ACTION_SUCCESS ]) connection.send(sent_data) print("data received at philosopher : ", str(values.unserialize(sent_data))) return True
def get_fork1(self,forkl): while self.is_running: try: time.sleep(1) try: lfork = TCPClient(forkl[0],forkl[1]) lfork.connect() except socket.error as e: print("socekt error lf =",e) lfork.send(values.serialize([self.philosopher_id, values.ACTION_PICK_FORK])) data = lfork.receive(1024) print(str(values.unserialize(data))) if data: data_from_fork = values.unserialize(data) if \ data_from_fork[1] == self.philosopher_id \ and data_from_fork[2] == values.ACTION_PICK_FORK \ and data_from_fork[3] == values.ACTION_SUCCESS: return lfork lfork.close() except Exception as e: pass
def get_all_forks(): global forks port = 4001 try: msocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) msocket.sendto(values.serialize(["philosopher",port]),(values.MONITOR_IP,values.MONITOR_PORT2)) except socket.error as e: print("Connection error 0: "+str(e)) try: #this is test data, server = msocket.recvfrom(2048) forks = values.unserialize(data) values.forks = forks print(forks) except socket.error as e: print("Connection error 1: "+str(e))
def register_forks(): host = "localhost" port = 4446 try: msocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) msocket.sendto(values.serialize(["fork", port]), (values.MONITOR_IP, values.MONITOR_PORT2)) except socket.error as e: print("Connection error 0: " + str(e)) try: #this is test data, server = msocket.recvfrom(values.DATA_SIZE) data_array = values.unserialize(data) print(str(data_array)) return data_array except socket.error as e: print("Connection error 1: " + str(e)) finally: msocket.close()
def timeout(background_forks): running = True host = "localhost" port = 4446 try: msocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) msocket.sendto(values.serialize(["timeout", port]), (values.MONITOR_IP, values.MONITOR_PORT2)) except socket.error as e: print("Connection error 0: " + str(e)) while running: try: data, server = msocket.recvfrom(values.DATA_SIZE) data_array = values.unserialize(data) for p in background_forks: print("background forks terminated", p) p.terminate() running = False except socket.error as e: print("Connection error 1: " + str(e)) running = False pass