|
@@ -20,6 +20,9 @@
|
|
|
|
|
|
#define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
|
|
#define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
|
|
|
|
|
|
|
|
+#include "ConnectionPool.hpp"
|
|
|
|
+#include <pqxx/pqxx>
|
|
|
|
+
|
|
#include <memory>
|
|
#include <memory>
|
|
#include <redis++/redis++.h>
|
|
#include <redis++/redis++.h>
|
|
|
|
|
|
@@ -31,14 +34,65 @@ namespace ZeroTier {
|
|
|
|
|
|
struct RedisConfig;
|
|
struct RedisConfig;
|
|
|
|
|
|
|
|
+
|
|
|
|
+class PostgresConnection : public Connection {
|
|
|
|
+public:
|
|
|
|
+ virtual ~PostgresConnection() {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::shared_ptr<pqxx::connection> c;
|
|
|
|
+ int a;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class PostgresConnFactory : public ConnectionFactory {
|
|
|
|
+public:
|
|
|
|
+ PostgresConnFactory(std::string &connString)
|
|
|
|
+ : m_connString(connString)
|
|
|
|
+ {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ virtual std::shared_ptr<Connection> create() {
|
|
|
|
+ auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection());
|
|
|
|
+ c->c = std::make_shared<pqxx::connection>(m_connString);
|
|
|
|
+ return std::static_pointer_cast<Connection>(c);
|
|
|
|
+ }
|
|
|
|
+private:
|
|
|
|
+ std::string m_connString;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+class PostgreSQL;
|
|
|
|
+
|
|
|
|
+class MemberNotificationReceiver : public pqxx::notification_receiver {
|
|
|
|
+public:
|
|
|
|
+ MemberNotificationReceiver(PostgreSQL *p, pqxx::connection &c, const std::string &channel);
|
|
|
|
+ virtual ~MemberNotificationReceiver() {}
|
|
|
|
+
|
|
|
|
+ virtual void operator() (const std::string &payload, int backendPid);
|
|
|
|
+private:
|
|
|
|
+ PostgreSQL *_psql;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+class NetworkNotificationReceiver : public pqxx::notification_receiver {
|
|
|
|
+public:
|
|
|
|
+ NetworkNotificationReceiver(PostgreSQL *p, pqxx::connection &c, const std::string &channel);
|
|
|
|
+ virtual ~NetworkNotificationReceiver() {};
|
|
|
|
+
|
|
|
|
+ virtual void operator() (const std::string &payload, int packend_pid);
|
|
|
|
+private:
|
|
|
|
+ PostgreSQL *_psql;
|
|
|
|
+};
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* A controller database driver that talks to PostgreSQL
|
|
* A controller database driver that talks to PostgreSQL
|
|
*
|
|
*
|
|
* This is for use with ZeroTier Central. Others are free to build and use it
|
|
* This is for use with ZeroTier Central. Others are free to build and use it
|
|
- * but be aware taht we might change it at any time.
|
|
|
|
|
|
+ * but be aware that we might change it at any time.
|
|
*/
|
|
*/
|
|
class PostgreSQL : public DB
|
|
class PostgreSQL : public DB
|
|
{
|
|
{
|
|
|
|
+ friend class MemberNotificationReceiver;
|
|
|
|
+ friend class NetworkNotificationReceiver;
|
|
public:
|
|
public:
|
|
PostgreSQL(const Identity &myId, const char *path, int listenPort, RedisConfig *rc);
|
|
PostgreSQL(const Identity &myId, const char *path, int listenPort, RedisConfig *rc);
|
|
virtual ~PostgreSQL();
|
|
virtual ~PostgreSQL();
|
|
@@ -56,15 +110,22 @@ protected:
|
|
{
|
|
{
|
|
inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
|
|
inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
|
|
};
|
|
};
|
|
|
|
+ void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners) {
|
|
|
|
+ DB::_memberChanged(old, memberConfig, notifyListeners);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners) {
|
|
|
|
+ DB::_memberChanged(old, networkConfig, notifyListeners);
|
|
|
|
+ }
|
|
|
|
|
|
private:
|
|
private:
|
|
- void initializeNetworks(PGconn *conn);
|
|
|
|
- void initializeMembers(PGconn *conn);
|
|
|
|
|
|
+ void initializeNetworks();
|
|
|
|
+ void initializeMembers();
|
|
void heartbeat();
|
|
void heartbeat();
|
|
void membersDbWatcher();
|
|
void membersDbWatcher();
|
|
- void _membersWatcher_Postgres(PGconn *conn);
|
|
|
|
|
|
+ void _membersWatcher_Postgres();
|
|
void networksDbWatcher();
|
|
void networksDbWatcher();
|
|
- void _networksWatcher_Postgres(PGconn *conn);
|
|
|
|
|
|
+ void _networksWatcher_Postgres();
|
|
|
|
|
|
void _membersWatcher_Redis();
|
|
void _membersWatcher_Redis();
|
|
void _networksWatcher_Redis();
|
|
void _networksWatcher_Redis();
|
|
@@ -81,7 +142,7 @@ private:
|
|
NO_OVERRIDE = 1
|
|
NO_OVERRIDE = 1
|
|
};
|
|
};
|
|
|
|
|
|
- PGconn * getPgConn( OverrideMode m = ALLOW_PGBOUNCER_OVERRIDE );
|
|
|
|
|
|
+ std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
|
|
|
|
|
|
const Identity _myId;
|
|
const Identity _myId;
|
|
const Address _myAddress;
|
|
const Address _myAddress;
|