def __init__(self, local_ip, local_port, remote_ip, remote_port, transport_layer): # Port for recv and send packets. self.remote_ip = remote_ip self.remote_port = remote_port # Listening on local_port to recv packets. self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.s.bind((local_ip, local_port)) # self.s.setblocking(False) self.s.settimeout(0.5) # seconds. # Hold transport layer object for message demultiplexing. self.transport_layer = transport_layer # Buffer for holding messages to be delivered to transport layer. self.msg_buffer = collections.deque(maxlen=8) self.buffer_lock = threading.Lock() # Start reading network packet thread. self.stop_accept_pkt = False # Periodically reading packets from socket. self.periodic_packet_receiver = util.PeriodicClosure( self._packet_reader, config.UDT_PACKET_RECV_INTERVAL_SEC) self.periodic_packet_receiver.start() # Periodically sending packets to transport layer. self.periodic_packet_deliver = util.PeriodicClosure( self._packet_deliver, config.UDT_PACKET_DELIVER_INTERVAL_SEC) self.periodic_packet_deliver.start()
def start(self): # Start a periodic closure to update config. self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._config_updater.start() # TODO: init and start other threads. self._send_updater = util.PeriodicClosure( self.send_update_to_neighbors,_CONFIG_UPDATE_INTERVAL_SEC) while True: threading.Thread(target=self.msg_handler()).start() print(self._forwarding_table)
def start(self): # Start a periodic closure to update config. self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._start_time = time.time() self._config_updater.start() while True: self.listener_thread()
def start(self): # Start a periodic closure to update config. self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._config_updater.start() # TODO: init and start other threads. while True: self.update_msg_reader()
def __init__(self, local_ip, local_port, remote_ip, remote_port, msg_handler): self.network_layer = udt.NetworkLayer(local_ip, local_port, remote_ip, remote_port, self) self.msg_handler = msg_handler self.base = 1 self.nextseqnum = 1 self.expseqnum = 1 self.timer = util.PeriodicClosure(self.timer_handler, config.TIMEOUT_MSEC / 1000) self.ls = []
def __init__(self, local_ip, local_port, remote_ip, remote_port, msg_handler): self.network_layer = udt.NetworkLayer(local_ip, local_port, remote_ip, remote_port, self) self.msg_handler = msg_handler self.sequence = 0 self.base = 0 self.expect_receive = 0 self.dic = dict() self.timer = util.PeriodicClosure(self.timer_handler, 2)
def start(self): # Start a periodic closure to update config. self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._config_updater.start() # TODO: init and start other threads. self._running = True self._recv_thread.start() while True: pass
def start(self): # Start a periodic closure to update config. self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._config_updater.start() # TODO: init and start other threads. self.buffer_lock = threading.Lock() self.msg_buffer = collections.deque(maxlen=8) self.stop_accept_packet = False thread1 = threading.Thread(target=self._PacketReader()) thread1.start()
def __init__(self, local_ip, local_port, remote_ip, remote_port, msg_handler): self.network_layer = udt.NetworkLayer(local_ip, local_port, remote_ip, remote_port, self) self.msg_handler = msg_handler self.sequence = 0 self.ack_receive = -1 self.expect_receive = 0 self.timer = util.PeriodicClosure(self.timer_handler, 0.1) self.packet = None self.ss_type = 1 # 1-receiver 0-sender
def __init__(self, local_ip, local_port, remote_ip, remote_port, msg_handler): self.network_layer = udt.NetworkLayer(local_ip, local_port, remote_ip, remote_port, self) self.msg_handler = msg_handler self.timer = util.PeriodicClosure(self.timer_handler, 1) self.nextseqnum = 0 self.buffer = b'' self.recv_buffer = b'' self.expseqnum = 0 self.can_sent = True self.can_end = False
def start(self): # Start a periodic closure to update config self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._config_updater.start() # keep a list of neighbour routers to whom the # router should send update message self.neighbour_routers = [] # keep a copy of the actual table to check if # the values in config file changes self.copy_of_forwarding_table = {} while True: pass
def start(self): # Start a periodic closure to update config. self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._config_updater.start() # TODO: init and start other threads. # host_port = _ToPort(self._router_id) # self._socket.bind(self.host_ip, host_port) while True: data, addr = self._socket.recvfrom(1024) src_id = _ToRouterId(addr[1]) ls_pair = self.unpack(data) self.update_from_neighbor(ls_pair, src_id)
def __init__(self, role, local_ip, local_port, remote_ip, remote_port, msg_handler): self.role = role self.network_layer = udt.NetworkLayer(local_ip, local_port, remote_ip, remote_port, self) self.msg_handler = msg_handler self.timer = util.PeriodicClosure(self.timeout_handler, config.TIMEOUT_MSEC / 1000) # fields for a tl sender self.seq_pkt_delivered = -1 self.seq_pkt_to_send = 0 self.msg_to_send = None self.pkt_to_send = None self.lock = Lock() self.sender_arv_lock = Lock() # fields for a tl receiver self.seq_pkt_received = -1 self.seq_pkt_to_receive = 0 self.recvlock = Lock()
def start(self): print('Router started.') # Start a periodic closure to update config. self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._config_updater.start() time.sleep(2) # TODO: init and start other threads. # Receive update messages from neighbor routers in background. self._receive_update_message_thread.start() # Send update messages to neighbor routers in background. self._send_update_message_thread.start() while True: pass
def start(self): # Start a periodic closure to update config. self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._config_updater.start() while True: byte_fw, (_, port) = self._socket.recvfrom(_MAX_UPDATE_MSG_SIZE) with self.lock: prev_ss, prev_dv, prev_ndv = self._forwarding_table.snapshot( ), dict(self.dv), dict(self.neighbor_dvs) # save them for comparing with new self.neighbor_dvs[ _ToRouterId(port)] = _decode_forwarding_table_to_graph( byte_fw) # get neighbor's dv self.calculate_forwarding_table() # do Bellman-Ford algorithm self.send_to_neighbors( ) # send new forwarding table to neighbors self.printState(prev_ss, prev_dv, prev_ndv) # print printState
def __init__(self, role, local_ip, local_port, remote_ip, remote_port, msg_handler): self.role = role self.network_layer = udt.NetworkLayer(local_ip, local_port, remote_ip, remote_port, self) self.msg_handler = msg_handler self.timer = util.PeriodicClosure(self.timeout_handler, config.TIMEOUT_MSEC / 1000) self.starttime = time.time() # fields for a tl sender self.seq_pkt_delivered = -1 self.seq_pkt_index = 0 self.seq_pkt_window_left = 0 self.seq_pkt_window_right = config.WINDOW_SIZE # not inclusive self.buffer = deque() # buffer of packages to send self.lock = Lock() # fields for a tl receiver self.seq_pkt_received = -1 self.seq_pkt_to_receive = 0 self.recvlock = Lock()
def start(self): # Start the Periodic Closure to update the distance table and forwarding table every 5 seconds self._config_updater = util.PeriodicClosure( self.load_config, _CONFIG_UPDATE_INTERVAL_SEC) self._config_updater.start() # PART I: # initialize the router's distance table and forwarding table with open(self._config_filename) as file: router_id = int(file.readline().strip()) self._router_id = router_id self._socket.bind(('localhost', _ToPort(self._router_id))) self._distance_table[self._router_id] = {} for aline in file.readlines(): record = aline.split(',') neighbor_id = int(record[0]) cost = int(record[1]) # Write into the distance table Map<Destination,Map<NextHop,TotalCost>> self._distance_table[neighbor_id] = {} self._distance_table.get(neighbor_id)[neighbor_id] = cost # Write into the forwarding table Map<Destination,[NextHop,TotalCost]> self._forwarding_table._table[neighbor_id] = [ neighbor_id, cost ] #[] is list in python # Initialize the neighbor list self._neighbors.append(neighbor_id) self.print_distance_table() self.print_forwarding_table() # prepare my first udpate message and broadcast it to my neighbors my_update_message = b'' my_update_message += len(self._forwarding_table._table).to_bytes( 2, 'big') for dest in self._forwarding_table._table.keys(): my_update_message += dest.to_bytes(2, 'big') my_update_message += self._forwarding_table._table[dest][ 1].to_bytes(2, 'big') for neighbor in self._neighbors: self._socket.sendto(my_update_message, ('localhost', _ToPort(neighbor))) # PART II: # keep receiving update messages from my neighbors # update my distance table and forwarding table while True: # If no message is received, continue receiving try: data, address = self._socket.recvfrom(_MAX_UPDATE_MSG_SIZE) except ConnectionResetError: continue print('Received Message From Router ', _ToRouterId(address[1])) # Update my distance table and forwarding table with the update message received entry_count = int.from_bytes(data[0:2], 'big') body = data[2:] src_r_id = _ToRouterId( address[1]) # The RouterID of the sender (IP, PORT) # STEP 1: Prepare the Map<Destination,Cost> pairs from neighbor n for updating two tables update_map = {} # Map<Destination,Cost> for i in range(entry_count): dest = int.from_bytes(body[i * 4:i * 4 + 2], 'big') cost = int.from_bytes(body[i * 4 + 2:i * 4 + 4], 'big') update_map[dest] = cost print('Update Message From Router[', src_r_id, ']:', update_map) # STEP 2: update my distance table and forwarding table # P.1 update the distance table self._forwarding_table._lock.acquire() self._distance_table_lock.acquire() link_cost = self._distance_table.get(src_r_id).get(src_r_id) for dst in update_map.keys(): if dst == self._router_id: continue new_cost = update_map[dst] + link_cost if dst not in self._distance_table.keys(): self._distance_table[dst] = {} self._distance_table[dst][src_r_id] = new_cost else: self._distance_table[dst][src_r_id] = new_cost # P.2 update the forwarding table for dst in self._distance_table.keys(): if dst == self._router_id: continue # Get the new best <Next, Cost> pair to Router[dst] best_next = None min_cost = sys.maxsize routes_to_dst = self._distance_table.get(dst) for nxt in routes_to_dst.keys(): cur_cost = routes_to_dst.get(nxt) if cur_cost < min_cost: best_next = nxt min_cost = cur_cost # Update the forwarding table if necessary self._forwarding_table._table[dst] = [best_next, min_cost] self.print_forwarding_table() self._distance_table_lock.release() self._forwarding_table._lock.release()