Add legislator factory

This commit is contained in:
Julien CLEMENT 2020-05-05 14:47:12 +02:00
parent d7b740b86e
commit 1f2cf940ce
8 changed files with 69 additions and 14 deletions

View File

@ -81,6 +81,10 @@ EVENTS = \
src/events/reverse-proxy.cc \ src/events/reverse-proxy.cc \
src/events/reverse-proxy.hh src/events/reverse-proxy.hh
LEGISLATOR = \
src/legislator/legislator.cc \
src/legislator/legislator-factory.cc
SOCKET = \ SOCKET = \
src/socket/default-socket.cc \ src/socket/default-socket.cc \
src/socket/default-socket.hh \ src/socket/default-socket.hh \
@ -88,6 +92,7 @@ SOCKET = \
SOURCES = \ SOURCES = \
$(CONFIG) \ $(CONFIG) \
$(LEGISLATOR) \
$(MISC) \ $(MISC) \
$(SOCKET) \ $(SOCKET) \
$(SCHEDULER) \ $(SCHEDULER) \

View File

@ -8,8 +8,9 @@
namespace paxos namespace paxos
{ {
ServerConfig::ServerConfig(const std::vector<LegislatorConfig>& legislators) ServerConfig::ServerConfig(const std::vector<LegislatorConfig>& legislators,
: legislators_(legislators) const LegislatorConfig& self)
: legislators_(legislators), self_(self)
{ {
} }
@ -61,7 +62,8 @@ namespace paxos
} }
static std::vector<LegislatorConfig> parse_legislators(const json& j) static std::pair<std::vector<LegislatorConfig>, LegislatorConfig>
parse_legislators(const json& j)
{ {
/* Get 'vhosts' value */ /* Get 'vhosts' value */
std::vector<json> legislators; std::vector<json> legislators;
@ -69,16 +71,20 @@ namespace paxos
/* Create VHostConfig vector and fill with VHostConfig */ /* Create VHostConfig vector and fill with VHostConfig */
std::vector<LegislatorConfig> legislator_configs; std::vector<LegislatorConfig> legislator_configs;
LegislatorConfig self;
for (auto it : legislators) for (auto it : legislators)
{ {
/* Differenciable vhost checking */ /* Differenciable vhost checking */
auto legislator = it.get<paxos::LegislatorConfig>(); auto legislator = it.get<paxos::LegislatorConfig>();
if (!legislator.is_self)
legislator_configs.push_back(legislator); legislator_configs.push_back(legislator);
else
self = legislator;
} }
return legislator_configs; return std::pair<std::vector<LegislatorConfig>, LegislatorConfig>
(legislator_configs, self);
} }
@ -105,8 +111,8 @@ namespace paxos
error_and_exit(1, e.what()); error_and_exit(1, e.what());
} }
std::vector<LegislatorConfig> legislators = parse_legislators(json_dict); auto legislators = parse_legislators(json_dict);
return ServerConfig(legislators); return ServerConfig(legislators.first, legislators.second);
} }
} }

View File

@ -16,10 +16,13 @@ namespace paxos
struct ServerConfig struct ServerConfig
{ {
ServerConfig(const std::vector<LegislatorConfig>& legislators); ServerConfig(const std::vector<LegislatorConfig>& legislators,
const LegislatorConfig& self);
std::vector<LegislatorConfig> legislators_; std::vector<LegislatorConfig> legislators_;
LegislatorConfig self_;
static ServerConfig parse(const std::string& path); static ServerConfig parse(const std::string& path);
}; };
} }

View File

@ -0,0 +1,12 @@
#include <memory>
#include "legislator-factory.hh"
namespace paxos
{
shared_legislator LegislatorFactory::Create(LegislatorConfig& config)
{
auto shared = std::make_shared<Legislator>(config);
return shared;
}
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "legislator.hh"
#include "config/config.hh"
namespace paxos
{
class LegislatorFactory
{
public:
static shared_legislator Create(LegislatorConfig& config);
};
}

View File

@ -0,0 +1,9 @@
#include "legislator.hh"
namespace paxos
{
Legislator::Legislator(const LegislatorConfig& config)
: config_(config)
{
}
}

View File

@ -1,12 +1,14 @@
#pragma once #pragma once
#include "legislator-config.hh" #include "config/config.hh"
namespace paxos namespace paxos
{ {
class Legislator class Legislator
{ {
public: public:
LegislatorConfig config; Legislator(const LegislatorConfig& config);
LegislatorConfig config_;
} };
using shared_legislator = std::shared_ptr<Legislator>;
} }

View File

@ -1,10 +1,15 @@
#include <iostream> #include <iostream>
#include "config/config.hh" #include "config/config.hh"
#include "legislator/legislator-factory.hh"
int main(int, char **argv) int main(int, char **argv)
{ {
paxos::ServerConfig config = paxos::ServerConfig::parse(argv[1]); paxos::ServerConfig server_config = paxos::ServerConfig::parse(argv[1]);
std::cout << config.legislators_[0].name << "\n";
for (auto config : server_config.legislators_)
{
paxos::LegislatorFactory::Create(config);
}
} }