DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
OksSystem::FIFOConnection Class Reference

#include <FIFOConnection.hpp>

Inheritance diagram for OksSystem::FIFOConnection:
[legend]
Collaboration diagram for OksSystem::FIFOConnection:
[legend]

Public Member Functions

 FIFOConnection (const std::string &name)
 FIFOConnection (const File &file)
 ~FIFOConnection ()
void make (mode_t perm=0622) const
std::string read_message () const
void send_message (const std::string &message) const
OksSystem::Descriptor * open_r (bool block=true)
 It opens the FIFO in read-only mode.
OksSystem::Descriptor * open_w (bool block=true)
 It opens the FIFO in write-only mode.
OksSystem::Descriptor * open_rw (bool block=true)
 It opens the FIFO in read and write mode.
void send (const std::string &message) const
 It writes a message to a FIFO.
std::string read () const
 It reads a single message (string) from a FIFO.
int fd () const
 It gets the FIFO file descriptor.
void close ()
 It closes the FIFO file descriptor.
Public Member Functions inherited from OksSystem::File
 File (const std::string &name)
 File (const char *name)
 File (const File &other)
virtual ~File ()
 operator std::string () const throw ()
 operator const char * () const throw ()
 operator bool () const throw ()
bool equals (const File &other) const throw ()
 compare two files *‍/
 operator size_t () const
const std::string & full_name () const throw ()
 full name for file *‍/
const char * c_full_name () const throw ()
std::string short_name () const throw ()
 short name for file *‍/
std::string parent_name () const throw ()
 path of directory containing file *‍/
std::string extension () const throw ()
 extension for file *‍/
int depth () const throw ()
 depth of the file *‍/
OksSystem::File parent () const
 parent of the current file *‍/
OksSystem::File child (const std::string &name) const
 named child of the current directory *‍/
OksSystem::File temporary (const std::string &prefix) const
bool exists () const throw ()
 does the file exist *‍/
mode_t permissions () const
 permissions for the file *‍/
std::string pretty_permissions () const
 pretty permissions for the file *‍/
size_t size () const
 size of file *‍/
uid_t owner_id () const
 owner id of file *‍/
User owner () const
 owner of the file *‍/
gid_t group () const
 group of file *‍/
bool is_regular () const
 is the file a directory *‍/
bool is_directory () const
 is the file a regular file *‍/
bool is_fifo () const
 is the file a named pipe *‍/
std::string file_type () const
 type of the file *‍/
file_list_t directory () const
 list of file in directory *‍/
void unlink () const
 deletes (unlinks) file *‍/
void rmdir () const
 deletes directory *‍/
void remove () const
 recursively delete files and directories *‍/
void rename (const File &other) const
 rename or moves the file *‍/
void permissions (mode_t permissions) const
 sets the type of the file *‍/
void make_dir (mode_t permissions) const
 creates a directory *‍/
void make_path (mode_t permissions) const
 creates a full path *‍/
void make_fifo (mode_t permissions) const
 creates a FIFO (named pipe) *‍/
void ensure_path (mode_t permissions) const
 creates the parent path *‍/
std::istream * input () const
 returns an input stream from the file*‍/
std::ostream * output (bool append=false) const
 returns an output stream to the file*‍/

Static Public Attributes

static const unsigned int MAX_MESSAGE_LEN = 512
Static Public Attributes inherited from OksSystem::File
static const char *const FILE_FLAG_STR = "-rwxS"
 column headers for display of permissions *‍/
static const char *const FILE_PROTOCOL = "file"
 string for the file protocol *‍/

Private Attributes

OksSystem::Descriptor * m_fifo_fd
bool m_is_blocking

Additional Inherited Members

Public Types inherited from OksSystem::File
typedef std::vector< OksSystem::File > file_list_t
Static Public Member Functions inherited from OksSystem::File
static std::string protocol (const std::string &url) throw ()
 extracts the protocol part of an url *‍/
static std::string extension (const std::string &url) throw ()
 extracts the extension of a path or an url *‍/
static std::string uri (const std::string &url) throw ()
 extracts the local path of an url *‍/
