|
10 | 10 | namespace server_detail { |
11 | 11 | class impl { |
12 | 12 | public: |
13 | | - explicit impl(int concurrency) : ioc(concurrency) { |
| 13 | + explicit impl(int concurrency) : ioc_(concurrency) { |
14 | 14 | using reading = MeterReadingController; |
15 | 15 | using price_plan = PricePlanComparatorController; |
16 | | - router.to<reading, &reading::Read>(R"(/readings/read/([a-zA-Z0-9_-]+))", electricityReadingService, meterReadingService); |
17 | | - router.to<reading, &reading::Store>(R"(/readings/store)", electricityReadingService, meterReadingService); |
18 | | - router.to<price_plan, &price_plan::Compare>(R"(/price-plans/compare-all/([a-zA-Z0-9_-]+))", pricePlanService); |
19 | | - router.to<price_plan, &price_plan::Recommend>(R"(/price-plans/recommend/([a-zA-Z0-9_-]+)\?(limit)=([0-9]+))", |
20 | | - pricePlanService); |
| 16 | + router_.to<reading, &reading::Read>(R"(/readings/read/([a-zA-Z0-9_-]+))", electricityReadingService_, meterReadingService); |
| 17 | + router_.to<reading, &reading::Store>(R"(/readings/store)", electricityReadingService_, meterReadingService); |
| 18 | + router_.to<price_plan, &price_plan::Compare>(R"(/price-plans/compare-all/([a-zA-Z0-9_-]+))", pricePlanService_); |
| 19 | + router_.to<price_plan, &price_plan::Recommend>(R"(/price-plans/recommend/([a-zA-Z0-9_-]+)\?(limit)=([0-9]+))", |
| 20 | + pricePlanService_); |
21 | 21 | } |
22 | 22 |
|
23 | 23 | void launch(const char *address, unsigned short port) { |
24 | 24 | using tcp = boost::asio::ip::tcp; |
25 | 25 | auto endpoint = tcp::endpoint{boost::asio::ip::make_address(address), port}; |
26 | | - std::make_shared<listener>(ioc, endpoint, handler)->run(); |
| 26 | + std::make_shared<listener>(ioc_, endpoint, handler)->run(); |
27 | 27 | } |
28 | 28 |
|
29 | | - void run() { ioc.run(); } |
| 29 | + void run() { ioc_.run(); } |
30 | 30 |
|
31 | | - void stop() { ioc.stop(); } |
| 31 | + void stop() { ioc_.stop(); } |
32 | 32 |
|
33 | 33 | private: |
34 | | - boost::asio::io_context ioc; |
35 | | - std::unordered_map<std::string, std::vector<ElectricityReading>> meterAssociatedReadings{readings()}; |
36 | | - ElectricityReadingService electricityReadingService{meterAssociatedReadings}; |
37 | | - MeterReadingService meterReadingService{meterAssociatedReadings}; |
38 | | - std::vector<PricePlan> price_plans{pricePlans()}; |
39 | | - PricePlanService pricePlanService{price_plans, meterReadingService}; |
40 | | - router router; |
41 | | - std::function<http::response<http::string_body>(const http::request<http::string_body> &)> handler = router.handler(); |
| 34 | + boost::asio::io_context ioc_; |
| 35 | + std::unordered_map<std::string, std::vector<ElectricityReading>> meterAssociatedReadings_{readings()}; |
| 36 | + ElectricityReadingService electricityReadingService_{meterAssociatedReadings_}; |
| 37 | + MeterReadingService meterReadingService{meterAssociatedReadings_}; |
| 38 | + std::vector<PricePlan> price_plans_{pricePlans()}; |
| 39 | + PricePlanService pricePlanService_{price_plans_, meterReadingService}; |
| 40 | + router router_; |
| 41 | + std::function<http::response<http::string_body>(const http::request<http::string_body> &)> handler = router_.handler(); |
42 | 42 | }; |
43 | 43 | } // namespace server_detail |
44 | 44 |
|
45 | | -server::server(int concurrency) : impl(std::make_unique<server_detail::impl>(concurrency)), concurrency(concurrency) {} |
| 45 | +server::server(int concurrency) : impl_(std::make_unique<server_detail::impl>(concurrency)), concurrency_(concurrency) {} |
46 | 46 |
|
47 | 47 | server::~server() { |
48 | | - impl->stop(); |
49 | | - for (auto &worker : threads) { |
| 48 | + impl_->stop(); |
| 49 | + for (auto &worker : threads_) { |
50 | 50 | if (worker.joinable()) { |
51 | 51 | worker.join(); |
52 | 52 | } |
53 | 53 | } |
54 | 54 | } |
55 | 55 |
|
56 | 56 | void server::run(const char *address, unsigned short port) { |
57 | | - impl->launch(address, port); |
58 | | - threads.reserve(concurrency); |
59 | | - for (auto i = concurrency; i > 0; --i) { |
60 | | - threads.emplace_back([this] { impl->run(); }); |
| 57 | + impl_->launch(address, port); |
| 58 | + threads_.reserve(concurrency_); |
| 59 | + for (auto i = concurrency_; i > 0; --i) { |
| 60 | + threads_.emplace_back([this] { impl_->run(); }); |
61 | 61 | } |
62 | 62 | } |
0 commit comments