Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

PdnsSlaveConfig.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // Created by robin on 8/8/15.
  3. //
  4. #include <fstream>
  5. #include <iostream>
  6. # include <json/json.h>
  7. #include "PdnsSlaveConfig.h"
  8. PdnsSlaveConfig::PdnsSlaveConfig(const std::string &filePath)
  9. : _filePath(filePath)
  10. {
  11. }
  12. BResult PdnsSlaveConfig::readConfig()
  13. {
  14. std::ifstream file(_filePath);
  15. if (!file)
  16. return BResult().error("Could not open file");
  17. Json::Value root;
  18. try
  19. {
  20. file >> root;
  21. file.close();
  22. }
  23. catch (...)
  24. {
  25. file.close();
  26. return BResult().error("Could not parse JSON");
  27. }
  28. BResult res;
  29. if (!(res = readString(root, "dhcpd-file", _dhcpdFilePath)))
  30. return res;
  31. if (!(res = readString(root, "dhcpd-template", _dhcpdTemplatePath)))
  32. return res;
  33. if (!(res = readString(root, "hosts-file", _hostsPath)))
  34. return res;
  35. if (!(res = readString(root, "db-type", _dbType)))
  36. return res;
  37. if (_dbType != "mysql" && _dbType != "pgsql")
  38. {
  39. return BResult().error("Invalid database type " + _dbType);
  40. }
  41. if (!(res = readSqlConfiguration(root, "master", _masterConfig)))
  42. return res;
  43. if (!(res = readSqlConfiguration(root, "slave", _slaveConfig)))
  44. return res;
  45. return true;
  46. }
  47. BResult PdnsSlaveConfig::readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration &sqlConfiguration)
  48. {
  49. std::string str;
  50. BResult res;
  51. if (!(res = readString(value, name + "-host", str)))
  52. return res;
  53. sqlConfiguration.setHost(str);
  54. if (!(res = readString(value, name + "-user", str)))
  55. return res;
  56. sqlConfiguration.setUser(str);
  57. if (!(res = readString(value, name + "-password", str)))
  58. return res;
  59. sqlConfiguration.setPassword(str);
  60. if (!(res = readString(value, name + "-database", str)))
  61. return res;
  62. sqlConfiguration.setDatabase(str);
  63. return true;
  64. }
  65. BResult PdnsSlaveConfig::readString(const Json::Value &value, const std::string &name, std::string &dest)
  66. {
  67. auto v = value[name];
  68. if (!v)
  69. return BResult().error("Could not find value " + name);
  70. dest = v.asString();
  71. return true;
  72. }
  73. const std::string &PdnsSlaveConfig::getDhcpdFilePath() const
  74. {
  75. return _dhcpdFilePath;
  76. }
  77. const std::string &PdnsSlaveConfig::getDhcpdTemplatePath() const
  78. {
  79. return _dhcpdTemplatePath;
  80. }
  81. const std::string &PdnsSlaveConfig::getHostsPath() const
  82. {
  83. return _hostsPath;
  84. }
  85. const SqlConfiguration &PdnsSlaveConfig::getMasterConfig() const
  86. {
  87. return _masterConfig;
  88. }
  89. const SqlConfiguration &PdnsSlaveConfig::getSlaveConfig() const
  90. {
  91. return _slaveConfig;
  92. }
  93. const std::string &PdnsSlaveConfig::getDbType() const
  94. {
  95. return _dbType;
  96. }