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