Commit 11e79cb5 authored by Mathieu Stefani's avatar Mathieu Stefani

Peer data is now stored inside an unordered_map and indexed by string

parent 001c561b
......@@ -22,6 +22,8 @@ static constexpr char CR = 0xD;
static constexpr char LF = 0xA;
static constexpr char CRLF[] = {CR, LF};
static constexpr const char* ParserData = "__Parser";
namespace Private {
Parser::Buffer::Buffer()
......@@ -476,7 +478,7 @@ Handler::onOutput() {
void
Handler::onConnection(Tcp::Peer& peer) {
peer.setData(std::make_shared<Private::Parser>());
peer.putData(ParserData, std::make_shared<Private::Parser>());
}
void
......@@ -484,9 +486,8 @@ Handler::onDisconnection(Tcp::Peer& peer) {
}
Private::Parser&
Handler::getParser(Tcp::Peer& peer) {
auto data = peer.data();
return *std::static_pointer_cast<Private::Parser>(data);
Handler::getParser(Tcp::Peer& peer) const {
return *peer.getData<Private::Parser>(ParserData);
}
Endpoint::Endpoint()
......
......@@ -347,7 +347,7 @@ public:
virtual void onRequest(const Request& request, Tcp::Peer& peer) = 0;
private:
Private::Parser& getParser(Tcp::Peer& peer);
Private::Parser& getParser(Tcp::Peer& peer) const;
};
class Endpoint {
......
......@@ -15,13 +15,11 @@ using namespace std;
Peer::Peer()
: fd_(-1)
, data_(nullptr)
{ }
Peer::Peer(const Address& addr)
: addr(addr)
, fd_(-1)
, data_(nullptr)
{ }
Address
......@@ -49,13 +47,31 @@ Peer::fd() const {
}
void
Peer::setData(std::shared_ptr<void> data) {
data_ = std::move(data);
Peer::putData(std::string name, std::shared_ptr<void> data) {
auto it = data_.find(name);
if (it != std::end(data_)) {
throw std::runtime_error("The data already exists");
}
data_.insert(std::make_pair(std::move(name), std::move(data)));
}
std::shared_ptr<void>
Peer::getData(std::string name) const {
auto data = tryGetData(std::move(name));
if (data == nullptr) {
throw std::runtime_error("The data does not exist");
}
return data;
}
std::shared_ptr<void>
Peer::data() const {
return data_;
Peer::tryGetData(std::string(name)) const {
auto it = data_.find(name);
if (it == std::end(data_)) return nullptr;
return it->second;
}
std::ostream& operator<<(std::ostream& os, const Peer& peer) {
......
......@@ -11,6 +11,7 @@
#include <string>
#include <iostream>
#include <memory>
#include <unordered_map>
namespace Net {
......@@ -27,15 +28,29 @@ public:
void associateFd(Fd fd);
Fd fd() const;
std::shared_ptr<void> data() const;
void setData(std::shared_ptr<void> data);
void putData(std::string name, std::shared_ptr<void> data);
std::shared_ptr<void> getData(std::string name) const;
template<typename T>
std::shared_ptr<T> getData(std::string name) const {
return std::static_pointer_cast<T>(getData(std::move(name)));
}
std::shared_ptr<void> tryGetData(std::string name) const;
template<typename T>
std::shared_ptr<T> tryGetData(std::string name) const {
auto data = tryGetData(std::move(name));
if (data == nullptr) return nullptr;
return std::static_pointer_cast<T>(data);
}
private:
Address addr;
std::string hostname_;
Fd fd_;
std::shared_ptr<void> data_;
std::unordered_map<std::string, std::shared_ptr<void>> data_;
};
std::ostream& operator<<(std::ostream& os, const Peer& peer);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment