Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Hub.hxx
Go to the documentation of this file.
1
34#ifndef _UTILS_HUB_HXX_
35#define _UTILS_HUB_HXX_
36
37#include <stdint.h>
38#include <string>
39
41#include "can_frame.h"
42
43class PipeBuffer;
44class PipeMember;
45
47template<class S> class StructContainer : public S {
48public:
50 S& value() {
51 return *this;
52 }
53
55 const void* data() const {
56 return &value();
57 }
58
60 void* data() {
61 return &value();
62 }
63
65 size_t size() {
66 return sizeof(S);
67 }
68};
69
71struct CanFrameContainer : public StructContainer<can_frame>
72{
73 /* Constructor. Sets up (outgoing) frames to be empty extended frames by
74 * default. */
76 {
77 can_id = 0;
78 CLR_CAN_FRAME_ERR(*this);
79 CLR_CAN_FRAME_RTR(*this);
80 SET_CAN_FRAME_EFF(*this);
81 can_dlc = 0;
82 }
83
85 struct can_frame *mutable_frame()
86 {
87 return this;
88 }
90 const struct can_frame &frame() const
91 {
92 return *this;
93 }
94};
95
100template <class T> class HubContainer : public T
101{
102public:
103 // typedef FlowInterface<Buffer<HubContainer<T>>> HubMember;
105 {
106 }
108 typedef uintptr_t id_type;
114 {
115 return reinterpret_cast<uintptr_t>(skipMember_);
116 }
117};
118
126
135
144
146static const uintptr_t POINTER_MASK = UINTPTR_MAX;
147
149template<class D> class GenericHubFlow : public DispatchFlow<Buffer<D>, 1>
150{
151public:
153 typedef D value_type;
158
161 {
162 this->negateMatch_ = true;
163 }
164
168 {
169 this->register_handler(port, reinterpret_cast<uintptr_t>(port),
171 }
172
175 {
176 this->unregister_handler(port, reinterpret_cast<uintptr_t>(port),
178 }
179};
180
185
187class DisplayPort : public HubPort
188{
189public:
193 DisplayPort(Service *service, bool timestamped)
195 , timestamped_(timestamped)
196 {
197 }
198
199 Action entry() override
200 {
201 string s(message()->data()->data(), message()->data()->size());
202 if (timestamped_) {
203 long long ts = os_get_time_monotonic();
204 printf("%lld.%06lld: %s", ts / 1000000000, (ts / 1000) % 1000000,
205 s.c_str());
206 } else {
207 printf("%s", s.c_str());
208 }
209 return release_and_exit();
210 }
211
212private:
215};
216
217
220public:
222 int fd()
223 {
224 return fd_;
225 }
226
227protected:
228 FdHubPortInterface() : fd_(-1) {}
229
230 FdHubPortInterface(int fd) : fd_(fd) {}
231
233 int fd_{-1};
234};
235
236namespace openlcb
237{
238class FdToTcpParser;
239}
240
243{
244public:
246 virtual void report_write_error() = 0;
247
249 virtual void report_read_error() = 0;
250
251protected:
252 // For barrier_.
253 template <class HFlow> friend class HubDeviceSelectReadFlow;
254 friend class openlcb::FdToTcpParser;
255
261 , Service(exec)
262 {
263 }
264
267};
268
269#endif // _UTILS_HUB_HXX_
FlowInterface< Buffer< HubData > > HubPortInterface
All ports interfacing via a hub will have to derive from this flow.
Definition Hub.hxx:137
static const uintptr_t POINTER_MASK
This should work for both 32 and 64-bit architectures.
Definition Hub.hxx:146
StateFlow< Buffer< HubData >, QList< 1 > > HubPort
Base class for a port to an ascii hub that is implemented as a stateflow.
Definition Hub.hxx:139
StateFlow< Buffer< CanHubData >, QList< 1 > > CanHubPort
Base class for a port to an CAN hub that is implemented as a stateflow.
Definition Hub.hxx:143
GenericHubFlow< HubData > HubFlow
A generic hub that proxies packets of untyped (aka string) data.
Definition Hub.hxx:182
HubContainer< CanFrameContainer > CanHubData
This class can be sent via a Buffer to a CAN hub.
Definition Hub.hxx:134
FlowInterface< Buffer< CanHubData > > CanHubPortInterface
Interface class for a port to an CAN hub.
Definition Hub.hxx:141
HubContainer< string > HubData
This class can be sent via a Buffer to a hub.
Definition Hub.hxx:125
GenericHubFlow< CanHubData > CanHubFlow
A hub that proxies packets of CAN frames.
Definition Hub.hxx:184
A BarrierNotifiable allows to create a number of child Notifiable and wait for all of them to finish.
Base class for all QMember types that hold data in an expandable format.
Definition Buffer.hxx:195
Base class of everything with a virtual destructor.
Type-specific implementations of the DispatchFlow methods.
void register_handler(HandlerType *handler, ID id, ID mask)
Adds a new handler to this dispatcher.
void unregister_handler(HandlerType *handler, ID id, ID mask)
Removes a specific instance of a handler from this dispatcher.
This port prints all traffic from a (string-typed) hub to stdout.
Definition Hub.hxx:188
bool timestamped_
true if the timestamp and debug info should be printed.
Definition Hub.hxx:213
Action entry() override
Entry into the StateFlow activity.
Definition Hub.hxx:199
DisplayPort(Service *service, bool timestamped)
Constructor.
Definition Hub.hxx:193
This class implements an execution of tasks pulled off an input queue.
Definition Executor.hxx:64
Shared base class for thread-based and select-based hub devices.
Definition Hub.hxx:219
int fd_
The device file descriptor.
Definition Hub.hxx:233
Shared base class for thread-based and select-based hub devices.
Definition Hub.hxx:243
FdHubPortService(ExecutorBase *exec, int fd)
Constructor.
Definition Hub.hxx:259
virtual void report_write_error()=0
Callback from the write flow when it encounters an error.
virtual void report_read_error()=0
Callback from the readflow when it encounters an error.
BarrierNotifiable barrier_
This notifiable will be called (if not NULL) upon read or write error.
Definition Hub.hxx:266
Abstract class for message recipients.
Templated implementation of the HubFlow.
Definition Hub.hxx:150
GenericHubFlow(Service *s)
Constructor.
Definition Hub.hxx:160
FlowInterface< buffer_type > port_type
Base type of an individual port.
Definition Hub.hxx:157
void register_port(port_type *port)
Adds a new port.
Definition Hub.hxx:167
Buffer< value_type > buffer_type
Type of a biffer being forwarded.
Definition Hub.hxx:155
D value_type
Payload of the buffer.
Definition Hub.hxx:153
void unregister_port(port_type *port)
Removes a previously added port.
Definition Hub.hxx:174
Data type wrapper for sending data through a Hub.
Definition Hub.hxx:101
uintptr_t id_type
The type of the identified of these object in the HUB.
Definition Hub.hxx:108
id_type id()
Defines the indentifier used for the DispatchFlow inside the Hub.
Definition Hub.hxx:113
FlowInterface< Buffer< HubContainer< T > > > * skipMember_
Defines which registered member of the hub should be skipped when the output members are enumerated.
Definition Hub.hxx:111
State flow implementing select-aware fd reads.
A list of queues.
Definition Queue.hxx:466
Collection of related state machines that pend on incoming messages.
Return type for a state flow callback.
Service * service()
Return a pointer to the service I am bound to.
Action release_and_exit()
Terminates the processing of the current message.
State flow with a given typed input queue.
Container for an arbitrary structure to pass through a Hub.
Definition Hub.hxx:47
size_t size()
Definition Hub.hxx:65
S & value()
Definition Hub.hxx:50
const void * data() const
Definition Hub.hxx:55
void * data()
Definition Hub.hxx:60
This flow is listening to data from a TCP connection, segments the incoming data into TcpMessages bas...
long long os_get_time_monotonic(void)
Get the monotonic time since the system started.
Definition os.c:571
Container for (binary) CAN frames going through Hubs.
Definition Hub.hxx:72
const struct can_frame & frame() const
Definition Hub.hxx:90
struct can_frame * mutable_frame()
Definition Hub.hxx:85