Permalink
Browse files

test restbed

  • Loading branch information...
AmarOk1412 committed Oct 24, 2017
1 parent dc99919 commit e33ac02351e6bfdb8624d73d4f2159ed9aa2cc03
Showing with 152 additions and 2 deletions.
  1. +5 −0 .gitignore
  2. +6 −2 CMakeLists.txt
  3. +37 −0 include/opendht/dhtproxyserver.h
  4. +5 −0 include/opendht/dhtrunner.h
  5. +90 −0 src/dhtproxyserver.cpp
  6. +7 −0 src/dhtrunner.cpp
  7. +2 −0 tools/dhtchat.cpp
View
@@ -48,6 +48,11 @@ Makefile.in
# Python
*.pyc
python/opendht.cpp
python/setup.py
# Doxygen
doc/Doxyfile
# git backup files
*.orig
View
@@ -104,6 +104,7 @@ list (APPEND opendht_SOURCES
src/node.cpp
src/value.cpp
src/dht.cpp
src/dhtproxyserver.cpp
src/storage.h
src/listener.h
src/search.h
@@ -131,6 +132,7 @@ list (APPEND opendht_HEADERS
include/opendht/node.h
include/opendht/value.h
include/opendht/dht.h
include/opendht/dhtproxyserver.h
include/opendht/callbacks.h
include/opendht/routing_table.h
include/opendht/node_cache.h
@@ -177,7 +179,8 @@ if (OPENDHT_STATIC)
target_link_libraries(opendht-static ${argon2_LIBRARIES})
target_include_directories(opendht-static SYSTEM PRIVATE ${argon2_INCLUDE_DIRS})
endif ()
target_link_libraries(opendht-static ${CMAKE_THREAD_LIBS_INIT} ${GNUTLS_LIBRARIES} ${Nettle_LIBRARIES})
# TODO clean RESTBED link
target_link_libraries(opendht-static ${CMAKE_THREAD_LIBS_INIT} ${GNUTLS_LIBRARIES} ${Nettle_LIBRARIES} -lrestbed)
install (TARGETS opendht-static DESTINATION ${CMAKE_INSTALL_LIBDIR} EXPORT opendht)
endif ()
@@ -198,7 +201,8 @@ if (OPENDHT_SHARED)
target_link_libraries(opendht PRIVATE ${argon2_LIBRARIES})
target_include_directories(opendht SYSTEM PRIVATE ${argon2_INCLUDE_DIRS})
endif ()
target_link_libraries(opendht PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${GNUTLS_LIBRARIES} ${Nettle_LIBRARIES})
# TODO clean RESTBED link
target_link_libraries(opendht PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${GNUTLS_LIBRARIES} ${Nettle_LIBRARIES} -lrestbed)
install (TARGETS opendht DESTINATION ${CMAKE_INSTALL_LIBDIR} EXPORT opendht)
endif ()
@@ -0,0 +1,37 @@
// TODO gpl
#pragma once
#include "def.h"
#include <thread>
#include <memory>
// TODO add ifdef RESTBED
#include <restbed>
namespace dht {
class DhtRunner;
class OPENDHT_PUBLIC DhtProxyServer
{
public:
DhtProxyServer(DhtRunner* dht);
virtual ~DhtProxyServer();
DhtProxyServer(const DhtProxyServer& other) = default;
DhtProxyServer(DhtProxyServer&& other) = default;
DhtProxyServer& operator=(const DhtProxyServer& other) = default;
DhtProxyServer& operator=(DhtProxyServer&& other) = default;
private:
void getId(const std::shared_ptr<restbed::Session>& session) const;
void getNodeId(const std::shared_ptr<restbed::Session>& session) const;
std::thread server_thread {};
std::unique_ptr<restbed::Service> service_;
DhtRunner* dht_;
};
}
@@ -36,6 +36,8 @@
#include <queue>
#include <chrono>
#include "dhtproxyserver.h"
namespace dht {
struct Node;
@@ -362,6 +364,8 @@ class OPENDHT_PUBLIC DhtRunner {
*/
void join();
void startProxyInterface(); // TODO arguments
private:
static constexpr std::chrono::seconds BOOTSTRAP_PERIOD {10};
@@ -391,6 +395,7 @@ class OPENDHT_PUBLIC DhtRunner {
std::mutex sock_mtx {};
std::vector<std::pair<Blob, SockAddr>> rcv {};
std::unique_ptr<DhtProxyServer> proxy_server_;
/** true if currently actively boostraping */
std::atomic_bool bootstraping {false};
/* bootstrap nodes given as (host, service) pairs */
View
@@ -0,0 +1,90 @@
// TODO GPL
#include "dhtproxyserver.h"
#include <functional>
#include "dhtrunner.h"
namespace dht {
DhtProxyServer::DhtProxyServer(DhtRunner* dht) : dht_(dht)
{
// NOTE in c++14, use make_unique
service_ = std::unique_ptr<restbed::Service>(new restbed::Service());
auto resource = std::make_shared<restbed::Resource>();
resource->set_path("/getId");
resource->set_method_handler("GET",
[this](const std::shared_ptr<restbed::Session> session)
{
this->getId(session);
}
);
service_->publish(resource);
resource = std::make_shared<restbed::Resource>();
resource->set_path("/getNodeId");
resource->set_method_handler("GET",
[this](const std::shared_ptr<restbed::Session> session)
{
this->getNodeId(session);
}
);
service_->publish(resource);
server_thread = std::thread([this]() {
auto settings = std::make_shared<restbed::Settings>();
settings->set_port(1984); // TODO add argument and move this
settings->set_default_header("Connection", "close");
service_->start(settings);
});
}
DhtProxyServer::~DhtProxyServer()
{
if (server_thread.joinable())
server_thread.join();
}
void
DhtProxyServer::getId(const std::shared_ptr<restbed::Session>& session) const
{
const auto request = session->get_request();
int content_length = std::stoi(request->get_header("Content-Length", "0"));
session->fetch(content_length,
[&](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& b)
{
if (dht_) {
auto id = dht_->getId().toString();
s->close(restbed::OK, id, {{"Content-Length", std::to_string(id.length())}});
} else {
s->close(restbed::OK, "", {{"Content-Length", "0"}});
}
}
);
}
void
DhtProxyServer::getNodeId(const std::shared_ptr<restbed::Session>& session) const
{
const auto request = session->get_request();
int content_length = std::stoi(request->get_header("Content-Length", "0"));
session->fetch(content_length,
[&](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& b)
{
if (dht_) {
auto id = dht_->getNodeId().toString();
s->close(restbed::OK, id, {{"Content-Length", std::to_string(id.length())}});
} else {
s->close(restbed::OK, "", {{"Content-Length", "0"}});
}
}
);
}
}
View
@@ -800,4 +800,11 @@ DhtRunner::findCertificate(InfoHash hash, std::function<void(const std::shared_p
cv.notify_all();
}
void
DhtRunner::startProxyInterface()
{
// NOTE in c++14, use make_unique
proxy_server_ = std::unique_ptr<DhtProxyServer>(new DhtProxyServer(this));
}
}
View
@@ -67,6 +67,8 @@ main(int argc, char **argv)
if (not params.bootstrap.first.empty())
dht.bootstrap(params.bootstrap.first.c_str(), params.bootstrap.second.c_str());
dht.startProxyInterface();
print_node_info(dht, params);
std::cout << " type 'c {hash}' to join a channel" << std::endl << std::endl;

0 comments on commit e33ac02

Please sign in to comment.