def receive_hello(self, stop_event, locks, receiver, node, neighbor_id_weight): time_since_last_hello = time.time() while not stop_event.is_set(): is_connected = receiver.wait_for_connection() if not is_connected: if time.time() - time_since_last_hello > transm_global_params.HELLO_TIMEOUT: locks[0].acquire() if neighbor_id_weight in node.neighbor_ids_: print("node #", node.id_, ":", "node #", neighbor_id_weight[0], "seems to be dead, excluding from neighbors", get_time_h_m_s()) node.neighbor_ids_.remove(neighbor_id_weight) locks[0].release() continue hello = receiver.receive() if hello is not None: time_since_last_hello = time.time() locks[1].acquire() if neighbor_id_weight not in node.neighbor_ids_: print("node #", node.id_, ":", "node #", neighbor_id_weight[0], "discovered, appending to neighbors", get_time_h_m_s()) node.neighbor_ids_.append(neighbor_id_weight) locks[1].release()
def run(self): threads_send = [] stop_coord_send_event = threading.Event() for i in range(len(self.senders_)): threads_send.append( threading.Thread(target=self.send_coord, args=(stop_coord_send_event, self.senders_[i], self.coord_))) for thread in threads_send: thread.start() start_time = time.time() start_standing = time.time() while time.time( ) - start_time < transm_global_params.LIGHT_SRC_TIMEOUT: if time.time( ) - start_standing > transm_global_params.LIGHT_SRC_MOVING_TIME_INTERVAL: print("light source :", "moving", get_time_h_m_s()) self.move_light_src() start_standing = time.time() stop_coord_send_event.set() for thread in threads_send: thread.join() print("light source :", "I'm out", get_time_h_m_s())
def run(self): threads_send = [] threads_receive = [] stop_node_event = threading.Event() stop_topology_send_event = threading.Event() stop_topology_send_event.set() for i in range(len(self.senders_)): threads_send.append( threading.Thread( target=self.send_topology, args=(stop_node_event, stop_topology_send_event, self.senders_[i], self.adjacency_list_))) lock = threading.Lock() for i in range(len(self.receivers_)): threads_receive.append( threading.Thread(target=self.receive_topology_update, args=(stop_node_event, lock, self.receivers_[i], self, i))) for thread in threads_send: thread.start() for thread in threads_receive: thread.start() start_time = time.time() time_since_last_topology_send = time.time() while time.time( ) - start_time < transm_global_params.DESIGNATED_NODE_LIFETIME: if time.time( ) - time_since_last_topology_send > transm_global_params.GRAPH_SYNC_TIME_INTERVAL: print("designated node :", "sending current topology to the nodes", get_time_h_m_s()) stop_topology_send_event.clear() time.sleep(transm_global_params.GRAPH_SYNC_TIME) stop_topology_send_event.set() stop_node_event.set() for thread in threads_receive: thread.join() for thread in threads_send: thread.join() print("designated node :", "I'm out", get_time_h_m_s())
def receive_topology_update(self, stop_event, lock, receiver, node, sender_id): while not stop_event.is_set(): is_connected = receiver.wait_for_connection() if is_connected: adj_list_update = receiver.receive() if adj_list_update is not None: print("designated node :", "received update from node #", sender_id, adj_list_update[0], get_time_h_m_s()) lock.acquire() adj_list_update = adj_list_update[0] for key in node.adjacency_list_: node.adjacency_list_[key] = [ i for i in node.adjacency_list_[key] if i[0] != sender_id ] if len(adj_list_update) == 0: node.adjacency_list_[sender_id] = adj_list_update else: node.adjacency_list_[sender_id] = adj_list_update for neighbor in adj_list_update: index = neighbor[0] weight = neighbor[1] if index not in node.adjacency_list_.keys(): node.adjacency_list_[index] = [(sender_id, weight)] else: node.adjacency_list_[index].append( (sender_id, weight)) lock.release()
def run(self, is_first_to_adjust=False): threads_send_hello = [] threads_receive_hello = [] stop_hello_event = threading.Event() for i in self.senders_mirrors_: threads_send_hello.append( threading.Thread(target=self.send_hello, args=(stop_hello_event, self.senders_mirrors_[i]))) lock = threading.Lock() for i in self.receivers_mirrors_: threads_receive_hello.append( threading.Thread(target=self.receive_hello, args=(stop_hello_event, lock, self.receivers_mirrors_[i], self, i))) for thread in threads_send_hello: thread.start() for thread in threads_receive_hello: thread.start() time.sleep(transm_global_params.HELLO_TIMEOUT) stop_hello_event.set() for thread in threads_receive_hello: thread.join() for thread in threads_send_hello: thread.join() if is_first_to_adjust: self.is_turn_to_adjust = True threads_receive_adj_ctrl = [] stop_mirror_proc_event = threading.Event() for i in self.neighbor_ids_: threads_receive_adj_ctrl.append( threading.Thread(target=self.receive_adj_ctrl, args=(stop_mirror_proc_event, self.receivers_mirrors_[i], self, i))) for thread in threads_receive_adj_ctrl: thread.start() start_time = time.time() while time.time() - start_time < transm_global_params.MIRROR_TIMEOUT: if self.is_turn_to_adjust: self.adjust() self.is_turn_to_adjust = False self.send_adj_ctrl() stop_mirror_proc_event.set() for thread in threads_receive_adj_ctrl: thread.join() print("mirror #", self.id_, ":", "I'm out", get_time_h_m_s())
def receive_topology(self, stop_event, receiver, node): while not stop_event.is_set(): is_connected = receiver.wait_for_connection() if is_connected: adj_list_updated = receiver.receive() if adj_list_updated is not None: node.adjacency_list_ = adj_list_updated[0] print("node #", node.id_, ":", "successfully received updated topology, rebuilding shortest paths", # node.adjacency_list_, get_time_h_m_s()) if node.id_ in node.adjacency_list_.keys() and len(node.adjacency_list_[node.id_]) != 0: for i in node.adjacency_list_: if i != node.id_: node.shortest_paths_[i] = node.find_shortest_path(node.id_, i) print("node #", node.id_, ":", "shortest paths have been rebuilt", get_time_h_m_s()) else: node.shortest_paths_ = {} print("node #", node.id_, ":", "node isolated from other, can't determine paths", get_time_h_m_s())
def wait_for_connection(self): time_start = time.time() time_since_last_try = time.time() while time.time() - time_start < transm_global_params.CONNECTION_TIMEOUT: if time.time() - time_since_last_try > transm_global_params.CONNECTION_ESTABLISHMENT_INTERVAL: if self.VERBOSE: print("sender: Trying to connect", get_time_h_m_s()) frame = Frame(None, transm_global_params.ESTABLISH_CONNECTION_CODE, False, False) self.ipc_manager_.send_to_receiver(frame) time_since_last_try = time.time() ack = self.ipc_manager_.get_from_receiver() if ack is None: continue if ack.seq_num_ == transm_global_params.ESTABLISH_CONNECTION_CODE: if self.VERBOSE: print("sender: Connection established", get_time_h_m_s()) return True return False
def receive_adj_ctrl(self, stop_event, receiver, mirror, id_sender): while not stop_event.is_set(): is_connected = receiver.wait_for_connection() if is_connected: adj_ctrl = receiver.receive() if adj_ctrl is not None and adj_ctrl[ 0] == transm_global_params.MSG_TAKE_ADJUSTING_CONTROL: print("mirror #", self.id_, ":", "adjusting control is received", get_time_h_m_s()) mirror.is_turn_to_adjust = True mirror.prev_adjusted_id_ = id_sender
def send_topology_update(self, stop_event, sender, node): prev_eighbors_count = len(node.neighbor_ids_) while not stop_event.is_set(): if len(node.neighbor_ids_) != prev_eighbors_count: print("node #", node.id_, ":", "send neighbors list update to the designated node", get_time_h_m_s()) is_connected = sender.wait_for_connection() if is_connected: is_sent_successfully = sender.send([node.neighbor_ids_]) if is_sent_successfully: prev_eighbors_count = len(node.neighbor_ids_) else: print("node #", node.id_, ":", "timeout while sending neighbors", get_time_h_m_s())
def run(self): threads_send_hello = [] threads_receive_hello = [] stop_node_event = threading.Event() for i in self.senders_neighbors_: threads_send_hello.append( threading.Thread(target=self.send_hello, args=(stop_node_event, self.senders_neighbors_[i]))) locks = [threading.Lock(), threading.Lock()] for i in self.receivers_neighbors_: threads_receive_hello.append( threading.Thread(target=self.receive_hello, args=(stop_node_event, locks, self.receivers_neighbors_[i], self, (i, 1)))) thread_send_topology_update = threading.Thread(target=self.send_topology_update, args=(stop_node_event, self.sender_des_node_, self)) thread_receive_topology = threading.Thread(target=self.receive_topology, args=(stop_node_event, self.receiver_des_node_, self)) thread_send_topology_update.start() thread_receive_topology.start() for thread in threads_send_hello: thread.start() for thread in threads_receive_hello: thread.start() time.sleep(transm_global_params.NODE_LIFETIME) stop_node_event.set() for thread in threads_receive_hello: thread.join() for thread in threads_send_hello: thread.join() thread_send_topology_update.join() thread_receive_topology.join() print("node #", self.id_, ":", "I'm out, saving paths and topology", get_time_h_m_s()) with open(str(self.id_) + "_outfile.txt", "w") as file: file.write(str(self.adjacency_list_)) file.write("\n") file.write(str(self.shortest_paths_))
def wait_for_connection(self): time_start = time.time() while time.time( ) - time_start < transm_global_params.CONNECTION_TIMEOUT: req = self.ipc_manager_.get_from_sender() if req is None: continue if req.seq_num_ == transm_global_params.ESTABLISH_CONNECTION_CODE: ack = Frame(None, transm_global_params.ESTABLISH_CONNECTION_CODE, False, False) self.ipc_manager_.send_to_sender(ack) if self.VERBOSE: print("receiver: Connection established", get_time_h_m_s()) return True return False
def receive_hello(self, stop_event, lock, receiver, node, neighbor_id): while not stop_event.is_set(): is_connected = receiver.wait_for_connection() if is_connected: hello = receiver.receive() if hello is not None and hello[ 0] == transm_global_params.MSG_HELLO: lock.acquire() if neighbor_id not in node.neighbor_ids_: print("mirror #", node.id_, ":", "mirror #", neighbor_id, "discovered, appending to neighbors", get_time_h_m_s()) node.neighbor_ids_.append(neighbor_id) lock.release()
def adjust(self): print("mirror #", self.id_, ":", "start adjusting", get_time_h_m_s()) light_src_coord = self.receive_light_src_coord() if light_src_coord is None: return intensity_before_adjusting = self.receive_intensity() if intensity_before_adjusting is None: return cur_intensity = intensity_before_adjusting dir_from_light = [ self.coord_[0] - light_src_coord[0], self.coord_[1] - light_src_coord[1] ] rotation_angle = 0 iter_count = 0 dir_on_focus = [] while intensity_before_adjusting == cur_intensity: if iter_count != 0: rotate(self.normal_, transm_global_params.ROTATION_DELTA_RAD) rotation_angle += transm_global_params.ROTATION_DELTA_RAD if dir_from_light[0] * self.normal_[0] + dir_from_light[ 1] * self.normal_[1] <= 0: dir_on_focus = get_reflected_vector(dir_from_light, self.normal_) normalize(dir_on_focus) ray = Ray(point=self.coord_, vector=dir_on_focus) is_sent = self.send_ray(ray) if not is_sent: return time.sleep(0.5) cur_intensity = self.receive_intensity() if cur_intensity is None: return iter_count += 1 print("mirror #", self.id_, ":", "final angle:", rotation_angle, get_time_h_m_s()) print("mirror #", self.id_, ":", "rotations count:", iter_count - 1, get_time_h_m_s()) print("mirror #", self.id_, ":", "final normal:", self.normal_[0], self.normal_[1], get_time_h_m_s()) print("mirror #", self.id_, ":", "final dir on focus:", dir_on_focus[0], dir_on_focus[1], get_time_h_m_s()) print("mirror #", self.id_, ":", "end adjusting", get_time_h_m_s())
def send_adj_ctrl(self): id_to_send_to = -1 for i in self.neighbor_ids_: if i != self.prev_adjusted_id_ or len(self.neighbor_ids_) == 1: id_to_send_to = i continue start_time = time.time() while time.time( ) - start_time < transm_global_params.MIRROR_TO_MIRROR_TIMEOUT: is_connected = self.senders_mirrors_[ id_to_send_to].wait_for_connection() if is_connected: is_sent = self.senders_mirrors_[id_to_send_to].send( [transm_global_params.MSG_TAKE_ADJUSTING_CONTROL]) if is_sent: print("mirror #", self.id_, ":", "transfer control to mirror #", id_to_send_to, get_time_h_m_s()) return True return False
def find_shortest_path(self, start_id, finish_id): distances = {} is_visited = {} for i in self.adjacency_list_.keys(): is_visited[i] = False distances[i] = math.inf distances[start_id] = 0 prev = {} q = PriorityQueue() q.put((0, start_id)) while not q.empty(): cur_node = q.get()[1] if cur_node == finish_id: break is_visited[cur_node] = True for neighbor in self.adjacency_list_[cur_node]: neighbor_id = neighbor[0] edge_weight = neighbor[1] if not is_visited[neighbor_id]: if distances[neighbor_id] > edge_weight + distances[cur_node]: distances[neighbor_id] = edge_weight + distances[cur_node] prev[neighbor_id] = cur_node q.put((distances[neighbor_id], neighbor_id)) if finish_id not in prev.keys(): print("node #", start_id, ":", "node #", finish_id, "is unreachable", get_time_h_m_s()) return [] prev_id = prev[finish_id] path = [finish_id] while prev_id != start_id: path.append(prev_id) prev_id = prev[prev_id] path.append(start_id) path.reverse() return path
def run(self): threads_send = [] threads_receive = [] stop_receivers_event = threading.Event() stop_senders_event = threading.Event() for i in range(len(self.senders_)): threads_send.append( threading.Thread(target=self.send_intensity, args=(stop_senders_event, self.senders_[i], self))) for i in range(len(self.receivers_)): threads_send.append( threading.Thread(target=self.receive_ray, args=(stop_receivers_event, self.receivers_[i], self))) for thread in threads_send: thread.start() for thread in threads_receive: thread.start() time.sleep(transm_global_params.FOCUS_TIMEOUT) stop_receivers_event.set() stop_senders_event.set() for thread in threads_send: thread.join() for thread in threads_receive: thread.join() print("focus :", "I'm out", get_time_h_m_s())
def receive_sel_repeat(self): out_data_list = [] received_frames = [] seq_num_expected = 0 time_since_last_frame = None is_last_frame = False if self.VERBOSE: print("receiver: Start transmission", get_time_h_m_s()) start_transmission_time = time.time() while True: if time.time( ) - start_transmission_time > transm_global_params.TRANSMISSION_TIMEOUT: return None frame = self.ipc_manager_.get_from_sender() if is_last_frame: # resend ack for each new frame after the last frame until timeout if frame is not None and not frame.is_corrupted_: self.total_received_ += 1 ack_frame = Frame( data=None, seq_num=seq_num_expected, is_last=False, is_corrupted= False # flip_biased_coin(transm_global_params.ERROR_PROBABILITY) ) if self.VERBOSE: print( "receiver: Resend ack after obtaining all frames", seq_num_expected, get_time_h_m_s()) self.ipc_manager_.send_to_sender(ack_frame) self.total_sent_ack_ += 1 if time_since_last_frame is not None: if time.time( ) - time_since_last_frame > transm_global_params.TIMEOUT_RECEIVER: if self.VERBOSE: print( "receiver: Timeout on resending last ack, terminating", get_time_h_m_s()) break continue if frame is None: continue if frame.is_corrupted_: # without nack, receive re-sent frames after sender timeout if self.VERBOSE: print("receiver: Corrupted frame, ignoring", frame.seq_num_, get_time_h_m_s()) continue self.total_received_ += 1 if frame.seq_num_ < seq_num_expected: ack_frame = Frame( data=None, seq_num=seq_num_expected, is_last=False, is_corrupted= False # flip_biased_coin(transm_global_params.ERROR_PROBABILITY) ) if self.VERBOSE: print("receiver: Send ack for seq num less than expected", seq_num_expected, "is corrupted:", ack_frame.is_corrupted_, get_time_h_m_s()) self.ipc_manager_.send_to_sender(ack_frame) self.total_sent_ack_ += 1 continue if frame.seq_num_ == seq_num_expected: if self.VERBOSE: print("receiver: Received expected frame", frame.seq_num_, get_time_h_m_s()) if frame.is_last_: is_last_frame = True seq_num_expected += 1 out_data_list.append(frame.data_) ack_frame = Frame( data=None, seq_num=seq_num_expected, is_last=False, is_corrupted= False # flip_biased_coin(transm_global_params.ERROR_PROBABILITY) ) if self.VERBOSE: print("receiver: Send ack", seq_num_expected, get_time_h_m_s()) self.ipc_manager_.send_to_sender(ack_frame) self.total_sent_ack_ += 1 prev_seq_num = frame.seq_num_ while len(received_frames) != 0: if received_frames[0].seq_num_ == prev_seq_num + 1: if received_frames[0].is_last_: is_last_frame = True seq_num_expected += 1 out_data_list.append(received_frames[0].data_) # ack_frame_prev = Frame( # data=None, # seq_num=seq_num_expected, # is_last=False, # is_corrupted=False # flip_biased_coin(transm_global_params.ERROR_PROBABILITY) # ) # if self.VERBOSE: # print("receiver: Send ack for previous frame", seq_num_expected, "is corrupted:", # ack_frame_prev.is_corrupted_, get_time_h_m_s()) # self.ipc_manager_.send_to_sender(ack_frame_prev) # self.total_sent_ack_ += 1 del received_frames[0] prev_seq_num += 1 else: break else: if self.VERBOSE: print("receiver: Received unexpected frame", frame.seq_num_, get_time_h_m_s()) if len(received_frames) != 0: insertion_idx = 0 while insertion_idx < len( received_frames ) and frame.seq_num_ > received_frames[ insertion_idx].seq_num_: insertion_idx += 1 if insertion_idx < len( received_frames) and received_frames[ insertion_idx].seq_num_ != frame.seq_num_: received_frames.insert(insertion_idx, frame) elif insertion_idx == len(received_frames): received_frames.append(frame) else: received_frames.append(frame) ack_frame = Frame( data=None, seq_num=frame.seq_num_ + 1, is_last=False, is_corrupted= False # flip_biased_coin(transm_global_params.ERROR_PROBABILITY) ) if self.VERBOSE: print("receiver: Send ack", frame.seq_num_ + 1, get_time_h_m_s()) self.ipc_manager_.send_to_sender(ack_frame) self.total_sent_ack_ += 1 if is_last_frame: if self.VERBOSE: print("receiver: Last frame is received", seq_num_expected, get_time_h_m_s()) if time_since_last_frame is None: time_since_last_frame = time.time() return out_data_list
def receive_go_back_n(self): out_data_list = [] seq_num_expected = 0 time_since_last_frame = None is_last_frame = False if self.VERBOSE: print("receiver: Start transmission", get_time_h_m_s()) start_transmission_time = time.time() while True: if time.time( ) - start_transmission_time > transm_global_params.TRANSMISSION_TIMEOUT: return None frame = self.ipc_manager_.get_from_sender() if is_last_frame: # resend ack for each new frame after the last frame until timeout if frame is not None and not frame.is_corrupted_: self.total_received_ += 1 ack_frame = Frame( data=None, seq_num=seq_num_expected, is_last=False, is_corrupted= False # flip_biased_coin(transm_global_params.ERROR_PROBABILITY) ) if self.VERBOSE: print("receiver: Send ack", seq_num_expected, get_time_h_m_s()) self.ipc_manager_.send_to_sender(ack_frame) self.total_sent_ack_ += 1 if time_since_last_frame is not None: if time.time( ) - time_since_last_frame > transm_global_params.TIMEOUT_RECEIVER: if self.VERBOSE: print( "receiver: Timeout on resending last ack, terminating", get_time_h_m_s()) break continue if frame is None: continue if frame.is_corrupted_: if self.VERBOSE: print("receiver: Corrupted frame, ignoring", frame.seq_num_, get_time_h_m_s()) continue self.total_received_ += 1 if frame.seq_num_ == seq_num_expected: if self.VERBOSE: print("receiver: Received expected frame", frame.seq_num_, get_time_h_m_s()) if frame.is_last_: if self.VERBOSE: print("receiver: Last frame is received", seq_num_expected, get_time_h_m_s()) is_last_frame = True if time_since_last_frame is None: time_since_last_frame = time.time() seq_num_expected += 1 out_data_list.append(frame.data_) else: if self.VERBOSE: print("receiver: Received unexpected frame", frame.seq_num_, get_time_h_m_s()) ack_frame = Frame( data=None, seq_num=seq_num_expected, is_last=False, is_corrupted= False # flip_biased_coin(transm_global_params.ERROR_PROBABILITY) ) if self.VERBOSE: print("receiver: Send ack", seq_num_expected, get_time_h_m_s()) self.ipc_manager_.send_to_sender(ack_frame) self.total_sent_ack_ += 1 return out_data_list
def send_sel_repeat(self, data_list): seq_num_first = 0 seq_num_last = 0 cur_data_block_idx = 0 is_waiting_last_ack = False # last_frame_timeout_start = 0 sent_frames = [] timers = [] if self.VERBOSE: print("sender: Start transmission", get_time_h_m_s()) start_transmission_time = time.time() while True: if time.time() - start_transmission_time > transm_global_params.TRANSMISSION_TIMEOUT: return False if seq_num_last < seq_num_first + transm_global_params.WINDOW_SIZE and not is_waiting_last_ack: frame = Frame( data=data_list[cur_data_block_idx], seq_num=seq_num_last, is_last=True if cur_data_block_idx == len(data_list) - 1 else False, is_corrupted=flip_biased_coin(transm_global_params.ERROR_PROBABILITY) ) if self.VERBOSE: print("sender: Send frame", seq_num_last, "is corrupted:", frame.is_corrupted_, get_time_h_m_s()) self.ipc_manager_.send_to_receiver(frame) self.total_sent_ += 1 cur_data_block_idx += 1 if cur_data_block_idx == len(data_list): if self.VERBOSE: print("sender: Wait last ack, no new frame", get_time_h_m_s()) is_waiting_last_ack = True # last_frame_timeout_start = time.time() sent_frames.append(frame) timers.append(time.time()) seq_num_last += 1 ack = self.ipc_manager_.get_from_receiver() if ack is not None and not ack.is_corrupted_: self.total_received_ack_ += 1 if seq_num_first < ack.seq_num_ <= seq_num_last: if self.VERBOSE: print("sender: Received ack", ack.seq_num_, get_time_h_m_s()) idx_del = 0 for i in range(len(sent_frames)): if sent_frames[i] is not None and ack.seq_num_ - 1 == sent_frames[i].seq_num_: idx_del = i break sent_frames[idx_del] = None timers[idx_del] = None if idx_del == 0: while len(sent_frames) != 0 and sent_frames[0] is None: del sent_frames[0] del timers[0] seq_num_first += 1 if is_waiting_last_ack and len(sent_frames) == 0: if self.VERBOSE: print("sender: Received last ack, terminate transmission", get_time_h_m_s()) return True if ack is not None and ack.is_corrupted_: if self.VERBOSE: print("sender: Corrupted ack, ignoring", ack.seq_num_, get_time_h_m_s()) for i in range(len(timers)): if timers[i] is None: continue cur_time = time.time() if cur_time - timers[i] > transm_global_params.TIMEOUT_SEL_REPEAT_SENDER: sent_frames[i].is_corrupted_ = flip_biased_coin(transm_global_params.ERROR_PROBABILITY) self.ipc_manager_.send_to_receiver(sent_frames[i]) self.total_sent_ += 1 timers[i] = time.time() if self.VERBOSE: print("sender: Timeout, retransmit", sent_frames[i].seq_num_, "is corrupted:", sent_frames[i].is_corrupted_, get_time_h_m_s())
processes = [ Process(target=run_focus, args=(focus_coord, transmission_protocol, senders_ipc_managers_f2m, receivers_ipc_managers_f2m)), Process(target=run_light_src, args=(light_src_coord, transmission_protocol, senders_ipc_managers_l2m)) ] for i in range(nodes_number): processes.append( Process(target=run_mirror, args=(mirrors_coordinates[i], transmission_protocol, i, senders_ipc_managers_m2f[i], receivers_ipc_managers_m2f[i], receivers_ipc_managers_m2l[i], senders_ipc_managers_m2m[i], receivers_ipc_managers_m2m[i]))) print("Network launch", get_time_h_m_s()) for i in range(len(processes)): processes[i].start() time.sleep(transm_global_params.NETWORK_TIMEOUT) print("Network timeout, exiting", get_time_h_m_s()) for p in processes: p.join()
def send_go_back_n(self, data_list): seq_num_first = 0 seq_num_last = 0 time_since_last_ack = time.time() cur_data_block_idx = 0 is_waiting_last_ack = False # last_frame_timeout_start = 0 sent_frames = [] if self.VERBOSE: print("sender: Start transmission", get_time_h_m_s()) start_transmission_time = time.time() while True: if time.time() - start_transmission_time > transm_global_params.TRANSMISSION_TIMEOUT: return False if seq_num_last < seq_num_first + transm_global_params.WINDOW_SIZE and not is_waiting_last_ack: frame = Frame( data=data_list[cur_data_block_idx], seq_num=seq_num_last, is_last=True if cur_data_block_idx == len(data_list) - 1 else False, is_corrupted=flip_biased_coin(transm_global_params.ERROR_PROBABILITY) ) if self.VERBOSE: print("sender: Send frame", seq_num_last, "is corrupted:", frame.is_corrupted_, get_time_h_m_s()) self.ipc_manager_.send_to_receiver(frame) self.total_sent_ += 1 cur_data_block_idx += 1 if cur_data_block_idx == len(data_list): if self.VERBOSE: print("sender: Wait last ack, no new frame", get_time_h_m_s()) is_waiting_last_ack = True # last_frame_timeout_start = time.time() sent_frames.append(frame) seq_num_last += 1 ack = self.ipc_manager_.get_from_receiver() if ack is not None and not ack.is_corrupted_: self.total_received_ack_ += 1 if seq_num_first < ack.seq_num_ <= seq_num_last: if self.VERBOSE: print("sender: Received ack", ack.seq_num_, get_time_h_m_s()) while seq_num_first < ack.seq_num_: seq_num_first += 1 time_since_last_ack = time.time() del sent_frames[0] if is_waiting_last_ack and len(sent_frames) == 0: if self.VERBOSE: print("sender: Received last ack, terminate transmission", get_time_h_m_s()) return True else: if ack is not None and ack.is_corrupted_: if self.VERBOSE: print("sender: Corrupted ack, ignoring", ack.seq_num_, get_time_h_m_s()) if time.time() - time_since_last_ack > transm_global_params.TIMEOUT_GO_BACK_N_SENDER: if self.VERBOSE: print("sender: Timeout, resend entire window", get_time_h_m_s()) for frame in sent_frames: frame.is_corrupted_ = flip_biased_coin(transm_global_params.ERROR_PROBABILITY) self.ipc_manager_.send_to_receiver(frame) self.total_sent_ += 1 time_since_last_ack = time.time()
sender.wait_for_connection() start_time = datetime.datetime.now() sender.send(list(split_string(data, frames_count))) finish_time = datetime.datetime.now() # with open('sender_results.txt', 'a') as file: # file.write( # str(transm_global_params.TRANSMISSION_PROTOCOL_TYPE) # + " " # + str(transm_global_params.ERROR_PROBABILITY) # + " " # + str(get_delta_ms(start_time, finish_time)) # + " ms" # + " total_sent: " # + str(sender.total_sent_) # + " total_received_ack: " # + str(sender.total_received_ack_) # + " win size: " # + str(transm_global_params.WINDOW_SIZE) # + "\n" # ) print("sender: Wait before shutdown connection", get_time_h_m_s()) time.sleep(2) m.shutdown()
def check_and_submit_intersection(self, ray): if is_point_on_ray(ray, self.coord_): print("focus :", "mirror successfully adjusted", get_time_h_m_s()) self.intensity_ += 1