Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
receiver_abstraction.h
Go to the documentation of this file.
1// NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2// software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3// derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4// or works. Modified works should carry a notice stating that you changed the software and should note the date and
5// nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6// source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7// EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9// WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10// CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11// THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12// are solely responsible for determining the appropriateness of using and distributing the software and you assume
13// all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14// with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15// operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16// damage to property. The software developed by NIST employees is not subject to copyright protection within the
17// United States.
18
19
20
21#ifndef HEDGEHOG_RECEIVER_ABSTRACTION_H
22#define HEDGEHOG_RECEIVER_ABSTRACTION_H
23
24#include <memory>
25#include <utility>
26#include <ostream>
27
28#include "sender_abstraction.h"
29#include "../any_groupable_abstraction.h"
30#include "../node/node_abstraction.h"
31
33namespace hh {
35namespace core {
36
37#ifndef DOXYGEN_SHOULD_SKIP_THIS
39namespace implementor {
42template<class Input>
43class ImplementorReceiver;
44}
45#endif //DOXYGEN_SHOULD_SKIP_THIS
46
47
49namespace abstraction {
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
54template<class Output>
55class SenderAbstraction;
56#endif //DOXYGEN_SHOULD_SKIP_THIS
57
60template<class Input>
62 private:
63 std::shared_ptr<implementor::ImplementorReceiver<Input>>
65
66 std::shared_ptr<std::mutex> const
67 slotMutex_ = nullptr;
68
69 public:
74 std::shared_ptr<implementor::ImplementorReceiver<Input>> concreteReceiver,
75 std::shared_ptr<std::mutex> slotMutex) :
76 concreteReceiver_(std::move(concreteReceiver)),
77 slotMutex_(std::move(slotMutex)) {
78 concreteReceiver_->initialize(this);
79 }
80
82 virtual ~ReceiverAbstraction() = default;
83
88 [[nodiscard]] std::set<ReceiverAbstraction *> const &receivers() const { return concreteReceiver_->receivers(); }
89
94 [[nodiscard]] std::set<ReceiverAbstraction *> &receivers() { return concreteReceiver_->receivers(); }
95
98 [[nodiscard]] std::set<SenderAbstraction<Input> *> const &connectedSenders() const {
99 return concreteReceiver_->connectedSenders();
100 }
101
106 void receive(std::shared_ptr<Input> const inputData) {
107 slotMutex_->lock();
108 concreteReceiver_->receive(inputData);
109 slotMutex_->unlock();
110 }
111
114 std::shared_ptr<Input> getInputData() { return concreteReceiver_->getInputData(); }
115
118 [[nodiscard]] size_t numberElementsReceived() const { return concreteReceiver_->numberElementsReceived(); }
119
122 [[nodiscard]] size_t maxNumberElementsReceived() const { return concreteReceiver_->maxNumberElementsReceived(); }
123
126 [[nodiscard]] bool empty() const { return concreteReceiver_->empty(); }
127
130 void addSender(SenderAbstraction<Input> *const sender) { concreteReceiver_->addSender(sender); }
131
134 void removeSender(SenderAbstraction<Input> *const sender) { concreteReceiver_->removeSender(sender); }
135
140 auto nodeReceiver = dynamic_cast<NodeAbstraction const *>(this);
141 if (nodeReceiver == nullptr) {
142 throw std::runtime_error("To print an edge, a receiver should be a NodeAbstraction.");
143 }
144
145 for (auto receiver : ReceiverAbstraction<Input>::receivers()) {
146 for (SenderAbstraction<Input> *connectedSender : receiver->connectedSenders()) {
147 for (auto &s : connectedSender->senders()) {
148 auto nodeSender = dynamic_cast<NodeAbstraction const *>(s);
149 if (nodeSender == nullptr) {
150 throw std::runtime_error("To print an edge, a sender should be a NodeAbstraction.");
151 }
152 printer->printEdge(
153 nodeSender, nodeReceiver,
154 tool::typeToStr<Input>(),
155 receiver->numberElementsReceived(), receiver->maxNumberElementsReceived());
156 }
157 }
158 }
159 }
160
161 protected:
165 this->concreteReceiver_ = copyableCore->concreteReceiver_;
166 }
167};
168}
169}
170}
171#endif //HEDGEHOG_RECEIVER_ABSTRACTION_H
Hedgehog main namespace.
Printer abstraction to get a snapshot of the metrics of the Hedgehog graph.
Definition: printer.h:52
virtual void printEdge(core::abstraction::NodeAbstraction const *from, core::abstraction::NodeAbstraction const *to, std::string const &edgeType, size_t const &queueSize, size_t const &maxQueueSize)=0
Print edge information.
Core's abstraction to receive a piece of data.
void copyInnerStructure(ReceiverAbstraction< Input > *copyableCore)
Copy inner structure of the receiver to this one.
std::shared_ptr< std::mutex > const slotMutex_
Mutex shared from the slot abstraction.
void removeSender(SenderAbstraction< Input > *const sender)
Remove a SenderAbstraction to the concrete receiver implementation.
virtual ~ReceiverAbstraction()=default
Default destructor.
std::set< ReceiverAbstraction * > & receivers()
Accessor to receivers.
size_t maxNumberElementsReceived() const
Accessor to the maximum number of input data received and waiting to be processed.
std::shared_ptr< Input > getInputData()
Get an input data from the concrete receiver implementation.
std::set< SenderAbstraction< Input > * > const & connectedSenders() const
Accessor to the senders attached to this receiver.
void printEdgeInformation(Printer *printer)
Add to the printer the edge information.
void receive(std::shared_ptr< Input > const inputData)
Receive a piece of data.
ReceiverAbstraction(std::shared_ptr< implementor::ImplementorReceiver< Input > > concreteReceiver, std::shared_ptr< std::mutex > slotMutex)
Constructor using a concrete implementation of a receiver implementor, and the mutex from the slot.
std::shared_ptr< implementor::ImplementorReceiver< Input > > concreteReceiver_
Concrete implementation of the interface.
std::set< ReceiverAbstraction * > const & receivers() const
Const accessor to receivers.
bool empty() const
Test if the receiver is empty.
size_t numberElementsReceived() const
Accessor to the current number of input data received and waiting to be processed.
void addSender(SenderAbstraction< Input > *const sender)
Add a SenderAbstraction to the concrete receiver implementation.
Core abstraction to send data.
Implementor for the ReceiverAbstraction.