static std::string short_name (const std::string &url) throw ()
 extracts the short file name of an url *‍/
static int depth (const std::string &path) throw ()
 calculates depth of a path *‍/
static std::string first_line (const std::string &text)
 extracts the first line of a text *‍/
static File from_url (const std::string &url)
 build a file out of an URL *‍/
static std::string working_directory ()
 current directory of process *‍/
static void working_directory (const File &dir)
 set working directory of process *‍/
static std::string expand_home (const std::string path)
 resolve home directory *‍/
static std::string pretty_permissions (mode_t permissions)
 pretty prints permissions *‍/
static std::string to_string (mode_t permissions) throw ()
 converts permission to string *‍/
static std::string pretty_open_flag (int flags)
 pretty prints open flags *‍/
static std::string pretty_size (size_t size, bool cut_small)
 pretty prints a file size *‍/
Protected Member Functions inherited from OksSystem::File
void set_name (const std::string &name)
 sets the name of the file *‍/
mode_t get_mode () const
 get mode associated with file (permission + type) *‍/
Static Protected Member Functions inherited from OksSystem::File
static int unit (int u)
 calculates the value of a computer unit of order n (i.e KB,GB, etc *‍/
Protected Attributes inherited from OksSystem::File
std::string m_full_name
 full name (path) of the file *‍/
Static Protected Attributes inherited from OksSystem::File
static const char *const HUMAN_SIZE_STR [] = { "B", "KB", "MB", "GB" }
 strings for pretty printing file sizes *‍/
static const char *const HUMAN_OPEN_STR []
 strings for pretty printing open flags *‍/
static const int KILOBYTE = 1024
 number of bytes in a kilobyte *‍/
static const char *const FILE_COMMAND_PATH = "/usr/bin/file"

Detailed Description

This class offers some basic tool to communicate simple messages using a filesystem based FIFO (named pipe).

Author
Matthias Wiesmann
Version
1.0

Definition at line 29 of file FIFOConnection.hpp.

Constructor & Destructor Documentation

◆ FIFOConnection() [1/2]

OksSystem::FIFOConnection::FIFOConnection ( const std::string & name)

Constructor, does not actually create the named pipe in the file okssystem. To create it, use the make method

Parameters
namethe name (full path) of the named pipe

Definition at line 29 of file FIFOConnection.cpp.

30 : OksSystem::File(name)
31{
32 m_fifo_fd = 0;
33 m_is_blocking = true;
34}
OksSystem::Descriptor * m_fifo_fd

◆ FIFOConnection() [2/2]

OksSystem::FIFOConnection::FIFOConnection ( const File & file)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 38 of file FIFOConnection.cpp.

39 : OksSystem::File(file)
40{
41 m_fifo_fd = 0;
42 m_is_blocking = true;
43}

◆ ~FIFOConnection()

OksSystem::FIFOConnection::~FIFOConnection ( )

Definition at line 45 of file FIFOConnection.cpp.

46{
47 delete m_fifo_fd;
48}

Member Function Documentation

◆ close()

void OksSystem::FIFOConnection::close ( )

It closes the FIFO file descriptor.

Note
This method has to be used to close the FIFO file descriptor only when it is opened using one of the open_r(), open_w() or open_wr() methods.

Definition at line 238 of file FIFOConnection.cpp.

239{
240
241 delete m_fifo_fd;
242 m_fifo_fd = 0;
243}

◆ fd()

int OksSystem::FIFOConnection::fd ( ) const

It gets the FIFO file descriptor.

Note
This method can to be used only when the FIFO is opened using the open_r(), open_w() or open_rw() methods.
Returns
The FIFO file descriptor.

Definition at line 253 of file FIFOConnection.cpp.

254{
255
257 return m_fifo_fd->fd();
258}
#define ERS_ASSERT(expression)
int fd() const
file descritptor

◆ make()

void OksSystem::FIFOConnection::make ( mode_t perm = 0622) const

Definition at line 51 of file FIFOConnection.cpp.

52{
53 File::make_fifo(perm);
54} // make
void make_fifo(mode_t permissions) const
creates a FIFO (named pipe) *‍/
Definition File.cpp:880

◆ open_r()

OksSystem::Descriptor * OksSystem::FIFOConnection::open_r ( bool block = true)

It opens the FIFO in read-only mode.

Returns
A pointer to a OksSystem::Descriptor object holding the FIFO file descriptor.
Parameters
blockIf TRUE the FIFO will be opened in blocking mode.

Definition at line 148 of file FIFOConnection.cpp.

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}

