| 1 | /*
|
---|
| 2 | * Dht.h
|
---|
| 3 | *
|
---|
| 4 | * Created on: 20.06.2012
|
---|
| 5 | * Author: mario
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef DHT_H_
|
---|
| 9 | #define DHT_H_
|
---|
| 10 |
|
---|
| 11 | #include "ariba/ariba.h"
|
---|
| 12 | #include "ariba/utility/system/SystemQueue.h"
|
---|
| 13 | #include "ariba/utility/logging/Logging.h"
|
---|
| 14 | #include "DhtAnswerInterface.h"
|
---|
| 15 | #include <boost/date_time/posix_time/posix_time.hpp>
|
---|
| 16 | #include <boost/noncopyable.hpp>
|
---|
| 17 | #include <set>
|
---|
| 18 |
|
---|
| 19 | namespace ariba_service {
|
---|
| 20 | namespace dht {
|
---|
| 21 |
|
---|
| 22 | using ariba::utility::SystemQueue;
|
---|
| 23 | using ariba::utility::SystemEvent;
|
---|
| 24 | using ariba::utility::SystemEventType;
|
---|
| 25 | using ariba::utility::SystemEventListener;
|
---|
| 26 |
|
---|
| 27 | using ariba::utility::NodeID;
|
---|
| 28 | using ariba::utility::LinkID;
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | // Forward declarations to avoid adding messages/*.h to the public interface
|
---|
| 32 | class DhtMessage;
|
---|
| 33 |
|
---|
| 34 | #define MEET_REPUBLISH_INTERVAL 10
|
---|
| 35 | #define MEET_DHT_TTL 30
|
---|
| 36 | #define CLEANUP_INTERVAL (5 * 60)
|
---|
| 37 |
|
---|
| 38 | class Dht :
|
---|
| 39 | public ariba::CommunicationListener,
|
---|
| 40 | public ariba::utility::SystemEventListener,
|
---|
| 41 | public boost::noncopyable
|
---|
| 42 | {
|
---|
| 43 | use_logging_h(Dht)
|
---|
| 44 | public:
|
---|
| 45 | Dht(ariba::ServiceID serviceID, ariba::Node* node);
|
---|
| 46 | virtual ~Dht();
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Put the value into the DHT under the specified key
|
---|
| 50 | *
|
---|
| 51 | * @param key
|
---|
| 52 | * Key to put the value under
|
---|
| 53 | * @param value
|
---|
| 54 | * The value which is put
|
---|
| 55 | * @param ttl
|
---|
| 56 | * The lifetime of the entry in seconds. The value will be removed
|
---|
| 57 | * automatically when it expires
|
---|
| 58 | */
|
---|
| 59 | void put(
|
---|
| 60 | const std::string& key,
|
---|
| 61 | const std::string& value,
|
---|
| 62 | uint16_t ttl);
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * Get the values specified by the key
|
---|
| 66 | *
|
---|
| 67 | * @param key
|
---|
| 68 | * Key of the values which should be fetched
|
---|
| 69 | */
|
---|
| 70 | void get(const std::string& key);
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Put and get in one single operation
|
---|
| 74 | *
|
---|
| 75 | * @param key
|
---|
| 76 | * The key the value will be put under and retrieved from
|
---|
| 77 | * @param value
|
---|
| 78 | * The value is first put then all values for that key, including the
|
---|
| 79 | * one just inserted will be sent back
|
---|
| 80 | * @param ttl
|
---|
| 81 | * The lifetime of the entry in seconds. The value will be removed
|
---|
| 82 | * automatically when it expires
|
---|
| 83 | */
|
---|
| 84 | void atomic_put_and_get(
|
---|
| 85 | const std::string& key,
|
---|
| 86 | const std::string& value,
|
---|
| 87 | uint16_t ttl);
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * Periodically put and get the value
|
---|
| 91 | *
|
---|
| 92 | * @param key
|
---|
| 93 | * The key the value will be put under and retrieved from
|
---|
| 94 | * @param value
|
---|
| 95 | * The value that will be periodically put into the DHT. The value is
|
---|
| 96 | * first put then all values for that key, including the one just
|
---|
| 97 | * inserted will be sent back
|
---|
| 98 | * @param ttl
|
---|
| 99 | * How long should we try to put the value periodically (measured in
|
---|
| 100 | * seconds). 0 means putting the value until stop_meet() is called
|
---|
| 101 | */
|
---|
| 102 | void meet(
|
---|
| 103 | const std::string& key,
|
---|
| 104 | const std::string& value,
|
---|
| 105 | uint16_t ttl);
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Stop periodically pushing the value
|
---|
| 109 | */
|
---|
| 110 | void stop_meet(const std::string& key, const std::string& value);
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * Remove the value under the specified key
|
---|
| 114 | */
|
---|
| 115 | void remove(const std::string& key, const std::string& value);
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Register a listener which is called when an answer is received
|
---|
| 119 | */
|
---|
| 120 | bool add_listener(DhtAnswerInterface* new_listener);
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * Unregister a listener
|
---|
| 124 | *
|
---|
| 125 | * @returns true if the handler was successfully unregistered, false if the
|
---|
| 126 | * listener was not registered
|
---|
| 127 | */
|
---|
| 128 | bool remove_listener(DhtAnswerInterface* new_listener);
|
---|
| 129 |
|
---|
| 130 | protected:
|
---|
| 131 | /*** CommunicationListener interface ***/
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * Called when a message is incoming
|
---|
| 135 | * @param msg The data message that is received
|
---|
| 136 | * @param remote The remote node that sent the message
|
---|
| 137 | * @param lnk The link id of the link where the message is received
|
---|
| 138 | */
|
---|
| 139 | virtual void onMessage(const ariba::DataMessage& msg, const ariba::NodeID& source,
|
---|
| 140 | const ariba::LinkID& lnk = ariba::LinkID::UNSPECIFIED);
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | /*** SystemEventListener interface ***/
|
---|
| 144 | virtual void handleSystemEvent( const SystemEvent& event );
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | private:
|
---|
| 148 | class ValueEntry {
|
---|
| 149 | public:
|
---|
| 150 | ValueEntry(const std::string& value, uint16_t ttl = 0);
|
---|
| 151 |
|
---|
| 152 | void refresh();
|
---|
| 153 |
|
---|
| 154 | const std::string& get_value() const;
|
---|
| 155 |
|
---|
| 156 | uint16_t get_age() const;
|
---|
| 157 |
|
---|
| 158 | uint16_t get_ttl() const;
|
---|
| 159 | void set_ttl(uint16_t ttl);
|
---|
| 160 | bool is_ttl_elapsed() const;
|
---|
| 161 | uint16_t get_remaining_ttl() const;
|
---|
| 162 |
|
---|
| 163 | bool operator<(const ValueEntry& rhs) const;
|
---|
| 164 |
|
---|
| 165 | private:
|
---|
| 166 | uint16_t ttl;
|
---|
| 167 | boost::posix_time::ptime last_update;
|
---|
| 168 | std::string value;
|
---|
| 169 | };
|
---|
| 170 |
|
---|
| 171 | struct Key_Value
|
---|
| 172 | {
|
---|
| 173 | string key;
|
---|
| 174 | string value;
|
---|
| 175 | };
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | private:
|
---|
| 179 | void handle_dht_message(const DhtMessage& message, const NodeID& source);
|
---|
| 180 |
|
---|
| 181 | void answer_dht_request(const std::string& key, const NodeID& source);
|
---|
| 182 | void send_meet_message(const std::string& key, const std::string& value);
|
---|
| 183 | void meet_update_event(const std::string& key, const std::string& value);
|
---|
| 184 |
|
---|
| 185 | // just for debug purpose
|
---|
| 186 | void print_dht();
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | ariba::ServiceID serviceID;
|
---|
| 190 | ariba::Node* node;
|
---|
| 191 |
|
---|
| 192 | typedef std::map< std::string, std::vector<ValueEntry> > DhtTableType;
|
---|
| 193 | DhtTableType table;
|
---|
| 194 | DhtTableType meet_store;
|
---|
| 195 |
|
---|
| 196 | void insert_into_table(DhtTableType& table,
|
---|
| 197 | const std::string& key,
|
---|
| 198 | const vector<std::string>& values,
|
---|
| 199 | uint16_t ttl);
|
---|
| 200 | void remove_from_table(DhtTableType& table,
|
---|
| 201 | const std::string& key,
|
---|
| 202 | const vector<std::string>& values);
|
---|
| 203 | void cleanup_table(DhtTableType& table);
|
---|
| 204 | void cleanup_entries(DhtTableType::mapped_type& entries);
|
---|
| 205 |
|
---|
| 206 | void schedule_cleanup_event(bool reschedule = false);
|
---|
| 207 | bool cleanup_running;
|
---|
| 208 |
|
---|
| 209 | // AnswerListener
|
---|
| 210 | DhtAnswerInterface* listener;
|
---|
| 211 | };
|
---|
| 212 |
|
---|
| 213 |
|
---|
| 214 | }} /* namespace ariba_service::dht */
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | #endif /* DHT_H_ */
|
---|