DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
FIFOConnection.cpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3// Fork baseline commit: system-00-00-20 (2020-09-25).
4// Renamed since fork: yes (from src/FIFOConnection.cxx to src/FIFOConnection.cpp).
5
6/*
7 * FIFOConnection.cxx
8 * OksSystem
9 *
10 * Created by Matthias Wiesmann on 07.04.05.
11 * Copyright 2005 CERN. All rights reserved.
12 *
13 */
14
15#include <fcntl.h>
16
17#include "ers/ers.hpp"
18
21
22const unsigned int OksSystem::FIFOConnection::MAX_MESSAGE_LEN = 512;
23
28
30 : OksSystem::File(name)
31{
32 m_fifo_fd = 0;
33 m_is_blocking = true;
34}
35
37
39 : OksSystem::File(file)
40{
41 m_fifo_fd = 0;
42 m_is_blocking = true;
43}
44
49
50void
52{
53 File::make_fifo(perm);
54} // make
55
60
61std::string
63{
64 ERS_ASSERT_MSG(exists(), "FIFO " << c_full_name() << " does not exist.");
65 if (!is_fifo()) {
66 ers::warning(OksSystem::Exception(ERS_HERE, std::string(c_full_name()) + std::string(" is not a FIFO")));
67 } // should probably be FIFO
68 OksSystem::Descriptor connection_fd(this, O_RDONLY, 0);
69 char buffer[MAX_MESSAGE_LEN];
70 while (true) {
71 const int status = connection_fd.read(buffer, sizeof(buffer) - 1);
72 if (status > 0) {
73 ERS_ASSERT(status < (int)sizeof(buffer));
74 buffer[status] = '\0'; // we make sure we have a C-string
75 return std::string(buffer);
76 } // if
77 usleep(100000);
78 } // while
79} // read_message
80
81void
82OksSystem::FIFOConnection::send_message(const std::string& message) const
83{
84 ERS_ASSERT_MSG(exists(), "FIFO " << c_full_name() << " does not exist. Cannot put " << message << " into FIFO.");
85 if (!is_fifo()) {
86 ers::warning(OksSystem::Exception(ERS_HERE, std::string(c_full_name()) + std::string(" is not a FIFO")));
87 } // should probably be FIFO
88 const unsigned int l = message.size();
90 OksSystem::Descriptor connection_fd(this, O_WRONLY, 0);
91 connection_fd.write(message.data(), l);
92} // send_message
93
103
104void
105OksSystem::FIFOConnection::send(const std::string& message) const
106{
108 const unsigned int l = message.size();
110 m_fifo_fd->write(message.data(), l);
111} // send
112
122
123std::string
125{
127 char buffer[MAX_MESSAGE_LEN];
128 while (true) {
129 const int status = m_fifo_fd->read(buffer, sizeof(buffer) - 1);
130 if (status > 0) {
131 ERS_ASSERT(status < (int)sizeof(buffer));
132 buffer[status] = '\0'; // we make sure we have a C-string
133 return std::string(buffer);
134 } // if
135 if (m_is_blocking == false && status == 0)
136 return std::string("");
137 usleep(100000); // Slow down the loop
138 } // while
139} // read
140
146
149{
150
152
153 int flags = O_RDONLY;
154
155 if (block == false) {
156 flags = O_RDONLY | O_NONBLOCK;
157 m_is_blocking = false;
158 }
159
160 try {
161 m_fifo_fd = new OksSystem::Descriptor(this, flags, 0);
162 } catch (OksSystem::OpenFileIssue& ex) {
163 delete m_fifo_fd;
164 m_fifo_fd = 0;
165 throw;
166 }
167
168 return m_fifo_fd;
169}
170
176
179{
180
182
183 int flags = O_WRONLY;
184
185 if (block == false) {
186 flags = O_WRONLY | O_NONBLOCK;
187 m_is_blocking = false;
188 }
189
190 try {
191 m_fifo_fd = new OksSystem::Descriptor(this, flags, 0);
192 } catch (OksSystem::OpenFileIssue& ex) {
193 delete m_fifo_fd;
194 m_fifo_fd = 0;
195 throw;
196 }
197
198 return m_fifo_fd;
199}
200
206
209{
210
212
213 int flags = O_RDWR;
214
215 if (block == false) {
216 flags = O_RDWR | O_NONBLOCK;
217 m_is_blocking = false;
218 }
219
220 try {
221 m_fifo_fd = new OksSystem::Descriptor(this, flags, 0);
222 } catch (OksSystem::OpenFileIssue& ex) {
223 delete m_fifo_fd;
224 m_fifo_fd = 0;
225 throw;
226 }
227
228 return m_fifo_fd;
229}
230
236
237void
239{
240
241 delete m_fifo_fd;
242 m_fifo_fd = 0;
243}
244
251
252int
254{
255
257 return m_fifo_fd->fd();
258}
#define ERS_ASSERT_MSG(expression, message)
#define ERS_RANGE_CHECK(min, val, max)
#define ERS_ASSERT(expression)
#define ERS_HERE
File descriptor / Socket wrapper.
int read(void *buffer, size_t number) const
int write(const void *buffer, size_t number) const
FIFOConnection(const std::string &name)
void close()
It closes the FIFO file descriptor.
void send(const std::string &message) const
It writes a message to a FIFO.
void send_message(const std::string &message) const
OksSystem::Descriptor * open_rw(bool block=true)
It opens the FIFO in read and write mode.
std::string read_message() const
static const unsigned int MAX_MESSAGE_LEN
void make(mode_t perm=0622) const
OksSystem::Descriptor * m_fifo_fd
int fd() const
It gets the FIFO file descriptor.
OksSystem::Descriptor * open_w(bool block=true)
It opens the FIFO in write-only mode.
std::string read() const
It reads a single message (string) from a FIFO.
OksSystem::Descriptor * open_r(bool block=true)
It opens the FIFO in read-only mode.
bool exists() const
does the file exist *‍/
Definition File.cpp:526
File(const std::string &name)
Definition File.cpp:327
const char * c_full_name() const
Definition File.cpp:451
void make_fifo(mode_t permissions) const
creates a FIFO (named pipe) *‍/
Definition File.cpp:880
bool is_fifo() const
is the file a named pipe *‍/
Definition File.cpp:669
void warning(const Issue &issue)
Definition ers.hpp:150