Skip to content
Snippets Groups Projects
Commit 51a4c176 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

DhtMessage : introduce Service and ServiceFilter

parent a5f82063
No related branches found
No related tags found
No related merge requests found
...@@ -186,7 +186,7 @@ struct Value : public Serializable ...@@ -186,7 +186,7 @@ struct Value : public Serializable
}; };
} }
static Filter chainFilters(Filter& f1, Filter& f2) { static Filter chainFilters(Filter&& f1, Filter&& f2) {
return [f1,f2](const Value& v){ return [f1,f2](const Value& v){
return f1(v) && f2(v); return f1(v) && f2(v);
}; };
...@@ -364,26 +364,55 @@ private: ...@@ -364,26 +364,55 @@ private:
sockaddr_storage ss; sockaddr_storage ss;
}; };
struct DhtMessage : public Serializable struct DhtMessage : public Serializable
{ {
DhtMessage(uint16_t s, Blob msg = {}) : service(s), message(msg) {} enum class Service : uint16_t {
UNDEFINED = 0,
IM_MESSAGE,
ICE_CANDIDATES
};
DhtMessage(uint16_t s, Blob msg = {}) : service(static_cast<Service>(s)), message(msg) {}
DhtMessage(Service s, Blob msg = {}) : service(s), message(msg) {}
DhtMessage(const Blob& b) { DhtMessage(const Blob& b) {
unpackBlob(b); unpackBlob(b);
} }
Service getService() const {
return service;
}
const Blob& getMessage() const {
return message;
}
virtual void pack(Blob& res) const; virtual void pack(Blob& res) const;
virtual void unpack(Blob::const_iterator& begin, Blob::const_iterator& end); virtual void unpack(Blob::const_iterator& begin, Blob::const_iterator& end);
static const ValueType TYPE; static const ValueType TYPE;
static bool storePolicy(InfoHash, std::shared_ptr<Value>&, InfoHash, const sockaddr*, socklen_t); static bool storePolicy(InfoHash, std::shared_ptr<Value>&, InfoHash, const sockaddr*, socklen_t);
static Value::Filter ServiceFilter(Service s) {
return Value::chainFilters(
Value::TypeFilter(TYPE),
[s](const Value& v) {
try {
auto b = v.data.cbegin(), e = v.data.cend();
auto service = deserialize<Service>(b, e);
return service == s;
} catch (const std::exception& e) {
return false;
}
}
);
}
/** print value for debugging */ /** print value for debugging */
friend std::ostream& operator<< (std::ostream&, const DhtMessage&); friend std::ostream& operator<< (std::ostream&, const DhtMessage&);
private: private:
uint16_t service; Service service;
Blob message; Blob message;
}; };
......
...@@ -227,14 +227,14 @@ std::ostream& operator<< (std::ostream& s, const DhtMessage& v) ...@@ -227,14 +227,14 @@ std::ostream& operator<< (std::ostream& s, const DhtMessage& v)
void void
DhtMessage::pack(Blob& res) const DhtMessage::pack(Blob& res) const
{ {
serialize<int16_t>(service, res); serialize<Service>(service, res);
serialize<Blob>(message, res); serialize<Blob>(message, res);
} }
void void
DhtMessage::unpack(Blob::const_iterator& begin, Blob::const_iterator& end) DhtMessage::unpack(Blob::const_iterator& begin, Blob::const_iterator& end)
{ {
service = deserialize<int16_t>(begin, end); service = deserialize<Service>(begin, end);
message = deserialize<Blob>(begin, end); message = deserialize<Blob>(begin, end);
} }
...@@ -242,7 +242,7 @@ bool ...@@ -242,7 +242,7 @@ bool
DhtMessage::storePolicy(InfoHash, std::shared_ptr<Value>& v, InfoHash, const sockaddr* from, socklen_t fromlen) DhtMessage::storePolicy(InfoHash, std::shared_ptr<Value>& v, InfoHash, const sockaddr* from, socklen_t fromlen)
{ {
DhtMessage request {v->data}; DhtMessage request {v->data};
if (request.service == 0) if (request.service == Service::UNDEFINED)
return false; return false;
return true; return true;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment