Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
HubDevice.cxx
Go to the documentation of this file.
1
34#include "utils/HubDevice.hxx"
35#include "nmranet_config.h"
36#include <stdlib.h>
37
38template <>
39const int FdHubPort<CanHubFlow>::ReadThread::kUnit = sizeof(struct can_frame);
40template <>
42 struct can_frame);
43
44template <>
46{
47 // No semaphores used for CAN-bus connection. We have to read as fast as we
48 // can or else packet loss will happen.
49}
50
51template <>
53{
54 HASSERT(size == sizeof(struct can_frame));
55 auto *b = port()->hub_->alloc();
56 b->data()->skipMember_ = &port()->writeFlow_;
57 memcpy(b->data()->mutable_frame(), buf, size);
58 port()->hub_->send(b, 0);
59}
60
61template <> const int FdHubPort<HubFlow>::ReadThread::kUnit = 1;
62template <> const int FdHubPort<HubFlow>::ReadThread::kBufSize = 64;
63
64
65template <>
67{
68 if (config_gridconnect_port_max_incoming_packets() < 1000000)
69 {
70 semaphores_ = new SemaphoreNotifiableBlock(
71 config_gridconnect_port_max_incoming_packets());
72 }
73}
74
75template <>
76void FdHubPort<HubFlow>::ReadThread::send_message(const void *buf, int size)
77{
78 BarrierNotifiable *done = nullptr;
79 if (semaphores_)
80 {
81 done = semaphores_->acquire();
82 }
83 auto *b = port()->hub_->alloc();
84 if (done)
85 {
86 b->set_done(done);
87 }
88 b->data()->skipMember_ = &port()->writeFlow_;
89 b->data()->assign((const char *)buf, size);
90 port()->hub_->send(b);
91}
92
94static const char kThreadPrefix[] = "thread_fd_";
95
96void FdHubPortBase::fill_thread_name(char *buf, char mode, int fd)
97{
98 memcpy(buf, kThreadPrefix, sizeof(kThreadPrefix));
99 static_assert(
100 sizeof(kThreadPrefix) == 11, "size of thread prefix incorrect");
101 char *p = buf + sizeof(kThreadPrefix) - 1;
102 *p++ = mode;
103 *p++ = '_';
104 char *q = p;
105 int ff = fd;
106 do
107 {
108 ff /= 10;
109 q++;
110 } while (ff);
111 *q = 0;
112 q--;
113 ff = fd;
114 do
115 {
116 *q = '0' + (ff % 10);
117 q--;
118 ff /= 10;
119 } while (ff);
120}
static const char kThreadPrefix[]
Prefix for thread names created by the FdHubPort for reading and writing.
Definition HubDevice.cxx:94
A BarrierNotifiable allows to create a number of child Notifiable and wait for all of them to finish.
static void fill_thread_name(char *buf, char mode, int fd)
Puts the desired thread name for the read or write thread.
Definition HubDevice.cxx:96
void init()
Initializes the semaphore notifiables.
void send_message(const void *buf, int size) OVERRIDE
Sends off a buffer.
HubPort that connects a raw device to a strongly typed Hub.
A block of BarrierNotifiable objects, with a synchronous allocation call.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138