|
| 1 | +#include <iostream> |
| 2 | + |
| 3 | +#include <reactor-cpp/mutations/bank.hh> |
| 4 | +#include <reactor-cpp/mutations/connection.hh> |
| 5 | + |
| 6 | +#include "../../lib/mutation/bank.cc" |
| 7 | +#include "../../lib/mutation/connection.cc" |
| 8 | +#include "./consumer.hh" |
| 9 | +#include "./load_balancer.hh" |
| 10 | +#include "./producer.hh" |
| 11 | +#include <reactor-cpp/reactor-cpp.hh> |
| 12 | + |
| 13 | +class Deployment final : public Reactor { // NOLINT |
| 14 | + class Inner : public MutableScope { |
| 15 | + int state = 0; |
| 16 | + |
| 17 | + public: |
| 18 | + Inner(Reactor* reactor) |
| 19 | + : MutableScope(reactor) {} |
| 20 | + void reaction_1(const Input<unsigned>& scale, std::vector<std::unique_ptr<Consumer>>& reactor_bank, |
| 21 | + ModifableMultiport<Output<unsigned>>& load_balancer) { |
| 22 | + std::size_t new_size = *scale.get(); |
| 23 | + std::size_t old_size = reactor_bank.size(); |
| 24 | + |
| 25 | + std::function lambda = [](Reactor* reactor, std::size_t index) { |
| 26 | + std::string _lf_inst_name = "consumer_" + std::to_string(index); |
| 27 | + return std::make_unique<Consumer>(_lf_inst_name, reactor->environment(), index); |
| 28 | + }; |
| 29 | + |
| 30 | + auto change_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<Consumer>>>( |
| 31 | + &reactor_bank, this->reactor_, new_size, lambda); |
| 32 | + |
| 33 | + add_to_transaction(change_size); |
| 34 | + |
| 35 | + commit_transaction(); |
| 36 | + |
| 37 | + if (old_size < new_size) { |
| 38 | + for (auto i = 0; i < new_size; i++) { |
| 39 | + auto add_conn = std::make_shared<MutationAddConnection<Output<unsigned>, Input<unsigned>>>( |
| 40 | + &load_balancer[i], &reactor_bank[i].get()->in, reactor_); |
| 41 | + add_to_transaction(add_conn); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + commit_transaction(true); |
| 46 | + } |
| 47 | + |
| 48 | + friend LoadBalancer; |
| 49 | + }; |
| 50 | + |
| 51 | + std::unique_ptr<Producer> producer_; |
| 52 | + std::unique_ptr<LoadBalancer> load_balancer_; |
| 53 | + std::vector<std::unique_ptr<Consumer>> consumers_; |
| 54 | + |
| 55 | + Reaction scale_bank{"scale_bank", 1, this, |
| 56 | + [this]() { this->_inner.reaction_1(this->scale, this->consumers_, load_balancer_->out); }}; |
| 57 | + |
| 58 | + Inner _inner; |
| 59 | + |
| 60 | +public: |
| 61 | + Deployment(const std::string& name, Environment* env) |
| 62 | + : Reactor(name, env) |
| 63 | + , _inner(this) |
| 64 | + , producer_(std::make_unique<Producer>("producer", environment())) |
| 65 | + , load_balancer_(std::make_unique<LoadBalancer>("load_balancer", environment())) { |
| 66 | + std::cout << "creating instance of deployment" << '\n'; |
| 67 | + consumers_.reserve(4); |
| 68 | + for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) { |
| 69 | + std::string _lf_inst_name = "consumer_" + std::to_string(_lf_idx); |
| 70 | + consumers_.push_back(std::make_unique<Consumer>(_lf_inst_name, environment(), _lf_idx)); |
| 71 | + } |
| 72 | + } |
| 73 | + ~Deployment() override = default; |
| 74 | + |
| 75 | + Input<unsigned> scale{"scale", this}; // NOLINT |
| 76 | + |
| 77 | + void assemble() override { |
| 78 | + for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) { |
| 79 | + environment()->draw_connection(load_balancer_->out[_lf_idx], consumers_[_lf_idx]->in, ConnectionProperties{}); |
| 80 | + environment()->draw_connection(producer_->value, load_balancer_->inbound, ConnectionProperties{}); |
| 81 | + } |
| 82 | + environment()->draw_connection(load_balancer_->scale_bank, scale, ConnectionProperties{}); |
| 83 | + scale_bank.declare_trigger(&this->scale); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +auto main() -> int { |
| 88 | + Environment env{4, true}; |
| 89 | + auto deployment = std::make_unique<Deployment>("c1", &env); |
| 90 | + env.optimize(); |
| 91 | + env.assemble(); |
| 92 | + auto thread = env.startup(); |
| 93 | + thread.join(); |
| 94 | + return 0; |
| 95 | +} |
0 commit comments