Self if now also in legislators map

This commit is contained in:
Julien CLEMENT 2020-05-06 19:07:37 +02:00
parent 5288ed69f4
commit 53d36d8569
3 changed files with 12 additions and 20 deletions

@ -8,9 +8,8 @@
namespace paxos namespace paxos
{ {
ServerConfig::ServerConfig(const std::vector<LegislatorConfig>& legislators, ServerConfig::ServerConfig(const std::vector<LegislatorConfig>& legislators)
const LegislatorConfig& self) : legislators_(legislators)
: legislators_(legislators), self_(self)
{ {
} }
@ -60,8 +59,8 @@ namespace paxos
} }
static std::pair<std::vector<LegislatorConfig>, LegislatorConfig> static std::vector<LegislatorConfig> parse_legislators(const json& j,
parse_legislators(const json& j, const std::string& name) const std::string& name)
{ {
/* Get 'vhosts' value */ /* Get 'vhosts' value */
std::vector<json> legislators; std::vector<json> legislators;
@ -69,21 +68,16 @@ 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>();
legislator.is_self = name == legislator.name; legislator.is_self = name == legislator.name;
if (!legislator.is_self)
legislator_configs.push_back(legislator); legislator_configs.push_back(legislator);
else
self = legislator;
} }
return std::pair<std::vector<LegislatorConfig>, LegislatorConfig> return legislator_configs;
(legislator_configs, self);
} }
@ -115,6 +109,6 @@ namespace paxos
auto legislators = parse_legislators(json_dict, name); auto legislators = parse_legislators(json_dict, name);
return ServerConfig(legislators.first, legislators.second); return ServerConfig(legislators);
} }
} }

@ -16,13 +16,10 @@ 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(char **argv); static ServerConfig parse(char **argv);
}; };
} }

@ -36,11 +36,12 @@ int main(int argc, char **argv)
ev_signal_init(&sigstop_watcher, sigstop_cb, SIGTSTP); ev_signal_init(&sigstop_watcher, sigstop_cb, SIGTSTP);
paxos::event_register.get_event_loop().register_sigint_watcher(&sigstop_watcher); paxos::event_register.get_event_loop().register_sigint_watcher(&sigstop_watcher);
paxos::self = paxos::LegislatorFactory::Create(server_config.self_);
for (auto config : server_config.legislators_) for (auto config : server_config.legislators_)
{ {
paxos::legislators.insert(std::pair(config.name, paxos::LegislatorFactory::Create(config))); paxos::shared_legislator legislator = paxos::LegislatorFactory::Create(config);
paxos::legislators.insert(std::pair(config.name, legislator));
if (config.is_self)
paxos::self = legislator;
} }
paxos::event_register.get_event_loop()(); paxos::event_register.get_event_loop()();