Nice.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2009 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00025 #ifndef __NICE_H_
00026 #define __NICE_H_
00027 
00028 namespace oversim
00029 {
00030 
00031 class Nice;
00032 
00033 }
00034 
00035 #include <BaseOverlay.h>
00036 #include "NiceCluster.h"
00037 #include "NiceMessage_m.h"
00038 #include "NicePeerInfo.h"
00039 #include <hashWatch.h>
00040 #include <vector>
00041 #include "combination.h"
00042 #include <algorithm>
00043 
00044 #include <fstream>
00045 
00046 namespace oversim
00047 {
00048 
00059 class Nice : public BaseOverlay
00060 {
00061 
00062     /* The maximum of supported layers, fix for easier handling of structures */
00063     const static short maxLayers = 10;
00064 
00065     friend class NicePeerInfo;
00066 
00067 public:
00068 
00069     /* Define set of TransportAdresses and iterator */
00070     typedef std::set<TransportAddress> TaSet;
00071     typedef std::set<TransportAddress>::iterator TaSetIt;
00072 
00073     /* Constructor and Destructor */
00074     Nice();
00075     virtual ~Nice();
00076 
00077     /* See BaseOverlay.h */
00078     virtual void initializeOverlay( int stage );
00079 
00080     virtual void handleTimerEvent(cMessage* msg);
00081 
00082     virtual void handleUDPMessage(BaseOverlayMessage* msg);
00083 
00084     virtual void handleAppMessage(cMessage* msg);
00085 
00086     virtual void finishOverlay();
00087 
00088 
00089 protected:
00090 
00096     virtual void changeState(int toState);
00097 
00098     /* see BaseOverlay.h */
00099     virtual void joinOverlay();
00100 
00101     virtual void handleNodeLeaveNotification();
00102 
00103 
00104 private:
00105 
00106     int pimp;
00107 
00108     TransportAddress RendevouzPoint;
00109 
00110     bool isRendevouzPoint;
00111 
00112     /* Periodic Self-Messages and their intervals */
00113 
00114     /* Timer for heartbeats by every member node */
00115     cMessage* heartbeatTimer;
00116     simtime_t heartbeatInterval;
00117 
00118     /* Timer structure maintenance */
00119     cMessage* maintenanceTimer;
00120     simtime_t maintenanceInterval;
00121 
00122     /* Timer for query timeouts */
00123     cMessage* queryTimer;
00124     simtime_t queryInterval;
00125 
00126     /* Timer poll the Rendevouz Point */
00127     cMessage* rpPollTimer;
00128     simtime_t rpPollTimerInterval;
00129 
00130     double peerTimeoutHeartbeats;
00131 
00132     /* For Visualization only */
00133     cMessage* visualizationTimer;
00134 
00135     void updateVisualization();
00136 
00137     /* Double leader resolution */
00138     simtime_t first_HB;
00139     TransportAddress first_leader;
00140     simtime_t second_HB;
00141     TransportAddress second_leader;
00142 
00143     std::vector<std::pair<TransportAddress, simtime_t> > leaderHeartbeats;
00144 
00145     /* DEBUG */
00146     int clusterrefinement;
00147     int debug_heartbeats;
00148     int debug_visualization;
00149     int debug_join;
00150     int debug_peertimeouts;
00151     int debug_removes;
00152     int debug_queries;
00153 
00154     /* Cluster parameter k */
00155     unsigned short k;
00156 
00157     /* Cluster Information */
00158     NiceCluster clusters[maxLayers];
00159 
00160     /* Holds the current layer we query, if we do */
00161     int evalLayer;
00162     int joinLayer;
00163 
00164     /* The address of the point which we polled as RendevouzPoint */
00165     TransportAddress polledRendevouzPoint;
00166 
00167     /* Holds the query start time for RTT evaluation */
00168     simtime_t query_start;
00169 
00170     /* The current queried node */
00171     TransportAddress tempResolver;
00172 
00173     /* distance to first queried node that answers */
00174     simtime_t query_compare;
00175 
00176     /* Layer intended to join */
00177     short targetLayer;
00178 
00179     /* Map for all peer infos */
00180     std::map<TransportAddress, NicePeerInfo*> peerInfos;
00181 
00182     /* set holding temporary peered joiner nodes */
00183     std::map<TransportAddress, simtime_t> tempPeers;
00184     bool isTempPeered;
00185 
00186     double CLUSTERLEADERBOUND;
00187     double CLUSTERLEADERCOMPAREDIST;
00188     double SC_PROC_DISTANCE;
00189     double SC_MIN_OFFSET;
00190 
00191     // Statistics
00192     int numInconsistencies;
00193 
00194     int numQueryTimeouts;
00195 
00196     int numPeerTimeouts;
00197 
00198     int numTempPeerTimeouts;
00199 
00200     int numStructurePartitions;
00201 
00202     int numOwnMessagesReceived;
00203 
00204     double totalSCMinCompare;
00205 
00206     int numJoins;
00207 
00208     int numForward;
00209 
00210     int totalForwardBytes;
00211 
00212     int numReceived;
00213 
00214     int totalReceivedBytes;
00215 
00216     int numHeartbeat;
00217 
00218     int totalHeartbeatBytes;
00219 
00220     /* Find highest layer in which node is leader */
00221     int getHighestLeaderLayer();
00222 
00223     /* Find highest layer in which node resides. Returns -1 when node not yet joined. */
00224     int getHighestLayer();
00225 
00226     /* Joining the hierarchy from scratch */
00227     void BasicJoinLayer(short layer);
00228 
00229     /* Splits a cluster */
00230     void ClusterSplit(int layer);
00231 
00232     /* Request sublayer information from a cluster leader */
00233     void Query(const TransportAddress& node, short layer);
00234 
00235     /* NICE message handlers */
00236     void handleNiceClusterMergeRequest(NiceClusterMerge* mergeMsg);
00237 
00238     void handleNiceForceMerge(NiceMessage* msg);
00239 
00240     void handleNiceHeartbeat(NiceHeartbeat* msg);
00241 
00242     void handleNiceLeaderHeartbeat(NiceLeaderHeartbeat* lhbMsg);
00243 
00244     void handleNiceLeaderTransfer(NiceLeaderHeartbeat* transferMsg);
00245 
00246     void handleNiceJoinCluster(NiceMessage* joinMsg);
00247 
00248     void handleNiceJoineval(NiceMessage* msg);
00249 
00250     void handleNiceJoinevalResponse(NiceMessage* msg);
00251 
00252     void handleNiceLeaderHeartbeatOrTransfer(NiceMessage* msg);
00253 
00254     void handleNiceMulticast(NiceMulticastMessage* multicastMsg);
00255 
00256     void handleNicePeerTemporary(NiceMessage* msg);
00257 
00258     void handleNicePeerTemporaryRelease(NiceMessage* msg);
00259 
00260     void handleNicePingProbe(NiceMessage* msg);
00261 
00262     void handleNicePingProbeResponse(NiceMessage* msg);
00263 
00264     void handleNicePollRp(NiceMessage* msg);
00265 
00266     void handleNicePollRpResponse(NiceMessage* msg);
00267 
00268     void handleNiceQuery(NiceMessage* queryMsg);
00269 
00270     void handleNiceQueryResponse(NiceMemberMessage* queryRspMsg);
00271 
00272     void handleNiceRemove(NiceMessage* msg);
00273 
00274 
00275     /* Joins a cluster */
00276     void JoinCluster(const TransportAddress& leader, short layer);
00277 
00278     /* Sends heartbeats to all clusters the node resides in */
00279     void sendHeartbeats();
00280 
00285     void cleanPeers();
00286 
00291     bool splitNeeded();
00292 
00297     bool mergeNeeded();
00298 
00303     bool checkLeaderHeartbeatsForCollisions(NiceLeaderHeartbeat* hbMsg);
00304 
00305     /* Periodic maintenance of the protocol hierarchy */
00306     void maintenance();
00307 
00308     /* Determines the max distance of a member in a cluster */
00309     simtime_t getMaxDistance(TransportAddress member, const std::set<TransportAddress>& neighbors);
00310     template <class ConstIter>
00311     simtime_t getMaxDistance(TransportAddress member, ConstIter neighborsBegin, ConstIter neighborsEnd);
00312 
00313     /* Determines the mean distance of a cluster */
00314     simtime_t getMeanDistance(std::set<TransportAddress> neighbors);
00315 
00316     /* Initiates a LeaderTransfer message */
00317     void LeaderTransfer(int layer, TransportAddress leader, TaSet cluster, TransportAddress sc_leader, TaSet superCluster);
00318 
00319     /* Transfers leadership of whole cluster to given node */
00320     void LeaderTransfer(int layer, TransportAddress leader);
00321 
00322     /* Removes node from cluster */
00323     void Remove(int layer);
00324 
00325     /* Sends Heartbeat to specific member */
00326     void sendHeartbeatTo(const TransportAddress& node, int layer);
00327 
00328     /* Sends a remove message to the given node at layer */
00329     void sendRemoveTo(const TransportAddress& node, int layer);
00330 
00331     /* Merges two clusters */
00332     void ClusterMerge(int layer);
00333 
00334     /* Sends a ClusterMerge request message */
00335     void ClusterMergeRequest(const TransportAddress& node, int layer);
00336 
00337     /* Leaves structure gracefully */
00338     void gracefulLeave(short bottomLayer);
00339 
00340     /* Determines the center of a cluster */
00341     std::pair<TransportAddress, simtime_t> findCenter(TaSet cluster, bool allowRandom = false);
00342     std::pair<TransportAddress, simtime_t> findCenter(std::vector<TransportAddress> cluster, bool allowRandom = false);
00343     std::pair<TransportAddress, simtime_t> findCenter(const NiceCluster& cluster, bool allowRandom = false);
00344     template <class ConstIter>
00345     std::pair<TransportAddress, simtime_t> findCenter(ConstIter begin, ConstIter end, bool allowRandom = false);
00346 
00347     void sendDataToOverlay(NiceMulticastMessage *appMsg);
00348 
00349     void pollRP(int layer);
00350 
00351 };
00352 
00353 }; //namespace
00354 
00355 #endif
00356