DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
okssystem
src
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
19
#include "
okssystem/FIFOConnection.hpp
"
20
#include "
okssystem/exceptions.hpp
"
21
22
const
unsigned
int
OksSystem::FIFOConnection::MAX_MESSAGE_LEN
= 512;
23
28
29
OksSystem::FIFOConnection::FIFOConnection
(
const
std::string& name)
30
:
OksSystem
::
File
(name)
31
{
32
m_fifo_fd
= 0;
33
m_is_blocking
=
true
;
34
}
35
37
38
OksSystem::FIFOConnection::FIFOConnection
(
const
File
& file)
39
:
OksSystem
::
File
(file)
40
{
41
m_fifo_fd
= 0;
42
m_is_blocking
=
true
;
43
}
44
45
OksSystem::FIFOConnection::~FIFOConnection
()
46
{
47
delete
m_fifo_fd
;
48
}
49
50
void
51
OksSystem::FIFOConnection::make
(mode_t perm)
const
52
{
53
File::make_fifo
(perm);
54
}
// make
55
60
61
std::string
62
OksSystem::FIFOConnection::read_message
()
const
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
81
void
82
OksSystem::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();
89
ERS_RANGE_CHECK
(1, l,
MAX_MESSAGE_LEN
);
90
OksSystem::Descriptor
connection_fd(
this
, O_WRONLY, 0);
91
connection_fd.
write
(message.data(), l);
92
}
// send_message
93
103
104
void
105
OksSystem::FIFOConnection::send
(
const
std::string& message)
const
106
{
107
ERS_ASSERT
(
m_fifo_fd
);
108
const
unsigned
int
l = message.size();
109
ERS_RANGE_CHECK
(1, l,
MAX_MESSAGE_LEN
);
110
m_fifo_fd
->write(message.data(), l);
111
}
// send
112
122
123
std::string
124
OksSystem::FIFOConnection::read
()
const
125
{
126
ERS_ASSERT
(
m_fifo_fd
);
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
147
OksSystem::Descriptor
*
148
OksSystem::FIFOConnection::open_r
(
bool
block)
149
{
150
151
ERS_ASSERT
(!
m_fifo_fd
);
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
177
OksSystem::Descriptor
*
178
OksSystem::FIFOConnection::open_w
(
bool
block)
179
{
180
181
ERS_ASSERT
(!
m_fifo_fd
);
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
207
OksSystem::Descriptor
*
208
OksSystem::FIFOConnection::open_rw
(
bool
block)
209
{
210
211
ERS_ASSERT
(!
m_fifo_fd
);
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
237
void
238
OksSystem::FIFOConnection::close
()
239
{
240
241
delete
m_fifo_fd
;
242
m_fifo_fd
= 0;
243
}
244
251
252
int
253
OksSystem::FIFOConnection::fd
()
const
254
{
255
256
ERS_ASSERT
(
m_fifo_fd
);
257
return
m_fifo_fd
->fd();
258
}
ERS_ASSERT_MSG
#define ERS_ASSERT_MSG(expression, message)
ERS_RANGE_CHECK
#define ERS_RANGE_CHECK(min, val, max)
ERS_ASSERT
#define ERS_ASSERT(expression)
FIFOConnection.hpp
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:127
OksSystem::Descriptor
File descriptor / Socket wrapper.
Definition
Descriptor.hpp:35
OksSystem::Descriptor::read
int read(void *buffer, size_t number) const
Definition
Descriptor.cpp:108
OksSystem::Descriptor::write
int write(const void *buffer, size_t number) const
Definition
Descriptor.cpp:117
OksSystem::FIFOConnection::FIFOConnection
FIFOConnection(const std::string &name)
Definition
FIFOConnection.cpp:29
OksSystem::FIFOConnection::close
void close()
It closes the FIFO file descriptor.
Definition
FIFOConnection.cpp:238
OksSystem::FIFOConnection::send
void send(const std::string &message) const
It writes a message to a FIFO.
Definition
FIFOConnection.cpp:105
OksSystem::FIFOConnection::send_message
void send_message(const std::string &message) const
Definition
FIFOConnection.cpp:82
OksSystem::FIFOConnection::open_rw
OksSystem::Descriptor * open_rw(bool block=true)
It opens the FIFO in read and write mode.
Definition
FIFOConnection.cpp:208
OksSystem::FIFOConnection::read_message
std::string read_message() const
Definition
FIFOConnection.cpp:62
OksSystem::FIFOConnection::MAX_MESSAGE_LEN
static const unsigned int MAX_MESSAGE_LEN
Definition
FIFOConnection.hpp:33
OksSystem::FIFOConnection::make
void make(mode_t perm=0622) const
Definition
FIFOConnection.cpp:51
OksSystem::FIFOConnection::m_fifo_fd
OksSystem::Descriptor * m_fifo_fd
Definition
FIFOConnection.hpp:54
OksSystem::FIFOConnection::fd
int fd() const
It gets the FIFO file descriptor.
Definition
FIFOConnection.cpp:253
OksSystem::FIFOConnection::open_w
OksSystem::Descriptor * open_w(bool block=true)
It opens the FIFO in write-only mode.
Definition
FIFOConnection.cpp:178
OksSystem::FIFOConnection::read
std::string read() const
It reads a single message (string) from a FIFO.
Definition
FIFOConnection.cpp:124
OksSystem::FIFOConnection::open_r
OksSystem::Descriptor * open_r(bool block=true)
It opens the FIFO in read-only mode.
Definition
FIFOConnection.cpp:148
OksSystem::FIFOConnection::~FIFOConnection
~FIFOConnection()
Definition
FIFOConnection.cpp:45
OksSystem::FIFOConnection::m_is_blocking
bool m_is_blocking
Definition
FIFOConnection.hpp:55
OksSystem::File::exists
bool exists() const
does the file exist */
Definition
File.cpp:526
OksSystem::File::File
File(const std::string &name)
Definition
File.cpp:327
OksSystem::File::c_full_name
const char * c_full_name() const
Definition
File.cpp:451
OksSystem::File::make_fifo
void make_fifo(mode_t permissions) const
creates a FIFO (named pipe) */
Definition
File.cpp:880
OksSystem::File::is_fifo
bool is_fifo() const
is the file a named pipe */
Definition
File.cpp:669
ers.hpp
OksSystem
Definition
Descriptor.hpp:22
ers::warning
void warning(const Issue &issue)
Definition
ers.hpp:150
exceptions.hpp
Generated on
for DUNE-DAQ by
1.18.0