source: source/services/dht/Dht.h@ 10673

Last change on this file since 10673 was 10653, checked in by Michael Tänzer, 12 years ago

Merge the ASIO branch back into trunk

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