BootstrapList.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2008 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 
00024 #include <BaseApp.h>
00025 #include <BootstrapList.h>
00026 #include <GlobalNodeList.h>
00027 #include <BaseOverlay.h>
00028 #include <UnderlayConfiguratorAccess.h>
00029 #include <ZeroconfConnector.h>
00030 #include <CommonMessages_m.h>
00031 #include <hashWatch.h>
00032 
00033 #include <fstream>
00034 #include <iostream>
00035 
00036 using namespace std;
00037 
00038 Define_Module(BootstrapList);
00039 
00040 std::ostream& operator<<(std::ostream& os, const BootstrapNodeHandle* n)
00041 {
00042     if (n == NULL) {
00043         os << "<NULL>";
00044         return os;
00045     }
00046 
00047     if (n->isUnspecified()) {
00048         os << "<unspec>";
00049     } else {
00050         os << n->ip << ":" << n->port << " " << n->key
00051            << " last ping:" <<  n->getLastPing()
00052            << " prio:" << n->getNodePrio();
00053     }
00054 
00055     return os;
00056 };
00057 
00058 
00059 BootstrapList::BootstrapList()
00060 {
00061     zeroconfConnector = NULL;
00062     timerMsg = NULL;
00063 }
00064 
00065 
00066 BootstrapList::~BootstrapList()
00067 {
00068     cancelAndDelete(timerMsg);
00069 }
00070 
00071 
00072 void BootstrapList::initializeApp(int stage)
00073 {
00074     if (stage != MIN_STAGE_APP) {
00075         return;
00076     }
00077 
00078     mergeOverlayPartitions = par("mergeOverlayPartitions");
00079     maintainList = par("maintainList");
00080 
00081     if (getParentModule()->getParentModule()->getSubmodule("zeroconfConnector", 0)) {
00082         zeroconfConnector = check_and_cast<ZeroconfConnector *>(getParentModule()->
00083                 getParentModule()->getSubmodule("zeroconfConnector", 0));
00084         if (!zeroconfConnector->isEnabled()) {
00085             zeroconfConnector = NULL;
00086         }
00087     }
00088 
00089     // load local cache list only when using SingleHostUnderlayConfigurator
00090     if (zeroconfConnector) {
00091         ifstream nodeListFile("nodelist.dat", ios::in);
00092         if (!nodeListFile) {
00093             EV << "[BootstrapList::initializeApp() @ " << overlay->getThisNode().ip
00094                << " (" << overlay->getThisNode().key << ")]\n"
00095                << "    nodelist.dat not found"
00096                << endl;
00097         } else {
00098             char address[16];
00099             int port;
00100             string key;
00101             BootstrapNodeHandle *node;
00102 
00103             while (nodeListFile >> address >> port >> key) {
00104                 node = new BootstrapNodeHandle(OverlayKey(key, 16),
00105                                                IPvXAddress(address), port,
00106                                                CACHE);
00107                 if (node)
00108                     insertBootstrapCandidate(*node);
00109             }
00110             nodeListFile.close();
00111         }
00112     }
00113 
00114     WATCH_UNORDERED_MAP(bootstrapList);
00115 
00116     if (maintainList) {
00117         timerMsg = new cMessage("BootstrapListTimer");
00118         scheduleAt(simTime() + timerInterval, timerMsg);
00119     }
00120 }
00121 
00122 
00123 void BootstrapList::handleTimerEvent(cMessage *msg)
00124 {
00125     if (msg == timerMsg) {
00126         handleBootstrapListTimerExpired();
00127         scheduleAt(simTime() + timerInterval, msg);
00128     } else {
00129         throw cRuntimeError("BootstrapList::handleTimerEvent(): "
00130                              "Received unknown self-message!");
00131     }
00132 }
00133 
00134 void BootstrapList::handleBootstrapListTimerExpired()
00135 {
00136     simtime_t current = simTime();
00137     BootstrapNodeHandle *node;
00138     BootstrapNodeSet::iterator iter;
00139 
00140     for (iter = bootstrapList.begin(); iter != bootstrapList.end(); iter++) {
00141         node = iter->second;
00142         if (timerInterval <= SIMTIME_DBL(current - node->getLastPing())) {
00143             pingNode(*node);
00144         }
00145     }
00146 }
00147 
00148 
00149 void BootstrapList::handleRpcResponse(BaseResponseMessage* msg,
00150                                       cPolymorphic* context,
00151                                       int rpcId, simtime_t rtt)
00152 {
00153     RPC_SWITCH_START(msg)
00154     RPC_ON_RESPONSE(Lookup) {
00155         handleLookupResponse(_LookupResponse);
00156         EV << "[BootstrapList::handleRpcResponse() @ "
00157            << overlay->getThisNode().ip
00158            << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00159            << "    Lookup RPC Response received: id=" << rpcId << "\n"
00160            << "    msg=" << *_LookupResponse << " rtt=" << rtt
00161            << endl;
00162     }
00163     RPC_SWITCH_END( )
00164 }
00165 
00166 void BootstrapList::handleLookupResponse(LookupResponse* msg)
00167 {
00168     EV << "[BootstrapList::handleLookupResponse() @ "
00169        << overlay->getThisNode().ip
00170        << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00171        << "    Lookup response for key " << msg->getKey()<< " : ";
00172 
00173     if (msg->getIsValid()) {
00174         for (uint32_t i=0; i<msg->getSiblingsArraySize(); i++) {
00175             if (msg->getSiblings(i).key == msg->getKey()) {
00176                 EV << "Key " << msg->getSiblings(i)
00177                    << "belongs to a local node." << endl;
00178                 return;
00179             }
00180         }
00181 
00182         EV << "New partition discovered, going to join." << endl;
00183         BootstrapNodeSet::iterator iter;
00184         for (iter = bootstrapList.begin(); iter != bootstrapList.end(); iter++) {
00185             if (iter->second->key == msg->getKey())
00186                 overlay->joinForeignPartition(*iter->second);
00187         }
00188 
00189     } else {
00190         EV << "failed" << endl;
00191     }
00192 
00193     return;
00194 }
00195 
00196 
00197 void BootstrapList::pingResponse(PingResponse* pingResponse,
00198                                  cPolymorphic* context,
00199                                  int rpcId, simtime_t rtt)
00200 {
00201     NodeHandle &srcNode = pingResponse->getSrcNode();
00202     BootstrapNodeSet::iterator iter;
00203 
00204     EV << "[BootstrapList::pingResponse() @ " << overlay->getThisNode().ip
00205            << " (" << overlay->getThisNode().key << ")]\n"
00206            << "    Ping response from node " << srcNode
00207            << " with rtt " << rtt << endl;
00208 
00209     // update the lastPing field of the source node
00210     if (!srcNode.isUnspecified())
00211         if ((iter = bootstrapList.find(srcNode)) != bootstrapList.end())
00212             iter->second->setLastPing(simTime());
00213 
00214     return;
00215 }
00216 
00217 
00218 void BootstrapList::pingTimeout(PingCall* pingCall,
00219                                 const TransportAddress& dest,
00220                                 cPolymorphic* context,
00221                                 int rpcId)
00222 {
00223     EV << "[BootstrapList::pingTimeout() @ " << overlay->getThisNode().ip
00224        << " (" << overlay->getThisNode().key << ")]\n"
00225        << "    Ping timeout for node " << dest << endl;
00226     // destination does not answer, remove it from the list
00227     removeBootstrapCandidate(dest);
00228 }
00229 
00230 
00231 const TransportAddress BootstrapList::getBootstrapNode()
00232 {
00233     if (!maintainList) {
00234         return overlay->globalNodeList->getBootstrapNode();
00235     } else {
00236         const NodeHandle *bootstrapNode = &NodeHandle::UNSPECIFIED_NODE;
00237         int i, j = 0;
00238 
00239         // pick a random node from the list
00240         if (!bootstrapList.empty()) {
00241             i = intrand(bootstrapList.size());
00242             BootstrapNodeSet::iterator iter = bootstrapList.begin();
00243 
00244             while (j < i) {
00245                 iter++;
00246                 j++;
00247             }
00248 
00249             bootstrapNode = (NodeHandle *)iter->second;
00250         } else {
00251             // if the list empty, get a bootstrap node from GlobalNodeList
00252             if (!zeroconfConnector)
00253                 bootstrapNode = &overlay->globalNodeList->
00254                         getBootstrapNode(overlay->getThisNode());
00255         }
00256         return *bootstrapNode;
00257     }
00258 }
00259 
00260 
00261 void BootstrapList::locateBootstrapNode(const NodeHandle& node)
00262 {
00263     Enter_Method_Silent();
00264 
00265     if ((overlay->getState() == BaseOverlay::READY) && (!node.isUnspecified())
00266             && mergeOverlayPartitions) {
00267        LookupCall* call = new LookupCall();
00268        call->setBitLength(0);
00269        call->setKey(node.key);
00270        call->setNumSiblings(overlay->getMaxNumSiblings());
00271        sendInternalRpcCall(OVERLAY_COMP, call);
00272     }
00273 
00274     return;
00275 }
00276 
00277 
00278 bool BootstrapList::insertBootstrapCandidate(const NodeHandle& node,
00279                                              BootstrapNodePrioType prio)
00280 {
00281     Enter_Method_Silent();
00282 
00283     BootstrapNodeSet::iterator iter;
00284     BootstrapNodeHandle *bootstrapNode;
00285 
00286     // if node already in the list, update its lastPing and nodePrio fields
00287     if ((iter = bootstrapList.find(node)) != bootstrapList.end()) {
00288         iter->second->setLastPing(simTime());
00289         iter->second->setNodePrio(prio);
00290 
00291         return true;
00292     }
00293 
00294     if ((bootstrapNode = new BootstrapNodeHandle(node, prio))) {
00295         bootstrapNode->setLastPing(simTime());
00296         bootstrapList.insert(NodePair(*bootstrapNode, bootstrapNode));
00297     }
00298 
00299     locateBootstrapNode(node);
00300 
00301     return false;
00302 }
00303 
00304 
00305 bool BootstrapList::insertBootstrapCandidate(BootstrapNodeHandle& node)
00306 {
00307     Enter_Method_Silent();
00308 
00309     BootstrapNodeSet::iterator iter;
00310 
00311     node.setLastPing(simTime());
00312 
00313     // if node already in the list, update its lastPing and nodePrio fields
00314     if ((iter = bootstrapList.find(node)) != bootstrapList.end()) {
00315         iter->second->setLastPing(node.getLastPing());
00316         iter->second->setNodePrio(node.getNodePrio());
00317 
00318         return true;
00319     }
00320 
00321     bootstrapList.insert(NodePair(node, &node));
00322     locateBootstrapNode(node);
00323 
00324     return false;
00325 }
00326 
00327 
00328 void BootstrapList::removeBootstrapCandidate(const TransportAddress& addr)
00329 {
00330     BootstrapNodeSet::iterator iter;
00331     BootstrapNodeHandle* bootstrapNode;
00332 
00333     if (!addr.isUnspecified()) {
00334         if ((iter = bootstrapList.find(addr)) != bootstrapList.end()) {
00335             bootstrapNode = iter->second;
00336             bootstrapList.erase(iter);
00337             delete bootstrapNode;
00338         }
00339     }
00340 
00341     return;
00342 }
00343 
00344 
00345 void BootstrapList::removeBootstrapNode(const NodeHandle& node)
00346 {
00347     overlay->globalNodeList->removePeer(node);
00348     // at this point, we consider this node not being able to provide the
00349     // boot service anymore, therefore we have to revoke
00350     // the service via zeroconfConnector
00351     if (zeroconfConnector) {
00352         zeroconfConnector->revokeService();
00353     }
00354 }
00355 
00356 
00357 void BootstrapList::registerBootstrapNode(const NodeHandle& node)
00358 {
00359     globalNodeList->registerPeer(node);
00360 
00361     // at this point, we consider this node as booted and therefore have to
00362     // announce the boot service for it via zeroconfConnector
00363     if (zeroconfConnector) {
00364         zeroconfConnector->announceService(node);
00365     }
00366 }
00367 
00368 
00369 void BootstrapList::finishApp()
00370 {
00371     // dump the list of bootstrap nodes into the local cache only when
00372     // using SingleHostUnderlayConfigurator
00373     if (zeroconfConnector) {
00374         ofstream nodeListFile("nodelist.dat", ios::out|ios::trunc);
00375         if (!nodeListFile) {
00376             std::cerr << "nodelist.dat could not be opened" << endl;
00377         } else {
00378             for (BootstrapNodeSet::iterator iter = bootstrapList.begin();
00379                  iter != bootstrapList.end(); iter++) {
00380                 nodeListFile << iter->second->ip.str() << ' '
00381                              << iter->second->port << ' '
00382                              << iter->second->key.toString(16) << '\n';
00383             }
00384             nodeListFile.close();
00385         }
00386     }
00387 
00388     // delete all bootstrap nodes and clear the list
00389     for (BootstrapNodeSet::iterator iter = bootstrapList.begin();
00390          iter != bootstrapList.end(); iter++) {
00391         delete iter->second;
00392     }
00393 
00394     bootstrapList.clear();
00395 }

Generated on Wed Mar 25 16:32:48 2009 for OverSim by  doxygen 1.5.5