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