config.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <iostream>
  2. #include "config.hpp"
  3. // The next line suppresses a deprecation warning within boost
  4. #define BOOST_BIND_GLOBAL_PLACEHOLDERS
  5. #include "boost/property_tree/ptree.hpp"
  6. #include "boost/property_tree/json_parser.hpp"
  7. // Split a hostport string like "127.0.0.1:12000" at the rightmost colon
  8. // into a host part "127.0.0.1" and a port part "12000".
  9. static bool split_host_port(std::string &host, std::string &port,
  10. const std::string &hostport)
  11. {
  12. size_t colon = hostport.find_last_of(':');
  13. if (colon == std::string::npos) {
  14. std::cerr << "Cannot parse \"" << hostport << "\" as host:port\n";
  15. return false;
  16. }
  17. host = hostport.substr(0, colon);
  18. port = hostport.substr(colon+1);
  19. return true;
  20. }
  21. bool config_parse(Config &config, const std::string configstr,
  22. const std::string &myname)
  23. {
  24. bool found_my_node = false;
  25. bool found_params = false;
  26. bool ret = true;
  27. std::istringstream configstream(configstr);
  28. boost::property_tree::ptree conftree;
  29. read_json(configstream, conftree);
  30. for (auto & entry : conftree) {
  31. if (!entry.first.compare("params")) {
  32. for (auto & pentry : entry.second) {
  33. if (!pentry.first.compare("msgsize")) {
  34. config.msgsize = pentry.second.get_value<uint16_t>();
  35. } else {
  36. std::cerr << "Unknown field in params: " <<
  37. pentry.first << "\n";
  38. ret = false;
  39. }
  40. }
  41. found_params = true;
  42. } else if (!entry.first.compare("nodes")) {
  43. for (auto & node : entry.second) {
  44. NodeConfig nc;
  45. nc.weight = 1; // default
  46. for (auto & nentry : node.second) {
  47. if (!nentry.first.compare("name")) {
  48. nc.name = nentry.second.get_value<std::string>();
  49. if (!myname.compare(nc.name)) {
  50. config.mynodenum = config.nodes.size();
  51. found_my_node = true;
  52. }
  53. } else if (!nentry.first.compare("pubkey")) {
  54. nc.pubkey = nentry.second.get_value<std::string>();
  55. } else if (!nentry.first.compare("weight")) {
  56. nc.weight = nentry.second.get_value<std::uint8_t>();
  57. } else if (!nentry.first.compare("listen")) {
  58. ret &= split_host_port(nc.listenhost, nc.listenport,
  59. nentry.second.get_value<std::string>());
  60. } else if (!nentry.first.compare("clisten")) {
  61. ret &= split_host_port(nc.clistenhost, nc.clistenport,
  62. nentry.second.get_value<std::string>());
  63. } else {
  64. std::cerr << "Unknown field in host config: " <<
  65. nentry.first << "\n";
  66. ret = false;
  67. }
  68. }
  69. config.nodes.push_back(std::move(nc));
  70. }
  71. } else {
  72. std::cerr << "Unknown key in config: " <<
  73. entry.first << "\n";
  74. ret = false;
  75. }
  76. }
  77. if (!found_params) {
  78. std::cerr << "Could not find params in config\n";
  79. ret = false;
  80. }
  81. if (!found_my_node) {
  82. std::cerr << "Could not find my own node entry in config\n";
  83. ret = false;
  84. }
  85. return ret;
  86. }