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