00001 #include "config.h"
00002 #include "Configuration.h"
00003 #include "ibrdtn/utils/Utils.h"
00004 #include "core/Node.h"
00005 #include "ibrcommon/net/NetInterface.h"
00006 #include <ibrcommon/Logger.h>
00007
00008 using namespace dtn::net;
00009 using namespace dtn::core;
00010 using namespace dtn::utils;
00011 using namespace ibrcommon;
00012
00013 namespace dtn
00014 {
00015 namespace daemon
00016 {
00017 Configuration::NetConfig::NetConfig(std::string n, NetType t, const ibrcommon::NetInterface &i, int p, bool d)
00018 : name(n), type(t), interface(i), port(p), discovery(d)
00019 {
00020 }
00021
00022 Configuration::NetConfig::NetConfig(std::string n, NetType t, const std::string &a, int p, bool d)
00023 : name(n), type(t), interface("lo"), address(a), port(p), discovery(d)
00024 {
00025 }
00026
00027 Configuration::NetConfig::~NetConfig()
00028 {
00029 }
00030
00031 std::string Configuration::version()
00032 {
00033 std::stringstream ss;
00034 ss << PACKAGE_VERSION;
00035 #ifdef SVN_REVISION
00036 ss << " (build " << SVN_REVISION << ")";
00037 #endif
00038
00039 return ss.str();
00040 }
00041
00042 Configuration::Configuration()
00043 : _filename("config.ini"), _default_net("lo"), _use_default_net(false), _doapi(true), _dodiscovery(true), _debuglevel(0), _debug(false), _quiet(false)
00044 {
00045 }
00046
00047 Configuration::~Configuration()
00048 {}
00049
00050 Configuration& Configuration::getInstance()
00051 {
00052 static Configuration conf;
00053 return conf;
00054 }
00055
00056 void Configuration::params(int argc, char *argv[])
00057 {
00058 for (int i = 0; i < argc; i++)
00059 {
00060 std::string arg = argv[i];
00061
00062 if (arg == "-c" && argc > i)
00063 {
00064 _filename = argv[i + 1];
00065 }
00066
00067 if (arg == "-i" && argc > i)
00068 {
00069 _default_net = ibrcommon::NetInterface(argv[i + 1]);
00070 _use_default_net = true;
00071 }
00072
00073 if (arg == "--noapi")
00074 {
00075 _doapi = false;
00076 }
00077
00078 if ((arg == "--version") || (arg == "-v"))
00079 {
00080 std::cout << "IBR-DTN version: " << version() << std::endl;
00081 exit(0);
00082 }
00083
00084 if (arg == "--nodiscovery")
00085 {
00086 _dodiscovery = false;
00087 }
00088
00089 if (arg == "-d")
00090 {
00091 _debuglevel = atoi(argv[i + 1]);
00092 _debug = true;
00093 }
00094
00095 if (arg == "-q")
00096 {
00097 _quiet = true;
00098 }
00099 }
00100 }
00101
00102 void Configuration::load()
00103 {
00104 load(_filename);
00105 }
00106
00107 void Configuration::load(string filename)
00108 {
00109 try {
00110 _conf = ibrcommon::ConfigFile(filename);;
00111 IBRCOMMON_LOGGER(info) << "Configuration: " << filename << IBRCOMMON_LOGGER_ENDL;
00112 } catch (ibrcommon::ConfigFile::file_not_found ex) {
00113 IBRCOMMON_LOGGER(info) << "Using defaults. To use custom config file use parameter -c configfile." << IBRCOMMON_LOGGER_ENDL;
00114 _conf = ConfigFile();
00115 }
00116 }
00117
00118 bool Configuration::beQuiet() const
00119 {
00120 return _quiet;
00121 }
00122
00123 bool Configuration::doDebug() const
00124 {
00125 return _debug;
00126 }
00127
00128 int Configuration::getDebugLevel() const
00129 {
00130 return _debuglevel;
00131 }
00132
00133 string Configuration::getNodename()
00134 {
00135 try {
00136 return _conf.read<string>("local_uri");
00137 } catch (ibrcommon::ConfigFile::key_not_found ex) {
00138 char *hostname_array = new char[64];
00139 if ( gethostname(hostname_array, 64) != 0 )
00140 {
00141
00142 delete[] hostname_array;
00143 return "local";
00144 }
00145
00146 string hostname(hostname_array);
00147 delete[] hostname_array;
00148
00149 stringstream ss;
00150 ss << "dtn://" << hostname;
00151 ss >> hostname;
00152
00153 return hostname;
00154 }
00155 }
00156
00157 std::list<Configuration::NetConfig> Configuration::getInterfaces()
00158 {
00159 std::list<NetConfig> ret;
00160
00161 if (_use_default_net)
00162 {
00163 ret.push_back( Configuration::NetConfig("default", Configuration::NetConfig::NETWORK_TCP, _default_net, 4556) );
00164 return ret;
00165 }
00166
00167 try {
00168 vector<string> nets = dtn::utils::Utils::tokenize(" ", _conf.read<string>("net_interfaces") );
00169 for (vector<string>::const_iterator iter = nets.begin(); iter != nets.end(); iter++)
00170 {
00171 std::string netname = (*iter);
00172
00173 std::string key_type = "net_" + netname + "_type";
00174 std::string key_port = "net_" + netname + "_port";
00175 std::string key_interface = "net_" + netname + "_interface";
00176 std::string key_address = "net_" + netname + "_address";
00177 std::string key_discovery = "net_" + netname + "_discovery";
00178
00179 std::string type_name = _conf.read<string>(key_type, "tcp");
00180 Configuration::NetConfig::NetType type = Configuration::NetConfig::NETWORK_UNKNOWN;
00181
00182 if (type_name == "tcp") type = Configuration::NetConfig::NETWORK_TCP;
00183 if (type_name == "udp") type = Configuration::NetConfig::NETWORK_UDP;
00184 if (type_name == "http") type = Configuration::NetConfig::NETWORK_HTTP;
00185 if (type_name == "lowpan") type = Configuration::NetConfig::NETWORK_LOWPAN;
00186
00187 switch (type)
00188 {
00189 case Configuration::NetConfig::NETWORK_HTTP:
00190 {
00191 Configuration::NetConfig::NetConfig nc(netname, type,
00192 _conf.read<std::string>(key_address, "http://localhost/"), 0,
00193 _conf.read<std::string>(key_discovery, "yes") == "no");
00194
00195 ret.push_back(nc);
00196 break;
00197 }
00198
00199 default:
00200 {
00201 Configuration::NetConfig::NetConfig nc(netname, type,
00202 ibrcommon::NetInterface(_conf.read<std::string>(key_interface, "lo")),
00203 _conf.read<unsigned int>(key_port, 4556),
00204 _conf.read<std::string>(key_discovery, "yes") == "yes");
00205
00206 ret.push_back(nc);
00207 break;
00208 }
00209 }
00210 }
00211 } catch (ConfigFile::key_not_found ex) {
00212 return ret;
00213 }
00214
00215 return ret;
00216 }
00217
00218 std::string Configuration::getDiscoveryAddress()
00219 {
00220 try {
00221 return _conf.read<string>("discovery_address");
00222 } catch (ConfigFile::key_not_found ex) {
00223 throw ParameterNotFoundException();
00224 }
00225 }
00226
00227 int Configuration::getDiscoveryPort()
00228 {
00229 return _conf.read<int>("discovery_port", 4551);
00230 }
00231
00232 Configuration::NetConfig Configuration::getAPIInterface()
00233 {
00234 return Configuration::NetConfig("local", Configuration::NetConfig::NETWORK_TCP, ibrcommon::NetInterface("lo"), 4550);
00235 }
00236
00237 list<dtn::routing::StaticRoutingExtension::StaticRoute> Configuration::getStaticRoutes()
00238 {
00239 list<dtn::routing::StaticRoutingExtension::StaticRoute> ret;
00240 string key = "route1";
00241 unsigned int keynumber = 1;
00242
00243 while (_conf.keyExists( key ))
00244 {
00245 vector<string> route = dtn::utils::Utils::tokenize(" ", _conf.read<string>(key, "dtn:none dtn:none"));
00246 ret.push_back( dtn::routing::StaticRoutingExtension::StaticRoute( route.front(), route.back() ) );
00247
00248 keynumber++;
00249 stringstream ss; ss << "route" << keynumber; ss >> key;
00250 }
00251
00252 return ret;
00253 }
00254
00255 list<Node> Configuration::getStaticNodes()
00256 {
00257 std::list<Node> nodes;
00258
00259
00260 int count = 1;
00261
00262
00263 std::string prefix = "static1_";
00264
00265 while ( _conf.keyExists(prefix + "uri") )
00266 {
00267 Node n(Node::NODE_PERMANENT);
00268
00269 n.setAddress( _conf.read<std::string>(prefix + "address", "127.0.0.1") );
00270 n.setPort( _conf.read<unsigned int>(prefix + "port", 4556) );
00271 n.setURI( _conf.read<std::string>(prefix + "uri", "dtn:none") );
00272
00273 std::string protocol = _conf.read<std::string>(prefix + "proto", "tcp");
00274 if (protocol == "tcp") n.setProtocol(Node::CONN_TCPIP);
00275 if (protocol == "udp") n.setProtocol(Node::CONN_UDPIP);
00276 if (protocol == "zigbee") n.setProtocol(Node::CONN_ZIGBEE);
00277 if (protocol == "bluetooth") n.setProtocol(Node::CONN_BLUETOOTH);
00278 if (protocol == "http") n.setProtocol(Node::CONN_HTTP);
00279
00280 count++;
00281
00282 std::stringstream prefix_stream;
00283 prefix_stream << "static" << count << "_";
00284 prefix = prefix_stream.str();
00285
00286 nodes.push_back(n);
00287 }
00288
00289 return nodes;
00290 }
00291
00292 int Configuration::getTimezone()
00293 {
00294 return _conf.read<int>( "timezone", 0 );
00295 }
00296
00297 ibrcommon::File Configuration::getPath(string name)
00298 {
00299 stringstream ss;
00300 ss << name << "_path";
00301 string key; ss >> key;
00302
00303 try {
00304 return ibrcommon::File(_conf.read<string>(key));
00305 } catch (ConfigFile::key_not_found ex) {
00306 throw ParameterNotSetException();
00307 }
00308 }
00309
00310 unsigned int Configuration::getUID()
00311 {
00312 try {
00313 return _conf.read<unsigned int>("user");
00314 } catch (ConfigFile::key_not_found ex) {
00315 throw ParameterNotSetException();
00316 }
00317 }
00318
00319 unsigned int Configuration::getGID()
00320 {
00321 try {
00322 return _conf.read<unsigned int>("group");
00323 } catch (ConfigFile::key_not_found ex) {
00324 throw ParameterNotSetException();
00325 }
00326 }
00327
00328
00329 bool Configuration::doDiscovery()
00330 {
00331 return _dodiscovery;
00332 }
00333
00334 bool Configuration::doAPI()
00335 {
00336 return _doapi;
00337 }
00338
00339 string Configuration::getNotifyCommand()
00340 {
00341 try {
00342 return _conf.read<string>("notify_cmd");
00343 } catch (ConfigFile::key_not_found ex) {
00344 throw ParameterNotSetException();
00345 }
00346 }
00347
00348 Configuration::RoutingExtension Configuration::getRoutingExtension()
00349 {
00350 try {
00351 string mode = _conf.read<string>("routing");
00352 if ( mode == "epidemic" ) return EPIDEMIC_ROUTING;
00353 return DEFAULT_ROUTING;
00354 } catch (ConfigFile::key_not_found ex) {
00355 return DEFAULT_ROUTING;
00356 }
00357 }
00358
00359
00360 bool Configuration::doForwarding()
00361 {
00362 try {
00363 if (_conf.read<std::string>("routing_forwarding") == "yes")
00364 {
00365 return true;
00366 }
00367 else
00368 {
00369 return false;
00370 }
00371 } catch (ConfigFile::key_not_found ex) {
00372 return true;
00373 }
00374 }
00375
00376 bool Configuration::useStatLogger()
00377 {
00378 return _conf.keyExists("statistic_type");
00379 }
00380
00381 ibrcommon::File Configuration::getStatLogfile()
00382 {
00383 try {
00384 return ibrcommon::File(_conf.read<std::string>("statistic_file"));
00385 } catch (ConfigFile::key_not_found ex) {
00386 throw ParameterNotSetException();
00387 }
00388 }
00389
00390 std::string Configuration::getStatLogType()
00391 {
00392 return _conf.read<std::string>("statistic_type", "stdout");
00393 }
00394
00395 unsigned int Configuration::getStatLogInterval()
00396 {
00397 return _conf.read<unsigned int>("statistic_interval", 300);
00398 }
00399
00400 std::string Configuration::getStatAddress()
00401 {
00402 return _conf.read<std::string>("statistic_address", "127.0.0.1");
00403 }
00404
00405 unsigned int Configuration::getStatPort()
00406 {
00407 return _conf.read<unsigned int>("statistic_port", 1234);
00408 }
00409
00410 size_t Configuration::getLimit(std::string suffix)
00411 {
00412 std::string unparsed = _conf.read<std::string>("limit_" + suffix, "0");
00413
00414 std::stringstream ss(unparsed);
00415
00416 float value; ss >> value;
00417 char multiplier = 0; ss >> multiplier;
00418
00419 switch (multiplier)
00420 {
00421 default:
00422 return (size_t)value;
00423 break;
00424
00425 case 'G':
00426 return (size_t)(value * 1000000000);
00427 break;
00428
00429 case 'M':
00430 return (size_t)(value * 1000000);
00431 break;
00432
00433 case 'K':
00434 return (size_t)(value * 1000);
00435 break;
00436 }
00437
00438 return 0;
00439 }
00440 }
00441 }