◆ open_rw()

OksSystem::Descriptor * OksSystem::FIFOConnection::open_rw ( bool block = true)

It opens the FIFO in read and write mode.

Returns
A pointer to a OksSystem::Descriptor object holding the FIFO file descriptor.
Parameters
blockIf TRUE the FIFO will be opened in blocking mode.

Definition at line 208 of file FIFOConnection.cpp.

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}

◆ open_w()

OksSystem::Descriptor * OksSystem::FIFOConnection::open_w ( bool block = true)

It opens the FIFO in write-only mode.

Returns
A pointer to a OksSystem::Descriptor object holding the FIFO file descriptor.
Parameters
blockIf TRUE the FIFO will be opened in blocking mode.

Definition at line 178 of file FIFOConnection.cpp.

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}

◆ read()

std::string OksSystem::FIFOConnection::read ( ) const

It reads a single message (string) from a FIFO.

Returns
The actual message.
Note
  • The maximum message length is MAX_MESSAGE_LEN-1 bytes.
  • This method does not take care of opening the FIFO (as it happens in read_message()), and should be used only when the FIFO is opened using one of the open_r() or open_wr() methods (see linux "open", "fifo" and "read" man/info pages for more details).
  • This method will block or not depending on the way the FIFO has been opened.

Definition at line 124 of file FIFOConnection.cpp.

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
int read(void *buffer, size_t number) const
static const unsigned int MAX_MESSAGE_LEN

◆ read_message()

std::string OksSystem::FIFOConnection::read_message ( ) const

Blocking read of a single message (string) from a FIFO

Returns
the actual message
Note
maximum message length is MAX_MESSAGE_LEN-1 bytes

Definition at line 62 of file FIFOConnection.cpp.

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
#define ERS_ASSERT_MSG(expression, message)
#define ERS_HERE
bool exists() const
does the file exist *‍/
Definition File.cpp:526
const char * c_full_name() const
Definition File.cpp:451
bool is_fifo() const
is the file a named pipe *‍/
Definition File.cpp:669
void warning(const Issue &issue)
Definition ers.hpp:150

◆ send()

void OksSystem::FIFOConnection::send ( const std::string & message) const

It writes a message to a FIFO.

Parameters
messageThe message to write into the FIFO.
Note
  • The maximum message length is MAX_MESSAGE_LEN-1 bytes.
  • This method does not take care of opening the FIFO (as it happens in send_message()), and should be used only when the FIFO is opened using one of the open_w() or open_wr() methods (see linux "open", "fifo" and "write" man/info pages for more details).
  • This method will block or not depending on the way the FIFO has been opened.

Definition at line 105 of file FIFOConnection.cpp.

106{
108 const unsigned int l = message.size();
110 m_fifo_fd->write(message.data(), l);
111} // send
#define ERS_RANGE_CHECK(min, val, max)
int write(const void *buffer, size_t number) const
message(message)
Definition __init__.py:84

◆ send_message()

void OksSystem::FIFOConnection::send_message ( const std::string & message) const

Definition at line 82 of file FIFOConnection.cpp.

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

Member Data Documentation

◆ m_fifo_fd

OksSystem::Descriptor* OksSystem::FIFOConnection::m_fifo_fd
private

Definition at line 54 of file FIFOConnection.hpp.

◆ m_is_blocking

bool OksSystem::FIFOConnection::m_is_blocking
private

Definition at line 55 of file FIFOConnection.hpp.

◆ MAX_MESSAGE_LEN

const unsigned int OksSystem::FIFOConnection::MAX_MESSAGE_LEN = 512
static

Definition at line 33 of file FIFOConnection.hpp.


The documentation for this class was generated from the following files: