OverSim
Nice.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2009 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
25 #ifndef __NICE_H_
26 #define __NICE_H_
27 
28 namespace oversim
29 {
30 
31 class Nice;
32 
33 }
34 
35 #include <BaseOverlay.h>
36 #include "NiceCluster.h"
37 #include "NiceMessage_m.h"
38 #include "NicePeerInfo.h"
39 #include <hashWatch.h>
40 #include <vector>
41 #include "combination.h"
42 #include <algorithm>
43 
44 #include <fstream>
45 
46 namespace oversim
47 {
48 
59 class Nice : public BaseOverlay
60 {
61 
62  /* The maximum of supported layers, fix for easier handling of structures */
63  const static short maxLayers = 10;
64 
65  friend class NicePeerInfo;
66 
67 public:
68 
69  /* Define set of TransportAdresses and iterator */
70  typedef std::set<TransportAddress> TaSet;
71  typedef std::set<TransportAddress>::iterator TaSetIt;
72 
73  /* Constructor and Destructor */
74  Nice();
75  virtual ~Nice();
76 
77  /* See BaseOverlay.h */
78  virtual void initializeOverlay( int stage );
79 
80  virtual void handleTimerEvent(cMessage* msg);
81 
82  virtual void handleUDPMessage(BaseOverlayMessage* msg);
83 
84  virtual void handleAppMessage(cMessage* msg);
85 
86  virtual void finishOverlay();
87 
88 
89 protected:
90 
96  virtual void changeState(int toState);
97 
98  /* see BaseOverlay.h */
99  virtual void joinOverlay();
100 
101  virtual void handleNodeLeaveNotification();
102 
103 
104 private:
105 
106  int pimp;
107 
109 
111 
112  /* Periodic Self-Messages and their intervals */
113 
114  /* Timer for heartbeats by every member node */
115  cMessage* heartbeatTimer;
116  simtime_t heartbeatInterval;
117 
118  /* Timer structure maintenance */
119  cMessage* maintenanceTimer;
121 
122  /* Timer for query timeouts */
123  cMessage* queryTimer;
124  simtime_t queryInterval;
125 
126  /* Timer poll the Rendevouz Point */
127  cMessage* rpPollTimer;
129 
131 
132  /* For Visualization only */
134 
135  void updateVisualization();
136 
137  /* Double leader resolution */
138  simtime_t first_HB;
140  simtime_t second_HB;
142 
143  std::vector<std::pair<TransportAddress, simtime_t> > leaderHeartbeats;
144 
145  /* DEBUG */
153 
154  /* Cluster parameter k */
155  unsigned short k;
156 
157  /* Cluster Information */
159 
160  /* Holds the current layer we query, if we do */
163 
164  /* The address of the point which we polled as RendevouzPoint */
166 
167  /* Holds the query start time for RTT evaluation */
168  simtime_t query_start;
169 
170  /* The current queried node */
172 
173  /* distance to first queried node that answers */
174  simtime_t query_compare;
175 
176  /* Layer intended to join */
177  short targetLayer;
178 
179  /* Map for all peer infos */
180  std::map<TransportAddress, NicePeerInfo*> peerInfos;
181 
182  /* set holding temporary peered joiner nodes */
183  std::map<TransportAddress, simtime_t> tempPeers;
185 
190 
191  // Statistics
193 
195 
197 
199 
201 
203 
205 
206  int numJoins;
207 
209 
211 
213 
215 
217 
219 
220  /* Find highest layer in which node is leader */
221  int getHighestLeaderLayer();
222 
223  /* Find highest layer in which node resides. Returns -1 when node not yet joined. */
224  int getHighestLayer();
225 
226  /* Joining the hierarchy from scratch */
227  void BasicJoinLayer(short layer);
228 
229  /* Splits a cluster */
230  void ClusterSplit(int layer);
231 
232  /* Request sublayer information from a cluster leader */
233  void Query(const TransportAddress& node, short layer);
234 
235  /* NICE message handlers */
237 
239 
241 
243 
245 
246  void handleNiceJoinCluster(NiceMessage* joinMsg);
247 
248  void handleNiceJoineval(NiceMessage* msg);
249 
251 
253 
254  void handleNiceMulticast(NiceMulticastMessage* multicastMsg);
255 
257 
259 
260  void handleNicePingProbe(NiceMessage* msg);
261 
263 
264  void handleNicePollRp(NiceMessage* msg);
265 
267 
268  void handleNiceQuery(NiceMessage* queryMsg);
269 
270  void handleNiceQueryResponse(NiceMemberMessage* queryRspMsg);
271 
272  void handleNiceRemove(NiceMessage* msg);
273 
274 
275  /* Joins a cluster */
276  void JoinCluster(const TransportAddress& leader, short layer);
277 
278  /* Sends heartbeats to all clusters the node resides in */
279  void sendHeartbeats();
280 
285  void cleanPeers();
286 
291  bool splitNeeded();
292 
297  bool mergeNeeded();
298 
304 
305  /* Periodic maintenance of the protocol hierarchy */
306  void maintenance();
307 
308  /* Determines the max distance of a member in a cluster */
309  simtime_t getMaxDistance(TransportAddress member, const std::set<TransportAddress>& neighbors);
310  template <class ConstIter>
311  simtime_t getMaxDistance(TransportAddress member, ConstIter neighborsBegin, ConstIter neighborsEnd);
312 
313  /* Determines the mean distance of a cluster */
314  simtime_t getMeanDistance(std::set<TransportAddress> neighbors);
315 
316  /* Initiates a LeaderTransfer message */
317  void LeaderTransfer(int layer, TransportAddress leader, TaSet cluster, TransportAddress sc_leader, TaSet superCluster);
318 
319  /* Transfers leadership of whole cluster to given node */
320  void LeaderTransfer(int layer, TransportAddress leader);
321 
322  /* Removes node from cluster */
323  void Remove(int layer);
324 
325  /* Sends Heartbeat to specific member */
326  void sendHeartbeatTo(const TransportAddress& node, int layer);
327 
328  /* Sends a remove message to the given node at layer */
329  void sendRemoveTo(const TransportAddress& node, int layer);
330 
331  /* Merges two clusters */
332  void ClusterMerge(int layer);
333 
334  /* Sends a ClusterMerge request message */
335  void ClusterMergeRequest(const TransportAddress& node, int layer);
336 
337  /* Leaves structure gracefully */
338  void gracefulLeave(short bottomLayer);
339 
340  /* Determines the center of a cluster */
341  std::pair<TransportAddress, simtime_t> findCenter(TaSet cluster, bool allowRandom = false);
342  std::pair<TransportAddress, simtime_t> findCenter(std::vector<TransportAddress> cluster, bool allowRandom = false);
343  std::pair<TransportAddress, simtime_t> findCenter(const NiceCluster& cluster, bool allowRandom = false);
344  template <class ConstIter>
345  std::pair<TransportAddress, simtime_t> findCenter(ConstIter begin, ConstIter end, bool allowRandom = false);
346 
348 
349  void pollRP(int layer);
350 
351 };
352 
353 }; //namespace
354 
355 #endif
356