Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions custom_router/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
from dataclasses import dataclass
from datetime import datetime
from collections import deque
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.


@dataclass
class Packet:
Expand All @@ -12,12 +13,14 @@ class Packet:
timestamp: datetime = datetime.now()

class Router:
def __init__(self, router_id: str):
def __init__(self, router_id: str, max_capacity: int = 100):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the max_capacity parameter of the queue to set its maximum size and avoid hardcoding it here.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

self.router_id = router_id
self.routing_table: Dict[str, Dict[str, int]] = {} # {destination: {next_hop: cost}}
self.neighbors: Dict[str, int] = {} # {neighbor_id: cost}
self.packet_queue: List[Packet] = []
self.packet_queue: deque[Packet] = deque(maxlen=max_capacity) # FIFO queue with max capacity
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more descriptive variable name for this constant, such as DEFAULT_QUEUE_SIZE.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

self.network_graph = nx.Graph()
self.max_capacity = max_capacity
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not used. Consider removing it or documenting why it's needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MAX_QUEUE_SIZE constant should be defined as an integer literal, not a string literal.

self.packet_count: Dict[str, List[Packet]] = {} # {destination: [packets]}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding type hints to the queue constructor to make its usage clearer for users.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.


def add_neighbor(self, neighbor_id: str, cost: int):
"""Add a neighbor router with associated cost."""
Expand All @@ -32,23 +35,53 @@ def remove_neighbor(self, neighbor_id: str):
self.network_graph.remove_edge(self.router_id, neighbor_id)
self.update_routing_table()

def receive_packet(self, packet: Packet):
def receive_packet(self, packet: Packet) -> bool:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The queue size check is redundant with the is_queue_full() method. Consider removing this check or renaming it to better reflect its purpose.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method does not use the self parameter, and should be defined as a static method instead.

"""Receive a packet and either forward it or process it."""
if len(self.packet_queue) >= self.max_capacity:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more descriptive variable name for this constant, such as DEFAULT_MAX_CAPACITY.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

return False # Queue is full
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used. Consider removing it or documenting why it's needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to use a ternary operator here when you could simply use an if statement.


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

if packet.destination == self.router_id:
self.packet_queue.append(packet)
self._update_packet_count(packet)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more descriptive variable name for this constant, such as DEFAULT_QUEUE_SIZE.

return True
else:
return self.forward_packet(packet)

def forward_packet(self, packet: Packet) -> bool:
"""Forward a packet to the next hop based on routing table."""
"""Forward a packet to the next hop based on routing table using FIFO."""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The queue size check is redundant with the is_queue_full() method. Consider removing this check or renaming it to better reflect its purpose.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to use a ternary operator here when you could simply use an if statement.

if packet.destination in self.routing_table:
next_hop = min(self.routing_table[packet.destination].items(),
key=lambda x: x[1])[0]
# In a real implementation, this would send the packet to next_hop
self._update_packet_count(packet)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not used. Consider removing it or documenting why it's needed.

return True
return False

def _update_packet_count(self, packet: Packet):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding type hints to the queue constructor to make its usage clearer for users.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method does not use the self parameter, and should be defined as a static method instead.

"""Update packet count for the destination."""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The queue size check is redundant with the is_queue_full() method. Consider removing this check or renaming it to better reflect its purpose.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

if packet.destination not in self.packet_count:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not used. Consider removing it or documenting why it's needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

self.packet_count[packet.destination] = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The queue size check is redundant with the is_queue_full() method. Consider removing this check or renaming it to better reflect its purpose.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

self.packet_count[packet.destination].append(packet)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not used. Consider removing it or documenting why it's needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The queue size check is redundant with the is_queue_full() method. Consider removing this check or renaming it to better reflect its purpose.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

def get_packet_count(self, destination: str, start_time: Optional[datetime] = None,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

end_time: Optional[datetime] = None) -> int:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding type hints to the queue constructor to make its usage clearer for users.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

"""Get count of packets for a destination within a timeframe."""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

# can this be done more optimally?
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

if destination not in self.packet_count:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

return 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not used. Consider removing it or documenting why it's needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

packets = self.packet_count[destination]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

if start_time is None and end_time is None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

return len(packets)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

filtered_packets = packets
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

if start_time:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

filtered_packets = [p for p in filtered_packets if p.timestamp >= start_time]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

if end_time:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

filtered_packets = [p for p in filtered_packets if p.timestamp <= end_time]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

return len(filtered_packets)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if statement in this method is redundant with the if statement in the previous line. Consider combining them into a single check.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

def update_routing_table(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

"""Update routing table using distance vector algorithm with split horizon."""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Consider removing it or documenting why it's needed.

# Initialize routing table with direct neighbors
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to specify the int type for variables in Python. The interpreter can automatically infer the types.

Expand All @@ -68,4 +101,12 @@ def get_routing_table(self) -> Dict[str, Dict[str, int]]:

def get_packet_queue(self) -> List[Packet]:
"""Return the current packet queue."""
return self.packet_queue
return list(self.packet_queue)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The queue size check is redundant with the is_queue_full() method. Consider removing this check or renaming it to better reflect its purpose.


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used. Consider removing it or documenting why it's needed.

def get_queue_size(self) -> int:
"""Return the current size of the packet queue."""
return len(self.packet_queue)

def is_queue_full(self) -> bool:
"""Check if the packet queue is full."""
return len(self.packet_queue) >= self.max_capacity
Loading