Skip to content

Commit

Permalink
test restbed
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarOk1412 committed Nov 1, 2017
1 parent dc99919 commit e33ac02
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Expand Up @@ -48,6 +48,11 @@ Makefile.in

# Python
*.pyc
python/opendht.cpp
python/setup.py

# Doxygen
doc/Doxyfile

# git backup files
*.orig
Expand Down
8 changes: 6 additions & 2 deletions CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ()

Expand All @@ -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 ()

Expand Down
37 changes: 37 additions & 0 deletions include/opendht/dhtproxyserver.h
@@ -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_;
};

}
5 changes: 5 additions & 0 deletions include/opendht/dhtrunner.h
Expand Up @@ -36,6 +36,8 @@
#include <queue>
#include <chrono>

#include "dhtproxyserver.h"

namespace dht {

struct Node;
Expand Down Expand Up @@ -362,6 +364,8 @@ class OPENDHT_PUBLIC DhtRunner {
*/
void join();

void startProxyInterface(); // TODO arguments

private:
static constexpr std::chrono::seconds BOOTSTRAP_PERIOD {10};

Expand Down Expand Up @@ -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 */
Expand Down
90 changes: 90 additions & 0 deletions src/dhtproxyserver.cpp
@@ -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"}});
}
}
);
}


}
7 changes: 7 additions & 0 deletions src/dhtrunner.cpp
Expand Up @@ -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));
}

}
2 changes: 2 additions & 0 deletions tools/dhtchat.cpp
Expand Up @@ -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;

Expand Down

0 comments on commit e33ac02

Please sign in to comment.