cleanup virtual inheritence for Router/Reliable/Flooding/DSR

pull/145/head
geeksville 2020-05-23 09:24:22 -07:00
rodzic 8e2e154cdd
commit c9cb293bf2
7 zmienionych plików z 96 dodań i 69 usunięć

Wyświetl plik

@ -48,7 +48,8 @@ void DSRRouter::sniffReceived(const MeshPacket *p)
if (weAreInRoute(p->decoded.request)) { if (weAreInRoute(p->decoded.request)) {
DEBUG_MSG("Ignoring a route request that contains us\n"); DEBUG_MSG("Ignoring a route request that contains us\n");
} else { } else {
updateRoutes(p->decoded.request, false); // Update our routing tables based on the route that came in so far on this request updateRoutes(p->decoded.request,
false); // Update our routing tables based on the route that came in so far on this request
if (p->decoded.dest == getNodeNum()) { if (p->decoded.dest == getNodeNum()) {
// They were looking for us, send back a route reply (the sender address will be first in the list) // They were looking for us, send back a route reply (the sender address will be first in the list)
@ -67,12 +68,17 @@ void DSRRouter::sniffReceived(const MeshPacket *p)
} }
} }
// Handle route reply packets
if (p->decoded.which_payload == SubPacket_reply_tag) {
updateRoutes(p->decoded.reply, true);
}
// Handle regular packets // Handle regular packets
if (p->to == getNodeNum()) { // Destined for us (at least for this hop) if (p->to == getNodeNum()) { // Destined for us (at least for this hop)
// We need to route this packet // We need to route this packet to some other node
if (p->decoded.dest != p->to) { if (p->decoded.dest && p->decoded.dest != p->to) {
// FIXME // FIXME if we have a route out, resend the packet to the next hop, otherwise return a nak with no-route available
} }
} }

Wyświetl plik

@ -17,39 +17,37 @@ ErrorCode FloodingRouter::send(MeshPacket *p)
return Router::send(p); return Router::send(p);
} }
/** bool FloodingRouter::shouldFilterReceived(const MeshPacket *p)
* Called from loop()
* Handle any packet that is received by an interface on this node.
* Note: some packets may merely being passed through this node and will be forwarded elsewhere.
*
* Note: this method will free the provided packet
*/
void FloodingRouter::handleReceived(MeshPacket *p)
{ {
if (wasSeenRecently(p)) { if (wasSeenRecently(p)) {
DEBUG_MSG("Ignoring incoming msg, because we've already seen it\n"); DEBUG_MSG("Ignoring incoming msg, because we've already seen it\n");
packetPool.release(p); return true;
} else {
// If a broadcast, possibly _also_ send copies out into the mesh.
// (FIXME, do something smarter than naive flooding here)
if (p->to == NODENUM_BROADCAST && p->hop_limit > 0) {
if (p->id != 0) {
MeshPacket *tosend = packetPool.allocCopy(*p); // keep a copy because we will be sending it
tosend->hop_limit--; // bump down the hop count
DEBUG_MSG("Rebroadcasting received floodmsg to neighbors, fr=0x%x,to=0x%x,id=%d,hop_limit=%d\n", p->from, p->to,
p->id, tosend->hop_limit);
// Note: we are careful to resend using the original senders node id
// We are careful not to call our hooked version of send() - because we don't want to check this again
Router::send(tosend);
} else {
DEBUG_MSG("Ignoring a simple (0 id) broadcast\n");
}
}
// handle the packet as normal
Router::handleReceived(p);
} }
return Router::shouldFilterReceived(p);
}
void FloodingRouter::sniffReceived(const MeshPacket *p)
{
// If a broadcast, possibly _also_ send copies out into the mesh.
// (FIXME, do something smarter than naive flooding here)
if (p->to == NODENUM_BROADCAST && p->hop_limit > 0) {
if (p->id != 0) {
MeshPacket *tosend = packetPool.allocCopy(*p); // keep a copy because we will be sending it
tosend->hop_limit--; // bump down the hop count
DEBUG_MSG("Rebroadcasting received floodmsg to neighbors, fr=0x%x,to=0x%x,id=%d,hop_limit=%d\n", p->from, p->to,
p->id, tosend->hop_limit);
// Note: we are careful to resend using the original senders node id
// We are careful not to call our hooked version of send() - because we don't want to check this again
Router::send(tosend);
} else {
DEBUG_MSG("Ignoring a simple (0 id) broadcast\n");
}
}
// handle the packet as normal
Router::sniffReceived(p);
} }

Wyświetl plik

@ -46,11 +46,15 @@ class FloodingRouter : public Router, protected PacketHistory
protected: protected:
/** /**
* Called from loop() * Should this incoming filter be dropped?
* Handle any packet that is received by an interface on this node.
* Note: some packets may merely being passed through this node and will be forwarded elsewhere.
* *
* Note: this method will free the provided packet * Called immedately on receiption, before any further processing.
* @return true to abandon the packet
*/ */
virtual void handleReceived(MeshPacket *p); virtual bool shouldFilterReceived(const MeshPacket *p);
/**
* Look for broadcasts we need to rebroadcast
*/
virtual void sniffReceived(const MeshPacket *p);
}; };

Wyświetl plik

