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