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::Descriptoropen_r (bool block=true)
 It opens the FIFO in read-only mode.
 
OksSystem::Descriptoropen_w (bool block=true)
 It opens the FIFO in write-only mode.
 
OksSystem::Descriptoropen_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::Descriptorm_fifo_fd
 
bool m_is_blocking
 

Additional Inherited Members

- Public Types inherited from OksSystem::File
typedef std::vector< OksSystem::Filefile_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 [] = { "READ", "WRITE", "NOBLOCK", "APPEND", "CREATE", "TRUNCATE","EXCLUSIVE" }
 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 24 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 24 of file FIFOConnection.cpp.

24 : OksSystem::File(name) {
25 m_fifo_fd = 0;
26 m_is_blocking = true;
27}
OksSystem::Descriptor * m_fifo_fd
Wrapper for file operations.
Definition File.hpp:32

◆ 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 31 of file FIFOConnection.cpp.

31 : OksSystem::File(file) {
32 m_fifo_fd = 0;
33 m_is_blocking = true;
34}

◆ ~FIFOConnection()

OksSystem::FIFOConnection::~FIFOConnection ( )

Definition at line 36 of file FIFOConnection.cpp.

36 {
37 delete m_fifo_fd;
38}

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 217 of file FIFOConnection.cpp.

217 {
218
219 delete m_fifo_fd;
220 m_fifo_fd = 0;
221
222}

◆ 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 231 of file FIFOConnection.cpp.

231 {
232
234 return m_fifo_fd->fd();
235
236}
#define ERS_ASSERT(expression)
int fd() const
file descritptor

◆ make()

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

Definition at line 40 of file FIFOConnection.cpp.

40 {
41 File::make_fifo(perm);
42} // make
void make_fifo(mode_t permissions) const
creates a FIFO (named pipe) *‍/
Definition File.cpp:786

◆ 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 126 of file FIFOConnection.cpp.

126 {
127
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}
File descriptor / Socket wrapper.

◆ 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 187 of file FIFOConnection.cpp.

187 {
188
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}

◆ 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 157 of file FIFOConnection.cpp.

157 {
158
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}

◆ 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 105 of file FIFOConnection.cpp.

105 {
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
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 49 of file FIFOConnection.cpp.

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

◆ 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 88 of file FIFOConnection.cpp.

88 {
90 const unsigned int l = message.size();
92 m_fifo_fd->write(message.data(),l);
93} // send
#define ERS_RANGE_CHECK(min, val, max)
int write(const void *buffer, size_t number) const

◆ send_message()

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

Definition at line 67 of file FIFOConnection.cpp.

67 {
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();
74 OksSystem::Descriptor connection_fd(this,O_WRONLY,0);
75 connection_fd.write(message.data(),l);
76} // send_message

Member Data Documentation

◆ m_fifo_fd

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

Definition at line 48 of file FIFOConnection.hpp.

◆ m_is_blocking

bool OksSystem::FIFOConnection::m_is_blocking
private

Definition at line 49 of file FIFOConnection.hpp.

◆ MAX_MESSAGE_LEN

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

Definition at line 27 of file FIFOConnection.hpp.


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