Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
508 changes: 1 addition & 507 deletions src/cpp/common/py_monero_common.cpp

Large diffs are not rendered by default.

237 changes: 0 additions & 237 deletions src/cpp/common/py_monero_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ enum PyMoneroConnectionType : uint8_t {
I2P
};

/*
* Specify behavior when polling.
*/
enum PyMoneroConnectionPollType : uint8_t {
PRIORITIZED = 0,
CURRENT,
ALL,
UNDEFINED
};

class PyMoneroConnectionPriorityComparator {
public:

Expand Down Expand Up @@ -563,230 +553,3 @@ class PyMoneroRpcConnection : public monero::monero_rpc_connection {
}

};

struct monero_connection_manager_listener {
public:
virtual void on_connection_changed(const std::shared_ptr<PyMoneroRpcConnection> &connection) {
throw std::runtime_error("monero_connection_manager_listener::on_connection_changed(): not implemented");
}
};

class PyMoneroConnectionManagerListener : public monero_connection_manager_listener {
public:
void on_connection_changed(const std::shared_ptr<PyMoneroRpcConnection> &connection) override {
PYBIND11_OVERRIDE_PURE(void, monero_connection_manager_listener, on_connection_changed, connection);
}
};

class PyMoneroConnectionManager : public PyThreadPoller {
public:

~PyMoneroConnectionManager();
PyMoneroConnectionManager() { }

/**
* Add a listener to receive notifications when the connection changes.
*
* @param listener the listener to add
*/
void add_listener(const std::shared_ptr<monero_connection_manager_listener> &listener);

/**
* Remove a listener.
*
* @param listener the listener to remove
*/
void remove_listener(const std::shared_ptr<monero_connection_manager_listener> &listener);

/**
* Remove all listeners.
*/
void remove_listeners();

/**
* Get all listeners.
*
* @return all listeners
*/
std::vector<std::shared_ptr<monero_connection_manager_listener>> get_listeners() const;

/**
* Add a connection. The connection may have an elevated priority for this manager to use.
*
* @param connection the connection to add
*/
void add_connection(const std::shared_ptr<PyMoneroRpcConnection>& connection);

/**
* Add connection URI.
*
* @param uri uri of the connection to add
*/
void add_connection(const std::string &uri);

/**
* Remove a connection.
*
* @param uri uri of the connection to remove
*/
void remove_connection(const std::string &uri);

/**
* Set the current connection without changing the credentials.
* Replace connection if its URI was previously added. Otherwise add new connection.
* Notify if current connection changes.
* Does not check the connection.
*
* @param connection is the connection to make current
*/
void set_connection(const std::shared_ptr<PyMoneroRpcConnection>& connection);

/**
* Set the current connection without changing the credentials.
* Add new connection if URI not previously added.
* Notify if current connection changes.
* Does not check the connection.
*
* @param uri identifies the connection to make current
*/
void set_connection(const std::string& uri);

/**
* Indicates if this manager has a connection with the given URI.
*
* @param uri URI of the connection to check
* @return true if this manager has a connection with the given URI, false otherwise
*/
bool has_connection(const std::string& uri);

/**
* Get the current connection.
*
* @return the current connection or null if no connection set
*/
std::shared_ptr<PyMoneroRpcConnection> get_connection() const { return m_current_connection; }

/**
* Get a connection by URI.
*
* @param uri URI of the connection to get
* @return the connection with the URI or null if no connection with the URI exists
*/
std::shared_ptr<PyMoneroRpcConnection> get_connection_by_uri(const std::string &uri);

/**
* Get all connections in order of current connection (if applicable), online status, priority, and name.
*
* @return the list of sorted connections
*/
std::vector<std::shared_ptr<PyMoneroRpcConnection>> get_connections() const;

/**
* Get if auto switch is enabled or disabled.
*
* @return true if auto switch enabled, false otherwise
*/
bool get_auto_switch() const { return m_auto_switch; }
void set_timeout(uint64_t timeout_ms) { m_timeout = timeout_ms; }
uint64_t get_timeout() const { return m_timeout; }

/**
* Indicates if the connection manager is connected to a node.
*
* @return true if the current connection is set, online, and not unauthenticated, none if unknown, false otherwise
*/
boost::optional<bool> is_connected() const;

/**
* Check the current connection. If disconnected and auto switch enabled, switches to best available connection.
*/
void check_connection();

/**
* Automatically switch to the best available connection as connections are polled, based on priority, response time, and consistency.
*
* @param auto_switch specifies if the connection should auto switch to a better connection
*/
void set_auto_switch(bool auto_switch);

/**
* Stop polling connections.
*/
void stop_polling();
void start_polling(const boost::optional<uint64_t>& period_ms, const boost::optional<bool>& auto_switch, const boost::optional<uint64_t>& timeout_ms, const boost::optional<PyMoneroConnectionPollType>& poll_type, const boost::optional<std::vector<std::shared_ptr<PyMoneroRpcConnection>>> &excluded_connections);

/**
* Collect connectable peers of the managed connections.
*
* @return connectable peers
*/
std::vector<std::shared_ptr<PyMoneroRpcConnection>> get_peer_connections() const { throw std::runtime_error("PyMoneroConnectionManager::get_peer_connections(): not implemented"); }

/**
* Get the best available connection in order of priority then response time.
*
* @param excluded_connections to be excluded from consideration (optional)
* @return the best available connection in order of priority then response time, null if no connections available
*/
std::shared_ptr<PyMoneroRpcConnection> get_best_available_connection(const std::set<std::shared_ptr<PyMoneroRpcConnection>>& excluded_connections = {});

/**
* Get the best available connection in order of priority then response time.
*
* @param excluded_connection to be excluded from consideration
* @return the best available connection in order of priority then response time, null if no connections available
*/
std::shared_ptr<PyMoneroRpcConnection> get_best_available_connection(const std::shared_ptr<PyMoneroRpcConnection>& excluded_connection);

/**
* Check all managed connections.
*/
void check_connections();

/**
* Disconnect from the current connection.
*/
void disconnect();

/**
* Remove all connections.
*/
void clear();

/**
* Reset to default state.
*/
void reset();
void poll() override;

private:
// static variables
inline static const uint64_t DEFAULT_TIMEOUT = 5000;
inline static const uint64_t DEFAULT_POLL_PERIOD = 20000;
inline static const bool DEFAULT_AUTO_SWITCH = true;
inline static const int MIN_BETTER_RESPONSES = 3;

// instance variables
mutable boost::recursive_mutex m_listeners_mutex;
mutable boost::recursive_mutex m_connections_mutex;

std::vector<std::shared_ptr<monero_connection_manager_listener>> m_listeners;
std::vector<std::shared_ptr<PyMoneroRpcConnection>> m_connections;
std::shared_ptr<PyMoneroRpcConnection> m_current_connection;
std::set<std::shared_ptr<PyMoneroRpcConnection>> m_excluded_connections;

std::atomic<bool> m_auto_switch = true;
std::atomic<uint64_t> m_timeout = 5000;

std::map<std::shared_ptr<PyMoneroRpcConnection>, std::vector<boost::optional<long>>> m_response_times;

PyMoneroConnectionPollType m_poll_type = PyMoneroConnectionPollType::UNDEFINED;

void on_connection_changed(const std::shared_ptr<PyMoneroRpcConnection>& connection);
std::vector<std::vector<std::shared_ptr<PyMoneroRpcConnection>>> get_connections_in_ascending_priority();
bool check_connections(const std::vector<std::shared_ptr<PyMoneroRpcConnection>>& connections, const std::set<std::shared_ptr<PyMoneroRpcConnection>>& excluded_connections = {});
void check_prioritized_connections();
std::shared_ptr<PyMoneroRpcConnection> process_responses(const std::vector<std::shared_ptr<PyMoneroRpcConnection>>& responses);
std::shared_ptr<PyMoneroRpcConnection> get_best_connection_from_prioritized_responses(const std::vector<std::shared_ptr<PyMoneroRpcConnection>>& responses);
std::shared_ptr<PyMoneroRpcConnection> update_best_connection_in_priority();
};
Loading
Loading