只有hello包实现多跳,还没加入业务数据

具体的还要看opencode和gpt记录接着优化
This commit is contained in:
sinlatansen
2026-02-24 17:17:45 +08:00
parent 375febb4c0
commit d357a25076
14 changed files with 1690 additions and 58 deletions

View File

@@ -16,6 +16,7 @@ from sim.core.packet import Packet, PacketType
from sim.routing.gradient_routing import GradientRouting
from sim.mac.reliable_mac import ReliableMAC
from sim.radio.channel import Channel, ReceivedPacket
from sim.core.metrics import MetricsCollector
from sim import config
@@ -51,6 +52,7 @@ class Node:
y: float,
channel: Channel,
is_sink: bool = False,
metrics_collector: MetricsCollector = None,
):
"""
Initialize node.
@@ -62,6 +64,7 @@ class Node:
y: Y coordinate
channel: Wireless channel
is_sink: Whether this is the sink node
metrics_collector: Metrics collector for observability
"""
self.env = env
self.node_id = node_id
@@ -70,6 +73,9 @@ class Node:
self.channel = channel
self.is_sink = is_sink
# Metrics collector for hop tracking
self.metrics_collector = metrics_collector
# Register position with channel
self.channel.register_node(node_id, x, y)
@@ -199,18 +205,26 @@ class Node:
def _process_data(self, packet: Packet):
"""Process received DATA packet."""
# If we're the destination (sink), receive it
if packet.dst == self.node_id:
# If we're the sink, receive the packet
if self.is_sink:
self.stats.data_received += 1
# If sink, we're done
if self.is_sink:
return
# Record hop count for analysis
if self.metrics_collector:
# print(f"SINK received packet with hop={packet.hop}")
self.metrics_collector.record_hop_count(packet.hop)
return
# Otherwise forward to parent (for multi-hop)
next_hop = self.routing.get_next_hop()
if next_hop is not None and next_hop != self.node_id:
self._forward_data(packet)
# If not sink, check if we should forward
# Don't forward if we've already forwarded this packet (check path)
if self.node_id in packet.path:
# We've already seen and forwarded this packet, skip it
return
# Forward to parent
next_hop = self.routing.get_next_hop()
if next_hop is not None and next_hop != self.node_id:
self._forward_data(packet)
def _process_ack(self, packet: Packet):
"""Process received ACK packet."""
@@ -224,7 +238,7 @@ class Node:
src=self.node_id,
dst=config.SINK_NODE_ID,
seq=self.data_seq,
hop=0,
hop=1, # Start at 1 hop (first link)
payload=f"data_{self.data_seq}",
)
self.data_seq += 1
@@ -237,8 +251,8 @@ class Node:
def _forward_data(self, packet: Packet):
"""Forward a data packet towards sink."""
# Increment hop count
packet.hop += 1
# Record this node in the path and increment hop count
packet.add_hop(self.node_id)
# Send to parent
next_hop = self.routing.get_next_hop()