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 #include "NiceCluster.h" 00026 00027 // Constructor ***************************************************************** 00028 NiceCluster::NiceCluster() 00029 { 00030 leaderConfirmed = false; 00031 setLeader(TransportAddress::UNSPECIFIED_NODE); 00032 lastLT = 0.0; 00033 leaderHeartbeatsSent = 0; 00034 00035 WATCH_SET(cluster); 00036 WATCH(leader); 00037 WATCH(leaderConfirmed); 00038 00039 } // NICECluster 00040 00041 // Adds member to cluster ****************************************************** 00042 void NiceCluster::add( const TransportAddress& member ) 00043 { 00044 00045 cluster.insert(member); 00046 00047 } // add 00048 // 00049 // Clears all cluster contents ************************************************* 00050 void NiceCluster::clear() 00051 { 00052 00053 cluster.clear(); 00054 setLeader(TransportAddress::UNSPECIFIED_NODE); 00055 00056 lastLT = 0.0; 00057 00058 } // clear 00059 00060 // Check if cluster member ***************************************************** 00061 bool NiceCluster::contains( const TransportAddress& member ) 00062 { 00063 if (member.isUnspecified()) 00064 return false; 00065 00066 return cluster.find(member) != cluster.end(); 00067 } // contains 00068 // 00069 // Get cluster size ************************************************************ 00070 int NiceCluster::getSize() 00071 { 00072 00073 return cluster.size(); 00074 00075 } // getSize 00076 00077 // Get address of specific member ********************************************** 00078 const TransportAddress& NiceCluster::get( int i ) 00079 { 00080 00081 ConstClusterIterator it = cluster.begin(); 00082 00083 for (int j = 0; j < i; j++) 00084 it++; 00085 00086 return *it; 00087 00088 } // get 00089 00090 void NiceCluster::remove(const TransportAddress& member) 00091 { 00092 00093 cluster.erase(member); 00094 if (!leader.isUnspecified() && !member.isUnspecified() && leader == member) { 00095 setLeader(TransportAddress::UNSPECIFIED_NODE); 00096 } 00097 00098 } // remove 00099 00100 // set cluster leader ********************************************************** 00101 void NiceCluster::setLeader(const TransportAddress& leader) 00102 { 00103 00104 if (!leader.isUnspecified() && !contains(leader)) { 00105 throw cRuntimeError("Leader not member of cluster"); 00106 } 00107 this->leader = leader; 00108 00109 leaderConfirmed = false; 00110 00111 // //cout << "Leader set to " << leader << endl; 00112 00113 } // setLeader 00114 00115 // get current cluster leader ************************************************** 00116 const TransportAddress& NiceCluster::getLeader() 00117 { 00118 00119 return leader; 00120 00121 } // getLeader 00122 00123 std::set<TransportAddress>::const_iterator NiceCluster::begin() const 00124 { 00125 return cluster.begin(); 00126 } 00127 00128 std::set<TransportAddress>::const_iterator NiceCluster::end() const 00129 { 00130 return cluster.end(); 00131 } 00132 00133 simtime_t NiceCluster::getLastLT() 00134 { 00135 00136 return lastLT; 00137 00138 } 00139 00140 void NiceCluster::setLastLT() 00141 { 00142 00143 lastLT = simTime(); 00144 00145 } 00146 00147 bool NiceCluster::isLeaderConfirmed() 00148 { 00149 return leaderConfirmed; 00150 } 00151 00152 void NiceCluster::confirmLeader() 00153 { 00154 leaderConfirmed = true; 00155 } 00156 00157 int NiceCluster::getLeaderHeartbeatsSent() 00158 { 00159 return leaderHeartbeatsSent; 00160 } 00161 00162 void NiceCluster::setLeaderHeartbeatsSent(int heartbeats) 00163 { 00164 leaderHeartbeatsSent = heartbeats; 00165 } 00166