Explorar el Código

* added getTransmitDelay(), applied to Flood mode retransmissions.

Scott Powell hace 1 año
padre
commit
8983584dd8
Se han modificado 4 ficheros con 20 adiciones y 5 borrados
  1. 1 1
      examples/simple_repeater/main.cpp
  2. 9 3
      src/Mesh.cpp
  3. 6 1
      src/Mesh.h
  4. 4 0
      src/Utils.h

+ 1 - 1
examples/simple_repeater/main.cpp

@@ -144,7 +144,7 @@ protected:
     return airtime_factor;
   }
 
-  bool allowPacketForward(mesh::Packet* packet) override {
+  bool allowPacketForward(const mesh::Packet* packet) override {
     uint8_t hash[MAX_HASH_SIZE];
     packet->calculatePacketHash(hash);
 

+ 9 - 3
src/Mesh.cpp

@@ -11,9 +11,14 @@ void Mesh::loop() {
   Dispatcher::loop();
 }
 
-bool Mesh::allowPacketForward(mesh::Packet* packet) { 
+bool Mesh::allowPacketForward(const mesh::Packet* packet) { 
   return false;  // by default, Transport NOT enabled
 }
+uint32_t Mesh::getRetransmitDelay(const mesh::Packet* packet) { 
+  uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * 52 / 50) / 2;
+
+  return _rng->nextInt(0, 5)*t;
+}
 
 int Mesh::searchPeersByHash(const uint8_t* hash) {
   return 0;  // not found
@@ -34,7 +39,7 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) {
       // remove our hash from 'path', then re-broadcast
       pkt->path_len -= PATH_HASH_SIZE;
       memcpy(pkt->path, &pkt->path[PATH_HASH_SIZE], pkt->path_len);
-      return ACTION_RETRANSMIT(0);   // Routed traffic is HIGHEST priority
+      return ACTION_RETRANSMIT(0);   // Routed traffic is HIGHEST priority (and NO per-hop delay)
     }
     return ACTION_RELEASE;   // this node is NOT the next hop (OR this packet has already been forwarded), so discard.
   }
@@ -204,8 +209,9 @@ DispatcherAction Mesh::routeRecvPacket(Packet* packet) {
     // append this node's hash to 'path'
     packet->path_len += self_id.copyHashTo(&packet->path[packet->path_len]);
 
+    uint32_t d = getRetransmitDelay(packet);
     // as this propagates outwards, give it lower and lower priority
-    return ACTION_RETRANSMIT(packet->path_len);   // give priority to closer sources, than ones further away
+    return ACTION_RETRANSMIT_DELAYED(packet->path_len, d);   // give priority to closer sources, than ones further away
   }
   return ACTION_RELEASE;
 }

+ 6 - 1
src/Mesh.h

@@ -46,7 +46,12 @@ protected:
    * \brief  Check whether this packet should be forwarded (re-transmitted) or not.
    *     Is sub-classes responsibility to make sure given packet is only transmitted ONCE (by this node)
    */
-  virtual bool allowPacketForward(Packet* packet);
+  virtual bool allowPacketForward(const Packet* packet);
+
+  /**
+   * \returns  number of milliseconds delay to apply to retransmitting the given packet.
+   */
+  virtual uint32_t getRetransmitDelay(const Packet* packet);
 
   /**
    * \brief  Perform search of local DB of peers/contacts.

+ 4 - 0
src/Utils.h

@@ -9,6 +9,10 @@ namespace mesh {
 class RNG {
 public:
   virtual void random(uint8_t* dest, size_t sz) = 0;
+
+  /**
+   * \returns  random number between _min (inclusive) and _max (exclusive)
+   */
   uint32_t nextInt(uint32_t _min, uint32_t _max);
 };