You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.2 KiB

  1. From ab8ececbe70f7c83667d6ebb592fc1df17ad26a4 Mon Sep 17 00:00:00 2001
  2. From: Moritz Warning <moritzwarning@web.de>
  3. Date: Sat, 20 Jan 2018 21:55:52 +0100
  4. Subject: [PATCH] Revert "Do not serve controller requests until init is done."
  5. This reverts commit f4feccc6265cc480b84c85f897b225714072d4ec.
  6. ---
  7. controller/JSONDB.cpp | 20 +++++++-------------
  8. controller/JSONDB.hpp | 15 +++++++--------
  9. 2 files changed, 14 insertions(+), 21 deletions(-)
  10. diff --git a/controller/JSONDB.cpp b/controller/JSONDB.cpp
  11. index d3e76fc1..007e0fec 100644
  12. --- a/controller/JSONDB.cpp
  13. +++ b/controller/JSONDB.cpp
  14. @@ -26,8 +26,7 @@ static const nlohmann::json _EMPTY_JSON(nlohmann::json::object());
  15. static const std::map<std::string,std::string> _ZT_JSONDB_GET_HEADERS;
  16. JSONDB::JSONDB(const std::string &basePath) :
  17. - _basePath(basePath),
  18. - _ready(false)
  19. + _basePath(basePath)
  20. {
  21. if ((_basePath.length() > 7)&&(_basePath.substr(0,7) == "http://")) {
  22. // TODO: this doesn't yet support IPv6 since bracketed address notiation isn't supported.
  23. @@ -50,7 +49,7 @@ JSONDB::JSONDB(const std::string &basePath) :
  24. OSUtils::mkdir(_basePath.c_str());
  25. OSUtils::lockDownFile(_basePath.c_str(),true); // networks might contain auth tokens, etc., so restrict directory permissions
  26. }
  27. - _ready = _reload(_basePath,std::string());
  28. + _reload(_basePath,std::string());
  29. }
  30. bool JSONDB::writeRaw(const std::string &n,const std::string &obj)
  31. @@ -84,13 +83,9 @@ bool JSONDB::put(const std::string &n,const nlohmann::json &obj)
  32. const nlohmann::json &JSONDB::get(const std::string &n)
  33. {
  34. - while (!_ready) {
  35. - Thread::sleep(250);
  36. - _ready = _reload(_basePath,std::string());
  37. - }
  38. -
  39. if (!_isValidObjectName(n))
  40. return _EMPTY_JSON;
  41. +
  42. std::map<std::string,_E>::iterator e(_db.find(n));
  43. if (e != _db.end())
  44. return e->second.obj;
  45. @@ -138,7 +133,7 @@ void JSONDB::erase(const std::string &n)
  46. _db.erase(n);
  47. }
  48. -bool JSONDB::_reload(const std::string &p,const std::string &b)
  49. +void JSONDB::_reload(const std::string &p,const std::string &b)
  50. {
  51. if (_httpAddr) {
  52. std::string body;
  53. @@ -155,11 +150,11 @@ bool JSONDB::_reload(const std::string &p,const std::string &b)
  54. _db[tmp].obj = i.value();
  55. }
  56. }
  57. - return true;
  58. }
  59. - } catch ( ... ) {} // invalid JSON, so maybe incomplete request
  60. + } catch ( ... ) {
  61. + // TODO: report error?
  62. + }
  63. }
  64. - return false;
  65. } else {
  66. std::vector<std::string> dl(OSUtils::listDirectory(p.c_str(),true));
  67. for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
  68. @@ -169,7 +164,6 @@ bool JSONDB::_reload(const std::string &p,const std::string &b)
  69. this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR));
  70. }
  71. }
  72. - return true;
  73. }
  74. }
  75. diff --git a/controller/JSONDB.hpp b/controller/JSONDB.hpp
  76. index beafbaf5..c19112ed 100644
  77. --- a/controller/JSONDB.hpp
  78. +++ b/controller/JSONDB.hpp
  79. @@ -36,7 +36,6 @@
  80. #include "../ext/json/json.hpp"
  81. #include "../osdep/OSUtils.hpp"
  82. #include "../osdep/Http.hpp"
  83. -#include "../osdep/Thread.hpp"
  84. namespace ZeroTier {
  85. @@ -48,6 +47,12 @@ class JSONDB
  86. public:
  87. JSONDB(const std::string &basePath);
  88. + inline void reload()
  89. + {
  90. + _db.clear();
  91. + _reload(_basePath,std::string());
  92. + }
  93. +
  94. bool writeRaw(const std::string &n,const std::string &obj);
  95. bool put(const std::string &n,const nlohmann::json &obj);
  96. @@ -74,11 +79,6 @@ public:
  97. template<typename F>
  98. inline void filter(const std::string &prefix,F func)
  99. {
  100. - while (!_ready) {
  101. - Thread::sleep(250);
  102. - _ready = _reload(_basePath,std::string());
  103. - }
  104. -
  105. for(std::map<std::string,_E>::iterator i(_db.lower_bound(prefix));i!=_db.end();) {
  106. if ((i->first.length() >= prefix.length())&&(!memcmp(i->first.data(),prefix.data(),prefix.length()))) {
  107. if (!func(i->first,get(i->first))) {
  108. @@ -94,7 +94,7 @@ public:
  109. inline bool operator!=(const JSONDB &db) const { return (!(*this == db)); }
  110. private:
  111. - bool _reload(const std::string &p,const std::string &b);
  112. + void _reload(const std::string &p,const std::string &b);
  113. bool _isValidObjectName(const std::string &n);
  114. std::string _genPath(const std::string &n,bool create);
  115. @@ -108,7 +108,6 @@ private:
  116. InetAddress _httpAddr;
  117. std::string _basePath;
  118. std::map<std::string,_E> _db;
  119. - volatile bool _ready;
  120. };
  121. } // namespace ZeroTier
  122. --
  123. 2.15.1