DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
FIFOConnection.cpp
Go to the documentation of this file.
1/*
2 * FIFOConnection.cxx
3 * OksSystem
4 *
5 * Created by Matthias Wiesmann on 07.04.05.
6 * Copyright 2005 CERN. All rights reserved.
7 *
8 */
9
10#include <fcntl.h>
11
12#include "ers/ers.hpp"
13
16
17const unsigned int OksSystem::FIFOConnection::MAX_MESSAGE_LEN = 512;
18
24OksSystem::FIFOConnection::FIFOConnection(const std::string &name) : OksSystem::File(name) {
25 m_fifo_fd = 0;
26 m_is_blocking = true;
27}
28
35
37 delete m_fifo_fd;
38}
39
40void OksSystem::FIFOConnection::make(mode_t perm) const {
41 File::make_fifo(perm);
42} // make
43
50 ERS_ASSERT_MSG(exists(),"FIFO "<< c_full_name() << " does not exist.");
51 if (! is_fifo()) {
52 ers::warning(OksSystem::Exception(ERS_HERE, std::string(c_full_name()) + std::string(" is not a FIFO")));
53 } // should probably be FIFO
54 OksSystem::Descriptor connection_fd(this,O_RDONLY,0);
55 char buffer[MAX_MESSAGE_LEN];
56 while(true) {
57 const int status = connection_fd.read(buffer,sizeof(buffer)-1);
58 if (status>0) {
59 ERS_ASSERT(status<(int) sizeof(buffer));
60 buffer[status] = '\0';// we make sure we have a C-string
61 return std::string(buffer);
62 } // if
63 usleep(100000);
64 } // while
65} // read_message
66
67void OksSystem::FIFOConnection::send_message(const std::string &message) const {
68 ERS_ASSERT_MSG(exists(),"FIFO "<< c_full_name() << " does not exist. Cannot put "<< message << " into FIFO.");
69 if (! is_fifo()) {
70 ers::warning(OksSystem::Exception(ERS_HERE, std::string(c_full_name()) + std::string(" is not a FIFO")));
71 } // should probably be FIFO
72 const unsigned int l = message.size();
73 ERS_RANGE_CHECK(1,l,MAX_MESSAGE_LEN);
74 OksSystem::Descriptor connection_fd(this,O_WRONLY,0);
75 connection_fd.write(message.data(),l);
76} // send_message
77
88void OksSystem::FIFOConnection::send(const std::string &message) const {
89 ERS_ASSERT(m_fifo_fd);
90 const unsigned int l = message.size();
91 ERS_RANGE_CHECK(1,l,MAX_MESSAGE_LEN);
92 m_fifo_fd->write(message.data(),l);
93} // send
94
106 ERS_ASSERT(m_fifo_fd);
107 char buffer[MAX_MESSAGE_LEN];
108 while(true) {
109 const int status = m_fifo_fd->read(buffer,sizeof(buffer)-1);
110 if (status>0) {
111 ERS_ASSERT(status<(int) sizeof(buffer));
112 buffer[status] = '\0';// we make sure we have a C-string
113 return std::string(buffer);
114 } // if
115 if (m_is_blocking == false && status == 0) return std::string("");
116 usleep(100000); // Slow down the loop
117 } // while
118} // read
119
127
128 ERS_ASSERT(!m_fifo_fd);
129
130 int flags = O_RDONLY;
131
132 if(block == false) {
133 flags = O_RDONLY|O_NONBLOCK;
134 m_is_blocking = false;
135 }
136
137 try {
138 m_fifo_fd = new OksSystem::Descriptor(this,flags,0);
139 }
140 catch(OksSystem::OpenFileIssue &ex) {
141 delete m_fifo_fd;
142 m_fifo_fd = 0;
143 throw;
144 }
145
146 return m_fifo_fd;
147
148}
149
158
159 ERS_ASSERT(!m_fifo_fd);
160
161 int flags = O_WRONLY;
162
163 if(block == false) {
164 flags = O_WRONLY|O_NONBLOCK;
165 m_is_blocking = false;
166 }
167
168 try {
169 m_fifo_fd = new OksSystem::Descriptor(this,flags,0);
170 }
171 catch(OksSystem::OpenFileIssue &ex) {
172 delete m_fifo_fd;
173 m_fifo_fd = 0;
174 throw;
175 }
176
177 return m_fifo_fd;
178
179}
180
188
189 ERS_ASSERT(!m_fifo_fd);
190
191 int flags = O_RDWR;
192
193 if(block == false) {
194 flags = O_RDWR|O_NONBLOCK;
195 m_is_blocking = false;
196 }
197
198 try {
199 m_fifo_fd = new OksSystem::Descriptor(this,flags,0);
200 }
201 catch(OksSystem::OpenFileIssue &ex) {
202 delete m_fifo_fd;
203 m_fifo_fd = 0;
204 throw;
205 }
206
207 return m_fifo_fd;
208
209}
210
218
219 delete m_fifo_fd;
220 m_fifo_fd = 0;
221
222}
223
232
233 ERS_ASSERT(m_fifo_fd);
234 return m_fifo_fd->fd();
235
236}
#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.
Wrapper for file operations.
Definition File.hpp:32
void make_fifo(mode_t permissions) const
creates a FIFO (named pipe) *‍/
Definition File.cpp:786
void warning(const Issue &issue)
Definition ers.hpp:115