Falcon
Loading...
Searching...
No Matches
istreamports.hpp
1// ---------------------------------------------------------------------
2// This file is part of falcon-core.
3//
4// Copyright (C) 2015, 2016, 2017 Neuro-Electronics Research Flanders
5//
6// Falcon-server is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Falcon-server is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with falcon-core. If not, see <http://www.gnu.org/licenses/>.
18// ---------------------------------------------------------------------
19
20#pragma once
21
22#include <memory>
23#include <set>
24#include <string>
25#include <vector>
26
27#include "connections.hpp"
28#include "idata.hpp"
29#include "portpolicy.hpp"
30#include "streaminfo.hpp"
31
32#include "yaml-cpp/yaml.h"
33
34class IPortOut;
35class ISlotOut;
36class IPortIn;
37class ISlotIn;
38class IProcessor;
39
40class ISlotOut {
41 friend class ISlotIn;
42 template <typename DATATYPE>
43 friend class SlotIn;
44 friend class IPortOut;
45
46 public:
47 ISlotOut(IPortOut* parent, const SlotAddress& address)
48 : ring_batch_(1), buffer_size_(-1), parent_(parent), address_(address) {}
49
50 virtual ~ISlotOut() {}
51
52 const SlotAddress& address() const { return address_; }
53 IPortOut* parent() { return parent_; }
54 bool connected() const { return downstream_slots_.size() > 0; }
55 int nconnected() const { return downstream_slots_.size(); }
56 virtual IStreamInfo& streaminfo() = 0;
57 int buffer_size() const { return buffer_size_; }
58
59 protected:
60 // called by IPortOut
61 void Connect(ISlotIn* downstream);
62
63 // called by SlotIn
64 int64_t WaitFor(int64_t sequence) const { return barrier_->WaitFor(sequence); }
65 int64_t WaitFor(int64_t sequence, int64_t time_out) const {
66 return barrier_->WaitFor(sequence, time_out);
67 }
68
69 virtual typename AnyType::Data* DataAt(int64_t sequence) const = 0;
70 std::vector<RingSequence*> gating_sequences();
71
72 protected:
73 RingBatch ring_batch_;
74 bool has_publishable_data_ = false;
75
76 // need to go through base class, since we don't know
77 // the exact datatype of downstream slots
78 std::set<ISlotIn*> downstream_slots_;
79
80 std::unique_ptr<RingBarrier> barrier_ = nullptr;
81 int buffer_size_;
82
83 IPortOut* parent_; // observing pointer
84 SlotAddress address_;
85};
86
87class IPortOut {
88 friend class ProcessorEngine;
89 friend class IProcessor;
90
91 public:
92 IPortOut(IProcessor* parent, const PortAddress& address, PortOutPolicy policy)
93 : parent_(parent), address_(address), policy_(policy) {}
94
95 virtual ~IPortOut() {}
96 const PortAddress& address() const { return address_; }
97 const PortOutPolicy& policy() const { return policy_; }
98 IProcessor* parent() { return parent_; }
99
100 virtual std::string datatype() const = 0;
101 virtual ISlotOut* slot(std::size_t index) = 0;
102 virtual SlotType number_of_slots() const = 0;
103
104 YAML::Node ExportYAML() const;
105 std::string name() const { return address_.port(); }
106
107 protected:
108 // called by StreamOutConnector
109 virtual void Connect(int slot, ISlotIn* downstream) = 0;
110 virtual int ReserveSlot(int slot) = 0;
111 virtual void CreateRingBuffers() = 0;
112 virtual void UnlockSlots() = 0;
113 virtual void PrepareProcessing() = 0;
114 virtual void NewSlot(int n = 1) = 0;
115
116 void set_buffer_size(int sz) { policy_.set_buffer_size(sz); }
117
118 IProcessor* parent_; // observing pointer
119 PortAddress address_;
120
121 private:
122 std::string name_;
123 PortOutPolicy policy_;
124};
125
126class ISlotIn {
127 friend class IPortIn;
128 template <typename DATATYPE>
129 friend class PortIn;
130 friend class ISlotOut;
131
132 public:
133 ISlotIn(IPortIn* parent, const SlotAddress& address, bool cache = false)
134 : cache_enabled_(cache), parent_(parent), address_(address) {}
135
136 virtual ~ISlotIn() {}
137
138 const SlotAddress& address() const { return address_; }
139 IPortIn* parent() { return parent_; }
140
141 void NegotiateUpstream();
142 bool connected() const { return upstream_ != nullptr; }
143 void ReleaseData();
144
145 const SlotAddress& upstream_address() {
146 if (upstream_ == nullptr) {
147 throw std::runtime_error("Cannot get upstream address: slot is not connected.");
148 }
149 return upstream_->address();
150 }
151
152 const PortOutPolicy& upstream_policy() const {
153 if (upstream_ == nullptr) {
154 throw std::runtime_error("Cannot get upstream policy: slot is not connected.");
155 }
156 return upstream_->parent()->policy();
157 }
158
159 virtual void Validate() = 0;
160
161 protected:
162 // called by upstream ISlotOut
163 RingSequence* sequence() { return &sequence_; }
164
165 // called by IPortIn
166 void Connect(ISlotOut* upstream);
167 void PrepareProcessing();
168
169 protected:
170 bool cache_enabled_;
171 int64_t ncached_ = 0;
172 int64_t nretrieved_ = 0;
173
174 typename AnyType::Data* cache_ = nullptr;
175 // access to upstream slot needs to go through base pointer
176 // (since we don't know the exact datatype)
177 ISlotOut* upstream_ = nullptr;
178
179 RingSequence sequence_; // the input slot's read cursor into the buffer
180 IPortIn* parent_; // observing pointer
181 SlotAddress address_;
182};
183
184class IPortIn {
185 friend class ProcessorEngine;
186 friend class IProcessor;
187
188 public:
189 IPortIn(IProcessor* parent, const PortAddress& address, PortInPolicy policy)
190 : parent_(parent), address_(address), policy_(policy) {}
191
192 virtual ~IPortIn() {}
193
194 const PortAddress& address() const { return address_; }
195 const PortInPolicy& policy() const { return policy_; }
196 IProcessor* parent() { return parent_; }
197
198 virtual std::string datatype() const = 0;
199 virtual SlotType number_of_slots() const = 0;
200 virtual ISlotIn* slot(std::size_t index) = 0;
201
202 YAML::Node ExportYAML() const;
203 std::string name() const { return address_.port(); }
204
205 protected:
206 // called by StreamInConnector
207 virtual void Connect(int slot, ISlotOut* upstream) = 0;
208 virtual int ReserveSlot(int slot) = 0;
209 // called by ...
210 virtual void PrepareProcessing() = 0;
211 virtual void UnlockSlots() = 0;
212
213 IProcessor* parent_; // observing pointer
214 PortAddress address_;
215
216 private:
217 std::string name_;
218 PortInPolicy policy_;
219};
Definition istreamports.hpp:184
Definition istreamports.hpp:87
Definition iprocessor.hpp:59
Definition istreamports.hpp:126
Definition istreamports.hpp:40
Definition streaminfo.hpp:31
Definition connections.hpp:24
Definition portpolicy.hpp:43
Definition portpolicy.hpp:54
Definition connections.hpp:54