00001 /* 00002 * $Id: ConfigException.cpp 61 2008-01-14 17:38:19Z phbaer $ 00003 * 00004 * Copyright 2008 Carpe Noctem, Distributed Systems Group, 00005 * University of Kassel. All right reserved. 00006 * 00007 * The code is licensed under the Carpe Noctem Userfriendly BSD-Based 00008 * License (CNUBBL). Redistribution and use in source and binary forms, 00009 * with or without modification, are permitted provided that the 00010 * conditions of the CNUBBL are met. 00011 * 00012 * You should have received a copy of the CNUBBL along with this 00013 * software. The license is also available on our website: 00014 * http://carpenoctem.das-lab.net/license.txt 00015 */ 00016 00017 #include <stdlib.h> 00018 #include <unistd.h> 00019 #include <iostream> 00020 #include <fstream> 00021 00022 #include <boost/thread.hpp> 00023 00024 #include "asio.hpp" 00025 00026 #include "SystemConfig.h" 00027 00028 namespace castor { 00029 00030 // Initialize static variables 00031 bool SystemConfig::initialized = false; 00032 00033 boost::mutex SystemConfig::mutex; 00034 00035 SystemConfigPtr SystemConfig::instance; 00036 00037 boost::filesystem::path SystemConfig::rootPath; 00038 boost::filesystem::path SystemConfig::libPath; 00039 boost::filesystem::path SystemConfig::logPath; 00040 boost::filesystem::path SystemConfig::configPath; 00041 int SystemConfig::ownRobotID; 00042 00043 std::string SystemConfig::hostname; 00044 00045 std::map<std::string, boost::shared_ptr<Configuration> > SystemConfig::configs; 00046 00047 SystemConfigPtr SystemConfig::getInstance() { 00048 00049 boost::mutex::scoped_lock lock(mutex); 00050 00051 if (!initialized) { 00052 00053 instance = boost::shared_ptr<SystemConfig>(new SystemConfig()); 00054 00055 char *x = ::getenv("ES_ROOT"); 00056 00057 if (x == NULL) { 00058 00059 char cwd[PATH_MAX]; 00060 if (::getcwd(cwd, PATH_MAX) == NULL) { 00061 std::cout << "SystemConfig: Error while calling getcwd!" << std::endl; 00062 } 00063 00064 rootPath = boost::filesystem::path(cwd).normalize(); 00065 00066 } else { 00067 rootPath = boost::filesystem::path(x).normalize(); 00068 } 00069 00070 x = ::getenv("ES_CONFIG_ROOT"); 00071 00072 if (x == NULL) { 00073 configPath = (rootPath / "etc").normalize(); 00074 } else { 00075 configPath = boost::filesystem::path(x).normalize(); 00076 } 00077 00078 libPath = (rootPath / "lib").normalize(); 00079 logPath = (rootPath / "log").normalize(); 00080 char* envname = ::getenv("ROBOT"); 00081 if( (envname == NULL) || ((*envname)==0x0) ) { 00082 hostname = asio::ip::host_name(); 00083 } else { 00084 hostname = envname; 00085 } 00086 std::cout << "Root: " << rootPath << std::endl; 00087 std::cout << "ConfigRoot: " << configPath << std::endl; 00088 std::cout << "LibRoot: " << libPath << std::endl; 00089 std::cout << "LogRoot: " << logPath << std::endl; 00090 std::cout << "Hostname: " << hostname << std::endl; 00091 00092 initialized = true; 00093 } 00094 00095 return instance; 00096 } 00097 00098 Configuration *SystemConfig::operator[] (const std::string s) { 00099 00100 { 00101 boost::mutex::scoped_lock lock(mutex); 00102 00103 std::map<std::string, boost::shared_ptr<Configuration> >::iterator itr = configs.find(s); 00104 00105 if (itr != configs.end()) { 00106 return itr->second.get(); 00107 } 00108 } 00109 00110 std::vector<boost::filesystem::path> files; 00111 00112 std::string file = s + ".conf"; 00113 00114 // Check the local config 00115 files.push_back(boost::filesystem::path(file)); 00116 00117 boost::filesystem::path path(configPath); 00118 path /= hostname; 00119 path /= file; 00120 00121 // Check the host-specific config 00122 files.push_back(path); 00123 00124 path = boost::filesystem::path(configPath); 00125 path /= file; 00126 00127 // Check the global config 00128 files.push_back(path); 00129 00130 for (size_t i = 0; i < files.size(); i++) { 00131 00132 if (boost::filesystem::exists(files[i])) { 00133 00134 boost::mutex::scoped_lock lock(mutex); 00135 00136 boost::shared_ptr<Configuration> result = boost::shared_ptr<Configuration>(new Configuration(files[i].string())); 00137 configs[s] = result; 00138 00139 return result.get(); 00140 } 00141 } 00142 00143 std::ostringstream ss; 00144 00145 ss << "Configuration file " << file << " not found in either location:" << std::endl; 00146 00147 for (size_t i = 0; i < files.size(); i++) { 00148 ss << "- " << files[i].string() << std::endl; 00149 } 00150 00151 throw ConfigException(ss.str()); 00152 } 00153 int SystemConfig::GetOwnRobotID() { 00154 if(ownRobotID!=0) return ownRobotID; 00155 Configuration *tmp = (*SystemConfig::getInstance())["Globals"]; 00156 ownRobotID = tmp->get<int>("Globals","Team",SystemConfig::getHostname().c_str(),"ID", NULL); 00157 delete tmp; 00158 return ownRobotID; 00159 } 00160 std::string SystemConfig::getRootPath() { 00161 return rootPath.string(); 00162 } 00163 00164 std::string SystemConfig::getLibPath() { 00165 return libPath.string(); 00166 } 00167 00168 std::string SystemConfig::getLogPath() { 00169 return logPath.string(); 00170 } 00171 00172 std::string SystemConfig::getConfigPath() { 00173 return configPath.string(); 00174 } 00175 00176 std::string SystemConfig::getHostname(){ 00177 00178 return hostname; 00179 } 00180 00181 std::string SystemConfig::robotNodeName(const std::string& nodeName) { 00182 return SystemConfig::getHostname() + NODE_NAME_SEPERATOR + nodeName; 00183 } 00184 /* 00185 void SystemConfig::splitRobotNodeName(const std::string& name, std::string* robotName, std::string* nodeName) { 00186 int index = name.find_last_of(NODE_NAME_SEPERATOR); 00187 00188 robotName = new std::string(name.substr(0, index)); 00189 nodeName = new std::string(name.substr(index + 1)); 00190 }*/ 00191 };