sample_peer.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "sample_peer.h"
  2. using namespace syncspirit::test;
  3. void sample_peer_t::configure(r::plugin::plugin_base_t &plugin) noexcept {
  4. r::actor_base_t::configure(plugin);
  5. plugin.with_casted<r::plugin::starter_plugin_t>([&](auto &p) {
  6. p.subscribe_actor(&sample_peer_t::on_start_reading);
  7. p.subscribe_actor(&sample_peer_t::on_block_request);
  8. });
  9. if (configure_callback) {
  10. configure_callback(plugin);
  11. }
  12. }
  13. void sample_peer_t::on_start_reading(message::start_reading_t &) noexcept {
  14. ++start_reading;
  15. }
  16. void sample_peer_t::on_block_request(message::block_request_t &req) noexcept {
  17. assert(responses.size());
  18. requests.push_front(&req);
  19. while (requests.size() && requests.front()->payload.request_payload.block_index == responses.front().block_index) {
  20. reply_to(*requests.front(), responses.front().data);
  21. responses.pop_front();
  22. requests.pop_front();
  23. }
  24. }
  25. void sample_peer_t::push_response(const std::string& data, size_t index) noexcept {
  26. if (index == next_block) {
  27. index = responses.size();
  28. }
  29. responses.push_back(response_t{index, data});
  30. }