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) :
OksSystem
::
File
(name) {
30
m_fifo_fd
= 0;
31
m_is_blocking
=
true
;
32
}
33
35
36
OksSystem::FIFOConnection::FIFOConnection
(
const
File
&file) :
OksSystem
::
File
(file) {
37
m_fifo_fd
= 0;
38
m_is_blocking
=
true
;
39
}
40
41
OksSystem::FIFOConnection::~FIFOConnection
() {
42
delete
m_fifo_fd
;
43
}
44
45
void
OksSystem::FIFOConnection::make
(mode_t perm)
const
{
46
File::make_fifo
(perm);
47
}
// make
48
53
54
std::string
OksSystem::FIFOConnection::read_message
()
const
{
55
ERS_ASSERT_MSG
(
exists
(),
"FIFO "
<<
c_full_name
() <<
" does not exist."
);
56
if
(!
is_fifo
()) {
57
ers::warning
(OksSystem::Exception(
ERS_HERE
, std::string(
c_full_name
()) + std::string(
" is not a FIFO"
)));
58
}
// should probably be FIFO
59
OksSystem::Descriptor
connection_fd(
this
,O_RDONLY,0);
60
char
buffer[
MAX_MESSAGE_LEN
];
61
while
(
true
) {
62
const
int
status = connection_fd.
read
(buffer,
sizeof
(buffer)-1);
63
if
(status>0) {
64
ERS_ASSERT
(status<(
int
)
sizeof
(buffer));
65
buffer[status] =
'\0'
;
// we make sure we have a C-string
66
return
std::string(buffer);
67
}
// if
68
usleep(100000);
69
}
// while
70
}
// read_message
71
72
void
OksSystem::FIFOConnection::send_message
(
const
std::string &message)
const
{
73
ERS_ASSERT_MSG
(
exists
(),
"FIFO "
<<
c_full_name
() <<
" does not exist. Cannot put "
<< message <<
" into FIFO."
);
74
if
(!
is_fifo
()) {
75
ers::warning
(OksSystem::Exception(
ERS_HERE
, std::string(
c_full_name
()) + std::string(
" is not a FIFO"
)));
76
}
// should probably be FIFO
77
const
unsigned
int
l = message.size();
78
ERS_RANGE_CHECK
(1,l,
MAX_MESSAGE_LEN
);
79
OksSystem::Descriptor
connection_fd(
this
,O_WRONLY,0);
80
connection_fd.
write
(message.data(),l);
81
}
// send_message
82
92
93
void
OksSystem::FIFOConnection::send
(
const
std::string &message)
const
{
94
ERS_ASSERT
(
m_fifo_fd
);
95
const
unsigned
int
l = message.size();
96
ERS_RANGE_CHECK
(1,l,
MAX_MESSAGE_LEN
);
97
m_fifo_fd
->write(message.data(),l);
98
}
// send
99
109
110
std::string
OksSystem::FIFOConnection::read
()
const
{
111
ERS_ASSERT
(
m_fifo_fd
);
112
char
buffer[
MAX_MESSAGE_LEN
];
113
while
(
true
) {
114
const
int
status =
m_fifo_fd
->read(buffer,
sizeof
(buffer)-1);
115
if
(status>0) {
116
ERS_ASSERT
(status<(
int
)
sizeof
(buffer));
117
buffer[status] =
'\0'
;
// we make sure we have a C-string
118
return
std::string(buffer);
119
}
// if
120
if
(
m_is_blocking
==
false
&& status == 0)
return
std::string(
""
);
121
usleep(100000);
// Slow down the loop
122
}
// while
123
}
// read
124
130
131
OksSystem::Descriptor
*
OksSystem::FIFOConnection::open_r
(
bool
block) {
132
133
ERS_ASSERT
(!
m_fifo_fd
);
134
135
int
flags = O_RDONLY;
136
137
if
(block ==
false
) {
138
flags = O_RDONLY|O_NONBLOCK;
139
m_is_blocking
=
false
;
140
}
141
142
try
{
143
m_fifo_fd
=
new
OksSystem::Descriptor
(
this
,flags,0);
144
}
145
catch
(OksSystem::OpenFileIssue &ex) {
146
delete
m_fifo_fd
;
147
m_fifo_fd
= 0;
148
throw
;
149
}
150
151
return
m_fifo_fd
;
152
153
}
154
160
161
162
OksSystem::Descriptor
*
OksSystem::FIFOConnection::open_w
(
bool
block) {
163
164
ERS_ASSERT
(!
m_fifo_fd
);
165
166
int
flags = O_WRONLY;
167
168
if
(block ==
false
) {
169
flags = O_WRONLY|O_NONBLOCK;
170
m_is_blocking
=
false
;
171
}
172
173
try
{
174
m_fifo_fd
=
new
OksSystem::Descriptor
(
this
,flags,0);
175
}
176
catch
(OksSystem::OpenFileIssue &ex) {
177
delete
m_fifo_fd
;
178
m_fifo_fd
= 0;
179
throw
;
180
}
181
182
return
m_fifo_fd
;
183
184
}
185
191
192
OksSystem::Descriptor
*
OksSystem::FIFOConnection::open_rw
(
bool
block) {
193
194
ERS_ASSERT
(!
m_fifo_fd
);
195
196
int
flags = O_RDWR;
197
198
if
(block ==
false
) {
199
flags = O_RDWR|O_NONBLOCK;
200
m_is_blocking
=
false
;
201
}
202
203
try
{
204
m_fifo_fd
=
new
OksSystem::Descriptor
(
this
,flags,0);
205
}
206
catch
(OksSystem::OpenFileIssue &ex) {
207
delete
m_fifo_fd
;
208
m_fifo_fd
= 0;
209
throw
;
210
}
211
212
return
m_fifo_fd
;
213
214
}
215
221
222
void
OksSystem::FIFOConnection::close
() {
223
224
delete
m_fifo_fd
;
225
m_fifo_fd
= 0;
226
227
}
228
235
236
int
OksSystem::FIFOConnection::fd
()
const
{
237
238
ERS_ASSERT
(
m_fifo_fd
);
239
return
m_fifo_fd
->fd();
240
241
}
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:141
OksSystem::Descriptor
File descriptor / Socket wrapper.
Definition
Descriptor.hpp:34
OksSystem::Descriptor::read
int read(void *buffer, size_t number) const
Definition
Descriptor.cpp:98
OksSystem::Descriptor::write
int write(const void *buffer, size_t number) const
Definition
Descriptor.cpp:105
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:222
OksSystem::FIFOConnection::send
void send(const std::string &message) const
It writes a message to a FIFO.
Definition
FIFOConnection.cpp:93
OksSystem::FIFOConnection::send_message
void send_message(const std::string &message) const
Definition
FIFOConnection.cpp:72
OksSystem::FIFOConnection::open_rw
OksSystem::Descriptor * open_rw(bool block=true)
It opens the FIFO in read and write mode.
Definition
FIFOConnection.cpp:192
OksSystem::FIFOConnection::read_message
std::string read_message() const
Definition
FIFOConnection.cpp:54
OksSystem::FIFOConnection::MAX_MESSAGE_LEN
static const unsigned int MAX_MESSAGE_LEN
Definition
FIFOConnection.hpp:32
OksSystem::FIFOConnection::make
void make(mode_t perm=0622) const
Definition
FIFOConnection.cpp:45
OksSystem::FIFOConnection::m_fifo_fd
OksSystem::Descriptor * m_fifo_fd
Definition
FIFOConnection.hpp:53
OksSystem::FIFOConnection::fd
int fd() const
It gets the FIFO file descriptor.
Definition
FIFOConnection.cpp:236
OksSystem::FIFOConnection::open_w
OksSystem::Descriptor * open_w(bool block=true)
It opens the FIFO in write-only mode.
Definition
FIFOConnection.cpp:162
OksSystem::FIFOConnection::read
std::string read() const
It reads a single message (string) from a FIFO.
Definition
FIFOConnection.cpp:110
OksSystem::FIFOConnection::open_r
OksSystem::Descriptor * open_r(bool block=true)
It opens the FIFO in read-only mode.
Definition
FIFOConnection.cpp:131
OksSystem::FIFOConnection::~FIFOConnection
~FIFOConnection()
Definition
FIFOConnection.cpp:41
OksSystem::FIFOConnection::m_is_blocking
bool m_is_blocking
Definition
FIFOConnection.hpp:54
OksSystem::File::exists
bool exists() const
does the file exist */
Definition
File.cpp:477
OksSystem::File::File
File(const std::string &name)
Definition
File.cpp:301
OksSystem::File::c_full_name
const char * c_full_name() const
Definition
File.cpp:417
OksSystem::File::make_fifo
void make_fifo(mode_t permissions) const
creates a FIFO (named pipe) */
Definition
File.cpp:791
OksSystem::File::is_fifo
bool is_fifo() const
is the file a named pipe */
Definition
File.cpp:599
ers.hpp
OksSystem
Definition
Descriptor.hpp:22
ers::warning
void warning(const Issue &issue)
Definition
ers.hpp:126
exceptions.hpp
Generated on
for DUNE-DAQ by
1.17.0