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
29OksSystem::FIFOConnection::FIFOConnection(const std::string &name) : OksSystem::File(name) {
30 m_fifo_fd = 0;
31 m_is_blocking = true;
32}
33
35
40
44
45void OksSystem::FIFOConnection::make(mode_t perm) const {
46 File::make_fifo(perm);
47} // make
48
53
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
72void 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();
79 OksSystem::Descriptor connection_fd(this,O_WRONLY,0);
80 connection_fd.write(message.data(),l);
81} // send_message
82
92
93void OksSystem::FIFOConnection::send(const std::string &message) const {
95 const unsigned int l = message.size();
97 m_fifo_fd->write(message.data(),l);
98} // send
99
109
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
132
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
163
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
193
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
223
224 delete m_fifo_fd;
225 m_fifo_fd = 0;
226
227}
228
235
237
239 return m_fifo_fd->fd();
240
241}
#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:477
File(const std::string &name)
Definition File.cpp:301
const char * c_full_name() const
Definition File.cpp:417
void make_fifo(mode_t permissions) const
creates a FIFO (named pipe) *‍/
Definition File.cpp:791
bool is_fifo() const
is the file a named pipe *‍/
Definition File.cpp:599
void warning(const Issue &issue)
Definition ers.hpp:126