Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc99919
commit e33ac02
Showing
7 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"}}); | ||
} | ||
} | ||
); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters