From 51c35d8b54ca1f6f4c5a901d9f147a424c569bba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Sat, 14 Dec 2019 11:53:11 -0500
Subject: [PATCH] value: add priority

---
 include/opendht/value.h | 17 +++++++++++++++--
 src/value.cpp           |  9 +++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/include/opendht/value.h b/include/opendht/value.h
index 5677d459..b7452aa1 100644
--- a/include/opendht/value.h
+++ b/include/opendht/value.h
@@ -43,6 +43,7 @@
 
 namespace dht {
 
+static const std::string VALUE_KEY_PRIO("p");
 struct Value;
 struct Query;
 
@@ -408,7 +409,9 @@ struct OPENDHT_PUBLIC Value
 
     Value(Value&& o) noexcept
      : id(o.id), owner(std::move(o.owner)), recipient(o.recipient),
-     type(o.type), data(std::move(o.data)), user_type(std::move(o.user_type)), seq(o.seq), signature(std::move(o.signature)), cypher(std::move(o.cypher)) {}
+     type(o.type), data(std::move(o.data)), user_type(std::move(o.user_type)), seq(o.seq)
+     , signature(std::move(o.signature)), cypher(std::move(o.cypher))
+     , priority(o.priority) {}
 
     template <typename Type>
     Value(const Type& vs)
@@ -518,9 +521,12 @@ struct OPENDHT_PUBLIC Value
     template <typename Packer>
     void msgpack_pack(Packer& pk) const
     {
-        pk.pack_map(2);
+        pk.pack_map(2 + (priority?1:0));
         pk.pack(std::string("id"));  pk.pack(id);
         pk.pack(std::string("dat")); msgpack_pack_to_encrypt(pk);
+        if (priority) {
+            pk.pack(VALUE_KEY_PRIO);  pk.pack(priority);
+        }
     }
 
     template <typename Packer>
@@ -602,6 +608,13 @@ struct OPENDHT_PUBLIC Value
      */
     Blob cypher {};
 
+    /**
+     * Priority of this value (used for push notifications)
+     * 0 = high priority
+     * 1 = normal priority
+     */
+    unsigned priority {0};
+
 private:
     friend class SecureDht;
     /* Cache for crypto ops */
diff --git a/src/value.cpp b/src/value.cpp
index ed2a10d1..181b17d6 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -108,6 +108,10 @@ Value::msgpack_unpack(const msgpack::object& o)
         msgpack_unpack_body(*rdat);
     } else
         throw msgpack::type_error();
+
+    if (auto rprio = findMapValue(o, VALUE_KEY_PRIO)) {
+        priority = rprio->as<unsigned>();
+    }
 }
 
 void
@@ -195,6 +199,9 @@ Value::Value(const Json::Value& json)
     const auto& jutype = json["utype"];
     if (jutype.isString())
         user_type = jutype.asString();
+    const auto& jprio = json["prio"];
+    if (jprio.isIntegral())
+        priority = jprio.asUInt();
 }
 
 Json::Value
@@ -219,6 +226,8 @@ Value::toJson() const
         if (not user_type.empty())
             val["utype"] = user_type;
     }
+    if (priority)
+        val["prio"] = priority;
     return val;
 }
 
-- 
GitLab