headers: unify API across build options

This commit is contained in:
Adrien Béraud committed 2018-01-31 18:07:43 +01:00
1 parent bd9496e2ba
commit e274ffd83b
8 files changed
+67 -90

No files matched your search

+1 -3
View File
@@ -290,10 +290,8 @@ public:
std::vector<SockAddr> getPublicAddress(sa_family_t family = 0);
#if OPENDHT_PUSH_NOTIFICATIONS
void pushNotificationReceived(const std::map<std::string, std::string>&) {}
void resubscribe(const unsigned) {}
#endif // OPENDHT_PUSH_NOTIFICATIONS
void resubscribe(unsigned) {}
private:
+1 -3
View File
@@ -228,7 +228,6 @@ public:
DHT_LOG.setFilter(f);
}
#if OPENDHT_PUSH_NOTIFICATIONS
virtual void setPushNotificationToken(const std::string&) {};
/**
@@ -240,8 +239,7 @@ public:
* Refresh a listen via a token
* @param token
*/
virtual void resubscribe(const unsigned token) = 0;
#endif // OPENDHT_PUSH_NOTIFICATIONS
virtual void resubscribe(unsigned token) = 0;
protected:
bool logFilerEnable_ {};
+1 -6
View File
@@ -51,12 +51,7 @@ public:
* @note if the server fails to start (if port is already used or reserved),
* it will fails silently
*/
DhtProxyServer(std::shared_ptr<DhtRunner> dht,
in_port_t port = 8000.
#if OPENDHT_PUSH_NOTIFICATIONS
, const std::string& pushServer = ""
#endif // OPENDHT_PUSH_NOTIFICATIONS
);
DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port = 8000, const std::string& pushServer = "");
virtual ~DhtProxyServer();
DhtProxyServer(const DhtProxyServer& other) = delete;
+28 -34
View File
@@ -20,7 +20,6 @@
#pragma once
//#include "securedht.h"
#include "infohash.h"
#include "value.h"
#include "callbacks.h"
@@ -37,10 +36,6 @@
#include <queue>
#include <chrono>
#if OPENDHT_PROXY_CLIENT
#include "dht_proxy_client.h"
#endif // OPENDHT_PROXY_CLIENT
namespace dht {
struct Node;
@@ -58,6 +53,13 @@ class OPENDHT_PUBLIC DhtRunner {
public:
typedef std::function<void(NodeStatus, NodeStatus)> StatusCallback;
struct Config {
SecureDhtConfig dht_config;
bool threaded;
std::string proxy_server;
std::string push_node_id;
};
DhtRunner();
virtual ~DhtRunner();
@@ -299,13 +301,6 @@ public:
void registerCertificate(std::shared_ptr<crypto::Certificate> cert);
void setLocalCertificateStore(CertificateStoreQuery&& query_method);
struct Config {
SecureDhtConfig dht_config;
bool threaded;
std::string proxy_server;
std::string push_node_id;
};
/**
* @param port: Local port to bind. Both IPv4 and IPv6 will be tried (ANY).
* @param identity: RSA key pair to use for cryptographic operations.
@@ -377,13 +372,14 @@ public:
*/
void join();
#if OPENDHT_PROXY_CLIENT
void setProxyServer(const std::string& proxy, const std::string& pushNodeId = "") {
#if OPENDHT_PROXY_CLIENT
if (config_.proxy_server == proxy and config_.push_node_id == pushNodeId)
return;
config_.proxy_server = proxy;
config_.push_node_id = pushNodeId;
enableProxy(use_proxy and not config_.proxy_server.empty());
#endif
}
/**
@@ -393,12 +389,8 @@ public:
*/
void enableProxy(bool proxify);
#endif // OPENDHT_PROXY_CLIENT
#if OPENDHT_PROXY_SERVER
void forwardAllMessages(bool forward);
#endif // OPENDHT_PROXY_SERVER
/* Push notification methods */
#if OPENDHT_PUSH_NOTIFICATIONS
/**
* Updates the push notification device token
*/
@@ -412,8 +404,10 @@ public:
* Refresh a listen via a token
* @param token
*/
void resubscribe(const unsigned token);
#endif // OPENDHT_PUSH_NOTIFICATIONS
void resubscribe(unsigned token);
/* Proxy server mothods */
void forwardAllMessages(bool forward);
private:
static constexpr std::chrono::seconds BOOTSTRAP_PERIOD {10};
@@ -433,7 +427,18 @@ private:
return std::max(status4, status6);
}
/** Local DHT instance */
std::unique_ptr<SecureDht> dht_;
/** Proxy client instance */
std::unique_ptr<SecureDht> dht_via_proxy_;
/** true if we are currently using a proxy */
std::atomic_bool use_proxy {false};
/** Current configuration */
Config config_;
/**
* reset dht clients
*/
@@ -442,21 +447,7 @@ private:
* @return the current active DHT
*/
SecureDht* activeDht() const;
#if OPENDHT_PROXY_CLIENT
/**
* true if we are currently using a proxy
*/
std::atomic_bool use_proxy {false};
/**
* The current proxy client
*/
std::unique_ptr<SecureDht> dht_via_proxy_;
Config config_;
#if OPENDHT_PUSH_NOTIFICATIONS
std::string pushToken_;
#endif
#endif // OPENDHT_PROXY_CLIENT
/**
* Store current listeners and translates global tokens for each client.
*/
@@ -503,6 +494,9 @@ private:
int s4 {-1}, s6 {-1};
SockAddr bound4 {};
SockAddr bound6 {};
/** Push notification token */
std::string pushToken_;
};
}
-10
View File
@@ -293,21 +293,14 @@ public:
dht_->connectivityChanged();
}
#if OPENDHT_PROXY_CLIENT
#if OPENDHT_PUSH_NOTIFICATIONS
void setPushNotificationToken(const std::string& token = "") {
dht_->setPushNotificationToken(token);
}
#endif
#endif
#if OPENDHT_PROXY_SERVER
void forwardAllMessages(bool forward) {
forward_all_ = forward;
}
#endif //OPENDHT_PROXY_SERVER
#if OPENDHT_PUSH_NOTIFICATIONS
/**
* Call linked callback with push_notification
* @param notification to process
@@ -322,7 +315,6 @@ public:
void resubscribe(const unsigned token) {
dht_->resubscribe(token);
}
#endif // OPENDHT_PUSH_NOTIFICATIONS
void setLoggers(LogMethod error = NOLOG, LogMethod warn = NOLOG, LogMethod debug = NOLOG)
{
@@ -360,9 +352,7 @@ private:
std::uniform_int_distribution<Value::Id> rand_id {};
#if OPENDHT_PROXY_SERVER
std::atomic_bool forward_all_ {false};
#endif //OPENDHT_PROXY_SERVER
};
const ValueType CERTIFICATE_TYPE = {
+7 -9
View File
@@ -37,19 +37,17 @@ using namespace std::placeholders;
namespace dht {
DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port
#if OPENDHT_PUSH_NOTIFICATIONS
, const std::string& pushServer
#endif // OPENDHT_PUSH_NOTIFICATIONS
)
: dht_(dht)
#if OPENDHT_PUSH_NOTIFICATIONS
, pushServer_(pushServer)
#endif // OPENDHT_PUSH_NOTIFICATIONS
DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port , const std::string& pushServer)
: dht_(dht) , pushServer_(pushServer)
{
// NOTE in c++14, use make_unique
service_ = std::unique_ptr<restbed::Service>(new restbed::Service());
#if !OPENDHT_PUSH_NOTIFICATIONS
if (not pushServer.empty())
std::cerr << "Push server defined but built OpenDHT built without push notification support" << std::endl;
#endif
server_thread = std::thread([this, port]() {
// Create endpoints
auto resource = std::make_shared<restbed::Resource>();
+19 -12
View File
@@ -21,6 +21,10 @@
#include "dhtrunner.h"
#include "securedht.h"
#if OPENDHT_PROXY_CLIENT
#include "dht_proxy_client.h"
#endif
#ifndef _WIN32
#include <unistd.h>
#else
@@ -92,8 +96,8 @@ DhtRunner::run(const SockAddr& local4, const SockAddr& local6, DhtRunner::Config
#if OPENDHT_PROXY_CLIENT
config_ = config;
enableProxy(not config_.proxy_server.empty());
#endif
enableProxy(not config.proxy_server.empty());
running = true;
if (not config.threaded)
@@ -863,10 +867,10 @@ DhtRunner::activeDht() const
#endif // OPENDHT_PROXY_CLIENT
}
#if OPENDHT_PROXY_CLIENT
void
DhtRunner::enableProxy(bool proxify)
{
#if OPENDHT_PROXY_CLIENT
if (dht_via_proxy_) {
dht_via_proxy_->shutdown({});
}
@@ -908,51 +912,54 @@ DhtRunner::enableProxy(bool proxify)
});
}
}
#else
if (proxify)
std::cerr << "DHT proxy requested but OpenDHT built without proxy support." << std::endl;
#endif
}
#if OPENDHT_PUSH_NOTIFICATIONS
/**
* Updates the push notification device token
*/
void
DhtRunner::setPushNotificationToken(const std::string& token) {
#if OPENDHT_PROXY_CLIENT && OPENDHT_PUSH_NOTIFICATIONS
pushToken_ = token;
if (dht_via_proxy_)
dht_via_proxy_->setPushNotificationToken(token);
#endif
}
#endif // OPENDHT_PUSH_NOTIFICATIONS
#endif // OPENDHT_PROXY_CLIENT
#if OPENDHT_PROXY_SERVER
void
DhtRunner::forwardAllMessages(bool forward)
{
#if OPENDHT_PROXY_SERVER
#if OPENDHT_PROXY_CLIENT
if (dht_via_proxy_)
dht_via_proxy_->forwardAllMessages(forward);
#endif // OPENDHT_PROXY_CLIENT
if (dht_)
dht_->forwardAllMessages(forward);
}
#endif // OPENDHT_PROXY_SERVER
#if OPENDHT_PUSH_NOTIFICATIONS && OPENDHT_PROXY_CLIENT
}
void
DhtRunner::pushNotificationReceived(const std::map<std::string, std::string>& data) const
{
#if OPENDHT_PROXY_CLIENT && OPENDHT_PUSH_NOTIFICATIONS
if (dht_via_proxy_)
dht_via_proxy_->pushNotificationReceived(data);
#endif
}
void
DhtRunner::resubscribe(const unsigned token)
DhtRunner::resubscribe(unsigned token)
{
#if OPENDHT_PROXY_CLIENT && OPENDHT_PUSH_NOTIFICATIONS
if (dht_via_proxy_)
dht_via_proxy_->resubscribe(token);
#endif
}
#endif // OPENDHT_PUSH_NOTIFICATIONS && OPENDHT_PROXY_CLIENT
}
+10 -13
View File
@@ -491,14 +491,10 @@ main(int argc, char **argv)
config.dht_config.node_config.network = params.network;
config.dht_config.id = crt;
config.threaded = true;
#if OPENDHT_PROXY_CLIENT
config.proxy_server = params.proxyclient;
config.push_node_id = "dhtnode";
#if OPENDHT_PUSH_NOTIFICATIONS
if (not params.proxyclient.empty())
dht->setPushNotificationToken(params.devicekey);
#endif
#endif //OPENDHT_PROXY_CLIENT
dht->run(params.port, config);
@@ -517,15 +513,16 @@ main(int argc, char **argv)
}
#if OPENDHT_PROXY_SERVER
std::map<in_port_t, std::unique_ptr<DhtProxyServer>> proxies;
if (params.proxyserver != 0) {
proxies.emplace(params.proxyserver, new DhtProxyServer(dht, params.proxyserver
#if OPENDHT_PUSH_NOTIFICATIONS
, params.pushserver
#endif // OPENDHT_PUSH_NOTIFICATIONS
));
}
#endif //OPENDHT_PROXY_SERVER
std::map<in_port_t, std::unique_ptr<DhtProxyServer>> proxies;
#endif
if (params.proxyserver != 0) {
#if OPENDHT_PROXY_SERVER
proxies.emplace(params.proxyserver, new DhtProxyServer(dht, params.proxyserver, params.pushserver));
#else
std::cerr << "DHT proxy server requested but OpenDHT built without proxy server support." << std::endl;
exit(EXIT_FAILURE);
#endif
}
if (params.daemonize or params.service)
while (runner.wait());