-
Notifications
You must be signed in to change notification settings - Fork 0
add more functionality #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import json | ||
| from dataclasses import dataclass | ||
| from datetime import datetime | ||
| from collections import deque | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
@@ -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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a more descriptive variable name for this constant, such as There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| self.packet_count: Dict[str, List[Packet]] = {} # {destination: [packets]} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
|
|
||
| def add_neighbor(self, neighbor_id: str, cost: int): | ||
| """Add a neighbor router with associated cost.""" | ||
|
|
@@ -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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The queue size check is redundant with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method does not use the |
||
| """Receive a packet and either forward it or process it.""" | ||
| if len(self.packet_queue) >= self.max_capacity: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a more descriptive variable name for this constant, such as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| return False # Queue is full | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a more descriptive variable name for this constant, such as |
||
| 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.""" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The queue size check is redundant with the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method does not use the |
||
| """Update packet count for the destination.""" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The queue size check is redundant with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| if packet.destination not in self.packet_count: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] = [] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The queue size check is redundant with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| self.packet_count[packet.destination].append(packet) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The queue size check is redundant with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| def get_packet_count(self, destination: str, start_time: Optional[datetime] = None, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| """Get count of packets for a destination within a timeframe.""" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| if destination not in self.packet_count: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| if start_time is None and end_time is None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| if start_time: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| if end_time: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
| """Update routing table using distance vector algorithm with split horizon.""" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to specify the |
||
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The queue size check is redundant with the |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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.