@ -36,7 +36,7 @@ ErrorCode ReliableRouter::send(MeshPacket *p)
* *
* Otherwise, let superclass handle it. * Otherwise, let superclass handle it.
*/ */
void ReliableRouter::handleReceived(MeshPacket *p) void ReliableRouter::sniffReceived(const MeshPacket *p)
{ {
NodeNum ourNode = getNodeNum(); NodeNum ourNode = getNodeNum();
@ -55,28 +55,26 @@ void ReliableRouter::handleReceived(MeshPacket *p)
sendAckNak(true, p->from, p->id); sendAckNak(true, p->from, p->id);
} }
if (perhapsDecode(p)) { // If the payload is valid, look for ack/nak
// If the payload is valid, look for ack/nak
PacketId ackId = p->decoded.which_ack == SubPacket_success_id_tag ? p->decoded.ack.success_id : 0; PacketId ackId = p->decoded.which_ack == SubPacket_success_id_tag ? p->decoded.ack.success_id : 0;
PacketId nakId = p->decoded.which_ack == SubPacket_fail_id_tag ? p->decoded.ack.fail_id : 0; PacketId nakId = p->decoded.which_ack == SubPacket_fail_id_tag ? p->decoded.ack.fail_id : 0;
// we are careful to only read/update wasSeenRecently _after_ confirming this is an ack (to not mess // we are careful to only read/update wasSeenRecently _after_ confirming this is an ack (to not mess
// up broadcasts) // up broadcasts)
if ((ackId || nakId) && !wasSeenRecently(p, false)) { if ((ackId || nakId) && !wasSeenRecently(p, false)) {
if (ackId) { if (ackId) {
DEBUG_MSG("Received a ack=%d, stopping retransmissions\n", ackId); DEBUG_MSG("Received a ack=%d, stopping retransmissions\n", ackId);
stopRetransmission(p->to, ackId); stopRetransmission(p->to, ackId);
} else { } else {
DEBUG_MSG("Received a nak=%d, stopping retransmissions\n", nakId); DEBUG_MSG("Received a nak=%d, stopping retransmissions\n", nakId);
stopRetransmission(p->to, nakId); stopRetransmission(p->to, nakId);
}
} }
} }
} }
// handle the packet as normal // handle the packet as normal
FloodingRouter::handleReceived(p); FloodingRouter::sniffReceived(p);
} }
/** /**

Wyświetl plik

@ -88,13 +88,9 @@ class ReliableRouter : public FloodingRouter
protected: protected:
/** /**
* Called from loop() * Look for acks/naks or someone retransmitting us
* Handle any packet that is received by an interface on this node.
* Note: some packets may merely being passed through this node and will be forwarded elsewhere.
*
* Note: this method will free the provided packet
*/ */
virtual void handleReceived(MeshPacket *p); virtual void sniffReceived(const MeshPacket *p);
private: private:
/** /**

Wyświetl plik

@ -40,7 +40,7 @@ void Router::loop()
{ {
MeshPacket *mp; MeshPacket *mp;
while ((mp = fromRadioQueue.dequeuePtr(0)) != NULL) { while ((mp = fromRadioQueue.dequeuePtr(0)) != NULL) {
handleReceived(mp); perhapsHandleReceived(mp);
} }
} }
@ -191,6 +191,12 @@ void Router::handleReceived(MeshPacket *p)
notifyPacketReceived.notifyObservers(p); notifyPacketReceived.notifyObservers(p);
} }
} }
}
void Router::perhapsHandleReceived(MeshPacket *p)
{
if (!shouldFilterReceived(p))
handleReceived(p);
packetPool.release(p); packetPool.release(p);
} }

Wyświetl plik

@ -67,14 +67,12 @@ class Router
virtual ErrorCode send(MeshPacket *p); virtual ErrorCode send(MeshPacket *p);
/** /**
* Called from loop() * Should this incoming filter be dropped?
* Handle any packet that is received by an interface on this node.
* Note: some packets may merely being passed through this node and will be forwarded elsewhere.
* *
* Note: this packet will never be called for messages sent/generated by this node. * Called immedately on receiption, before any further processing.
* Note: this method will free the provided packet. * @return true to abandon the packet
*/ */
virtual void handleReceived(MeshPacket *p); virtual bool shouldFilterReceived(const MeshPacket *p) { return false; }
/** /**
* Every (non duplicate) packet this node receives will be passed through this method. This allows subclasses to * Every (non duplicate) packet this node receives will be passed through this method. This allows subclasses to
@ -88,6 +86,27 @@ class Router
* @return true for success, false for corrupt packet. * @return true for success, false for corrupt packet.
*/ */
bool perhapsDecode(MeshPacket *p); bool perhapsDecode(MeshPacket *p);
private:
/**
* Called from loop()
* Handle any packet that is received by an interface on this node.
* Note: some packets may merely being passed through this node and will be forwarded elsewhere.
*
* Note: this packet will never be called for messages sent/generated by this node.
* Note: this method will free the provided packet.
*/
void perhapsHandleReceived(MeshPacket *p);
/**
* Called from perhapsHandleReceived() - allows subclass message delivery behavior.
* Handle any packet that is received by an interface on this node.
* Note: some packets may merely being passed through this node and will be forwarded elsewhere.
*
* Note: this packet will never be called for messages sent/generated by this node.
* Note: this method will free the provided packet.
*/
void handleReceived(MeshPacket *p);
}; };
extern Router &router; extern Router &router;