RethinkDB.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef ZT_CONTROLLER_USE_RETHINKDB
  19. #ifndef ZT_CONTROLLER_RETHINKDB_HPP
  20. #define ZT_CONTROLLER_RETHINKDB_HPP
  21. #include "DB.hpp"
  22. #define ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS 4
  23. namespace ZeroTier
  24. {
  25. /**
  26. * A controller database driver that talks to RethinkDB
  27. *
  28. * This is for use with ZeroTier Central. Others are free to build and use it
  29. * but be aware that we might change it at any time.
  30. */
  31. class RethinkDB : public DB
  32. {
  33. public:
  34. RethinkDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path);
  35. virtual ~RethinkDB();
  36. virtual bool waitForReady();
  37. virtual bool isReady();
  38. virtual void save(nlohmann::json *orig,nlohmann::json &record);
  39. virtual void eraseNetwork(const uint64_t networkId);
  40. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId);
  41. virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress);
  42. protected:
  43. struct _PairHasher
  44. {
  45. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  46. };
  47. std::string _host;
  48. std::string _db;
  49. std::string _auth;
  50. int _port;
  51. void *_networksDbWatcherConnection;
  52. void *_membersDbWatcherConnection;
  53. std::thread _networksDbWatcher;
  54. std::thread _membersDbWatcher;
  55. BlockingQueue< nlohmann::json * > _commitQueue;
  56. std::thread _commitThread[ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS];
  57. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
  58. mutable std::mutex _lastOnline_l;
  59. std::thread _onlineNotificationThread;
  60. std::thread _heartbeatThread;
  61. mutable std::mutex _readyLock; // locked until ready
  62. std::atomic<int> _ready,_connected,_run;
  63. mutable volatile bool _waitNoticePrinted;
  64. };
  65. } // namespace ZeroTier
  66. #endif
  67. #endif // ZT_CONTROLLER_USE_RETHINKDB