00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef params_h
00034 #define params_h
00035
00036
00037 #include <string>
00038 #include <iostream>
00039 #include <boost/lexical_cast.hpp>
00040 #include "massert.h"
00041 #include "mdeque.h"
00042 #include "m_map.h"
00043 #include "mset.h"
00044 #include "MaceTypes.h"
00045
00063
00064 namespace params {
00065 static const std::string MACE_LOCAL_ADDRESS = "MACE_LOCAL_ADDRESS";
00066 static const std::string MACE_PORT = "MACE_PORT";
00067 static const std::string MACE_BIND_LOCAL_ADDRESS = "MACE_BIND_LOCAL_ADDRESS";
00068 static const std::string MACE_TRANSPORT_DISABLE_TRANSLATION = "MACE_TRANSPORT_DISABLE_TRANSLATION";
00069 static const std::string MACE_ALL_HOSTS_REACHABLE = "MACE_ALL_HOSTS_REACHABLE";
00070 static const std::string MACE_CERT_FILE = "MACE_CERT_FILE";
00071 static const std::string MACE_PRIVATE_KEY_FILE = "MACE_PRIVATE_KEY_FILE";
00072 static const std::string MACE_CA_FILE = "MACE_CA_FILE";
00073 static const std::string MACE_NO_VERIFY_HOSTNAMES = "MACE_NO_VERIFY_HOSTNAMES";
00074 static const std::string MACE_PRINT_HOSTNAME_ONLY = "MACE_PRINT_HOSTNAME_ONLY";
00075 static const std::string MACE_LOG_AUTO_SELECTORS = "MACE_LOG_AUTO_SELECTORS";
00076 static const std::string MACE_LOG_TIMESTAMP_HUMAN = "MACE_LOG_TIMESTAMP_HUMAN";
00077 static const std::string MACE_LOG_LEVEL = "MACE_LOG_LEVEL";
00078 static const std::string MACE_LOG_AUTO_ALL = "MACE_LOG_AUTO_ALL";
00079 static const std::string MACE_LOG_FILE = "MACE_LOG_FILE";
00080 static const std::string MACE_LOG_PATH = "MACE_LOG_PATH";
00081 static const std::string MACE_LOG_PREFIX = "MACE_LOG_PREFIX";
00082 static const std::string MACE_LOG_APPEND = "MACE_LOG_APPEND";
00083 static const std::string MACE_LOG_DISABLE_WARNING = "MACE_LOG_DISABLE_WARNING";
00084 static const std::string MACE_LOG_AUTO_BINARY = "MACE_LOG_AUTO_BINARY";
00085 static const std::string MACE_LOG_SCOPED_TIMES = "MACE_LOG_SCOPED_TIMES";
00086 static const std::string MACE_LOAD_MONITOR_FREQUENCY_MS = "MACE_LOAD_MONITOR_FREQUENCY_MS";
00087 static const std::string MACE_SWAP_MONITOR_FREQUENCY_MS = "MACE_SWAP_MONITOR_FREQUENCY_MS";
00088 static const std::string MACE_VMRSSLIMIT_FREQUENCY_MS = "MACE_VMRSSLIMIT_FREQUENCY_MS";
00089 static const std::string MACE_TRACE_FILE = "MACE_TRACE_FILE";
00090 static const std::string MACE_BIN_LOG_FILE = "MACE_BIN_LOG_FILE";
00091 static const std::string MACE_BOOTSTRAP_PEERS = "MACE_BOOTSTRAP_PEERS";
00092 static const std::string MACE_AUTO_BOOTSTRAP_PEERS = "MACE_AUTO_BOOTSTRAP_PEERS";
00093 static const std::string MACE_ADDRESS_ALLOW_LOOPBACK = "MACE_ADDRESS_ALLOW_LOOPBACK";
00094 static const std::string MACE_WARN_LOOPBACK = "MACE_WARN_LOOPBACK";
00095 static const std::string MACE_PROPERTY_CHECK_SERVER= "MACE_PROPERTY_CHECK_SERVER";
00096
00097 typedef mace::map<std::string, std::string> StringMap;
00098
00100 class Params {
00101 public:
00102
00103 bool containsKey(const std::string& key) {
00104 return data.containsKey(key);
00105 }
00106
00107 void set(const std::string& key, const std::string& value) {
00108 data[key] = value;
00109 }
00110
00111 void setAppend(const std::string& key, const std::string& value) {
00112 data[key].append(value);
00113 }
00114
00115 void setAppendNewline(const std::string& key, const std::string& value) {
00116 if (data.containsKey(key)) {
00117 data[key].append("\n");
00118 }
00119 data[key].append(value);
00120 }
00121
00122 template <typename T>
00123 T get(const std::string& key) {
00124 if (watch) {
00125 touched.insert(key);
00126 }
00127 StringMap::const_iterator i = data.find(key);
00128 if (i == data.end()) {
00129 std::cerr << "no param defined for " << key << std::endl;
00130 ABORT("undefined param");
00131 }
00132 return boost::lexical_cast<T>(i->second);
00133 }
00134
00135 template <typename T>
00136 T get(const std::string& key, const T& def) {
00137 if (watch) {
00138 touched.insert(key);
00139 }
00140 StringMap::const_iterator i = data.find(key);
00141 if (i != data.end()) {
00142 return boost::lexical_cast<T>(i->second);
00143 }
00144 else {
00145 return def;
00146 }
00147 }
00148
00149 void erase(const std::string& key) {
00150 data.erase(key);
00151 }
00152
00153 int loadparams(int& argc, char**& argv, bool requireFile = false);
00154
00155 int loadfile(const char* filename, bool allRequired = false);
00156
00157 void addRequired(const std::string& name, const std::string& desc = "");
00158
00159 int print(FILE* ostream);
00160 int print(std::ostream& os);
00161 void log();
00162
00163 void getBootstrapPeers(const mace::MaceKey& laddr, NodeSet& peers, bool& autoBootstrap);
00164
00165 void printUnusedParams(FILE* ostream);
00166
00167 void watchParams(bool w) { watch = w; }
00168
00169 const StringMap& getParams() const {
00170 return data;
00171 }
00172
00173 void setParams(const StringMap& p) { data = p; }
00174
00175 Params();
00176
00177 static Params* Instance() {
00178 if (_inst == NULL) {
00179 _inst = new Params();
00180 }
00181 return _inst;
00182 }
00183
00184 private:
00185 static Params *_inst;
00186
00187 bool watch;
00188 StringMap data;
00189 StringMap required;
00190 typedef mace::set<std::string> StringSet;
00191 StringSet touched;
00192
00193 std::string trim(const std::string& s);
00194 void verifyRequired();
00195
00196 };
00197
00198 inline const StringMap& getParams() {
00199 return Params::Instance()->getParams();
00200 }
00201
00202 inline void setParams(const StringMap& p) {
00203 Params::Instance()->setParams(p);
00204 }
00205
00206 inline void watchParams(bool t) {
00207 Params::Instance()->watchParams(t);
00208 }
00209
00210 template <typename T> inline T get(const std::string& key) {
00211 return Params::Instance()->get<T>(key);
00212 }
00213
00214 template <typename T> inline T get(const std::string& key, const T& def) {
00215 return Params::Instance()->get<T>(key, def);
00216 }
00217
00218 inline bool containsKey(const std::string& key) {
00219 return Params::Instance()->containsKey(key);
00220 }
00221
00222 template <typename T> inline mace::deque<T> getList(const std::string& key) {
00223 mace::deque<T, mace::SoftState> r;
00224 if (containsKey(key)) {
00225 std::string s = get<std::string>(key);
00226 mace::deque<std::string, mace::SoftState> l;
00227 std::istringstream in(s);
00228 while (!in.eof()) {
00229 std::string n;
00230 in >> n;
00231 l.push_back(n);
00232 }
00233 for (size_t i = 0; i < l.size(); i++) {
00234 r.push_back(boost::lexical_cast<T>(l[i]));
00235 }
00236 }
00237 return r;
00238 }
00239
00240 template <> inline NodeSet get<NodeSet>(const std::string& key) {
00241 std::string s = get<std::string>(key);
00242 NodeSet ns;
00243 std::istringstream in(s);
00244 while (!in.eof()) {
00245 std::string n;
00246 in >> n;
00247 ns.insert(MaceKey(n));
00248 }
00249 return ns;
00250 }
00251
00252 template <> inline NodeSet get<NodeSet>(const std::string& key, const NodeSet& def) {
00253 if (!containsKey(key)) {
00254 return def;
00255 }
00256 return get<NodeSet>(key);
00257 }
00258
00259 inline void set(const std::string& key, const std::string& value) {
00260 Params::Instance()->set(key, value);
00261 }
00262
00263 inline void setAppend(const std::string& key, const std::string& value) {
00264 Params::Instance()->setAppend(key, value);
00265 }
00266
00267 inline void setAppendNewline(const std::string& key,
00268 const std::string& value) {
00269 Params::Instance()->setAppendNewline(key, value);
00270 }
00271
00272 inline void erase(const std::string& key) {
00273 Params::Instance()->erase(key);
00274 }
00275
00291 inline int loadparams(int& argc, char**& argv, bool requireFile = false) {
00292 return Params::Instance()->loadparams(argc, argv, requireFile);
00293 }
00304 inline int loadfile(const char* filename, bool allRequired = false) {
00305 return Params::Instance()->loadfile(filename, allRequired);
00306 }
00308 inline void addRequired(const std::string& name, const std::string& desc = "") {
00309 Params::Instance()->addRequired(name, desc);
00310 }
00312 inline int print(FILE* ostream) {
00313 return Params::Instance()->print(ostream);
00314 }
00316 inline int print(std::ostream& os) {
00317 return Params::Instance()->print(os);
00318 }
00319
00320 inline void log() {
00321 Params::Instance()->log();
00322 }
00333 inline void getBootstrapPeers(const mace::MaceKey& laddr, NodeSet& peers, bool& autoBootstrap) {
00334 Params::Instance()->getBootstrapPeers(laddr, peers, autoBootstrap);
00335 }
00336 inline void printUnusedParams(FILE* ostream) {
00337 Params::Instance()->printUnusedParams(ostream);
00338 }
00339
00340 }
00341
00342 #endif