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

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

@ -8,8 +8,9 @@
namespace paxos
{
ServerConfig::ServerConfig(const std::vector<LegislatorConfig>& legislators)
: legislators_(legislators)
ServerConfig::ServerConfig(const std::vector<LegislatorConfig>& 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 */
std::vector<json> legislators;
@ -69,16 +71,20 @@ namespace paxos
/* Create VHostConfig vector and fill with VHostConfig */
std::vector<LegislatorConfig> legislator_configs;
LegislatorConfig self;
for (auto it : legislators)
{
/* Differenciable vhost checking */
auto legislator = it.get<paxos::LegislatorConfig>();
if (!legislator.is_self)
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());
}
std::vector<LegislatorConfig> legislators = parse_legislators(json_dict);
auto legislators = parse_legislators(json_dict);
return ServerConfig(legislators);
return ServerConfig(legislators.first, legislators.second);
}
}

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

@ -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;
}
}

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

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

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

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