/* sender.cpp Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "extended.h" #include "source.h" namespace sACN { /** * @brief Source::Source * @param cid * @param fctn */ Source::Source(UUID::uuid cid, std::string fctn) : Component(cid, fctn) , discovery_worker_(&Source::discovery_loop_, this) { } Source::~Source() { discovery_exitSignal_.set_value(); // set the exit signal for the discovery worker thread discovery_worker_.join(); // wait for discovery to terminate normally } /** * @brief Source::create * @param num */ void Source::create(const uint16_t num) { if (universes_.count(num)) return; auto metadata = std::make_shared(); metadata->source_name = this->name(); metadata->universe = num; std::unique_lock lk(mtx_universes_); universes_.emplace(num, std::make_shared(this)); universes_[num]->setMetadata(metadata); } /** * @brief Source::terminate * @param num */ void Source::terminate(const uint16_t num) { if (!universes_.count(num)) return; std::unique_lock lk(mtx_universes_); auto metadata = universes_.at(num)->metadata(); metadata->options.stream_terminated = true; universes_.at(num)->setMetadata(metadata); universes_.erase(num); } /** * @brief Source::sendExtendedFrame * @param vector * @param header * @param data * @param ip */ void Source::sendExtendedFrame(const uint16_t vector, std::shared_ptr header, std::shared_ptr data, const ACN::SDT::UDP::ipAddress& ip) { if (!(vector == VECTOR_E131_EXTENDED_DISCOVERY || vector == VECTOR_E131_EXTENDED_SYNCHRONIZATION)) return; auto framepdu = std::make_shared(); framepdu->setVector(vector); framepdu->setHeader(header); framepdu->setData(data); rlpSendUdp(VECTOR_ROOT_E131_EXTENDED, framepdu, ip); } /** * @brief Source::universe * @param num * @return */ std::shared_ptr Source::universe(const uint16_t num) { std::shared_lock lk(mtx_universes_); if (!universes_.count(num)) return nullptr; return universes_.at(num); } void Source::assignUserName(const std::string s) { std::shared_lock lk(mtx_universes_); ACN::Component::assignUserName(s); for( auto & [_, universe] : universes_ ) universe->metadata()->source_name = name(); } /** * @brief Source::discovery_loop_ */ void Source::discovery_loop_() { /// After terminating all universes, at least one empty universe /// discovery message should be sent. bool send_null_discovery = false; auto future = discovery_exitSignal_.get_future(); while (future.wait_for(std::chrono::milliseconds(E131_UNIVERSE_DISCOVER_INTERVAL)) != std::future_status::ready) { /// > \cite sACN 12.1 Universe Discovery and Termination /// > /// > Any source that is no longer sending any universe data may stop /// > sending E1.31 Universe Discovery Packets until such time that it /// > resumes transmission of E1.31 Data and/or Synchronization information. if (universes_.empty()) { if (send_null_discovery) send_null_discovery = false; else continue; } else send_null_discovery = true; discovery_send_(); } } /** * @brief Source::discovery_send_ */ void Source::discovery_send_() { // framing layer header auto frmheader = std::make_shared(); frmheader->source_name = name(); // universe discovery layer auto header = std::make_shared(); // known universes std::vector> list; { std::shared_lock lk(mtx_universes_); for (const auto & [num, univ] : universes_) { if (!univ->activeSlots()) continue; if (univ->metadata()->options.stream_terminated) continue; if (univ->destination.type != ACN::SDT::SDT_ADDR_NULL) continue; list.emplace_back(std::make_shared()); list.back()->universe = num; } } /// > \cite sACN 8.3 Page /// > /// > A single source may be transmitting on so many universes that the /// > total number of universes it must include in its List of Universes /// > will span multiple packets. Each one of these packets acts as a /// > “page” of those universes. /// /// > \cite sACN 8.4 Last Page /// > /// > The Universe Discovery Layer's Last Page field is an 8-bit field /// > indicating the number of the final page being to be transmitted. ... /// > Page numbers are indexed starting at 0. header->last_page = list.size() / 512; for (size_t i = 0; i <= header->last_page; i++) { /// > The Universe Discovery Layer's Page field is an 8-bit field /// > indicating the page number of this E1.31 Universe Discovery Packet. /// > Page numbers are indexed, starting at 0. header->page = i; // universe discover layer data auto data = std::make_shared(); for (size_t n = i * 512; n < (i * 512) + 512; n++) { if (n >= list.size()) break; data->found.push_back(list.at(n)); } // framing layer data auto dlistpdu = std::make_shared(); dlistpdu->setVector(VECTOR_UNIVERSE_DISCOVERY_UNIVERSE_LIST); dlistpdu->setHeader(header); dlistpdu->setData(data); // framing layer if (enable_IPv4) sendExtendedFrame(VECTOR_E131_EXTENDED_DISCOVERY, frmheader, dlistpdu, sACN::IPv4MulticastAddress(E131_DISCOVERY_UNIVERSE)); if (enable_IPv6) sendExtendedFrame(VECTOR_E131_EXTENDED_DISCOVERY, frmheader, dlistpdu, sACN::IPv6MulticastAddress(E131_DISCOVERY_UNIVERSE)); } } } // SACN