TokenMapLoader.cpp
Go to the documentation of this file.
00001 #include "robot_instance/TokenMapLoader.h"
00002 
00003 using namespace std;
00004 using namespace log4cpp;
00005 
00006 void TokenMapLoader::loadElements(RobotInstancePtr instance, TokenMapPtr tokenMap)
00007 {
00008     FileMap::const_iterator itr;
00009 
00010     tokenMap->clear();
00011 
00012     if (not instance->masterTokenFile.empty())
00013     {
00014         Private::loadElementsFromFile(instance->configurationSafetyBasePath + RobotInstance::TOKEN_PATH + instance->masterTokenFile, "", tokenMap);
00015     }
00016 
00017     for (itr = instance->nodeTokenMap.begin(); itr != instance->nodeTokenMap.end(); ++itr)
00018     {
00019         //cout << "Key: " << (*itr).first << " Value: " << instance->configurationSafetyBasePath + RobotInstance::TOKEN_PATH << (*itr).second << endl;
00020         if (not (*itr).second.empty())
00021         {
00022             Private::loadElementsFromFile(instance->configurationSafetyBasePath + RobotInstance::TOKEN_PATH + (*itr).second, (*itr).first, tokenMap);
00023         }
00024     }
00025 }
00026 
00027 void TokenMapLoader::Private::loadElementsFromFile(const string& filename, const string& namePrefix, TokenMapPtr tokenMap)
00028 {
00029     TiXmlDocument file(filename.c_str());
00030     bool loadOkay = file.LoadFile();
00031 
00032     if (!loadOkay)
00033     {
00034         stringstream err;
00035         err << "Failed to load file [" << filename << "]";
00036         NasaCommonLogging::Logger::log("gov.nasa.robonet.TokenMapLoader", Priority::FATAL, err.str());
00037         throw runtime_error(err.str());
00038     }
00039 
00040     TiXmlHandle doc(&file);
00041     NasaCommonLogging::Logger::log("gov.nasa.robonet.TokenMapLoader", Priority::INFO, "TokenFile [" + filename + "] successfully loaded.");
00042 
00043     string elementString;
00044 
00045     // Check for TokenData
00046     if (!(doc.FirstChildElement("TokenData").ToElement()))
00047     {
00048         stringstream err;
00049         err << "The file " << filename << " has no element named [TokenData]";
00050         NasaCommonLogging::Logger::log("gov.nasa.robonet.TokenMapLoader", Priority::ERROR, err.str());
00051         throw runtime_error(err.str());
00052     }
00053 
00054     // Check for Channels
00055     TiXmlHandle tokensElement(doc.FirstChildElement("TokenData").FirstChildElement("Tokens"));
00056 
00057     if (tokensElement.ToElement())
00058     {
00059         TiXmlHandle currentTokenElement = 0;
00060 
00061         // Iterate over channels
00062         currentTokenElement = tokensElement.FirstChild("Token");
00063 
00064         while (currentTokenElement.ToElement())
00065         {
00066             elementString.clear();
00067             elementString << *(currentTokenElement.ToElement());
00068             elementString.append("\n");
00069 
00070             std::pair<TokenMap::iterator, bool> ret = tokenMap->insert(TokenFactory::fromXml(elementString, namePrefix));
00071 
00072             if (not ret.second)
00073             {
00074                 NasaCommonLogging::Logger::log("gov.nasa.robonet.TokenMapLoader", Priority::NOTICE, "Duplicate token added: " + ret.first->first);
00075             }
00076 
00077             currentTokenElement = currentTokenElement.ToElement()->NextSiblingElement("Token");
00078         }
00079     }
00080     else
00081     {
00082         stringstream err;
00083         err << "The file " << filename << " has no element named [Tokens]";
00084         NasaCommonLogging::Logger::log("gov.nasa.robonet.TokenMapLoader", Priority::ERROR, err.str());
00085         throw runtime_error(err.str());
00086     }
00087 }


robot_instance
Author(s):
autogenerated on Sat Jun 8 2019 20:43:12