|
| 1 | +#include <sustain/framework/Service.h> |
| 2 | + |
| 3 | +#include <sustain/framework/Protocol.h> |
| 4 | +#include <sustain/framework/net/Multicast_Receiver.h> |
| 5 | +#include <sustain/framework/net/Multicast_Sender.h> |
| 6 | +#include <sustain/framework/util/Error.h> |
| 7 | + |
| 8 | +namespace pfc { |
| 9 | +struct Service::Implementation { |
| 10 | + Implementation(const std::string& bind_address, const std::string& multicast_address); |
| 11 | + ~Implementation(); |
| 12 | + Implementation(const Implementation&) = delete; |
| 13 | + Implementation(Implementation&&) = default; |
| 14 | + Implementation& operator=(const Implementation&) = delete; |
| 15 | + Implementation& operator=(Implementation&&) = delete; |
| 16 | + |
| 17 | + void announce_service_creation(std::ostream&); |
| 18 | + void handle_service_broadcasr_message(std::istream&); |
| 19 | + |
| 20 | + std::function<void(pfc_service_announcment&)> service_broadcast_callback; |
| 21 | + |
| 22 | + pfc_service_announcment service_config; |
| 23 | + Config::protocol style; |
| 24 | + |
| 25 | + Multicast_Sender multicast_announcment; |
| 26 | + Multicast_Receiver multicast_broadcast_listiner; |
| 27 | + pfc::Error server_status; |
| 28 | +}; |
| 29 | +//----------------------------------------------------------------------------- |
| 30 | +Service::Implementation::Implementation(const std::string& bind_address, const std::string& multicast_address) |
| 31 | + : multicast_announcment(multicast_address, g_pfc_registry_reg_port) |
| 32 | + , multicast_broadcast_listiner(bind_address, multicast_address, g_pfc_registry_announce_port) |
| 33 | +{ |
| 34 | +} |
| 35 | +//----------------------------------------------------------------------------- |
| 36 | +Service::Implementation::~Implementation() |
| 37 | +{ |
| 38 | + multicast_announcment.stop(); |
| 39 | + multicast_broadcast_listiner.stop(); |
| 40 | + |
| 41 | + multicast_announcment.join(); |
| 42 | + multicast_broadcast_listiner.join(); |
| 43 | +} |
| 44 | +//----------------------------------------------------------------------------- |
| 45 | +void Service::Implementation::announce_service_creation(std::ostream& os) |
| 46 | +{ |
| 47 | + service_config.serialize(os); |
| 48 | + std::cout << "Sending: " << service_config << "\n"; |
| 49 | +} |
| 50 | +//----------------------------------------------------------------------------- |
| 51 | +void Service::Implementation::handle_service_broadcasr_message(std::istream& is) |
| 52 | +{ |
| 53 | + pfc_service_announcment announcment; |
| 54 | + if (announcment.deserialize(is).is_ok()) { |
| 55 | + std::cout << "Registry Sent:" << announcment << "\n"; |
| 56 | + if(service_broadcast_callback) |
| 57 | + { service_broadcast_callback(announcment); } |
| 58 | + } |
| 59 | +} |
| 60 | +//----------------------------------------------------------------------------- |
| 61 | +Service::Service(const Config config, const std::string& multicast_bind_address, const std::string& registry_multicast_address) |
| 62 | + : _impl(std::make_unique<Implementation>(std::move(multicast_bind_address), std::move(registry_multicast_address))) |
| 63 | +{ |
| 64 | + pfc_service_announcment service; |
| 65 | + service._name = config.name; |
| 66 | + service._protacol = (config.style == Config::pub_sub) ? pfc_protocol::pub_sub : pfc_protocol::req_req; |
| 67 | + service._address = config.address; |
| 68 | + service._brief = config.brief; |
| 69 | + service._port = config.port; |
| 70 | + |
| 71 | + _impl->style = config.style; |
| 72 | + _impl->service_config = service; |
| 73 | +} |
| 74 | +//----------------------------------------------------------------------------- |
| 75 | +Service::Service(Service&& obj) |
| 76 | + : _impl(std::move(obj._impl)) |
| 77 | +{ |
| 78 | +} |
| 79 | +//----------------------------------------------------------------------------- |
| 80 | +Service::~Service() |
| 81 | +{ |
| 82 | + stop(); |
| 83 | + join(); |
| 84 | +} |
| 85 | +//----------------------------------------------------------------------------- |
| 86 | +void Service::start() |
| 87 | +{ |
| 88 | + using namespace std::placeholders; |
| 89 | + auto& impl = *_impl; |
| 90 | + _impl->multicast_announcment.async_send(std::bind(&Implementation::announce_service_creation, _impl.get(), _1)); |
| 91 | + _impl->multicast_broadcast_listiner.async_receive(std::bind(&Implementation::handle_service_broadcasr_message, _impl.get(), _1)); |
| 92 | +} |
| 93 | +//----------------------------------------------------------------------------- |
| 94 | +void Service::stop() |
| 95 | +{ |
| 96 | + _impl->multicast_announcment.stop(); |
| 97 | + _impl->multicast_broadcast_listiner.stop(); |
| 98 | +} |
| 99 | +//----------------------------------------------------------------------------- |
| 100 | +void Service::join() |
| 101 | +{ |
| 102 | + _impl->multicast_announcment.join(); |
| 103 | + _impl->multicast_broadcast_listiner.join(); |
| 104 | +} |
| 105 | +//----------------------------------------------------------------------------- |
| 106 | +void Service::set_service_announcment_callback(std::function<void(pfc_service_announcment&)> func) |
| 107 | +{ |
| 108 | + _impl->service_broadcast_callback = func; |
| 109 | +} |
| 110 | +//----------------------------------------------------------------------------- |
| 111 | +auto Service::name() const -> std::string |
| 112 | +{ |
| 113 | + return _impl->service_config._name; |
| 114 | +} |
| 115 | +//----------------------------------------------------------------------------- |
| 116 | +auto Service::address() const -> std::string |
| 117 | +{ |
| 118 | + return _impl->service_config._address; |
| 119 | +} |
| 120 | +//----------------------------------------------------------------------------- |
| 121 | +auto Service::brief() const -> std::string |
| 122 | +{ |
| 123 | + return _impl->service_config._brief; |
| 124 | +} |
| 125 | +//----------------------------------------------------------------------------- |
| 126 | +auto Service::port() const -> uint16_t |
| 127 | +{ |
| 128 | + return _impl->service_config._port; |
| 129 | +} |
| 130 | +//----------------------------------------------------------------------------- |
| 131 | +auto Service::style() const -> Config::protocol |
| 132 | +{ |
| 133 | + return _impl->style; |
| 134 | +} |
| 135 | +//----------------------------------------------------------------------------- |
| 136 | +bool Service::valid() const |
| 137 | +{ |
| 138 | + return _impl->server_status.is_ok(); |
| 139 | +} |
| 140 | +//----------------------------------------------------------------------------- |
| 141 | +Error Service::error() const |
| 142 | +{ |
| 143 | + return _impl->server_status; |
| 144 | +} |
| 145 | +//----------------------------------------------------------------------------- |
| 146 | +Service& Service::operator=(Service&& obj) |
| 147 | +{ |
| 148 | + _impl = std::move(obj._impl); |
| 149 | + return *this; |
| 150 | +} |
| 151 | +} |
0 commit comments