2017-11-03 18:39:27 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ZT_CONTROLLER_USE_RETHINKDB
|
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
#ifndef ZT_CONTROLLER_RETHINKDB_HPP
|
|
|
|
#define ZT_CONTROLLER_RETHINKDB_HPP
|
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
#include "DB.hpp"
|
2017-11-02 14:05:11 +00:00
|
|
|
|
2018-01-12 17:18:27 +00:00
|
|
|
#define ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS 8
|
2017-11-03 18:39:27 +00:00
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
namespace ZeroTier
|
|
|
|
{
|
|
|
|
|
2017-11-09 01:02:10 +00:00
|
|
|
/**
|
|
|
|
* A controller database driver that talks to RethinkDB
|
|
|
|
*
|
|
|
|
* This is for use with ZeroTier Central. Others are free to build and use it
|
|
|
|
* but be aware that we might change it at any time.
|
|
|
|
*/
|
2017-11-07 22:44:46 +00:00
|
|
|
class RethinkDB : public DB
|
2017-11-02 14:05:11 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-12-07 21:39:25 +00:00
|
|
|
RethinkDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path);
|
2017-11-07 22:44:46 +00:00
|
|
|
virtual ~RethinkDB();
|
|
|
|
|
2017-11-08 19:06:14 +00:00
|
|
|
virtual bool waitForReady();
|
|
|
|
virtual void save(nlohmann::json *orig,nlohmann::json &record);
|
2017-11-07 22:44:46 +00:00
|
|
|
virtual void eraseNetwork(const uint64_t networkId);
|
|
|
|
virtual void eraseMember(const uint64_t networkId,const uint64_t memberId);
|
2017-11-09 01:19:46 +00:00
|
|
|
virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress);
|
2017-11-08 19:06:14 +00:00
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
protected:
|
2017-11-09 00:52:36 +00:00
|
|
|
struct _PairHasher
|
|
|
|
{
|
|
|
|
inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
|
|
|
|
};
|
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
std::string _host;
|
|
|
|
std::string _db;
|
|
|
|
std::string _auth;
|
2017-11-03 18:39:27 +00:00
|
|
|
int _port;
|
2017-11-02 14:05:11 +00:00
|
|
|
|
|
|
|
void *_networksDbWatcherConnection;
|
|
|
|
void *_membersDbWatcherConnection;
|
|
|
|
std::thread _networksDbWatcher;
|
|
|
|
std::thread _membersDbWatcher;
|
|
|
|
|
2017-11-03 20:59:36 +00:00
|
|
|
BlockingQueue< nlohmann::json * > _commitQueue;
|
2017-11-03 18:39:27 +00:00
|
|
|
std::thread _commitThread[ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS];
|
2017-11-02 14:05:11 +00:00
|
|
|
|
2017-11-09 01:19:46 +00:00
|
|
|
std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
|
2017-11-08 19:06:14 +00:00
|
|
|
mutable std::mutex _lastOnline_l;
|
|
|
|
std::thread _onlineNotificationThread;
|
|
|
|
|
2017-11-04 00:55:16 +00:00
|
|
|
std::thread _heartbeatThread;
|
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
mutable std::mutex _readyLock; // locked until ready
|
|
|
|
std::atomic<int> _ready;
|
|
|
|
std::atomic<int> _run;
|
2017-11-03 18:39:27 +00:00
|
|
|
mutable volatile bool _waitNoticePrinted;
|
2017-11-02 14:05:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|
2017-11-03 18:39:27 +00:00
|
|
|
|
|
|
|
#endif // ZT_CONTROLLER_USE_RETHINKDB
|