| 1 | #ifndef DHTMESSAGE_H_
|
---|
| 2 | #define DHTMESSAGE_H_
|
---|
| 3 |
|
---|
| 4 | #include "ariba/utility/messages.h"
|
---|
| 5 | #include "ariba/utility/types/NodeID.h"
|
---|
| 6 | #include "ariba/utility/serialization.h"
|
---|
| 7 | #include "ariba/Name.h"
|
---|
| 8 |
|
---|
| 9 | namespace ariba_service {
|
---|
| 10 | namespace dht {
|
---|
| 11 |
|
---|
| 12 | using ariba::utility::Message;
|
---|
| 13 | using ariba::utility::NodeID;
|
---|
| 14 | using_serialization;
|
---|
| 15 |
|
---|
| 16 | class DhtMessage : public Message { VSERIALIZEABLE
|
---|
| 17 | public:
|
---|
| 18 | typedef enum {
|
---|
| 19 | DhtInvalid = 0,
|
---|
| 20 | DhtGet = 1,
|
---|
| 21 | DhtPut = 2,
|
---|
| 22 | DhtPutAndGet = 3,
|
---|
| 23 | DhtRemove = 4,
|
---|
| 24 | DhtRepublish = 5,
|
---|
| 25 | DhtAnswer = 8,
|
---|
| 26 | DhtReplica = 9
|
---|
| 27 | } DhtMessageType;
|
---|
| 28 |
|
---|
| 29 | DhtMessage();
|
---|
| 30 | DhtMessage( DhtMessageType type, const std::string& key, const NodeID& sourceNodeID = NodeID::UNSPECIFIED);
|
---|
| 31 | DhtMessage( DhtMessageType type, const std::string& key,
|
---|
| 32 | const std::string& value, uint16_t ttl = 0, const NodeID& sourceNodeID = NodeID::UNSPECIFIED );
|
---|
| 33 |
|
---|
| 34 | DhtMessage( DhtMessageType type, const std::string& key,
|
---|
| 35 | const vector<std::string>& values, uint16_t ttl = 0, const NodeID& sourceNodeID = NodeID::UNSPECIFIED );
|
---|
| 36 |
|
---|
| 37 | virtual ~DhtMessage();
|
---|
| 38 |
|
---|
| 39 | DhtMessageType getType() const {
|
---|
| 40 | return static_cast<DhtMessageType>(type);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | NodeID getHashedKey() const {
|
---|
| 44 | return ariba::Name(key).toNodeId();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | const std::string& getKey() const {
|
---|
| 48 | return key;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /// returns the first element of the key vector
|
---|
| 52 | const std::string& getValue() const {
|
---|
| 53 | return values.at(0);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /// return all values for the key
|
---|
| 57 | const vector<std::string>& getValues() const {
|
---|
| 58 | return values;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /// return all values for the key
|
---|
| 62 | vector<std::string>& getValues() {
|
---|
| 63 | return values;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | bool hasValues() const {
|
---|
| 67 | return values.size() != 0;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | uint16_t getTTL() const {
|
---|
| 71 | return ttl;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void setTTL( uint16_t ttl ) {
|
---|
| 75 | this->ttl = ttl;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void setReplace( bool replace ) {
|
---|
| 79 | this->replace = replace;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | NodeID getSourceNode() const {
|
---|
| 83 | return sourceNode;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | bool doReplace() const {
|
---|
| 87 | return replace;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | static string DhtMessageTypeToString(DhtMessageType type);
|
---|
| 91 |
|
---|
| 92 | protected:
|
---|
| 93 | uint8_t type;
|
---|
| 94 | uint16_t ttl;
|
---|
| 95 | bool replace;
|
---|
| 96 | std::string key;
|
---|
| 97 | vector<std::string> values;
|
---|
| 98 | NodeID sourceNode;
|
---|
| 99 |
|
---|
| 100 | private:
|
---|
| 101 | };
|
---|
| 102 |
|
---|
| 103 | }} // namespace ariba_service::dht
|
---|
| 104 |
|
---|
| 105 | sznBeginDefault( ariba_service::dht::DhtMessage, X ) {
|
---|
| 106 | X && type;
|
---|
| 107 |
|
---|
| 108 | // serialize tll
|
---|
| 109 | X && ttl;
|
---|
| 110 |
|
---|
| 111 | // key serialization
|
---|
| 112 | X && T(key);
|
---|
| 113 |
|
---|
| 114 | X && &sourceNode;
|
---|
| 115 |
|
---|
| 116 | // store number of values
|
---|
| 117 | uint16_t num_values = values.size();
|
---|
| 118 | X && num_values;
|
---|
| 119 |
|
---|
| 120 | // value serialization
|
---|
| 121 | for (size_t i=0; i<num_values; i++) {
|
---|
| 122 | if (X.isSerializer()) {
|
---|
| 123 | X && T(values[i]);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | if (X.isDeserializer()) {
|
---|
| 127 | std::string value;
|
---|
| 128 | X && T(value);
|
---|
| 129 | values.push_back(value);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | } sznEnd();
|
---|
| 133 |
|
---|
| 134 | #endif /* DHTMESSAGE_H_ */
|
---|