00001
00006
00007 #include "headers.h"
00008
00009 const string Config::MAX_CONNECTIONS_TAG="MaxConnections";
00010 const string Config::MAX_HOSTS_TAG="MaxHosts";
00011 const string Config::CERTIFICATE_PATH_TAG="CertPath";
00012 const string Config::MIN_CONNECTIONS_TAG="MinConnections";
00013 const string Config::CPP_TAG="ConnectPropagationProbability";
00014 const string Config::DPP_TAG="DiscoveryPropagationProbability";
00015 const string Config::DRP_TAG="DiscoveryResponseProbability";
00016 const string Config::RW_TIMEOUT_TAG="ReadWriteTimeout";
00017 const string Config::DO_FAST_ROUTING_TAG="DoFastRouting";
00018 const string Config::AVOID_CENSORED_NODES_TAG="AvoidCensoredNodes";
00019
00020
00024 Config::Config()
00025 {
00026
00027 m_maxConnections = MAX_CONNECTIONS;
00028 m_minConnections = MIN_CONNECTIONS;
00029 m_maxHosts = MAX_HOST_LIST_SIZE;
00030 m_certPath = DEFAULT_OPENSSL_PATH;
00031 m_hostFile = DEFAULT_HOST_FILE;
00032 m_cpp = DEFAULT_CONNECTION_PROPAGATION_PROBABILITY;
00033 m_dpp = DEFAULT_DISCOVERY_PROPAGATION_PROBABILITY;
00034 m_drp = DEFAULT_DISCOVERY_RESPONSE_PROBABILITY;
00035 m_rwTimeout = 0;
00036 m_doFastRouting = false;
00037 m_avoidCensoredNodes = false;
00038
00039
00040 ConnectionInterface* connection = ConnectionInterface::createConnectionObject(DEFAULT_CONNECTION_TYPE);
00041 connection->setSocketAddress(&SocketAddress(IpAddress((unsigned long)INADDR_ANY), DEFAULT_PORT));
00042 Node* node = new Node();
00043 IpAddress publicIp(GlobalObjects::instance()->getInterfaces()->getPublicIp());
00044 node->setSocketAddress(&SocketAddress(publicIp, DEFAULT_PORT));
00045
00046 m_selfNode = new ConnectionTableEntry(connection, node);
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 }
00069
00070
00071 Config::~Config() {
00072 delete m_selfNode->getNode();
00073 delete m_selfNode;
00074 m_selfNode = NULL;
00075
00076
00077
00078
00079
00080
00081 }
00082
00083
00084 ConnectionTableEntry*
00085 Config::getSelf() {
00086 return m_selfNode;
00087 }
00088
00089
00090 int
00091 Config::getMaxConnections() {
00092 return m_maxConnections;
00093 }
00094
00095
00096 int
00097 Config::getMaxHosts() {
00098 return m_maxHosts;
00099 }
00100
00101
00102 string
00103 Config::getPeerListFileName() {
00104 return m_hostFile;
00105 }
00106
00107
00108 double
00109 Config::getConnectPropagationProbability() {
00110 return m_cpp;
00111 }
00112
00113
00114 double
00115 Config::getDiscoveryPropagationProbability() {
00116 return m_dpp;
00117 }
00118
00119
00120 double
00121 Config::getDiscoveryResponseProbability() {
00122 return m_drp;
00123 }
00124
00125
00126 bool
00127 Config::doFastRouting() {
00128 return m_doFastRouting;
00129 }
00130
00131
00132 bool
00133 Config::avoidCensoredNodes() {
00134 return m_avoidCensoredNodes;
00135 }
00136
00137
00142 int
00143 Config::load() {
00144 return load(DEFAULT_CONFIG_FILE);
00145 }
00146
00147
00152 int
00153 Config::load(string path)
00154 {
00155 ifstream fp(path.c_str());
00156 if (!fp.is_open())
00157 {
00158 debug(DEBUG_CONFIG, "Error opening config file \"%s\"", path);
00159 return 0;
00160 }
00161
00162 this->load(fp);
00163
00164 fp.close();
00165
00166 return 1;
00167 }
00168
00169
00173 bool
00174 Config::matchTag(string buf, string tag) {
00175 return (buf.substr(0, tag.length()) == tag);
00176 }
00177
00178
00179 bool
00180 Config::readBool(string buf, string tag) {
00181 return stringToBool(buf.substr(tag.length() + 1));
00182 }
00183
00184
00185 int
00186 Config::readInt(string buf, string tag) {
00187 return atoi(buf.substr(tag.length() + 1).c_str());
00188 }
00189
00190
00191 double
00192 Config::readDouble(string buf, string tag) {
00193 return atof((buf.substr(tag.length() + 1)).c_str());
00194 }
00195
00196
00197 string
00198 Config::readString(string buf, string tag) {
00199 return buf.substr(tag.length() + 1);
00200 }
00201
00202
00206 void
00207 Config::load(std::istream& in) {
00208 string buf;
00209 getline(in, buf, '\n');
00210
00211 while(!in.eof())
00212 {
00213 if (buf.find(Node::beginTag) != string::npos) {
00214 in >> *(m_selfNode->getNode());
00215 }
00216
00217 if (matchTag(buf, MAX_CONNECTIONS_TAG)) {
00218 m_maxConnections = atoi((buf.substr(MAX_CONNECTIONS_TAG.length() + 1)).c_str());
00219 }
00220 if (matchTag(buf, MAX_HOSTS_TAG)) {
00221 m_maxHosts = readInt(buf, MAX_HOSTS_TAG);
00222 }
00223 if (matchTag(buf, CERTIFICATE_PATH_TAG)) {
00224 m_certPath = readString(buf, CERTIFICATE_PATH_TAG);
00225 }
00226 if (matchTag(buf, MIN_CONNECTIONS_TAG)) {
00227 m_minConnections = readInt(buf, MIN_CONNECTIONS_TAG);
00228 }
00229 if (matchTag(buf, CPP_TAG)) {
00230 m_cpp = readDouble(buf, CPP_TAG);
00231 }
00232 if (matchTag(buf, DPP_TAG)) {
00233 m_dpp = readDouble(buf, DPP_TAG);
00234 }
00235 if (matchTag(buf, DRP_TAG)) {
00236 m_drp = readDouble(buf, DRP_TAG);
00237 }
00238 if (matchTag(buf, RW_TIMEOUT_TAG)) {
00239 m_rwTimeout = readInt(buf, RW_TIMEOUT_TAG);
00240 }
00241 if (matchTag(buf, DO_FAST_ROUTING_TAG)) {
00242 m_doFastRouting = readBool(buf, DO_FAST_ROUTING_TAG);
00243 }
00244
00245 getline(in, buf, '\n');
00246 }
00247 }
00248
00249
00253 int
00254 Config::save() {
00255 return save(DEFAULT_CONFIG_FILE);
00256 }
00257
00258
00263 int
00264 Config::save(string path)
00265 {
00266 ofstream fp(path.c_str());
00267
00268 if (!fp.is_open())
00269 {
00270 debug(DEBUG_CONFIG, "Error opening config file \"%s\", going with defaults", path);
00271 return 0;
00272 }
00273 else {
00274
00275 this->save(fp);
00276 }
00277 fp.close();
00278 debug(DEBUG_CONFIG, "config file written");
00279 return 1;
00280 }
00281
00282
00286 void
00287 Config::save(std::ostream& out) {
00288 out << *(m_selfNode->getNode());
00289 out << MAX_CONNECTIONS_TAG << DELIM << m_maxConnections << "\n";
00290 out << MAX_HOSTS_TAG << DELIM << m_maxHosts << "\n";
00291 out << CERTIFICATE_PATH_TAG << DELIM << m_certPath << "\n";
00292 out << MIN_CONNECTIONS_TAG << DELIM << m_minConnections << "\n";
00293 out << CPP_TAG << DELIM << m_cpp << "\n";
00294 out << DPP_TAG << DELIM << m_dpp << "\n";
00295 out << DRP_TAG << DELIM << m_drp << "\n";
00296 out << RW_TIMEOUT_TAG << DELIM << m_rwTimeout << "\n";
00297 out << DO_FAST_ROUTING_TAG << DELIM << boolToString(m_doFastRouting) << "\n";
00298 out << AVOID_CENSORED_NODES_TAG << DELIM << boolToString(m_avoidCensoredNodes) << "\n";
00299 }
00300
00301
00302 void
00303 Config::setValue(string category, string var, string val) {
00304 m_categoryList[category].m_variableList[var] = val;
00305 }
00306
00307
00311 void
00312 Config::setValue(string var, string val) {
00313 setValue("Global", var, val);
00314 }
00315
00316
00317 void
00318 Config::setValue(string var, bool val) {
00319 string tmp = boolToString(val);
00320 setValue(var, tmp);
00321 }
00322
00323
00324 void
00325 Config::setValue(string var, double val) {
00326 stringstream tmp;
00327 tmp << val;
00328 setValue(var, tmp.str());
00329 }
00330
00331
00332 void
00333 Config::setValue(string var, int val) {
00334 stringstream tmp;
00335 tmp << val;
00336 setValue(var, tmp.str());
00337 }
00338
00339
00340 string
00341 Config::getString(string category, string var) {
00342 map<string, VariableList>::iterator theCategory;
00343
00344
00345 theCategory = m_categoryList.find(category);
00346
00347
00348 if (theCategory != m_categoryList.end()) {
00349
00350
00351 map<string, string>::iterator variable;
00352
00353 variable = theCategory->second.m_variableList.find(var);
00354
00355 if (variable != theCategory->second.m_variableList.end()) {
00356 return theCategory->second.m_variableList[var];
00357 }
00358 }
00359 return "";
00360 }
00361
00362
00363 string
00364 Config::getString(string var) {
00365 return getString("Global", var);
00366 }
00367
00368
00369 bool
00370 Config::getBool(string var) {
00371 string tmp = getString(var);
00372 return stringToBool(tmp);
00373 }
00374
00375
00376 int
00377 Config::getInt(string var) {
00378 string tmp = getString(var);
00379 return atoi(tmp.c_str());
00380 }
00381
00382
00383 double
00384 Config::getDouble(string var) {
00385 string tmp = getString(var);
00386 return atof(tmp.c_str());
00387 }
00388
00389
00394 bool
00395 Config::set(string var, string val)
00396 {
00397 stringstream value(val);
00398
00399 if (var == MAX_HOSTS_TAG) {
00400 value >> m_maxHosts;
00401 }
00402 else if (var == MAX_CONNECTIONS_TAG) {
00403 value >> m_maxConnections;
00404 }
00405 else if (var == MIN_CONNECTIONS_TAG) {
00406 value >> m_minConnections;
00407 }
00408 else if (var == CERTIFICATE_PATH_TAG) {
00409 struct stat sb;
00410 if (stat(val.c_str(), &sb) < 0) {
00411 return false;
00412 }
00413 m_certPath = val;
00414 }
00415 else if (var == CPP_TAG) {
00416 value >> m_cpp;
00417 }
00418 else if (var == DPP_TAG) {
00419 value >> m_dpp;
00420 }
00421 else if (var == DRP_TAG) {
00422 value >> m_drp;
00423 }
00424 else if (var == RW_TIMEOUT_TAG) {
00425 value >> m_rwTimeout;
00426 }
00427 else if (var == DO_FAST_ROUTING_TAG) {
00428 m_doFastRouting = stringToBool(val);
00429 }
00430 else if (var == AVOID_CENSORED_NODES_TAG) {
00431 m_avoidCensoredNodes = stringToBool(val);
00432 }
00433 else {
00434 return false;
00435 }
00436
00437 return true;
00438 }
00439
00440
00444 void
00445 Config::getHelpForVariables(std::ostream& out) {
00446 out <<" " << MAX_CONNECTIONS_TAG << " <number> (warning: restart after changing this)\n";
00447 out <<" " << MAX_HOSTS_TAG << " <number> (warning: restart after changing this)\n";
00448 out <<" " << MIN_CONNECTIONS_TAG << " <number>\n";
00449 out <<" " << CPP_TAG << " <percentage as a decimal>\n";
00450 out <<" " << DPP_TAG << " <percentage as a decimal>\n";
00451 out <<" " << DRP_TAG << " <percentage as a decimal>\n";
00452 out <<" " << CERTIFICATE_PATH_TAG << " <pathname> (needs a restart)\n";
00453 }
00454
00455
00459 void
00460 Config::toStream(std::ostream& out) {
00461 Node* selfNode = getSelf()->getNode();
00462 out << "****************************** CONFIGURATION *********************************\n";
00463 out << "Configuration file name: " << DEFAULT_CONFIG_FILE << "\n";
00464 out << "Host file name: " << getPeerListFileName() << "\n";
00465 #ifdef USE_SSL
00466 out << "SSL is enabled\n";
00467 #else
00468 out << "SSL is disabled\n";
00469 #endif
00470 save(out);
00471
00472 out << "\n";
00473 }
00474
00475